Skip to content

Commit c42a56f

Browse files
committed
fix: implement new token provider to work with v3
1 parent 4b2a28a commit c42a56f

File tree

1 file changed

+20
-8
lines changed

1 file changed

+20
-8
lines changed

packages/core/src/shared/clients/codecatalystClient.ts

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ import { ClientWrapper } from './clientWrapper'
9999
import { StandardRetryStrategy } from '@smithy/util-retry'
100100
import { ServiceException } from '@aws-sdk/smithy-client'
101101
import { AccessDeniedException } from '@aws-sdk/client-sso-oidc'
102+
import { TokenIdentityProvider } from '@aws-sdk/types'
102103

103104
const requiredDevEnvProps = [
104105
'id',
@@ -231,7 +232,7 @@ async function createCodeCatalystClient(
231232
}
232233

233234
function createCodeCatalystClientV3(
234-
tokenProvider: TokenProvider,
235+
tokenProvider: TokenIdentityProvider,
235236
regionCode: string,
236237
endpoint: string,
237238
maxRetries: number
@@ -266,7 +267,6 @@ export const onAccessDeniedException = onAccessDeniedExceptionEmitter.event
266267
interface AuthOptions {
267268
showReauthPrompt?: boolean
268269
}
269-
270270
export type CodeCatalystClientFactory = () => Promise<CodeCatalystClient>
271271
/**
272272
* Factory to create a new `CodeCatalystClient`. Call `onCredentialsChanged()` before making requests.
@@ -280,7 +280,7 @@ export async function createClient(
280280
): Promise<CodeCatalystClient> {
281281
const tokenProvider = new TokenProvider(connection)
282282
const sdkClient = await createCodeCatalystClient(tokenProvider, regionCode, endpoint, maxRetries)
283-
const sdkv3Client = createCodeCatalystClientV3(tokenProvider, regionCode, endpoint, maxRetries)
283+
const sdkv3Client = createCodeCatalystClientV3(getTokenProvider(connection), regionCode, endpoint, maxRetries)
284284
const c = new CodeCatalystClientInternal(connection, sdkClient, sdkv3Client)
285285
try {
286286
await c.verifySession()
@@ -297,6 +297,17 @@ export async function createClient(
297297
return c
298298
}
299299

300+
// TODO: move this to sso auth folder?
301+
function getTokenProvider(connection: SsoConnection): TokenIdentityProvider {
302+
return async (_props) => {
303+
const token = await connection.getToken()
304+
return {
305+
token: token.accessToken,
306+
expiration: token.expiresAt,
307+
}
308+
}
309+
}
310+
300311
// XXX: the backend currently rejects empty strings for `alias` so the field must be removed if falsey
301312
function fixAliasInRequest<T extends CreateDevEnvironmentRequest | UpdateDevEnvironmentRequest>(request: T): T {
302313
if (!request.alias) {
@@ -533,16 +544,17 @@ class CodeCatalystClientInternal extends ClientWrapper<CodeCatalystSDKClient> {
533544
if (CodeCatalystClientInternal.identityCache.has(accessToken)) {
534545
return CodeCatalystClientInternal.identityCache.get(accessToken)!
535546
}
536-
const resp: VerifySessionCommandOutput = await this.callV3(VerifySessionCommand, {}, false)
537-
assertHasProps(resp, 'identity')
538547

539-
CodeCatalystClientInternal.identityCache.set(accessToken, resp.identity)
548+
const r: VerifySessionCommandOutput = await this.callV3(VerifySessionCommand, {}, false)
549+
assertHasProps(r, 'identity')
550+
551+
CodeCatalystClientInternal.identityCache.set(accessToken, r.identity)
540552
setTimeout(() => {
541553
CodeCatalystClientInternal.identityCache.delete(accessToken)
542-
CodeCatalystClientInternal.userDetailsCache.delete(resp.identity)
554+
CodeCatalystClientInternal.userDetailsCache.delete(r.identity)
543555
}, expiresAt.getTime() - Date.now())
544556

545-
return resp.identity
557+
return r.identity
546558
}
547559

548560
public async getBearerToken(): Promise<string> {

0 commit comments

Comments
 (0)