-
Notifications
You must be signed in to change notification settings - Fork 1.4k
[core-http] Use an external token cache in BearerTokenAuthenticationPolicy #4174
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,61 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT License. | ||
|
|
||
| import { AccessToken } from "@azure/core-auth"; | ||
|
|
||
| /** | ||
| * Defines the default token refresh buffer duration. | ||
| */ | ||
| export const TokenRefreshBufferMs = 2 * 60 * 1000; // 2 Minutes | ||
|
|
||
| /** | ||
| * Provides a cache for an AccessToken that was that | ||
| * was returned from a TokenCredential. | ||
| */ | ||
| export interface AccessTokenCache { | ||
| /** | ||
| * Sets the cached token. | ||
| * | ||
| * @param The {@link AccessToken} to be cached or null to | ||
| * clear the cached token. | ||
| */ | ||
| setCachedToken(accessToken: AccessToken | undefined): void; | ||
|
|
||
| /** | ||
| * Returns the cached {@link AccessToken} or undefined if nothing is cached. | ||
| */ | ||
| getCachedToken(): AccessToken | undefined; | ||
| } | ||
|
|
||
| /** | ||
| * Provides an {@link AccessTokenCache} implementation which clears | ||
| * the cached {@link AccessToken}'s after the expiresOnTimestamp has | ||
| * passed. | ||
| */ | ||
| export class ExpiringAccessTokenCache implements AccessTokenCache { | ||
| private tokenRefreshBufferMs: number; | ||
| private cachedToken?: AccessToken = undefined; | ||
|
|
||
| /** | ||
| * Constructs an instance of {@link ExpiringAccessTokenCache} with | ||
| * an optional expiration buffer time. | ||
| */ | ||
| constructor(tokenRefreshBufferMs: number = TokenRefreshBufferMs) { | ||
| this.tokenRefreshBufferMs = tokenRefreshBufferMs; | ||
| } | ||
|
|
||
| setCachedToken(accessToken: AccessToken | undefined): void { | ||
| this.cachedToken = accessToken; | ||
| } | ||
|
|
||
| getCachedToken(): AccessToken | undefined { | ||
| if ( | ||
| this.cachedToken && | ||
| Date.now() + this.tokenRefreshBufferMs >= this.cachedToken.expiresOnTimestamp | ||
| ) { | ||
| this.cachedToken = undefined; | ||
| } | ||
|
|
||
| return this.cachedToken; | ||
| } | ||
| } |
This file contains hidden or 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 hidden or 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,39 @@ | ||
| import { assert } from "chai"; | ||
| import { ExpiringAccessTokenCache } from "../lib/credentials/accessTokenCache"; | ||
|
|
||
| function mockToken(expirationDeltaMs: number) { | ||
| return { | ||
| token: "token", | ||
| expiresOnTimestamp: Date.now() + expirationDeltaMs | ||
| }; | ||
| } | ||
|
|
||
| describe("ExpiringAccessTokenCache", function () { | ||
| it("returns a cached token within the expiration window", function() { | ||
| const tokenCache = new ExpiringAccessTokenCache(2000); | ||
| const accessToken = mockToken(5000); | ||
| tokenCache.setCachedToken(accessToken); | ||
|
|
||
| const cachedToken = tokenCache.getCachedToken(); | ||
| assert.isDefined(cachedToken, "A cached token was not returned!"); | ||
| }); | ||
|
|
||
| it("returns undefined when refresh buffer is passed", function() { | ||
| const tokenCache = new ExpiringAccessTokenCache(5000); | ||
| const accessToken = mockToken(-5000); | ||
| tokenCache.setCachedToken(accessToken); | ||
|
|
||
| const cachedToken = tokenCache.getCachedToken(); | ||
| assert.isUndefined(cachedToken, "A cached token was returned!"); | ||
| }); | ||
|
|
||
| it("clears the cached token when undefined is passed to setCachedToken", function() { | ||
| const tokenCache = new ExpiringAccessTokenCache(2000); | ||
| const accessToken = mockToken(5000); | ||
| tokenCache.setCachedToken(accessToken); | ||
| tokenCache.setCachedToken(undefined); | ||
|
|
||
| const cachedToken = tokenCache.getCachedToken(); | ||
| assert.isUndefined(cachedToken, "A cached token was returned!"); | ||
| }); | ||
| }); |
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Out of curiosity, what part will manage the cache?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The consuming policy, though right after sending this I realized that there's probably value in sharing token expiration logic in a more legitimate class instead of using a simple object that must be managed by the policy implementation. Thoughts?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree that there's definitely value in centralizing token expiration management, otherwise we will end up with a bug somewhere, or at least different implementations to a small extent. I wonder what would be the benefits of distributed token expiration logic, if that route is decided.