@@ -41,24 +41,16 @@ export function getCache(directory = getCacheDir()): SsoCache {
41
41
}
42
42
43
43
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
-
55
44
// Compatability for older Toolkit versions (format on disk is unchanged)
56
45
type StoredRegistration = Omit < ClientRegistration , 'expiresAt' > & { readonly expiresAt : string }
57
46
const read = ( data : StoredRegistration ) => ( { ...data , expiresAt : new Date ( data . expiresAt ) } )
58
47
const write = ( data : ClientRegistration ) => ( { ...data , expiresAt : data . expiresAt . toISOString ( ) } )
59
48
60
49
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
+ )
62
54
63
55
return mapCache ( cache , read , write )
64
56
}
@@ -112,24 +104,35 @@ export function getTokenCache(directory = getCacheDir()): KeyedCache<SsoAccess>
112
104
}
113
105
}
114
106
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.
120
118
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]
127
125
128
- return path . join ( directory , `${ hashedUrl } .json` )
129
- }
126
+ return path . join ( ssoCacheDir , `${ hashedUrl } .json` )
127
+ }
130
128
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
+ }
133
135
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` )
135
138
}
0 commit comments