@@ -30,7 +30,15 @@ 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 {
34+ AuthStateEvent ,
35+ cacheChangedEvent ,
36+ LanguageClientAuth ,
37+ Login ,
38+ SsoLogin ,
39+ IamLogin ,
40+ AuthState ,
41+ } from '../../auth/auth2'
3442import { builderIdStartUrl , internalStartUrl } from '../../auth/sso/constants'
3543import { VSCODE_EXTENSION_ID } from '../../shared/extensions'
3644import { RegionProfileManager } from '../region/regionProfileManager'
@@ -204,36 +212,30 @@ export class AuthUtil implements IAuthProvider {
204212 }
205213
206214 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
215+ if ( this . isSsoSession ( ) ) {
216+ const response = await this . session ! . getCredential ( )
217+ return response . credential as string
213218 } else {
214- throw new ToolkitError ( 'Cannot get credential without logging in.' )
219+ throw new ToolkitError ( 'Cannot get credential without logging in with SSO .' )
215220 }
216221 }
217222
218223 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 credential with SSO session' )
223- }
224- return credential
224+ if ( this . isIamSession ( ) ) {
225+ const response = await this . session ! . getCredential ( )
226+ return response . credential as IamCredentials
225227 } else {
226- throw new ToolkitError ( 'Cannot get credential without logging in.' )
228+ throw new ToolkitError ( 'Cannot get credential without logging in with IAM .' )
227229 }
228230 }
229231
230232 get connection ( ) {
231233 return this . session ?. data
232234 }
233235
234- getAuthState ( ) {
235- if ( this . session ) {
236- return this . session . getConnectionState ( )
236+ getAuthState ( ) : AuthState {
237+ if ( this . isSsoSession ( ) || this . isIamSession ( ) ) {
238+ return this . session ! . getConnectionState ( )
237239 } else {
238240 return 'notConnected'
239241 }
@@ -255,10 +257,6 @@ export class AuthUtil implements IAuthProvider {
255257 return Boolean ( this . connection ?. startUrl && this . connection ?. startUrl !== builderIdStartUrl )
256258 }
257259
258- isIamConnection ( ) {
259- return Boolean ( this . connection ?. accessKey && this . connection ?. secretKey )
260- }
261-
262260 isInternalAmazonUser ( ) : boolean {
263261 return this . isConnected ( ) && this . connection ?. startUrl === internalStartUrl
264262 }
@@ -360,11 +358,12 @@ export class AuthUtil implements IAuthProvider {
360358
361359 private async stateChangeHandler ( e : AuthStateEvent ) {
362360 if ( e . state === 'refreshed' ) {
363- const params = this . session ? ( await this . session . getCredential ( ) ) . updateCredentialsParams : undefined
364361 if ( this . isSsoSession ( ) ) {
365- await this . lspAuth . updateBearerToken ( params )
362+ const params = await this . session ! . getCredential ( )
363+ await this . lspAuth . updateBearerToken ( params . updateCredentialsParams )
366364 } else if ( this . isIamSession ( ) ) {
367- await this . lspAuth . updateIamCredential ( params )
365+ const params = await this . session ! . getCredential ( )
366+ await this . lspAuth . updateIamCredential ( params . updateCredentialsParams )
368367 }
369368 } else {
370369 this . logger . info ( `codewhisperer: connection changed to ${ e . state } ` )
@@ -387,11 +386,12 @@ export class AuthUtil implements IAuthProvider {
387386 this . session = undefined
388387 }
389388 if ( state === 'connected' ) {
390- const params = this . session ? ( await this . session . getCredential ( ) ) . updateCredentialsParams : undefined
391389 if ( this . isSsoSession ( ) ) {
392- await this . lspAuth . updateBearerToken ( params )
390+ const params = await this . session ! . getCredential ( )
391+ await this . lspAuth . updateBearerToken ( params . updateCredentialsParams )
393392 } else if ( this . isIamSession ( ) ) {
394- await this . lspAuth . updateIamCredential ( params )
393+ const params = await this . session ! . getCredential ( )
394+ await this . lspAuth . updateIamCredential ( params . updateCredentialsParams )
395395 }
396396
397397 if ( this . isIdcConnection ( ) ) {
0 commit comments