-
Notifications
You must be signed in to change notification settings - Fork 57
/
Copy pathclient-credentials-token-cache.ts
48 lines (44 loc) · 1.24 KB
/
client-credentials-token-cache.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import moment from 'moment';
import { Cache } from './cache';
import { headerForClientCredentials } from './xsuaa-service';
import {
ClientCredentials,
ClientCredentialsResponse
} from './xsuaa-service-types';
const ClientCredentialsTokenCache = (
cache: Cache<ClientCredentialsResponse>
) => ({
// TODO: this method name can be shortened
getGrantTokenFromCache: (
url,
credentials: ClientCredentials
): ClientCredentialsResponse | undefined =>
cache.get(getGrantTokenCacheKey(url, credentials)),
// TODO: this method name can be shortened
cacheRetrievedToken: (
url,
credentials: ClientCredentials,
token: ClientCredentialsResponse
): void => {
cache.set(
getGrantTokenCacheKey(url, credentials),
token,
token.expires_in
? moment().add(token.expires_in, 'second').unix() * 1000
: undefined
);
},
clear: (): void => {
cache.clear();
},
getCacheInstance: () => cache
});
export function getGrantTokenCacheKey(
url: string,
credentials: ClientCredentials
): string {
return [url, headerForClientCredentials(credentials).substring(6)].join(':');
}
export const clientCredentialsTokenCache = ClientCredentialsTokenCache(
new Cache<ClientCredentialsResponse>()
);