@@ -30,7 +30,7 @@ import { showAmazonQWalkthroughOnce } from '../../amazonq/onboardingPage/walkthr
3030import { setContext } from '../../shared/vscode/setContext'
3131import { openUrl } from '../../shared/utilities/vsCodeUtils'
3232import { telemetry } from '../../shared/telemetry/telemetry'
33- import { AuthStateEvent , cacheChangedEvent , LanguageClientAuth , Login , SsoLogin , IamLogin } from '../../auth/auth2'
33+ import { AuthStateEvent , cacheChangedEvent , stsCacheChangedEvent , LanguageClientAuth , Login , SsoLogin , IamLogin } from '../../auth/auth2'
3434import { builderIdStartUrl , internalStartUrl } from '../../auth/sso/constants'
3535import { VSCODE_EXTENSION_ID } from '../../shared/extensions'
3636import { RegionProfileManager } from '../region/regionProfileManager'
@@ -100,6 +100,7 @@ export class AuthUtil implements IAuthProvider {
100100 await this . setVscodeContextProps ( )
101101 } )
102102 lspAuth . registerCacheWatcher ( async ( event : cacheChangedEvent ) => await this . cacheChangedHandler ( event ) )
103+ lspAuth . registerStsCacheWatcher ( async ( event : stsCacheChangedEvent ) => await this . stsCacheChangedHandler ( event ) )
103104 }
104105
105106 // Do NOT use this in production code, only used for testing
@@ -349,6 +350,15 @@ export class AuthUtil implements IAuthProvider {
349350 }
350351 }
351352
353+ private async stsCacheChangedHandler ( event : stsCacheChangedEvent ) {
354+ this . logger . debug ( `Sts Cache change event received: ${ event } ` )
355+ if ( event === 'delete' ) {
356+ await this . logout ( )
357+ } else if ( event === 'create' ) {
358+ await this . restore ( )
359+ }
360+ }
361+
352362 private async stateChangeHandler ( e : AuthStateEvent ) {
353363 if ( e . state === 'refreshed' ) {
354364 const params = this . session ? ( await this . session . getCredential ( ) ) . updateCredentialsParams : undefined
@@ -365,7 +375,12 @@ export class AuthUtil implements IAuthProvider {
365375
366376 private async refreshState ( state = this . getAuthState ( ) ) {
367377 if ( state === 'expired' || state === 'notConnected' ) {
368- this . lspAuth . deleteBearerToken ( )
378+ if ( this . isSsoSession ( ) ) {
379+ this . lspAuth . deleteBearerToken ( )
380+ }
381+ else if ( this . isIamSession ( ) ) {
382+ this . lspAuth . deleteIamCredential ( )
383+ }
369384 if ( this . isIdcConnection ( ) ) {
370385 await this . regionProfileManager . invalidateProfile ( this . regionProfileManager . activeRegionProfile ?. arn )
371386 await this . regionProfileManager . clearCache ( )
0 commit comments