Skip to content

Commit 778daf0

Browse files
authored
Merge pull request #44 from lemoncloud-io/feature/louis-check-identity-token
refactor: improve token refresh logic and caching
2 parents a058af4 + 11c12c7 commit 778daf0

File tree

3 files changed

+29
-68
lines changed

3 files changed

+29
-68
lines changed

src/core/aws-web.core.ts

Lines changed: 3 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -276,51 +276,12 @@ export class AWSWebCore implements WebCoreService {
276276
{},
277277
{ ...body }
278278
);
279-
const refreshToken = {
280-
identityToken: response.data.identityToken || cached.identityToken,
281-
identityPoolId: cached.identityPoolId,
282-
...(response.data.Token ? response.data.Token : response.data),
283-
};
284-
this.logger.info('success to refresh token');
285-
return await this.buildCredentialsByToken(refreshToken);
286-
}
287-
288-
/**
289-
* Refreshes the cached token new version
290-
* @param {string} [domain=''] - The domain for the refresh request.
291-
* @param {string} [url=''] - The request url for refresh token
292-
* @returns {Promise<AWS.Credentials | null>} - The AWS credentials or null if refresh fails.
293-
*/
294-
async refreshCachedTokenV2(domain: string = '', url: string = '') {
295-
const cached = await this.tokenStorage.getCachedOAuthToken();
296-
if (!cached.authId) {
297-
throw new Error('authId is required for token refresh');
298-
}
299279

300-
const payload = {
301-
authId: cached.authId,
302-
accountId: cached.accountId,
303-
identityId: cached.identityId,
304-
identityToken: cached.identityToken,
305-
};
306-
const current = new Date().toISOString();
307-
const signature = calcSignature(payload, current);
308-
309-
let body: RefreshTokenBody = { current, signature };
310-
if (domain && domain.length > 0) {
311-
body = { ...body, domain };
312-
}
313-
314-
const response: HttpResponse<any> = await this.signedRequest(
315-
'POST',
316-
url ? url : `${this.config.oAuthEndpoint}/oauth/${cached.authId}/refresh`,
317-
{},
318-
{ ...body }
319-
);
280+
const tokenData = response.data.Token || response.data;
320281
const refreshToken = {
321-
...(response.data.Token ? response.data.Token : response.data),
322-
identityToken: response.data.identityToken || cached.identityToken,
282+
identityToken: tokenData.identityToken || cached.identityToken,
323283
identityPoolId: cached.identityPoolId,
284+
...tokenData,
324285
};
325286
this.logger.info('success to refresh token');
326287
return await this.buildCredentialsByToken(refreshToken);

src/token-storage/aws-storage.service.ts

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -48,29 +48,29 @@ export class AWSStorageService extends TokenStorageService {
4848
const issuedTime = +(await this.storage.getItem(`${this.prefix}.issued_time`));
4949
const now = new Date().getTime();
5050

51-
if (!expiredTime) {
52-
return true;
53-
}
54-
if (now >= expiredTime) {
55-
return true;
51+
if (!expiredTime || expiredTime <= 0) {
52+
return false;
5653
}
5754

58-
// 전략 1: 고정 버퍼 (5분 전에 refresh)
59-
const bufferTime = 5 * 60 * 1000;
60-
if (now >= expiredTime - bufferTime) {
55+
if (now >= expiredTime) {
6156
return true;
6257
}
6358

64-
// 전략 2: 토큰 수명의 75% 지점에서 refresh
65-
if (issuedTime) {
59+
if (issuedTime && issuedTime > 0) {
6660
const tokenLifetime = expiredTime - issuedTime;
67-
const refreshThreshold = issuedTime + tokenLifetime * 0.75;
68-
if (now >= refreshThreshold) {
61+
if (tokenLifetime <= 0) {
6962
return true;
7063
}
64+
65+
const lifetimeThreshold = issuedTime + tokenLifetime * 0.75; // 75%
66+
const bufferThreshold = expiredTime - 5 * 60 * 1000;
67+
68+
const refreshThreshold = Math.min(lifetimeThreshold, bufferThreshold);
69+
return now >= refreshThreshold;
7170
}
7271

73-
return false;
72+
const bufferTime = 5 * 60 * 1000;
73+
return now >= expiredTime - bufferTime;
7474
}
7575

7676
async getCachedCredentials(): Promise<LemonCredentials> {

src/token-storage/azure-storage.service.ts

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -62,29 +62,29 @@ export class AzureStorageService extends TokenStorageService {
6262
const issuedTime = +(await this.storage.getItem(`${this.prefix}.issued_time`));
6363
const now = new Date().getTime();
6464

65-
if (!expiredTime) {
66-
return true;
67-
}
68-
if (now >= expiredTime) {
69-
return true;
65+
if (!expiredTime || expiredTime <= 0) {
66+
return false;
7067
}
7168

72-
// 전략 1: 고정 버퍼 (5분 전에 refresh)
73-
const bufferTime = 5 * 60 * 1000;
74-
if (now >= expiredTime - bufferTime) {
69+
if (now >= expiredTime) {
7570
return true;
7671
}
7772

78-
// 전략 2: 토큰 수명의 75% 지점에서 refresh
79-
if (issuedTime) {
73+
if (issuedTime && issuedTime > 0) {
8074
const tokenLifetime = expiredTime - issuedTime;
81-
const refreshThreshold = issuedTime + tokenLifetime * 0.75;
82-
if (now >= refreshThreshold) {
75+
if (tokenLifetime <= 0) {
8376
return true;
8477
}
78+
79+
const lifetimeThreshold = issuedTime + tokenLifetime * 0.75; // 75%
80+
const bufferThreshold = expiredTime - 5 * 60 * 1000;
81+
82+
const refreshThreshold = Math.min(lifetimeThreshold, bufferThreshold);
83+
return now >= refreshThreshold;
8584
}
8685

87-
return false;
86+
const bufferTime = 5 * 60 * 1000;
87+
return now >= expiredTime - bufferTime;
8888
}
8989

9090
/**

0 commit comments

Comments
 (0)