Skip to content

Commit

Permalink
Support custom params for token request
Browse files Browse the repository at this point in the history
  • Loading branch information
kaviththiranga committed Jul 24, 2024
1 parent 9fe50c4 commit 0594053
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 21 deletions.
20 changes: 12 additions & 8 deletions lib/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,10 @@ export class AsgardeoSPAClient {
config?: SignInConfig,
authorizationCode?: string,
sessionState?: string,
state?: string
state?: string,
tokenRequestConfig?: {
params: Record<string, unknown>
}
): Promise<BasicUserInfo | undefined> {
await this._isInitialized();

Expand All @@ -384,15 +387,16 @@ export class AsgardeoSPAClient {

delete config?.callOnlyOnRedirect;

return this._client?.signIn(config, authorizationCode, sessionState, state).then((response: BasicUserInfo) => {
if (this._onSignInCallback) {
if (response.allowedScopes || response.displayName || response.email || response.username) {
this._onSignInCallback(response);
return this._client?.signIn(config, authorizationCode, sessionState, state, tokenRequestConfig)
.then((response: BasicUserInfo) => {
if (this._onSignInCallback) {
if (response.allowedScopes || response.displayName || response.email || response.username) {
this._onSignInCallback(response);
}
}
}

return response;
});
return response;
});
}

/**
Expand Down
19 changes: 14 additions & 5 deletions lib/src/clients/main-thread-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,12 +188,16 @@ export const MainThreadClient = async (
signInConfig?: GetAuthURLConfig,
authorizationCode?: string,
sessionState?: string,
state?: string
state?: string,
tokenRequestConfig?: {
params: Record<string, unknown>
}
): Promise<BasicUserInfo> => {

const basicUserInfo = await _authenticationHelper.handleSignIn(
shouldStopAuthn,
checkSession
checkSession,
undefined
);

if(basicUserInfo) {
Expand All @@ -217,7 +221,8 @@ export const MainThreadClient = async (

if (resolvedAuthorizationCode && resolvedState) {
setSessionStatus("true");
return requestAccessToken(resolvedAuthorizationCode, resolvedSessionState, resolvedState);
return requestAccessToken(resolvedAuthorizationCode, resolvedSessionState,
resolvedState, tokenRequestConfig);
}

return _authenticationClient.getAuthorizationURL(signInConfig).then(async (url: string) => {
Expand Down Expand Up @@ -304,14 +309,18 @@ export const MainThreadClient = async (
const requestAccessToken = async (
resolvedAuthorizationCode: string,
resolvedSessionState: string,
resolvedState: string
resolvedState: string,
tokenRequestConfig?: {
params: Record<string, unknown>
}
): Promise<BasicUserInfo> => {
return await _authenticationHelper.requestAccessToken(
resolvedAuthorizationCode,
resolvedSessionState,
checkSession,
undefined,
resolvedState
resolvedState,
tokenRequestConfig
);
};

Expand Down
16 changes: 12 additions & 4 deletions lib/src/clients/web-worker-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -473,7 +473,10 @@ export const WebWorkerClient = async (
const requestAccessToken = async (
resolvedAuthorizationCode: string,
resolvedSessionState: string,
resolvedState: string
resolvedState: string,
tokenRequestConfig?: {
params: Record<string, unknown>
}
): Promise<BasicUserInfo> => {
const config: AuthClientConfig<WebWorkerClientConfig> = await getConfigData();
const pkceKey: string = AuthenticationUtils.extractPKCEKeyFromStateParam(resolvedState);
Expand All @@ -483,7 +486,8 @@ export const WebWorkerClient = async (
code: resolvedAuthorizationCode,
pkce: config.enablePKCE ? SPAUtils.getPKCE(pkceKey) : undefined,
sessionState: resolvedSessionState,
state: resolvedState
state: resolvedState,
tokenRequestConfig
},
type: REQUEST_ACCESS_TOKEN
};
Expand Down Expand Up @@ -548,7 +552,10 @@ export const WebWorkerClient = async (
params?: GetAuthURLConfig,
authorizationCode?: string,
sessionState?: string,
state?: string
state?: string,
tokenRequestConfig?: {
params: Record<string, unknown>
}
): Promise<BasicUserInfo> => {

const basicUserInfo = await _authenticationHelper.handleSignIn(
Expand Down Expand Up @@ -577,7 +584,8 @@ export const WebWorkerClient = async (
}

if (resolvedAuthorizationCode && resolvedState) {
return requestAccessToken(resolvedAuthorizationCode, resolvedSessionState, resolvedState);
return requestAccessToken(resolvedAuthorizationCode, resolvedSessionState,
resolvedState, tokenRequestConfig);
}

return getAuthorizationURL(params)
Expand Down
7 changes: 5 additions & 2 deletions lib/src/helpers/authentication-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -471,7 +471,10 @@ export class AuthenticationHelper<
sessionState?: string,
checkSession?: () => Promise<void>,
pkce?: string,
state?: string
state?: string,
tokenRequestConfig?: {
params: Record<string, unknown>
}
): Promise<BasicUserInfo> {
const config = await this._dataLayer.getConfigData();

Expand All @@ -490,7 +493,7 @@ export class AuthenticationHelper<

if (authorizationCode) {
return this._authenticationClient
.requestAccessToken(authorizationCode, sessionState ?? "", state ?? "")
.requestAccessToken(authorizationCode, sessionState ?? "", state ?? "", undefined, tokenRequestConfig)
.then(async () => {
// Disable this temporarily
/* if (config.storage === Storage.BrowserMemory) {
Expand Down
10 changes: 8 additions & 2 deletions lib/src/models/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,10 @@ export interface MainThreadClientInterface {
config?: SignInConfig,
authorizationCode?: string,
sessionState?: string,
signInRedirectURL?: string
signInRedirectURL?: string,
tokenRequestConfig?: {
params: Record<string, unknown>
}
): Promise<BasicUserInfo>;
signOut(signOutRedirectURL?: string): Promise<boolean>;
requestCustomGrant(config: CustomGrantConfig): Promise<BasicUserInfo | FetchResponse>;
Expand Down Expand Up @@ -80,7 +83,10 @@ export interface WebWorkerClientInterface {
params?: SignInConfig,
authorizationCode?: string,
sessionState?: string,
signInRedirectURL?: string
signInRedirectURL?: string,
tokenRequestConfig?: {
params: Record<string, unknown>
}
): Promise<BasicUserInfo>;
signOut(signOutRedirectURL?: string): Promise<boolean>;
revokeAccessToken(): Promise<boolean>;
Expand Down
3 changes: 3 additions & 0 deletions lib/src/models/message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ export interface AuthorizationInfo {
sessionState: string;
pkce?: string;
state: string;
tokenRequestConfig?: {
params: Record<string, unknown>
}
}

export type MessageType =
Expand Down

0 comments on commit 0594053

Please sign in to comment.