-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy pathExternalTokenProvider.ts
More file actions
52 lines (43 loc) · 1.47 KB
/
ExternalTokenProvider.ts
File metadata and controls
52 lines (43 loc) · 1.47 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
import ITokenProvider from './ITokenProvider';
import Token from './Token';
/**
* Type for the callback function that retrieves tokens from external sources.
*/
export type TokenCallback = () => Promise<string>;
/**
* A token provider that delegates token retrieval to an external callback function.
* Useful for integrating with secret managers, vaults, or other token sources.
*/
export default class ExternalTokenProvider implements ITokenProvider {
private readonly getTokenCallback: TokenCallback;
private readonly parseJWT: boolean;
private readonly providerName: string;
/**
* Creates a new ExternalTokenProvider.
* @param getToken - Callback function that returns the access token string
* @param options - Optional configuration
* @param options.parseJWT - If true, attempt to extract expiration from JWT payload (default: true)
* @param options.name - Custom name for this provider (default: "ExternalTokenProvider")
*/
constructor(
getToken: TokenCallback,
options?: {
parseJWT?: boolean;
name?: string;
},
) {
this.getTokenCallback = getToken;
this.parseJWT = options?.parseJWT ?? true;
this.providerName = options?.name ?? 'ExternalTokenProvider';
}
async getToken(): Promise<Token> {
const accessToken = await this.getTokenCallback();
if (this.parseJWT) {
return Token.fromJWT(accessToken);
}
return new Token(accessToken);
}
getName(): string {
return this.providerName;
}
}