-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy pathTokenProviderAuthenticator.ts
More file actions
44 lines (35 loc) · 1.58 KB
/
TokenProviderAuthenticator.ts
File metadata and controls
44 lines (35 loc) · 1.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
import { HeadersInit } from 'node-fetch';
import IAuthentication from '../../contracts/IAuthentication';
import ITokenProvider from './ITokenProvider';
import IClientContext from '../../../contracts/IClientContext';
import { LogLevel } from '../../../contracts/IDBSQLLogger';
/**
* Adapts an ITokenProvider to the IAuthentication interface used by the driver.
* This allows token providers to be used with the existing authentication system.
*/
export default class TokenProviderAuthenticator implements IAuthentication {
private readonly tokenProvider: ITokenProvider;
private readonly context: IClientContext;
private readonly headers: HeadersInit;
/**
* Creates a new TokenProviderAuthenticator.
* @param tokenProvider - The token provider to use for authentication
* @param context - The client context for logging
* @param headers - Additional headers to include with each request
*/
constructor(tokenProvider: ITokenProvider, context: IClientContext, headers?: HeadersInit) {
this.tokenProvider = tokenProvider;
this.context = context;
this.headers = headers ?? {};
}
async authenticate(): Promise<HeadersInit> {
const logger = this.context.getLogger();
const providerName = this.tokenProvider.getName();
logger.log(LogLevel.debug, `TokenProviderAuthenticator: getting token from ${providerName}`);
const token = await this.tokenProvider.getToken();
if (token.isExpired()) {
logger.log(LogLevel.warn, `TokenProviderAuthenticator: token from ${providerName} is expired`);
}
return token.setAuthHeader(this.headers);
}
}