From 543f54bec2fe30cdec3017f3246e2e8a72571f4f Mon Sep 17 00:00:00 2001 From: Kavith Lokuhewage Date: Tue, 30 Jul 2024 11:07:31 +0530 Subject: [PATCH] Support passing custom params to token request vi silent sign-in method --- lib/src/client.ts | 30 +++++++++++++----------- lib/src/clients/main-thread-client.ts | 6 +++-- lib/src/clients/web-worker-client.ts | 6 +++-- lib/src/helpers/authentication-helper.ts | 9 ++++--- lib/src/models/client.ts | 7 ++++-- 5 files changed, 35 insertions(+), 23 deletions(-) diff --git a/lib/src/client.ts b/lib/src/client.ts index 058a7347..2df64ce0 100755 --- a/lib/src/client.ts +++ b/lib/src/client.ts @@ -416,7 +416,8 @@ export class AsgardeoSPAClient { *``` */ public async trySignInSilently( - additionalParams?: Record + additionalParams?: Record, + tokenRequestConfig?: { params: Record } ): Promise { await this._isInitialized(); @@ -425,21 +426,22 @@ export class AsgardeoSPAClient { return; } - return this._client?.trySignInSilently(additionalParams).then((response: BasicUserInfo | boolean) => { - if (this._onSignInCallback && response) { - const basicUserInfo = response as BasicUserInfo; - if ( - basicUserInfo.allowedScopes || - basicUserInfo.displayName || - basicUserInfo.email || - basicUserInfo.username - ) { - this._onSignInCallback(basicUserInfo); + return this._client?.trySignInSilently(additionalParams, tokenRequestConfig) + .then((response: BasicUserInfo | boolean) => { + if (this._onSignInCallback && response) { + const basicUserInfo = response as BasicUserInfo; + if ( + basicUserInfo.allowedScopes || + basicUserInfo.displayName || + basicUserInfo.email || + basicUserInfo.username + ) { + this._onSignInCallback(basicUserInfo); + } } - } - return response; - }); + return response; + }); } /** diff --git a/lib/src/clients/main-thread-client.ts b/lib/src/clients/main-thread-client.ts index b207450e..10b241c1 100755 --- a/lib/src/clients/main-thread-client.ts +++ b/lib/src/clients/main-thread-client.ts @@ -371,14 +371,16 @@ export const MainThreadClient = async ( * if the user is signed in or with `false` if there is no active user session in the server. */ const trySignInSilently = async ( - additionalParams: Record = {} + additionalParams?: Record, + tokenRequestConfig?: { params: Record } ): Promise => { return await _authenticationHelper.trySignInSilently( constructSilentSignInUrl, requestAccessToken, _sessionManagementHelper, - additionalParams + additionalParams, + tokenRequestConfig ); }; diff --git a/lib/src/clients/web-worker-client.ts b/lib/src/clients/web-worker-client.ts index 1a05ae39..ecb8744b 100755 --- a/lib/src/clients/web-worker-client.ts +++ b/lib/src/clients/web-worker-client.ts @@ -431,13 +431,15 @@ export const WebWorkerClient = async ( * if the user is signed in or with `false` if there is no active user session in the server. */ const trySignInSilently = async ( - additionalParams: Record = {} + additionalParams?: Record, + tokenRequestConfig?: { params: Record } ): Promise => { return await _authenticationHelper.trySignInSilently( constructSilentSignInUrl, requestAccessToken, _sessionManagementHelper, - additionalParams + additionalParams, + tokenRequestConfig ); }; diff --git a/lib/src/helpers/authentication-helper.ts b/lib/src/helpers/authentication-helper.ts index ca0c59d6..824514a3 100644 --- a/lib/src/helpers/authentication-helper.ts +++ b/lib/src/helpers/authentication-helper.ts @@ -540,9 +540,11 @@ export class AuthenticationHelper< public async trySignInSilently( constructSilentSignInUrl: (additionalParams?: Record) => Promise, - requestAccessToken: (authzCode: string, sessionState: string, state: string) => Promise, + requestAccessToken: (authzCode: string, sessionState: string, state: string, + tokenRequestConfig?: { params: Record }) => Promise, sessionManagementHelper: SessionManagementHelperInterface, - additionalParams?: Record + additionalParams?: Record, + tokenRequestConfig?: { params: Record } ): Promise { // This block is executed by the iFrame when the server redirects with the authorization code. @@ -590,7 +592,8 @@ export class AuthenticationHelper< } if (data?.type == CHECK_SESSION_SIGNED_IN && data?.data?.code) { - requestAccessToken(data?.data?.code, data?.data?.sessionState, data?.data?.state) + requestAccessToken(data?.data?.code, data?.data?.sessionState, + data?.data?.state, tokenRequestConfig) .then((response: BasicUserInfo) => { window.removeEventListener("message", listenToPromptNoneIFrame); resolve(response); diff --git a/lib/src/models/client.ts b/lib/src/models/client.ts index a0d14730..2b4d98fa 100755 --- a/lib/src/models/client.ts +++ b/lib/src/models/client.ts @@ -68,7 +68,9 @@ export interface MainThreadClientInterface { getDataLayer(): Promise>; isAuthenticated(): Promise; updateConfig(config: Partial>): Promise; - trySignInSilently(additionalParams?: Record): Promise; + trySignInSilently(additionalParams?: Record, + tokenRequestConfig?: { params: Record } + ): Promise; isSessionActive(): Promise; } @@ -103,5 +105,6 @@ export interface WebWorkerClientInterface { setHttpRequestFinishCallback(callback: () => void): void; refreshAccessToken(): Promise; updateConfig(config: Partial>): Promise; - trySignInSilently(additionalParams?: Record): Promise; + trySignInSilently(additionalParams?: Record, + tokenRequestConfig?: { params: Record }): Promise; }