From 594379a280c8d7f4c5a24be7ef5395794e4a9016 Mon Sep 17 00:00:00 2001 From: Reza Jooyandeh Date: Fri, 22 Jan 2021 15:04:07 -0800 Subject: [PATCH] [Communication] - Make AsyncTokenRefresher in CommunicationTokenRefreshOptions optional (#18149) * Communication - Make AsyncTokenRefresher in CommunicationTokenRefreshOptions optional * Update the tests to do not have custom logic in creation of token credential --- ...ure.Communication.Common.netstandard2.0.cs | 4 +-- .../src/AutoRefreshTokenCredential.cs | 25 +++++++------------ .../src/CommunicationTokenCredential.cs | 12 +++------ .../src/CommunicationTokenRefreshOptions.cs | 20 ++++++++------- .../CommunicationTokenCredentialTest.cs | 5 +--- 5 files changed, 27 insertions(+), 39 deletions(-) diff --git a/sdk/communication/Azure.Communication.Common/api/Azure.Communication.Common.netstandard2.0.cs b/sdk/communication/Azure.Communication.Common/api/Azure.Communication.Common.netstandard2.0.cs index 40b5c48dc9f8..8bc4200e6d3c 100644 --- a/sdk/communication/Azure.Communication.Common/api/Azure.Communication.Common.netstandard2.0.cs +++ b/sdk/communication/Azure.Communication.Common/api/Azure.Communication.Common.netstandard2.0.cs @@ -36,7 +36,7 @@ protected CommunicationIdentifier() { } } public sealed partial class CommunicationTokenCredential : System.IDisposable { - public CommunicationTokenCredential(Azure.Communication.CommunicationTokenRefreshOptions tokenRefreshOptions) { } + public CommunicationTokenCredential(Azure.Communication.CommunicationTokenRefreshOptions options) { } public CommunicationTokenCredential(string token) { } public void Dispose() { } public Azure.Core.AccessToken GetToken(System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; } @@ -44,7 +44,7 @@ public void Dispose() { } } public partial class CommunicationTokenRefreshOptions { - public CommunicationTokenRefreshOptions(bool refreshProactively, System.Func tokenRefresher, System.Func>? asyncTokenRefresher, string? token = null) { } + public CommunicationTokenRefreshOptions(bool refreshProactively, System.Func tokenRefresher, System.Func>? asyncTokenRefresher = null, string? initialToken = null) { } } public partial class CommunicationUserIdentifier : Azure.Communication.CommunicationIdentifier { diff --git a/sdk/communication/Azure.Communication.Common/src/AutoRefreshTokenCredential.cs b/sdk/communication/Azure.Communication.Common/src/AutoRefreshTokenCredential.cs index 48775305d6fc..282b08005431 100644 --- a/sdk/communication/Azure.Communication.Common/src/AutoRefreshTokenCredential.cs +++ b/sdk/communication/Azure.Communication.Common/src/AutoRefreshTokenCredential.cs @@ -12,28 +12,21 @@ internal sealed class AutoRefreshTokenCredential : ICommunicationTokenCredential { private readonly ThreadSafeRefreshableAccessTokenCache _accessTokenCache; - public AutoRefreshTokenCredential( - Func tokenRefresher, - Func> asyncTokenRefresher, - string? initialToken, - bool refreshProactively) - : this(tokenRefresher, asyncTokenRefresher, initialToken, refreshProactively, null, null) + public AutoRefreshTokenCredential(CommunicationTokenRefreshOptions options) + : this(options, null, null) { } internal AutoRefreshTokenCredential( - Func tokenRefresher, - Func> asyncTokenRefresher, - string? initialToken, - bool refreshProactively, + CommunicationTokenRefreshOptions options, Func? scheduler, Func? utcNowProvider) { - if (initialToken == null) + if (options.InitialToken is null) { _accessTokenCache = new ThreadSafeRefreshableAccessTokenCache( Refresh, RefreshAsync, - refreshProactively, + options.RefreshProactively, scheduler, utcNowProvider); } @@ -42,17 +35,17 @@ internal AutoRefreshTokenCredential( _accessTokenCache = new ThreadSafeRefreshableAccessTokenCache( Refresh, RefreshAsync, - refreshProactively, - initialValue: JwtTokenParser.CreateAccessToken(initialToken), + options.RefreshProactively, + initialValue: JwtTokenParser.CreateAccessToken(options.InitialToken), scheduler, utcNowProvider); } AccessToken Refresh(CancellationToken cancellationToken) - => JwtTokenParser.CreateAccessToken(tokenRefresher(cancellationToken)); + => JwtTokenParser.CreateAccessToken(options.TokenRefresher(cancellationToken)); async ValueTask RefreshAsync(CancellationToken cancellationToken) - => JwtTokenParser.CreateAccessToken(await asyncTokenRefresher(cancellationToken).ConfigureAwait(false)); + => JwtTokenParser.CreateAccessToken(await options.AsyncTokenRefresher(cancellationToken).ConfigureAwait(false)); } public AccessToken GetToken(CancellationToken cancellationToken = default) diff --git a/sdk/communication/Azure.Communication.Common/src/CommunicationTokenCredential.cs b/sdk/communication/Azure.Communication.Common/src/CommunicationTokenCredential.cs index e5ab207b0d46..fb40f8d3b458 100644 --- a/sdk/communication/Azure.Communication.Common/src/CommunicationTokenCredential.cs +++ b/sdk/communication/Azure.Communication.Common/src/CommunicationTokenCredential.cs @@ -27,15 +27,11 @@ public CommunicationTokenCredential(string token) /// /// Initializes a new instance of that automatically renews the token upon expiry or proactively prior to expiration to speed up the requests. /// - /// Options for how the token will be refreshed - public CommunicationTokenCredential(CommunicationTokenRefreshOptions tokenRefreshOptions) + /// Options for how the token will be refreshed + public CommunicationTokenCredential(CommunicationTokenRefreshOptions options) { - Argument.AssertNotNull(tokenRefreshOptions, nameof(tokenRefreshOptions)); - _tokenCredential = new AutoRefreshTokenCredential( - tokenRefreshOptions.TokenRefresher, - tokenRefreshOptions.AsyncTokenRefresher ?? (cancellationToken => new ValueTask(tokenRefreshOptions.TokenRefresher(cancellationToken))), - tokenRefreshOptions.Token, - tokenRefreshOptions.RefreshProactively); + Argument.AssertNotNull(options, nameof(options)); + _tokenCredential = new AutoRefreshTokenCredential(options); } /// diff --git a/sdk/communication/Azure.Communication.Common/src/CommunicationTokenRefreshOptions.cs b/sdk/communication/Azure.Communication.Common/src/CommunicationTokenRefreshOptions.cs index 8231a071b8c2..3f2b2fd41cf8 100644 --- a/sdk/communication/Azure.Communication.Common/src/CommunicationTokenRefreshOptions.cs +++ b/sdk/communication/Azure.Communication.Common/src/CommunicationTokenRefreshOptions.cs @@ -15,26 +15,28 @@ public class CommunicationTokenRefreshOptions { internal bool RefreshProactively { get; } internal Func TokenRefresher { get; } - internal Func>? AsyncTokenRefresher { get; } - internal string? Token { get; } + internal Func> AsyncTokenRefresher { get; } + internal string? InitialToken { get; } /// /// Initializes a new instance of . /// /// Indicates whether the token should be proactively renewed prior to expiry or renew on demand. - /// The function that provides the token acquired from the configurtaion SDK. - /// The async function that provides the token acquired from the configurtaion SDK. - /// Optional token value. + /// The function that provides the token acquired from CommunicationIdentityClient. + /// The async function that provides the token acquired from CommunicationIdentityClient, a null value defaults to . + /// Optional token value. public CommunicationTokenRefreshOptions( bool refreshProactively, Func tokenRefresher, - Func>? asyncTokenRefresher, - string? token = null) + Func>? asyncTokenRefresher = null, + string? initialToken = null) { + Argument.AssertNotNull(tokenRefresher, nameof(tokenRefresher)); + RefreshProactively = refreshProactively; TokenRefresher = tokenRefresher; - AsyncTokenRefresher = asyncTokenRefresher; - Token = token; + AsyncTokenRefresher = asyncTokenRefresher ?? (cancellationToken => new ValueTask(tokenRefresher(cancellationToken))); + InitialToken = initialToken; } } } diff --git a/sdk/communication/Azure.Communication.Common/tests/Identity/CommunicationTokenCredentialTest.cs b/sdk/communication/Azure.Communication.Common/tests/Identity/CommunicationTokenCredentialTest.cs index 4cd7696304b3..482a041ef8c2 100644 --- a/sdk/communication/Azure.Communication.Common/tests/Identity/CommunicationTokenCredentialTest.cs +++ b/sdk/communication/Azure.Communication.Common/tests/Identity/CommunicationTokenCredentialTest.cs @@ -25,10 +25,7 @@ private static AutoRefreshTokenCredential CreateTokenCredentialWithTestClock( string? initialToken = null) { return new AutoRefreshTokenCredential( - tokenRefresher, - asyncTokenRefresher ?? (cancellationToken => new ValueTask(tokenRefresher(cancellationToken))), - initialToken, - refreshProactively, + new CommunicationTokenRefreshOptions(refreshProactively, tokenRefresher, asyncTokenRefresher, initialToken), testClock.Schedule, () => testClock.UtcNow); }