forked from Azure/azure-sdk-for-js
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Identity] [core-http] Concept PR for the token refresher update (Azu…
…re#10085) * AccessTokenRefresher concept * formatting * simple test * changed to timeBetweenRefreshAttemptsInMs * forcedRefresh if no cached token * making timeBetweenRefreshAttemptsInMs private * undoing some of the previous test changes * much better approach after some tweaks * license header and lastCalled default value to 0 * mocking instead of delaying, for the tests * moved the undefined to another place, it looks better, and a fmt fix * updates after fixing conflicts * Apply suggestions from code review Co-authored-by: Jeff Fisher <xirzec@xirzec.com> * fixes after recent suggestions * formatting * requiredMillisecondsBeforeNewRefresh defaults to 30 seconds Co-authored-by: Jeff Fisher <xirzec@xirzec.com>
- Loading branch information
Showing
7 changed files
with
484 additions
and
89 deletions.
There are no files selected for viewing
52 changes: 52 additions & 0 deletions
52
sdk/core/core-http/src/credentials/accessTokenRefresher.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
// 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 | ||
) {} | ||
|
||
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. | ||
* @param options getToken options | ||
*/ | ||
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. | ||
* @param options getToken options | ||
*/ | ||
public refresh(options: GetTokenOptions): Promise<AccessToken | undefined> { | ||
if (!this.promise) { | ||
this.promise = this.getToken(options); | ||
} | ||
|
||
return this.promise; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.