Skip to content

Commit bcf7d39

Browse files
committed
Fix type checks for AuthUtil sessions
1 parent 6c54928 commit bcf7d39

File tree

3 files changed

+35
-33
lines changed

3 files changed

+35
-33
lines changed

packages/amazonq/test/unit/codewhisperer/util/authUtil.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -483,7 +483,7 @@ describe('AuthUtil', async function () {
483483
await auth.getIamCredential()
484484
assert.fail('Should have thrown an error')
485485
} catch (err) {
486-
assert.strictEqual((err as Error).message, 'Cannot get credential with SSO session')
486+
assert.strictEqual((err as Error).message, 'Cannot get credential without logging in with IAM.')
487487
}
488488
})
489489

@@ -494,7 +494,7 @@ describe('AuthUtil', async function () {
494494
await auth.getIamCredential()
495495
assert.fail('Should have thrown an error')
496496
} catch (err) {
497-
assert.strictEqual((err as Error).message, 'Cannot get credential without logging in.')
497+
assert.strictEqual((err as Error).message, 'Cannot get credential without logging in with IAM.')
498498
}
499499
})
500500
})

packages/core/src/codewhisperer/ui/codeWhispererNodes.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ export function createManageSubscription(): DataQuickPickItem<'manageSubscriptio
192192
export function createSignout(): DataQuickPickItem<'signout'> {
193193
const label = localize('AWS.codewhisperer.signoutNode.label', 'Sign Out')
194194
const icon = getIcon('vscode-export')
195-
const connection = AuthUtil.instance.isIamConnection()
195+
const connection = AuthUtil.instance.isIamSession()
196196
? 'IAM Credentials'
197197
: AuthUtil.instance.isBuilderIdConnection()
198198
? 'AWS Builder ID'

packages/core/src/codewhisperer/util/authUtil.ts

Lines changed: 32 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,16 @@ import { showAmazonQWalkthroughOnce } from '../../amazonq/onboardingPage/walkthr
3030
import { setContext } from '../../shared/vscode/setContext'
3131
import { openUrl } from '../../shared/utilities/vsCodeUtils'
3232
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'
3443
import { builderIdStartUrl, internalStartUrl } from '../../auth/sso/constants'
3544
import { VSCODE_EXTENSION_ID } from '../../shared/extensions'
3645
import { RegionProfileManager } from '../region/regionProfileManager'
@@ -108,11 +117,11 @@ export class AuthUtil implements IAuthProvider {
108117
}
109118

110119
isSsoSession(): boolean {
111-
return this.session instanceof SsoLogin
120+
return this.session?.loginType === LoginTypes.SSO
112121
}
113122

114123
isIamSession(): boolean {
115-
return this.session instanceof IamLogin
124+
return this.session?.loginType === LoginTypes.IAM
116125
}
117126

118127
/**
@@ -204,36 +213,31 @@ export class AuthUtil implements IAuthProvider {
204213
}
205214

206215
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
213219
} else {
214-
throw new ToolkitError('Cannot get credential without logging in.')
220+
throw new ToolkitError('Cannot get credential without logging in with SSO.')
215221
}
216222
}
217223

218224
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
225+
if (this.isIamSession()) {
226+
const response = await this.session!.getCredential()
227+
return response.credential as IamCredentials
225228
} else {
226-
throw new ToolkitError('Cannot get credential without logging in.')
229+
throw new ToolkitError('Cannot get credential without logging in with IAM.')
227230
}
228231
}
229232

230233
get connection() {
231234
return this.session?.data
232235
}
233236

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()
237241
} else {
238242
return 'notConnected'
239243
}
@@ -255,10 +259,6 @@ export class AuthUtil implements IAuthProvider {
255259
return Boolean(this.connection?.startUrl && this.connection?.startUrl !== builderIdStartUrl)
256260
}
257261

258-
isIamConnection() {
259-
return Boolean(this.connection?.accessKey && this.connection?.secretKey)
260-
}
261-
262262
isInternalAmazonUser(): boolean {
263263
return this.isConnected() && this.connection?.startUrl === internalStartUrl
264264
}
@@ -360,11 +360,12 @@ export class AuthUtil implements IAuthProvider {
360360

361361
private async stateChangeHandler(e: AuthStateEvent) {
362362
if (e.state === 'refreshed') {
363-
const params = this.session ? (await this.session.getCredential()).updateCredentialsParams : undefined
364363
if (this.isSsoSession()) {
365-
await this.lspAuth.updateBearerToken(params)
364+
const params = await this.session!.getCredential()
365+
await this.lspAuth.updateBearerToken(params.updateCredentialsParams)
366366
} else if (this.isIamSession()) {
367-
await this.lspAuth.updateIamCredential(params)
367+
const params = await this.session!.getCredential()
368+
await this.lspAuth.updateIamCredential(params.updateCredentialsParams)
368369
}
369370
} else {
370371
this.logger.info(`codewhisperer: connection changed to ${e.state}`)
@@ -387,11 +388,12 @@ export class AuthUtil implements IAuthProvider {
387388
this.session = undefined
388389
}
389390
if (state === 'connected') {
390-
const params = this.session ? (await this.session.getCredential()).updateCredentialsParams : undefined
391391
if (this.isSsoSession()) {
392-
await this.lspAuth.updateBearerToken(params)
392+
const params = await this.session!.getCredential()
393+
await this.lspAuth.updateBearerToken(params.updateCredentialsParams)
393394
} else if (this.isIamSession()) {
394-
await this.lspAuth.updateIamCredential(params)
395+
const params = await this.session!.getCredential()
396+
await this.lspAuth.updateIamCredential(params.updateCredentialsParams)
395397
}
396398

397399
if (this.isIdcConnection()) {

0 commit comments

Comments
 (0)