Skip to content

Commit e923bad

Browse files
committed
Implement _getIamCredential and its callers
1 parent a241b1c commit e923bad

File tree

2 files changed

+33
-39
lines changed

2 files changed

+33
-39
lines changed

packages/core/src/auth/auth2.ts

Lines changed: 28 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -121,11 +121,15 @@ export class LanguageClientAuth {
121121
)
122122
}
123123

124-
getIamCredential(login: boolean = false, cancellationToken?: CancellationToken): Promise<GetIamCredentialResult> {
124+
getIamCredential(
125+
profileName: string,
126+
login: boolean = false,
127+
cancellationToken?: CancellationToken
128+
): Promise<GetIamCredentialResult> {
125129
return this.client.sendRequest(
126130
getIamCredentialRequestType.method,
127131
{
128-
clientName: this.clientName,
132+
profileName: profileName,
129133
options: {
130134
loginOnInvalidToken: login,
131135
},
@@ -179,7 +183,7 @@ export class LanguageClientAuth {
179183
ssoSession: {
180184
name: profileName,
181185
settings: undefined,
182-
}
186+
},
183187
} satisfies UpdateProfileParams)
184188
}
185189

@@ -261,7 +265,6 @@ export abstract class BaseLogin {
261265
abstract reauthenticate(): Promise<GetSsoTokenResult | GetIamCredentialResult | undefined>
262266
abstract logout(): void
263267
abstract restore(): void
264-
abstract getToken(): Promise<{ token: string; updateCredentialsParams: UpdateCredentialsParams }>
265268

266269
get data() {
267270
return this._data
@@ -503,14 +506,14 @@ export class IamLogin extends BaseLogin {
503506
* Restore the connection state and connection details to memory, if they exist.
504507
*/
505508
async restore() {
506-
// const sessionData = await this.getProfile()
507-
// const credentials = sessionData?.iamSession?.credentials
508-
// if (credentials?.accessKeyId && credentials?.secretAccessKey) {
509-
// this._data = {
510-
// accessKey: credentials.accessKeyId,
511-
// secretKey: credentials.secretAccessKey,
512-
// }
513-
// }
509+
const sessionData = await this.getProfile()
510+
const credentials = sessionData?.profile?.settings
511+
if (credentials?.aws_access_key_id && credentials?.aws_secret_access_key) {
512+
this._data = {
513+
accessKey: credentials.aws_access_key_id,
514+
secretKey: credentials.aws_secret_access_key,
515+
}
516+
}
514517
try {
515518
await this._getIamCredential(false)
516519
} catch (err) {
@@ -519,23 +522,21 @@ export class IamLogin extends BaseLogin {
519522
}
520523

521524
/**
522-
* Returns both the decrypted access token and the payload to send to the `updateCredentials` LSP API
523-
* with encrypted token
525+
* Returns both the decrypted IAM credential and the payload to send to the `updateCredentials` LSP API
526+
* with encrypted credential
524527
*/
525-
async getToken() {
526-
// TODO: fix STS credential decryption
528+
async getCredentials() {
527529
const response = await this._getIamCredential(false)
528530
const accessKey = await this.decrypt(response.credentials.accessKeyId)
529-
// const secretKey = await this.decrypt(response.credentials.secretAccessKey)
530-
// let sessionToken: string | undefined
531-
// if (response.credentials.sessionToken) {
532-
// sessionToken = await this.decrypt(response.credentials.sessionToken)
533-
// }
531+
const secretKey = await this.decrypt(response.credentials.secretAccessKey)
532+
let sessionToken: string | undefined
533+
if (response.credentials.sessionToken) {
534+
sessionToken = await this.decrypt(response.credentials.sessionToken)
535+
}
534536
return {
535-
// accessKey: accessKey,
536-
// secretKey: secretKey,
537-
// sessionToken: sessionToken,
538-
token: accessKey,
537+
accessKey: accessKey,
538+
secretKey: secretKey,
539+
sessionToken: sessionToken,
539540
updateCredentialsParams: response.updateCredentialsParams,
540541
}
541542
}
@@ -549,25 +550,16 @@ export class IamLogin extends BaseLogin {
549550
this.cancellationToken = new CancellationTokenSource()
550551

551552
try {
552-
response = await this.lspAuth.getIamCredential(login, this.cancellationToken.token)
553+
response = await this.lspAuth.getIamCredential(this.profileName, login, this.cancellationToken.token)
553554
} catch (err: any) {
554555
switch (err.data?.awsErrorCode) {
555556
case AwsErrorCodes.E_CANCELLED:
556557
case AwsErrorCodes.E_SSO_SESSION_NOT_FOUND:
557558
case AwsErrorCodes.E_PROFILE_NOT_FOUND:
558-
case AwsErrorCodes.E_INVALID_SSO_TOKEN:
559559
this.updateConnectionState('notConnected')
560560
break
561-
case AwsErrorCodes.E_CANNOT_REFRESH_SSO_TOKEN:
562-
this.updateConnectionState('expired')
563-
break
564-
// TODO: implement when identity server emits E_NETWORK_ERROR, E_FILESYSTEM_ERROR
565-
// case AwsErrorCodes.E_NETWORK_ERROR:
566-
// case AwsErrorCodes.E_FILESYSTEM_ERROR:
567-
// // do stuff, probably nothing at all
568-
// break
569561
default:
570-
getLogger().error('SsoLogin: unknown error when requesting token: %s', err)
562+
getLogger().error('IamLogin: unknown error when requesting token: %s', err)
571563
break
572564
}
573565
throw err

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ export class AuthUtil implements IAuthProvider {
201201

202202
async getToken() {
203203
if (this.isSsoSession()) {
204-
return (await this.session!.getToken()).token
204+
return (await (this.session as SsoLogin).getToken()).token
205205
} else {
206206
throw new ToolkitError('Cannot get token for non-SSO session.')
207207
}
@@ -336,7 +336,9 @@ export class AuthUtil implements IAuthProvider {
336336

337337
private async stateChangeHandler(e: AuthStateEvent) {
338338
if (e.state === 'refreshed') {
339-
const params = this.isSsoSession() ? (await this.session!.getToken()).updateCredentialsParams : undefined
339+
const params = this.isSsoSession()
340+
? (await (this.session as SsoLogin).getToken()).updateCredentialsParams
341+
: undefined
340342
await this.lspAuth.updateBearerToken(params!)
341343
return
342344
} else {
@@ -354,7 +356,7 @@ export class AuthUtil implements IAuthProvider {
354356
}
355357
}
356358
if (state === 'connected') {
357-
const bearerTokenParams = (await this.session!.getToken()).updateCredentialsParams
359+
const bearerTokenParams = (await (this.session as SsoLogin).getToken()).updateCredentialsParams
358360
await this.lspAuth.updateBearerToken(bearerTokenParams)
359361

360362
if (this.isIdcConnection()) {

0 commit comments

Comments
 (0)