Skip to content

Commit

Permalink
[Communication] - Make AsyncTokenRefresher in CommunicationTokenRefre…
Browse files Browse the repository at this point in the history
…shOptions optional (#18149)

* Communication - Make AsyncTokenRefresher in CommunicationTokenRefreshOptions optional

* Update the tests to do not have custom logic in creation of token credential
  • Loading branch information
RezaJooyandeh authored Jan 22, 2021
1 parent 231fb45 commit 594379a
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,15 @@ 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; }
public System.Threading.Tasks.ValueTask<Azure.Core.AccessToken> GetTokenAsync(System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
}
public partial class CommunicationTokenRefreshOptions
{
public CommunicationTokenRefreshOptions(bool refreshProactively, System.Func<System.Threading.CancellationToken, string> tokenRefresher, System.Func<System.Threading.CancellationToken, System.Threading.Tasks.ValueTask<string>>? asyncTokenRefresher, string? token = null) { }
public CommunicationTokenRefreshOptions(bool refreshProactively, System.Func<System.Threading.CancellationToken, string> tokenRefresher, System.Func<System.Threading.CancellationToken, System.Threading.Tasks.ValueTask<string>>? asyncTokenRefresher = null, string? initialToken = null) { }
}
public partial class CommunicationUserIdentifier : Azure.Communication.CommunicationIdentifier
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,28 +12,21 @@ internal sealed class AutoRefreshTokenCredential : ICommunicationTokenCredential
{
private readonly ThreadSafeRefreshableAccessTokenCache _accessTokenCache;

public AutoRefreshTokenCredential(
Func<CancellationToken, string> tokenRefresher,
Func<CancellationToken, ValueTask<string>> asyncTokenRefresher,
string? initialToken,
bool refreshProactively)
: this(tokenRefresher, asyncTokenRefresher, initialToken, refreshProactively, null, null)
public AutoRefreshTokenCredential(CommunicationTokenRefreshOptions options)
: this(options, null, null)
{ }

internal AutoRefreshTokenCredential(
Func<CancellationToken, string> tokenRefresher,
Func<CancellationToken, ValueTask<string>> asyncTokenRefresher,
string? initialToken,
bool refreshProactively,
CommunicationTokenRefreshOptions options,
Func<Action, TimeSpan, ThreadSafeRefreshableAccessTokenCache.IScheduledAction>? scheduler,
Func<DateTimeOffset>? utcNowProvider)
{
if (initialToken == null)
if (options.InitialToken is null)
{
_accessTokenCache = new ThreadSafeRefreshableAccessTokenCache(
Refresh,
RefreshAsync,
refreshProactively,
options.RefreshProactively,
scheduler,
utcNowProvider);
}
Expand All @@ -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<AccessToken> RefreshAsync(CancellationToken cancellationToken)
=> JwtTokenParser.CreateAccessToken(await asyncTokenRefresher(cancellationToken).ConfigureAwait(false));
=> JwtTokenParser.CreateAccessToken(await options.AsyncTokenRefresher(cancellationToken).ConfigureAwait(false));
}

public AccessToken GetToken(CancellationToken cancellationToken = default)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,11 @@ public CommunicationTokenCredential(string token)
/// <summary>
/// Initializes a new instance of <see cref="CommunicationTokenCredential"/> that automatically renews the token upon expiry or proactively prior to expiration to speed up the requests.
/// </summary>
/// <param name="tokenRefreshOptions">Options for how the token will be refreshed</param>
public CommunicationTokenCredential(CommunicationTokenRefreshOptions tokenRefreshOptions)
/// <param name="options">Options for how the token will be refreshed</param>
public CommunicationTokenCredential(CommunicationTokenRefreshOptions options)
{
Argument.AssertNotNull(tokenRefreshOptions, nameof(tokenRefreshOptions));
_tokenCredential = new AutoRefreshTokenCredential(
tokenRefreshOptions.TokenRefresher,
tokenRefreshOptions.AsyncTokenRefresher ?? (cancellationToken => new ValueTask<string>(tokenRefreshOptions.TokenRefresher(cancellationToken))),
tokenRefreshOptions.Token,
tokenRefreshOptions.RefreshProactively);
Argument.AssertNotNull(options, nameof(options));
_tokenCredential = new AutoRefreshTokenCredential(options);
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,26 +15,28 @@ public class CommunicationTokenRefreshOptions
{
internal bool RefreshProactively { get; }
internal Func<CancellationToken, string> TokenRefresher { get; }
internal Func<CancellationToken, ValueTask<string>>? AsyncTokenRefresher { get; }
internal string? Token { get; }
internal Func<CancellationToken, ValueTask<string>> AsyncTokenRefresher { get; }
internal string? InitialToken { get; }

/// <summary>
/// Initializes a new instance of <see cref="CommunicationTokenRefreshOptions"/>.
/// </summary>
/// <param name="refreshProactively">Indicates whether the token should be proactively renewed prior to expiry or renew on demand.</param>
/// <param name="tokenRefresher">The function that provides the token acquired from the configurtaion SDK.</param>
/// <param name="asyncTokenRefresher">The async function that provides the token acquired from the configurtaion SDK.</param>
/// <param name="token">Optional token value.</param>
/// <param name="tokenRefresher">The function that provides the token acquired from CommunicationIdentityClient.</param>
/// <param name="asyncTokenRefresher">The async function that provides the token acquired from CommunicationIdentityClient, a null value defaults to <paramref name="tokenRefresher"/>.</param>
/// <param name="initialToken">Optional token value.</param>
public CommunicationTokenRefreshOptions(
bool refreshProactively,
Func<CancellationToken, string> tokenRefresher,
Func<CancellationToken, ValueTask<string>>? asyncTokenRefresher,
string? token = null)
Func<CancellationToken, ValueTask<string>>? asyncTokenRefresher = null,
string? initialToken = null)
{
Argument.AssertNotNull(tokenRefresher, nameof(tokenRefresher));

RefreshProactively = refreshProactively;
TokenRefresher = tokenRefresher;
AsyncTokenRefresher = asyncTokenRefresher;
Token = token;
AsyncTokenRefresher = asyncTokenRefresher ?? (cancellationToken => new ValueTask<string>(tokenRefresher(cancellationToken)));
InitialToken = initialToken;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,7 @@ private static AutoRefreshTokenCredential CreateTokenCredentialWithTestClock(
string? initialToken = null)
{
return new AutoRefreshTokenCredential(
tokenRefresher,
asyncTokenRefresher ?? (cancellationToken => new ValueTask<string>(tokenRefresher(cancellationToken))),
initialToken,
refreshProactively,
new CommunicationTokenRefreshOptions(refreshProactively, tokenRefresher, asyncTokenRefresher, initialToken),
testClock.Schedule,
() => testClock.UtcNow);
}
Expand Down

0 comments on commit 594379a

Please sign in to comment.