@@ -19,32 +19,35 @@ class AccessTokenManager {
1919 private secondaryToken : string ; // refreshToken || tokenSecret
2020 private tokenUrl : string ; // tokenURL to refresh accessToken
2121 private expires_in : any ;
22- private data : any ; // value of key(keyId) in teamSettings that needs to be updated if required
23- private keyId : any ; // key of object in teamSettings
22+ private tokensData : any ; // Full tokens data object
23+ private keyId : any ; // key of object in teamSettings
2424 private logger : any ; // Use to log console in debugger
2525 private agent : Agent ;
26+ private isNewStructure : boolean ;
2627 constructor (
2728 clientId : string ,
2829 clientSecret : string ,
2930 secondaryToken : string ,
3031 tokenUrl : string ,
3132 expires_in : any ,
3233 primaryToken : string ,
33- data : any ,
34+ tokensData : any ,
3435 keyId : any ,
3536 logger : any ,
3637 agent : Agent ,
38+ isNewStructure : boolean = false ,
3739 ) {
3840 this . clientId = clientId ;
3941 this . clientSecret = clientSecret ;
4042 this . primaryToken = primaryToken ;
4143 this . secondaryToken = secondaryToken ;
4244 this . tokenUrl = tokenUrl ;
4345 this . expires_in = expires_in ;
44- this . data = data ;
46+ this . tokensData = tokensData ;
4547 this . keyId = keyId ;
4648 this . logger = logger ;
4749 this . agent = agent ;
50+ this . isNewStructure = isNewStructure ;
4851 }
4952
5053 async getAccessToken ( ) : Promise < string > {
@@ -105,19 +108,52 @@ class AccessTokenManager {
105108 this . logger . debug ( 'Access token refreshed successfully.' ) ;
106109 const expiresInMilliseconds : number = response ?. data ?. expires_in ? response ?. data ?. expires_in * 1000 : response ?. data ?. expires_in ;
107110 const expirationTimestamp : number = expiresInMilliseconds ? new Date ( ) . getTime ( ) + expiresInMilliseconds : expiresInMilliseconds ;
108- this . data . primary = newAccessToken ;
109- this . data . expires_in = expirationTimestamp ? expirationTimestamp ?. toString ( ) : expirationTimestamp ;
110- //const oauthTeamSettings = new OauthTeamSettings();
111- //const save: any = await oauthTeamSettings.update({ keyId: this.keyId, data: this.data });
112111
113- const save : any = await managedVault . user ( AccessCandidate . agent ( this . agent . id ) ) . set ( this . keyId , JSON . stringify ( this . data ) ) ;
112+ // Maintain the same structure format when saving
113+ let updatedData ;
114+ if ( this . isNewStructure ) {
115+ // Maintain new structure format
116+ updatedData = {
117+ ...this . tokensData ,
118+ auth_data : {
119+ ...( this . tokensData ?. auth_data ?? { } ) ,
120+ primary : newAccessToken ,
121+ // Persist rotated refresh_token when provided; fall back to existing
122+ secondary : ( response ?. data ?. refresh_token ?? this . secondaryToken ) ,
123+ // Use nullish check so 0 is preserved
124+ expires_in : ( expirationTimestamp ?? undefined ) !== undefined ? String ( expirationTimestamp ) : undefined
125+ }
126+ } ;
127+ } else {
128+ // Maintain old structure format
129+ updatedData = {
130+ ...this . tokensData ,
131+ primary : newAccessToken ,
132+ expires_in : ( expirationTimestamp ?? undefined ) !== undefined ? String ( expirationTimestamp ) : undefined
133+ } ;
134+ // Persist rotated refresh_token when provided; otherwise keep existing
135+ updatedData . secondary = ( response ?. data ?. refresh_token ?? this . secondaryToken ) ;
136+ }
137+
138+ const save : any = await managedVault . user ( AccessCandidate . agent ( this . agent . id ) ) . set ( this . keyId , JSON . stringify ( updatedData ) ) ;
114139 if ( save && save . status === 200 ) {
115140 console . log ( 'Access token value is updated successfully.' ) ;
116141 this . logger . debug ( 'Access token value is updated successfully.' ) ;
117142 } else {
118143 console . log ( 'Warning: new access token value is not updated.' ) ;
119144 this . logger . debug ( 'Warning: new access token value is not updated.' ) ;
120145 }
146+
147+ // Update internal tokensData reference
148+ this . tokensData = updatedData ;
149+ this . primaryToken = newAccessToken ;
150+ // Update in-memory refresh token in case the provider rotated it
151+ this . secondaryToken = ( response ?. data ?. refresh_token ?? this . secondaryToken ) ;
152+ // Preserve 0 and avoid dropping undefined
153+ this . expires_in =
154+ ( expirationTimestamp ?? undefined ) !== undefined
155+ ? String ( expirationTimestamp )
156+ : undefined ;
121157 return newAccessToken ;
122158 } catch ( error ) {
123159 console . error ( 'Failed to refresh access token:' , error ) ;
0 commit comments