Skip to content

Commit b51ea11

Browse files
committed
Move auth2 encryption to LanguageClientAuth
1 parent 88eb779 commit b51ea11

File tree

1 file changed

+44
-29
lines changed

1 file changed

+44
-29
lines changed

packages/core/src/auth/auth2.ts

Lines changed: 44 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -119,12 +119,31 @@ export class LanguageClientAuth {
119119
return this.#stsCacheWatcher
120120
}
121121

122-
getSsoToken(
122+
/**
123+
* Encrypts an object
124+
*/
125+
private async encrypt<T>(request: T): Promise<string> {
126+
const payload = new TextEncoder().encode(JSON.stringify(request))
127+
const encrypted = await new jose.CompactEncrypt(payload)
128+
.setProtectedHeader({ alg: 'dir', enc: 'A256GCM' })
129+
.encrypt(this.encryptionKey)
130+
return encrypted
131+
}
132+
133+
/**
134+
* Decrypts an object
135+
*/
136+
private async decrypt<T>(request: string): Promise<T> {
137+
const result = await jose.compactDecrypt(request, this.encryptionKey)
138+
return JSON.parse(new TextDecoder().decode(result.plaintext)) as T
139+
}
140+
141+
async getSsoToken(
123142
tokenSource: TokenSource,
124143
login: boolean = false,
125144
cancellationToken?: CancellationToken
126145
): Promise<GetSsoTokenResult> {
127-
return this.client.sendRequest(
146+
const response: GetSsoTokenResult = await this.client.sendRequest(
128147
getSsoTokenRequestType.method,
129148
{
130149
clientName: this.clientName,
@@ -136,14 +155,17 @@ export class LanguageClientAuth {
136155
} satisfies GetSsoTokenParams,
137156
cancellationToken
138157
)
158+
// Decrypt the access token
159+
response.ssoToken.accessToken = await this.decrypt(response.ssoToken.accessToken)
160+
return response
139161
}
140162

141-
getIamCredential(
163+
async getIamCredential(
142164
profileName: string,
143165
login: boolean = false,
144166
cancellationToken?: CancellationToken
145167
): Promise<GetIamCredentialResult> {
146-
return this.client.sendRequest(
168+
const response: GetIamCredentialResult = await this.client.sendRequest(
147169
getIamCredentialRequestType.method,
148170
{
149171
profileName: profileName,
@@ -153,16 +175,25 @@ export class LanguageClientAuth {
153175
} satisfies GetIamCredentialParams,
154176
cancellationToken
155177
)
178+
// Decrypt the response credentials
179+
const { accessKeyId, secretAccessKey, sessionToken, expiration } = response.credential.credentials
180+
response.credential.credentials = {
181+
accessKeyId: await this.decrypt(accessKeyId),
182+
secretAccessKey: await this.decrypt(secretAccessKey),
183+
sessionToken: sessionToken ? await this.decrypt(sessionToken) : undefined,
184+
expiration: expiration,
185+
}
186+
return response
156187
}
157188

158-
updateSsoProfile(
189+
async updateSsoProfile(
159190
profileName: string,
160191
startUrl: string,
161192
region: string,
162193
scopes: string[]
163194
): Promise<UpdateProfileResult> {
164195
// Add SSO settings and delete credentials from profile
165-
return this.client.sendRequest(updateProfileRequestType.method, {
196+
const params = await this.encrypt({
166197
profile: {
167198
kinds: [ProfileKind.SsoTokenProfile],
168199
name: profileName,
@@ -182,10 +213,11 @@ export class LanguageClientAuth {
182213
sso_registration_scopes: scopes,
183214
},
184215
},
185-
} satisfies UpdateProfileParams)
216+
})
217+
return this.client.sendRequest(updateProfileRequestType.method, params)
186218
}
187219

188-
updateIamProfile(
220+
async updateIamProfile(
189221
profileName: string,
190222
accessKey: string,
191223
secretKey: string,
@@ -234,9 +266,8 @@ export class LanguageClientAuth {
234266
},
235267
}
236268
}
237-
return this.client.sendRequest(updateProfileRequestType.method, {
238-
profile: profile,
239-
} satisfies UpdateProfileParams)
269+
const params = await this.encrypt({ profile: profile })
270+
return this.client.sendRequest(updateProfileRequestType.method, params)
240271
}
241272

242273
listProfiles() {
@@ -377,14 +408,6 @@ export abstract class BaseLogin {
377408
this.eventEmitter.fire({ id: this.profileName, state: this.connectionState })
378409
}
379410
}
380-
381-
/**
382-
* Decrypts an encrypted string, removes its quotes, and returns the resulting string
383-
*/
384-
protected async decrypt(encrypted: string): Promise<string> {
385-
const decrypted = await jose.compactDecrypt(encrypted, this.lspAuth.encryptionKey)
386-
return decrypted.plaintext.toString().replaceAll('"', '')
387-
}
388411
}
389412

390413
/**
@@ -456,9 +479,8 @@ export class SsoLogin extends BaseLogin {
456479
*/
457480
async getCredential() {
458481
const response = await this._getSsoToken(false)
459-
const accessToken = await this.decrypt(response.ssoToken.accessToken)
460482
return {
461-
credential: accessToken,
483+
credential: response.ssoToken.accessToken,
462484
updateCredentialsParams: response.updateCredentialsParams,
463485
}
464486
}
@@ -609,15 +631,8 @@ export class IamLogin extends BaseLogin {
609631
*/
610632
async getCredential() {
611633
const response = await this._getIamCredential(false)
612-
const credentials: IamCredentials = {
613-
accessKeyId: await this.decrypt(response.credential.credentials.accessKeyId),
614-
secretAccessKey: await this.decrypt(response.credential.credentials.secretAccessKey),
615-
sessionToken: response.credential.credentials.sessionToken
616-
? await this.decrypt(response.credential.credentials.sessionToken)
617-
: undefined,
618-
}
619634
return {
620-
credential: credentials,
635+
credential: response.credential.credentials,
621636
updateCredentialsParams: response.updateCredentialsParams,
622637
}
623638
}

0 commit comments

Comments
 (0)