Skip to content

Commit

Permalink
Merge pull request #256 from kaviththiranga/master
Browse files Browse the repository at this point in the history
Support custom params for token request via sign-in method
  • Loading branch information
brionmario authored Jul 29, 2024
2 parents 9769b04 + 844637b commit b7fb3c4
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 5 deletions.
14 changes: 13 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,7 @@ auth.getAuthorizationURL(config).then((url)=>{
### requestAccessToken

```TypeScript
requestAccessToken(authorizationCode: string, sessionState: string, state: string, userID?: string): Promise<TokenResponse>
requestAccessToken(authorizationCode: string, sessionState: string, state: string, userID?: string, tokenRequestConfig: { params: Record<string, unknown> }): Promise<TokenResponse>
```

#### Arguments
Expand All @@ -435,6 +435,18 @@ requestAccessToken(authorizationCode: string, sessionState: string, state: strin

If you want to use the SDK to manage multiple user sessions, you can pass a unique ID here to request an access token specific to that user. This can be useful when this SDK is used in backend applications.

5. tokenRequestConfig: `object` (optional)

An optional configuration object that allows you to augment the token request.

- `params` (Mandatory): Key-value pairs to be sent as additional parameters in the token request payload.


```TypeScript
tokenRequestConfig: {
params: Record<string, unknown>
}
```
#### Returns

A Promise that resolves with the [`TokenResponse`](#TokenResponse) object.
Expand Down
11 changes: 8 additions & 3 deletions lib/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -283,14 +283,19 @@ export class AsgardeoAuthClient<T> {
authorizationCode: string,
sessionState: string,
state: string,
userID?: string
userID?: string,
tokenRequestConfig?: {
params: Record<string, unknown>
}
): Promise<TokenResponse> {
if (await this._dataLayer.getTemporaryDataParameter(OP_CONFIG_INITIATED)) {
return this._authenticationCore.requestAccessToken(authorizationCode, sessionState, state, userID);
return this._authenticationCore.requestAccessToken(authorizationCode, sessionState,
state, userID, tokenRequestConfig);
}

return this._authenticationCore.getOIDCProviderMetaData(false).then(() => {
return this._authenticationCore.requestAccessToken(authorizationCode, sessionState, state, userID);
return this._authenticationCore.requestAccessToken(authorizationCode, sessionState,
state, userID, tokenRequestConfig);
});
}

Expand Down
11 changes: 10 additions & 1 deletion lib/src/core/authentication-core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,10 @@ export class AuthenticationCore<T> {
authorizationCode: string,
sessionState: string,
state: string,
userID?: string
userID?: string,
tokenRequestConfig?: {
params: Record<string, unknown>
}
): Promise<TokenResponse> {
const tokenEndpoint: string | undefined = (await this._oidcProviderMetaData()).token_endpoint;
const configData: StrictAuthClientConfig = await this._config();
Expand Down Expand Up @@ -197,6 +200,12 @@ export class AuthenticationCore<T> {
body.set("grant_type", "authorization_code");
body.set("redirect_uri", configData.signInRedirectURL);

if (tokenRequestConfig?.params) {
Object.entries(tokenRequestConfig.params).forEach(([ key, value ]: [key: string, value: unknown]) => {
body.append(key, value as string);
});
}

if (configData.enablePKCE) {
body.set(
"code_verifier", `${await this._dataLayer.getTemporaryDataParameter(
Expand Down

0 comments on commit b7fb3c4

Please sign in to comment.