From 765d48f8c7c44ab2ea63343cb196c3e7235715f6 Mon Sep 17 00:00:00 2001 From: Amin Abbaspour Date: Tue, 15 Oct 2024 14:42:50 +1100 Subject: [PATCH] customize token endpoint handler --- src/Auth0Client.ts | 8 ++++++-- src/global.ts | 7 +++++++ src/index.ts | 2 ++ 3 files changed, 15 insertions(+), 2 deletions(-) diff --git a/src/Auth0Client.ts b/src/Auth0Client.ts index b338a7372..e3219f4ca 100644 --- a/src/Auth0Client.ts +++ b/src/Auth0Client.ts @@ -76,7 +76,8 @@ import { User, IdToken, GetTokenSilentlyVerboseResponse, - TokenEndpointResponse + TokenEndpointResponse, + TokenEndpointOptions } from './global'; // @ts-ignore @@ -123,6 +124,7 @@ export class Auth0Client { private readonly isAuthenticatedCookieName: string; private readonly nowProvider: () => number | Promise; private readonly httpTimeoutMs: number; + private readonly tokenEndpointHandler: (options: TokenEndpointOptions, worker?: Worker) => Promise; private readonly options: Auth0ClientOptions & { authorizationParams: AuthorizationParams; }; @@ -213,6 +215,8 @@ export class Auth0Client { this.nowProvider = this.options.nowProvider || DEFAULT_NOW_PROVIDER; + this.tokenEndpointHandler = this.options.tokenEndpointHandler || oauthToken; + this.cacheManager = new CacheManager( cache, !cache.allKeys @@ -1101,7 +1105,7 @@ export class Auth0Client { additionalParameters?: RequestTokenAdditionalParameters ) { const { nonceIn, organization } = additionalParameters || {}; - const authResult = await oauthToken( + const authResult = await this.tokenEndpointHandler( { baseUrl: this.domainUrl, client_id: this.options.clientId, diff --git a/src/global.ts b/src/global.ts index c86d3ca20..7007e3e08 100644 --- a/src/global.ts +++ b/src/global.ts @@ -269,6 +269,13 @@ export interface Auth0ClientOptions extends BaseLoginOptions { * **Note**: The worker is only used when `useRefreshTokens: true`, `cacheLocation: 'memory'`, and the `cache` is not custom. */ workerUrl?: string; + + /** + * If provided, the SDK will use this method to communicate with token endpoint. + * @param options + * @param worker + */ + tokenEndpointHandler?: (options: TokenEndpointOptions, worker?: Worker) => Promise; } /** diff --git a/src/index.ts b/src/index.ts index d1a559690..fb70bf87b 100644 --- a/src/index.ts +++ b/src/index.ts @@ -22,6 +22,8 @@ export async function createAuth0Client(options: Auth0ClientOptions) { export { Auth0Client }; +export { getJSON } from './http'; + export { GenericError, AuthenticationError,