@@ -30,7 +30,16 @@ import { showAmazonQWalkthroughOnce } from '../../amazonq/onboardingPage/walkthr
30
30
import { setContext } from '../../shared/vscode/setContext'
31
31
import { openUrl } from '../../shared/utilities/vsCodeUtils'
32
32
import { telemetry } from '../../shared/telemetry/telemetry'
33
- import { AuthStateEvent , cacheChangedEvent , LanguageClientAuth , Login , SsoLogin , IamLogin } from '../../auth/auth2'
33
+ import {
34
+ AuthStateEvent ,
35
+ cacheChangedEvent ,
36
+ LanguageClientAuth ,
37
+ Login ,
38
+ SsoLogin ,
39
+ IamLogin ,
40
+ AuthState ,
41
+ LoginTypes ,
42
+ } from '../../auth/auth2'
34
43
import { builderIdStartUrl , internalStartUrl } from '../../auth/sso/constants'
35
44
import { VSCODE_EXTENSION_ID } from '../../shared/extensions'
36
45
import { RegionProfileManager } from '../region/regionProfileManager'
@@ -108,11 +117,11 @@ export class AuthUtil implements IAuthProvider {
108
117
}
109
118
110
119
isSsoSession ( ) : boolean {
111
- return this . session instanceof SsoLogin
120
+ return this . session ?. loginType === LoginTypes . SSO
112
121
}
113
122
114
123
isIamSession ( ) : boolean {
115
- return this . session instanceof IamLogin
124
+ return this . session ?. loginType === LoginTypes . IAM
116
125
}
117
126
118
127
/**
@@ -204,36 +213,31 @@ export class AuthUtil implements IAuthProvider {
204
213
}
205
214
206
215
async getToken ( ) {
207
- if ( this . session ) {
208
- const token = ( await this . session . getCredential ( ) ) . credential
209
- if ( typeof token !== 'string' ) {
210
- throw new ToolkitError ( 'Cannot get token with IAM session' )
211
- }
212
- return token
216
+ if ( this . isSsoSession ( ) ) {
217
+ const response = await this . session ! . getCredential ( )
218
+ return response . credential as string
213
219
} else {
214
- throw new ToolkitError ( 'Cannot get credential without logging in.' )
220
+ throw new ToolkitError ( 'Cannot get credential without logging in with SSO .' )
215
221
}
216
222
}
217
223
218
224
async getIamCredential ( ) {
219
- if ( this . session ) {
220
- const credential = ( await this . session . getCredential ( ) ) . credential
221
- if ( typeof credential !== 'object' ) {
222
- throw new ToolkitError ( 'Cannot get token with SSO session' )
223
- }
224
- return credential
225
+ if ( this . isIamSession ( ) ) {
226
+ const response = await this . session ! . getCredential ( )
227
+ return response . credential as IamCredentials
225
228
} else {
226
- throw new ToolkitError ( 'Cannot get credential without logging in.' )
229
+ throw new ToolkitError ( 'Cannot get credential without logging in with IAM .' )
227
230
}
228
231
}
229
232
230
233
get connection ( ) {
231
234
return this . session ?. data
232
235
}
233
236
234
- getAuthState ( ) {
235
- if ( this . session ) {
236
- return this . session . getConnectionState ( )
237
+ getAuthState ( ) : AuthState {
238
+ // Check if getConnectionState exists in case of type casts
239
+ if ( typeof this . session ?. getConnectionState === 'function' ) {
240
+ return this . session ! . getConnectionState ( )
237
241
} else {
238
242
return 'notConnected'
239
243
}
@@ -356,11 +360,12 @@ export class AuthUtil implements IAuthProvider {
356
360
357
361
private async stateChangeHandler ( e : AuthStateEvent ) {
358
362
if ( e . state === 'refreshed' ) {
359
- const params = this . session ? ( await this . session . getCredential ( ) ) . updateCredentialsParams : undefined
360
363
if ( this . isSsoSession ( ) ) {
361
- await this . lspAuth . updateBearerToken ( params )
364
+ const params = await this . session ! . getCredential ( )
365
+ await this . lspAuth . updateBearerToken ( params . updateCredentialsParams )
362
366
} else if ( this . isIamSession ( ) ) {
363
- await this . lspAuth . updateIamCredential ( params )
367
+ const params = await this . session ! . getCredential ( )
368
+ await this . lspAuth . updateIamCredential ( params . updateCredentialsParams )
364
369
}
365
370
} else {
366
371
this . logger . info ( `codewhisperer: connection changed to ${ e . state } ` )
@@ -383,11 +388,12 @@ export class AuthUtil implements IAuthProvider {
383
388
this . session = undefined
384
389
}
385
390
if ( state === 'connected' ) {
386
- const params = this . session ? ( await this . session . getCredential ( ) ) . updateCredentialsParams : undefined
387
391
if ( this . isSsoSession ( ) ) {
388
- await this . lspAuth . updateBearerToken ( params )
392
+ const params = await this . session ! . getCredential ( )
393
+ await this . lspAuth . updateBearerToken ( params . updateCredentialsParams )
389
394
} else if ( this . isIamSession ( ) ) {
390
- await this . lspAuth . updateIamCredential ( params )
395
+ const params = await this . session ! . getCredential ( )
396
+ await this . lspAuth . updateIamCredential ( params . updateCredentialsParams )
391
397
}
392
398
393
399
if ( this . isIdcConnection ( ) ) {
0 commit comments