Skip to content

Commit

Permalink
feat: add refreshCachedTokenV2 method
Browse files Browse the repository at this point in the history
  • Loading branch information
louis-lemon committed Sep 26, 2024
1 parent 671625b commit f0ffb7e
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions src/core/aws-web.core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,43 @@ export class AWSWebCore implements WebCoreService {
return await this.buildCredentialsByToken(refreshToken);
}

/**
* Refreshes the cached token new version
* @param {string} [domain=''] - The domain for the refresh request.
* @param {string} [url=''] - The request url for refresh token
* @returns {Promise<AWS.Credentials | null>} - The AWS credentials or null if refresh fails.
*/
async refreshCachedTokenV2(domain: string = '', url: string = '') {
const cached = await this.tokenStorage.getCachedOAuthToken();
const payload = {
authId: cached.authId,
accountId: cached.accountId,
identityId: cached.identityId,
identityToken: cached.identityToken,
};
const current = new Date().toISOString();
const signature = calcSignature(payload, current);

let body: RefreshTokenBody = { current, signature };
if (domain && domain.length > 0) {
body = { ...body, domain };
}

const response: HttpResponse<any> = await this.signedRequest(
'POST',
url ? url : `${this.config.oAuthEndpoint}/oauth/${cached.authId}/refresh`,
{},
{ ...body }
);
const refreshToken = {
...response.data.Token,
identityToken: response.data.Token?.identityToken || cached.identityToken,
identityPoolId: cached.identityPoolId,
};
this.logger.info('success to refresh token');
return await this.buildCredentialsByToken(refreshToken);
}

/**
* Changes the user site and returns new AWS credentials.
*
Expand Down

0 comments on commit f0ffb7e

Please sign in to comment.