@@ -17,14 +17,17 @@ import { calcSignature, LoggerService } from '../utils';
17
17
import axios , { AxiosInstance , AxiosRequestConfig } from 'axios' ;
18
18
import { AWSHttpRequestBuilder , HttpRequestBuilder } from '../http' ;
19
19
import AWS from 'aws-sdk/global.js' ;
20
+ import { LemonTokenManager } from './token-manager' ;
21
+ import { RefreshableWebCore , TokenManager , TokenManagerConfig , TokenManagerEvents } from '../types/token-manager' ;
20
22
21
23
/**
22
24
* AWSWebCore class implements AWS-based operations for Lemoncloud authentication logic
23
25
*/
24
- export class AWSWebCore implements WebCoreService {
26
+ export class AWSWebCore implements WebCoreService , RefreshableWebCore {
25
27
private readonly tokenStorage : AWSStorageService ;
26
28
private readonly logger : LoggerService ;
27
29
private sharedAxiosInstance : AxiosInstance ;
30
+ private tokenManagerInstance : LemonTokenManager | null = null ;
28
31
29
32
/**
30
33
* Creates an instance of AWSWebCore.
@@ -36,6 +39,31 @@ export class AWSWebCore implements WebCoreService {
36
39
this . sharedAxiosInstance = axios . create ( ) ;
37
40
}
38
41
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
+
39
67
/**
40
68
* Gets the shared axios instance
41
69
* @returns The shared axios instance
@@ -285,47 +313,6 @@ export class AWSWebCore implements WebCoreService {
285
313
return await this . buildCredentialsByToken ( refreshToken ) ;
286
314
}
287
315
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
-
329
316
/**
330
317
* Changes the user site and returns new AWS credentials.
331
318
*
@@ -371,6 +358,9 @@ export class AWSWebCore implements WebCoreService {
371
358
* @returns {Promise<boolean> } - A promise that resolves to false.
372
359
*/
373
360
async logout ( ) : Promise < void > {
361
+ this . tokenManagerInstance ?. destroy ( ) ;
362
+ this . tokenManagerInstance = null ;
363
+
374
364
AWS . config . credentials = null ;
375
365
await this . tokenStorage . clearOAuthToken ( ) ;
376
366
return ;
0 commit comments