/
accessTokenRefresher.ts
54 lines (47 loc) · 1.71 KB
/
accessTokenRefresher.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
49
50
51
52
53
54
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
import { AccessToken, TokenCredential, GetTokenOptions } from "@azure/core-auth";
/**
* Helps the core-http token authentication policies with requesting a new token if we're not currently waiting for a new token.
*/
export class AccessTokenRefresher {
private promise: Promise<AccessToken | undefined> | undefined;
private lastCalled = 0;
constructor(
private credential: TokenCredential,
private scopes: string | string[],
private requiredMillisecondsBeforeNewRefresh: number = 30000
) {}
/**
* Returns true if the required milliseconds(defaulted to 30000) have been passed signifying
* that we are ready for a new refresh.
*/
public isReady(): boolean {
// We're only ready for a new refresh if the required milliseconds have passed.
return (
!this.lastCalled || Date.now() - this.lastCalled > this.requiredMillisecondsBeforeNewRefresh
);
}
/**
* Stores the time in which it is called,
* then requests a new token,
* then sets this.promise to undefined,
* then returns the token.
*/
private async getToken(options: GetTokenOptions): Promise<AccessToken | undefined> {
this.lastCalled = Date.now();
const token = await this.credential.getToken(this.scopes, options);
this.promise = undefined;
return token || undefined;
}
/**
* Requests a new token if we're not currently waiting for a new token.
* Returns null if the required time between each call hasn't been reached.
*/
public refresh(options: GetTokenOptions): Promise<AccessToken | undefined> {
if (!this.promise) {
this.promise = this.getToken(options);
}
return this.promise;
}
}