TokenCredential.supports_caching #19940
Closed
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.
Part of #19308. Today, BearerTokenCredentialPolicy caches the last access token it acquired and calls
get_token()
only when this cached token will soon expire. This isn't safe with on-behalf-of tokens because they contain a user assertion identifying the user on whose behalf the application accesses resources. When an application changes its intended user, a client must not continue using a token for the prior user. Our design for on-behalf-of authentication makes the credential responsible for tracking the application's user assertion changes, which is to say it requires BearerTokenCredentialPolicy to callget_token()
every time it authorizes a request. So, this PR addsTokenCredential.supports_caching()
. BearerTokenCredentialPolicy calls this method to learn whether a given TokenCredential maintains its own token cache. When this returns True, BearerTokenCredentialPolicy defers to the credential's cache, callingget_token()
every time it wants to authorize a request. The policy doesn't expect all credentials to implement this method, and assumes it's safe to cache tokens from credentials which do not.