-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy pathStaticTokenProvider.ts
More file actions
58 lines (53 loc) · 1.47 KB
/
StaticTokenProvider.ts
File metadata and controls
58 lines (53 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
53
54
55
56
57
58
import ITokenProvider from './ITokenProvider';
import Token from './Token';
/**
* A token provider that returns a static token.
* Useful for testing or when the token is obtained through external means.
*/
export default class StaticTokenProvider implements ITokenProvider {
private readonly token: Token;
/**
* Creates a new StaticTokenProvider.
* @param accessToken - The access token string
* @param options - Optional token configuration (tokenType, expiresAt, refreshToken, scopes)
*/
constructor(
accessToken: string,
options?: {
tokenType?: string;
expiresAt?: Date;
refreshToken?: string;
scopes?: string[];
},
) {
this.token = new Token(accessToken, options);
}
/**
* Creates a StaticTokenProvider from a JWT string.
* The expiration time will be extracted from the JWT payload.
* @param jwt - The JWT token string
* @param options - Optional token configuration
*/
static fromJWT(
jwt: string,
options?: {
tokenType?: string;
refreshToken?: string;
scopes?: string[];
},
): StaticTokenProvider {
const token = Token.fromJWT(jwt, options);
return new StaticTokenProvider(token.accessToken, {
tokenType: token.tokenType,
expiresAt: token.expiresAt,
refreshToken: token.refreshToken,
scopes: token.scopes,
});
}
async getToken(): Promise<Token> {
return this.token;
}
getName(): string {
return 'StaticTokenProvider';
}
}