Skip to content

Commit c9afa97

Browse files
authored
fix(codecatalyst): cache results of verifySession (#3113)
## Problem verifySession is called every time a new client is created ## Solution Cache verifySession based off the access token ### Testing The way the CodeCatalyst client is written makes it difficult to test in a sustainable way. Something similar to #2917 needs to be done to reduce the amount of custom logic in each service client.
1 parent 39a42bd commit c9afa97

File tree

1 file changed

+17
-1
lines changed

1 file changed

+17
-1
lines changed

src/shared/clients/codecatalystClient.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,19 @@ class CodeCatalystClientInternal {
180180
private userDetails?: UserDetails
181181
private readonly log: logger.Logger
182182

183+
/**
184+
* Maps bearer tokens to CAWS identities via `verifySession`
185+
*
186+
* It's assumed that an identity will never change over the lifetime of a token
187+
*/
183188
private static identityCache = new Map<string, string>()
189+
190+
/**
191+
* Maps CAWS identities to user details via `getUserDetails`
192+
*
193+
* User details _might_ change at some point, however, this is an uncommon occurence.
194+
* Cached user details are cleared when the access token is refreshed.
195+
*/
184196
private static userDetailsCache = new Map<UserDetails['userId'], UserDetails>()
185197

186198
public constructor(private readonly connection: SsoConnection, private readonly sdkClient: CodeCatalyst) {
@@ -320,7 +332,11 @@ class CodeCatalystClientInternal {
320332
const resp = await this.call(this.sdkClient.verifySession(), false)
321333
assertHasProps(resp, 'identity')
322334

323-
setTimeout(() => CodeCatalystClientInternal.identityCache.delete(accessToken), expiresAt.getTime() - Date.now())
335+
CodeCatalystClientInternal.identityCache.set(accessToken, resp.identity)
336+
setTimeout(() => {
337+
CodeCatalystClientInternal.identityCache.delete(accessToken)
338+
CodeCatalystClientInternal.userDetailsCache.delete(resp.identity)
339+
}, expiresAt.getTime() - Date.now())
324340

325341
return resp.identity
326342
}

0 commit comments

Comments
 (0)