You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Fix: Token Exchange Ignoring Scope and Audience Parameters (#1365)
## Overview This PR fixes a bug in the `exchangeToken()` method where `scope` and `audience` parameters were completely ignored in HTTP requests to the `/oauth/token` endpoint. Impact: Token exchange requests would fail with authorization errors because the Auth0 backend received requests without the required `audience` and `scope` parameters. ## Root Cause The bug was in `src/api.ts` where `audience` and `scope` were removed from the options object even in the case of TokenExchange (where it's needed): ```typescript export async function oauthToken({ baseUrl, timeout, audience, scope, auth0Client, useFormData, ...options }: TokenEndpointOptions) { const body = useFormData ? createQueryParams(options) // Missing audience & scope in case of tokenExchange : JSON.stringify(options); // Missing audience & scope in case of tokenExchange } ``` ## Changes - `src/api.ts`: Now properly includes `audience` and `scope` in request body, if the grant_type of the request is ```urn:ietf:params:oauth:grant-type:token-exchange``` - `src/Auth0Client.ts`: Changed from always using client defaults to respecting user-provided values - `src/TokenExchange.ts`: Changed `audience: string` to `audience?: string` to properly reflect fallback behavior - `EXAMPLES.md`: Added comprehensive token exchange examples showing both default and custom audience usage - Added tests for the original bug and tests to ensure that Access Token Descoping does not happen. ## Reproduction Steps Code: ```typescript const result = await auth0.exchangeToken({ subject_token: 'external-token', subject_token_type: 'urn:custom:token-type', audience: 'https://target-api.com', // Ignored before scope: 'read:data write:data' // Ignored before }); ``` HTTP Request Body (Before): ``` grant_type=urn%3Aietf%3Aparams%3Aoauth%3Agrant-type%3Atoken-exchange &subject_token=external-token &subject_token_type=urn%3Acustom%3Atoken-type &client_id=your-client-id // audience and scope completely missing ``` HTTP Request Body (After): ``` grant_type=urn%3Aietf%3Aparams%3Aoauth%3Agrant-type%3Atoken-exchange &subject_token=external-token &subject_token_type=urn%3Acustom%3Atoken-type &audience=https%3A//target-api.com &scope=openid%20profile%20read%3Adata%20write%3Adata &client_id=your-client-id ``` ## Breaking Changes **None** - This is a bug fix that restores intended functionality without changing the public API.
0 commit comments