Skip to content

Commit

Permalink
Merge pull request #177 from kaviththiranga/main
Browse files Browse the repository at this point in the history
Support passing custom params to token request via silent sign-in method
  • Loading branch information
brionmario authored Jul 30, 2024
2 parents 17ce783 + 543f54b commit ecdefbd
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 23 deletions.
30 changes: 16 additions & 14 deletions lib/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,8 @@ export class AsgardeoSPAClient {
*```
*/
public async trySignInSilently(
additionalParams?: Record<string, string | boolean>
additionalParams?: Record<string, string | boolean>,
tokenRequestConfig?: { params: Record<string, unknown> }
): Promise<BasicUserInfo | boolean | undefined> {
await this._isInitialized();

Expand All @@ -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;
});
}

/**
Expand Down
6 changes: 4 additions & 2 deletions lib/src/clients/main-thread-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, string | boolean> = {}
additionalParams?: Record<string, string | boolean>,
tokenRequestConfig?: { params: Record<string, unknown> }
): Promise<BasicUserInfo | boolean> => {

return await _authenticationHelper.trySignInSilently(
constructSilentSignInUrl,
requestAccessToken,
_sessionManagementHelper,
additionalParams
additionalParams,
tokenRequestConfig
);
};

Expand Down
6 changes: 4 additions & 2 deletions lib/src/clients/web-worker-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, string | boolean> = {}
additionalParams?: Record<string, string | boolean>,
tokenRequestConfig?: { params: Record<string, unknown> }
): Promise<BasicUserInfo | boolean> => {
return await _authenticationHelper.trySignInSilently(
constructSilentSignInUrl,
requestAccessToken,
_sessionManagementHelper,
additionalParams
additionalParams,
tokenRequestConfig
);
};

Expand Down
9 changes: 6 additions & 3 deletions lib/src/helpers/authentication-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -540,9 +540,11 @@ export class AuthenticationHelper<

public async trySignInSilently(
constructSilentSignInUrl: (additionalParams?: Record<string, string | boolean>) => Promise<string>,
requestAccessToken: (authzCode: string, sessionState: string, state: string) => Promise<BasicUserInfo>,
requestAccessToken: (authzCode: string, sessionState: string, state: string,
tokenRequestConfig?: { params: Record<string, unknown> }) => Promise<BasicUserInfo>,
sessionManagementHelper: SessionManagementHelperInterface,
additionalParams?: Record<string, string | boolean>
additionalParams?: Record<string, string | boolean>,
tokenRequestConfig?: { params: Record<string, unknown> }
): Promise<BasicUserInfo | boolean> {

// This block is executed by the iFrame when the server redirects with the authorization code.
Expand Down Expand Up @@ -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);
Expand Down
7 changes: 5 additions & 2 deletions lib/src/models/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,9 @@ export interface MainThreadClientInterface {
getDataLayer(): Promise<DataLayer<MainThreadClientConfig>>;
isAuthenticated(): Promise<boolean>;
updateConfig(config: Partial<AuthClientConfig<MainThreadClientConfig>>): Promise<void>;
trySignInSilently(additionalParams?: Record<string, string | boolean>): Promise<BasicUserInfo | boolean>;
trySignInSilently(additionalParams?: Record<string, string | boolean>,
tokenRequestConfig?: { params: Record<string, unknown> }
): Promise<BasicUserInfo | boolean>;
isSessionActive(): Promise<boolean>;
}

Expand Down Expand Up @@ -103,5 +105,6 @@ export interface WebWorkerClientInterface {
setHttpRequestFinishCallback(callback: () => void): void;
refreshAccessToken(): Promise<BasicUserInfo>;
updateConfig(config: Partial<AuthClientConfig<WebWorkerClientConfig>>): Promise<void>;
trySignInSilently(additionalParams?: Record<string, string | boolean>): Promise<BasicUserInfo | boolean>;
trySignInSilently(additionalParams?: Record<string, string | boolean>,
tokenRequestConfig?: { params: Record<string, unknown> }): Promise<BasicUserInfo | boolean>;
}

0 comments on commit ecdefbd

Please sign in to comment.