Skip to content

Commit 188a185

Browse files
refactor(auth): Extract sso cache get file funcs
Signed-off-by: Nikolas Komonen <[email protected]>
1 parent b5717bf commit 188a185

File tree

1 file changed

+31
-28
lines changed

1 file changed

+31
-28
lines changed

src/credentials/sso/cache.ts

Lines changed: 31 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -41,24 +41,16 @@ export function getCache(directory = getCacheDir()): SsoCache {
4141
}
4242

4343
export function getRegistrationCache(directory = getCacheDir()): KeyedCache<ClientRegistration, RegistrationKey> {
44-
const hashScopes = (scopes: string[]) => {
45-
const shasum = crypto.createHash('sha256')
46-
scopes.forEach(s => shasum.update(s))
47-
return shasum.digest('hex')
48-
}
49-
50-
const getTarget = (key: RegistrationKey) => {
51-
const suffix = `${key.region}${key.scopes && key.scopes.length > 0 ? `-${hashScopes(key.scopes)}` : ''}`
52-
return path.join(directory, `aws-toolkit-vscode-client-id-${suffix}.json`)
53-
}
54-
5544
// Compatability for older Toolkit versions (format on disk is unchanged)
5645
type StoredRegistration = Omit<ClientRegistration, 'expiresAt'> & { readonly expiresAt: string }
5746
const read = (data: StoredRegistration) => ({ ...data, expiresAt: new Date(data.expiresAt) })
5847
const write = (data: ClientRegistration) => ({ ...data, expiresAt: data.expiresAt.toISOString() })
5948

6049
const logger = (message: string) => getLogger().debug(`SSO registration cache: ${message}`)
61-
const cache: KeyedCache<StoredRegistration, RegistrationKey> = createDiskCache(getTarget, logger)
50+
const cache: KeyedCache<StoredRegistration, RegistrationKey> = createDiskCache(
51+
(registrationKey: RegistrationKey) => getRegistrationCacheFile(directory, registrationKey),
52+
logger
53+
)
6254

6355
return mapCache(cache, read, write)
6456
}
@@ -112,24 +104,35 @@ export function getTokenCache(directory = getCacheDir()): KeyedCache<SsoAccess>
112104
}
113105
}
114106

115-
const getTarget = (ssoUrl: string) => {
116-
const encoded = encodeURI(ssoUrl)
117-
// Per the spec: 'SSO Login Token Flow' the access token must be
118-
// cached as the SHA1 hash of the bytes of the UTF-8 encoded
119-
// startUrl value with ".json" appended to the end.
107+
const logger = (message: string) => getLogger().debug(`SSO token cache: ${message}`)
108+
const cache = createDiskCache<StoredToken, string>((ssoUrl: string) => getTokenCacheFile(directory, ssoUrl), logger)
109+
110+
return mapCache(cache, read, write)
111+
}
112+
113+
function getTokenCacheFile(ssoCacheDir: string, ssoUrl: string) {
114+
const encoded = encodeURI(ssoUrl)
115+
// Per the spec: 'SSO Login Token Flow' the access token must be
116+
// cached as the SHA1 hash of the bytes of the UTF-8 encoded
117+
// startUrl value with ".json" appended to the end.
120118

121-
const shasum = crypto.createHash('sha1')
122-
// Suppress warning because:
123-
// 1. SHA1 is prescribed by the AWS SSO spec
124-
// 2. the hashed startUrl value is not a secret
125-
shasum.update(encoded) // lgtm[js/weak-cryptographic-algorithm]
126-
const hashedUrl = shasum.digest('hex') // lgtm[js/weak-cryptographic-algorithm]
119+
const shasum = crypto.createHash('sha1')
120+
// Suppress warning because:
121+
// 1. SHA1 is prescribed by the AWS SSO spec
122+
// 2. the hashed startUrl value is not a secret
123+
shasum.update(encoded) // lgtm[js/weak-cryptographic-algorithm]
124+
const hashedUrl = shasum.digest('hex') // lgtm[js/weak-cryptographic-algorithm]
127125

128-
return path.join(directory, `${hashedUrl}.json`)
129-
}
126+
return path.join(ssoCacheDir, `${hashedUrl}.json`)
127+
}
130128

131-
const logger = (message: string) => getLogger().debug(`SSO token cache: ${message}`)
132-
const cache = createDiskCache<StoredToken, string>(getTarget, logger)
129+
const getRegistrationCacheFile = (ssoCacheDir: string, key: RegistrationKey) => {
130+
const hashScopes = (scopes: string[]) => {
131+
const shasum = crypto.createHash('sha256')
132+
scopes.forEach(s => shasum.update(s))
133+
return shasum.digest('hex')
134+
}
133135

134-
return mapCache(cache, read, write)
136+
const suffix = `${key.region}${key.scopes && key.scopes.length > 0 ? `-${hashScopes(key.scopes)}` : ''}`
137+
return path.join(ssoCacheDir, `aws-toolkit-vscode-client-id-${suffix}.json`)
135138
}

0 commit comments

Comments
 (0)