-
Notifications
You must be signed in to change notification settings - Fork 153
BearerTokenAuthenticationPolicy to use shared_lock in case no refresh is needed
#5188
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
Changes from all commits
556080e
f9a137b
26decfb
81c1119
5d205a2
8dc12c4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -61,21 +61,49 @@ bool BearerTokenAuthenticationPolicy::AuthorizeRequestOnChallenge( | |
| return false; | ||
| } | ||
|
|
||
| namespace { | ||
| bool TokenNeedsRefresh( | ||
| Azure::Core::Credentials::AccessToken const& cachedToken, | ||
| Azure::Core::Credentials::TokenRequestContext const& cachedTokenRequestContext, | ||
| Azure::DateTime const& currentTime, | ||
| Azure::Core::Credentials::TokenRequestContext const& newTokenRequestContext) | ||
| { | ||
| return newTokenRequestContext.TenantId != cachedTokenRequestContext.TenantId | ||
| || newTokenRequestContext.Scopes != cachedTokenRequestContext.Scopes | ||
| || currentTime > (cachedToken.ExpiresOn - newTokenRequestContext.MinimumExpiration); | ||
| } | ||
|
Comment on lines
+65
to
+74
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This logic could benefit from a comment. For example, I understand that we need a token refresh if the tenant id and scope don't match, but why are we using current token's and cached token's expirations together like this: |
||
|
|
||
| void ApplyBearerToken( | ||
| Azure::Core::Http::Request& request, | ||
| Azure::Core::Credentials::AccessToken const& token) | ||
| { | ||
| request.SetHeader("authorization", "Bearer " + token.Token); | ||
| } | ||
| } // namespace | ||
|
|
||
| void BearerTokenAuthenticationPolicy::AuthenticateAndAuthorizeRequest( | ||
| Request& request, | ||
| Credentials::TokenRequestContext const& tokenRequestContext, | ||
| Context const& context) const | ||
| { | ||
| std::lock_guard<std::mutex> lock(m_accessTokenMutex); | ||
| DateTime const currentTime = std::chrono::system_clock::now(); | ||
antkmsft marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| { | ||
| std::shared_lock<std::shared_timed_mutex> readLock(m_accessTokenMutex); | ||
| if (!TokenNeedsRefresh(m_accessToken, m_accessTokenContext, currentTime, tokenRequestContext)) | ||
| { | ||
| ApplyBearerToken(request, m_accessToken); | ||
| return; | ||
| } | ||
| } | ||
|
|
||
| if (tokenRequestContext.TenantId != m_accessTokenContext.TenantId | ||
| || tokenRequestContext.Scopes != m_accessTokenContext.Scopes | ||
| || std::chrono::system_clock::now() | ||
| > (m_accessToken.ExpiresOn - tokenRequestContext.MinimumExpiration)) | ||
| std::unique_lock<std::shared_timed_mutex> writeLock(m_accessTokenMutex); | ||
| // Check if token needs refresh for the second time in case another thread has just updated it. | ||
| if (TokenNeedsRefresh(m_accessToken, m_accessTokenContext, currentTime, tokenRequestContext)) | ||
| { | ||
| m_accessToken = m_credential->GetToken(tokenRequestContext, context); | ||
| m_accessTokenContext = tokenRequestContext; | ||
| } | ||
|
|
||
| request.SetHeader("authorization", "Bearer " + m_accessToken.Token); | ||
| ApplyBearerToken(request, m_accessToken); | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.