@@ -8,7 +8,7 @@ import globals from '../../shared/extensionGlobals'
8
8
import { SSOOIDCServiceException } from '@aws-sdk/client-sso-oidc'
9
9
import { openSsoPortalLink , SsoToken , ClientRegistration , isExpired , SsoProfile } from './model'
10
10
import { getCache } from './cache'
11
- import { hasProps , selectFrom } from '../../shared/utilities/tsUtils'
11
+ import { hasProps , RequiredProps , selectFrom } from '../../shared/utilities/tsUtils'
12
12
import { CancellationError } from '../../shared/utilities/timeoutUtils'
13
13
import { OidcClient } from './clients'
14
14
import { loadOr } from '../../shared/utilities/cacheUtils'
@@ -72,16 +72,16 @@ export class SsoAccessTokenProvider {
72
72
return data ?. token
73
73
}
74
74
75
- await this . invalidate ( )
76
-
77
- if ( data . registration && ! isExpired ( data . registration ) ) {
75
+ if ( data . registration && ! isExpired ( data . registration ) && hasProps ( data . token , 'refreshToken' ) ) {
78
76
const refreshed = await this . refreshToken ( data . token , data . registration )
79
77
80
78
if ( refreshed ) {
81
79
await this . cache . token . save ( this . tokenCacheKey , refreshed )
82
80
}
83
81
84
82
return refreshed ?. token
83
+ } else {
84
+ await this . invalidate ( )
85
85
}
86
86
}
87
87
@@ -99,21 +99,27 @@ export class SsoAccessTokenProvider {
99
99
100
100
try {
101
101
return await this . authorize ( registration )
102
- } catch ( error ) {
103
- if ( error instanceof SSOOIDCServiceException && isClientFault ( error ) ) {
102
+ } catch ( err ) {
103
+ if ( err instanceof SSOOIDCServiceException && isClientFault ( err ) ) {
104
104
this . cache . registration . clear ( cacheKey )
105
105
}
106
106
107
- throw error
107
+ throw err
108
108
}
109
109
}
110
110
111
- private async refreshToken ( token : SsoToken , registration : ClientRegistration ) {
112
- if ( hasProps ( token , 'refreshToken' ) ) {
111
+ private async refreshToken ( token : RequiredProps < SsoToken , 'refreshToken' > , registration : ClientRegistration ) {
112
+ try {
113
113
const clientInfo = selectFrom ( registration , 'clientId' , 'clientSecret' )
114
114
const response = await this . oidc . createToken ( { ...clientInfo , ...token , grantType : REFRESH_GRANT_TYPE } )
115
115
116
116
return this . formatToken ( response , registration )
117
+ } catch ( err ) {
118
+ if ( err instanceof SSOOIDCServiceException && isClientFault ( err ) ) {
119
+ this . cache . token . clear ( this . tokenCacheKey )
120
+ }
121
+
122
+ throw err
117
123
}
118
124
}
119
125
0 commit comments