-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy pathCachedTokenProvider.ts
More file actions
98 lines (82 loc) · 2.58 KB
/
CachedTokenProvider.ts
File metadata and controls
98 lines (82 loc) · 2.58 KB
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import ITokenProvider from './ITokenProvider';
import Token from './Token';
/**
* Default refresh threshold in milliseconds (5 minutes).
* Tokens will be refreshed when they are within this threshold of expiring.
*/
const DEFAULT_REFRESH_THRESHOLD_MS = 5 * 60 * 1000;
/**
* A token provider that wraps another provider with automatic caching.
* Tokens are cached and reused until they are close to expiring.
*/
export default class CachedTokenProvider implements ITokenProvider {
private readonly baseProvider: ITokenProvider;
private readonly refreshThresholdMs: number;
private cache: Token | null = null;
private refreshPromise: Promise<Token> | null = null;
/**
* Creates a new CachedTokenProvider.
* @param baseProvider - The underlying token provider to cache
* @param options - Optional configuration
* @param options.refreshThresholdMs - Refresh tokens this many ms before expiry (default: 5 minutes)
*/
constructor(
baseProvider: ITokenProvider,
options?: {
refreshThresholdMs?: number;
},
) {
this.baseProvider = baseProvider;
this.refreshThresholdMs = options?.refreshThresholdMs ?? DEFAULT_REFRESH_THRESHOLD_MS;
}
async getToken(): Promise<Token> {
// Return cached token if it's still valid
if (this.cache && !this.shouldRefresh(this.cache)) {
return this.cache;
}
// If already refreshing, wait for that to complete
if (this.refreshPromise) {
return this.refreshPromise;
}
// Start refresh
this.refreshPromise = this.refreshToken();
try {
const token = await this.refreshPromise;
return token;
} finally {
this.refreshPromise = null;
}
}
getName(): string {
return `cached[${this.baseProvider.getName()}]`;
}
/**
* Clears the cached token, forcing a refresh on the next getToken() call.
*/
clearCache(): void {
this.cache = null;
}
/**
* Determines if the token should be refreshed.
* @param token - The token to check
* @returns true if the token should be refreshed
*/
private shouldRefresh(token: Token): boolean {
// If no expiration is known, don't refresh proactively
if (!token.expiresAt) {
return false;
}
const now = Date.now();
const expiresAtMs = token.expiresAt.getTime();
const refreshAtMs = expiresAtMs - this.refreshThresholdMs;
return now >= refreshAtMs;
}
/**
* Fetches a new token from the base provider and caches it.
*/
private async refreshToken(): Promise<Token> {
const token = await this.baseProvider.getToken();
this.cache = token;
return token;
}
}