Skip to content

Commit 3926c92

Browse files
committed
feat(minor): add token-manager
1 parent 5792501 commit 3926c92

File tree

4 files changed

+383
-42
lines changed

4 files changed

+383
-42
lines changed

src/core/aws-web.core.ts

Lines changed: 32 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,17 @@ import { calcSignature, LoggerService } from '../utils';
1717
import axios, { AxiosInstance, AxiosRequestConfig } from 'axios';
1818
import { AWSHttpRequestBuilder, HttpRequestBuilder } from '../http';
1919
import AWS from 'aws-sdk/global.js';
20+
import { LemonTokenManager } from './token-manager';
21+
import { RefreshableWebCore, TokenManager, TokenManagerConfig, TokenManagerEvents } from '../types/token-manager';
2022

2123
/**
2224
* AWSWebCore class implements AWS-based operations for Lemoncloud authentication logic
2325
*/
24-
export class AWSWebCore implements WebCoreService {
26+
export class AWSWebCore implements WebCoreService, RefreshableWebCore {
2527
private readonly tokenStorage: AWSStorageService;
2628
private readonly logger: LoggerService;
2729
private sharedAxiosInstance: AxiosInstance;
30+
private tokenManagerInstance: LemonTokenManager | null = null;
2831

2932
/**
3033
* Creates an instance of AWSWebCore.
@@ -36,6 +39,31 @@ export class AWSWebCore implements WebCoreService {
3639
this.sharedAxiosInstance = axios.create();
3740
}
3841

42+
/**
43+
* Gets or creates the token manager instance
44+
* @param config Optional configuration for the token manager
45+
* @param events Optional event handlers
46+
* @returns The token manager instance
47+
*/
48+
getTokenManager(config?: TokenManagerConfig, events?: TokenManagerEvents): TokenManager {
49+
if (this.tokenManagerInstance && !this.tokenManagerInstance.isRunning()) {
50+
this.tokenManagerInstance.destroy();
51+
this.tokenManagerInstance = null;
52+
}
53+
54+
if (!this.tokenManagerInstance) {
55+
this.tokenManagerInstance = new LemonTokenManager(this, config, events);
56+
57+
if (config?.autoStart !== false) {
58+
this.tokenManagerInstance.start().catch(error => {
59+
this.logger.error('TokenManager auto-start failed:', error);
60+
});
61+
}
62+
}
63+
64+
return this.tokenManagerInstance;
65+
}
66+
3967
/**
4068
* Gets the shared axios instance
4169
* @returns The shared axios instance
@@ -285,47 +313,6 @@ export class AWSWebCore implements WebCoreService {
285313
return await this.buildCredentialsByToken(refreshToken);
286314
}
287315

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-
}
299-
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-
);
320-
const refreshToken = {
321-
...(response.data.Token ? response.data.Token : response.data),
322-
identityToken: response.data.identityToken || cached.identityToken,
323-
identityPoolId: cached.identityPoolId,
324-
};
325-
this.logger.info('success to refresh token');
326-
return await this.buildCredentialsByToken(refreshToken);
327-
}
328-
329316
/**
330317
* Changes the user site and returns new AWS credentials.
331318
*
@@ -371,6 +358,9 @@ export class AWSWebCore implements WebCoreService {
371358
* @returns {Promise<boolean>} - A promise that resolves to false.
372359
*/
373360
async logout(): Promise<void> {
361+
this.tokenManagerInstance?.destroy();
362+
this.tokenManagerInstance = null;
363+
374364
AWS.config.credentials = null;
375365
await this.tokenStorage.clearOAuthToken();
376366
return;

0 commit comments

Comments
 (0)