Skip to content

Commit

Permalink
Merge pull request #33 from lemoncloud-io/feature/louis-update-refresh
Browse files Browse the repository at this point in the history
feat: add refreshCachedTokenV2 method
  • Loading branch information
louis-lemon authored Sep 26, 2024
2 parents 671625b + f0ffb7e commit 8466897
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 8466897

Please sign in to comment.