Skip to content

Commit 594379a

Browse files
[Communication] - Make AsyncTokenRefresher in CommunicationTokenRefreshOptions optional (Azure#18149)
* Communication - Make AsyncTokenRefresher in CommunicationTokenRefreshOptions optional * Update the tests to do not have custom logic in creation of token credential
1 parent 231fb45 commit 594379a

File tree

5 files changed

+27
-39
lines changed

5 files changed

+27
-39
lines changed

sdk/communication/Azure.Communication.Common/api/Azure.Communication.Common.netstandard2.0.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,15 @@ protected CommunicationIdentifier() { }
3636
}
3737
public sealed partial class CommunicationTokenCredential : System.IDisposable
3838
{
39-
public CommunicationTokenCredential(Azure.Communication.CommunicationTokenRefreshOptions tokenRefreshOptions) { }
39+
public CommunicationTokenCredential(Azure.Communication.CommunicationTokenRefreshOptions options) { }
4040
public CommunicationTokenCredential(string token) { }
4141
public void Dispose() { }
4242
public Azure.Core.AccessToken GetToken(System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
4343
public System.Threading.Tasks.ValueTask<Azure.Core.AccessToken> GetTokenAsync(System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
4444
}
4545
public partial class CommunicationTokenRefreshOptions
4646
{
47-
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) { }
47+
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) { }
4848
}
4949
public partial class CommunicationUserIdentifier : Azure.Communication.CommunicationIdentifier
5050
{

sdk/communication/Azure.Communication.Common/src/AutoRefreshTokenCredential.cs

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,28 +12,21 @@ internal sealed class AutoRefreshTokenCredential : ICommunicationTokenCredential
1212
{
1313
private readonly ThreadSafeRefreshableAccessTokenCache _accessTokenCache;
1414

15-
public AutoRefreshTokenCredential(
16-
Func<CancellationToken, string> tokenRefresher,
17-
Func<CancellationToken, ValueTask<string>> asyncTokenRefresher,
18-
string? initialToken,
19-
bool refreshProactively)
20-
: this(tokenRefresher, asyncTokenRefresher, initialToken, refreshProactively, null, null)
15+
public AutoRefreshTokenCredential(CommunicationTokenRefreshOptions options)
16+
: this(options, null, null)
2117
{ }
2218

2319
internal AutoRefreshTokenCredential(
24-
Func<CancellationToken, string> tokenRefresher,
25-
Func<CancellationToken, ValueTask<string>> asyncTokenRefresher,
26-
string? initialToken,
27-
bool refreshProactively,
20+
CommunicationTokenRefreshOptions options,
2821
Func<Action, TimeSpan, ThreadSafeRefreshableAccessTokenCache.IScheduledAction>? scheduler,
2922
Func<DateTimeOffset>? utcNowProvider)
3023
{
31-
if (initialToken == null)
24+
if (options.InitialToken is null)
3225
{
3326
_accessTokenCache = new ThreadSafeRefreshableAccessTokenCache(
3427
Refresh,
3528
RefreshAsync,
36-
refreshProactively,
29+
options.RefreshProactively,
3730
scheduler,
3831
utcNowProvider);
3932
}
@@ -42,17 +35,17 @@ internal AutoRefreshTokenCredential(
4235
_accessTokenCache = new ThreadSafeRefreshableAccessTokenCache(
4336
Refresh,
4437
RefreshAsync,
45-
refreshProactively,
46-
initialValue: JwtTokenParser.CreateAccessToken(initialToken),
38+
options.RefreshProactively,
39+
initialValue: JwtTokenParser.CreateAccessToken(options.InitialToken),
4740
scheduler,
4841
utcNowProvider);
4942
}
5043

5144
AccessToken Refresh(CancellationToken cancellationToken)
52-
=> JwtTokenParser.CreateAccessToken(tokenRefresher(cancellationToken));
45+
=> JwtTokenParser.CreateAccessToken(options.TokenRefresher(cancellationToken));
5346

5447
async ValueTask<AccessToken> RefreshAsync(CancellationToken cancellationToken)
55-
=> JwtTokenParser.CreateAccessToken(await asyncTokenRefresher(cancellationToken).ConfigureAwait(false));
48+
=> JwtTokenParser.CreateAccessToken(await options.AsyncTokenRefresher(cancellationToken).ConfigureAwait(false));
5649
}
5750

5851
public AccessToken GetToken(CancellationToken cancellationToken = default)

sdk/communication/Azure.Communication.Common/src/CommunicationTokenCredential.cs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,11 @@ public CommunicationTokenCredential(string token)
2727
/// <summary>
2828
/// 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.
2929
/// </summary>
30-
/// <param name="tokenRefreshOptions">Options for how the token will be refreshed</param>
31-
public CommunicationTokenCredential(CommunicationTokenRefreshOptions tokenRefreshOptions)
30+
/// <param name="options">Options for how the token will be refreshed</param>
31+
public CommunicationTokenCredential(CommunicationTokenRefreshOptions options)
3232
{
33-
Argument.AssertNotNull(tokenRefreshOptions, nameof(tokenRefreshOptions));
34-
_tokenCredential = new AutoRefreshTokenCredential(
35-
tokenRefreshOptions.TokenRefresher,
36-
tokenRefreshOptions.AsyncTokenRefresher ?? (cancellationToken => new ValueTask<string>(tokenRefreshOptions.TokenRefresher(cancellationToken))),
37-
tokenRefreshOptions.Token,
38-
tokenRefreshOptions.RefreshProactively);
33+
Argument.AssertNotNull(options, nameof(options));
34+
_tokenCredential = new AutoRefreshTokenCredential(options);
3935
}
4036

4137
/// <summary>

sdk/communication/Azure.Communication.Common/src/CommunicationTokenRefreshOptions.cs

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,26 +15,28 @@ public class CommunicationTokenRefreshOptions
1515
{
1616
internal bool RefreshProactively { get; }
1717
internal Func<CancellationToken, string> TokenRefresher { get; }
18-
internal Func<CancellationToken, ValueTask<string>>? AsyncTokenRefresher { get; }
19-
internal string? Token { get; }
18+
internal Func<CancellationToken, ValueTask<string>> AsyncTokenRefresher { get; }
19+
internal string? InitialToken { get; }
2020

2121
/// <summary>
2222
/// Initializes a new instance of <see cref="CommunicationTokenRefreshOptions"/>.
2323
/// </summary>
2424
/// <param name="refreshProactively">Indicates whether the token should be proactively renewed prior to expiry or renew on demand.</param>
25-
/// <param name="tokenRefresher">The function that provides the token acquired from the configurtaion SDK.</param>
26-
/// <param name="asyncTokenRefresher">The async function that provides the token acquired from the configurtaion SDK.</param>
27-
/// <param name="token">Optional token value.</param>
25+
/// <param name="tokenRefresher">The function that provides the token acquired from CommunicationIdentityClient.</param>
26+
/// <param name="asyncTokenRefresher">The async function that provides the token acquired from CommunicationIdentityClient, a null value defaults to <paramref name="tokenRefresher"/>.</param>
27+
/// <param name="initialToken">Optional token value.</param>
2828
public CommunicationTokenRefreshOptions(
2929
bool refreshProactively,
3030
Func<CancellationToken, string> tokenRefresher,
31-
Func<CancellationToken, ValueTask<string>>? asyncTokenRefresher,
32-
string? token = null)
31+
Func<CancellationToken, ValueTask<string>>? asyncTokenRefresher = null,
32+
string? initialToken = null)
3333
{
34+
Argument.AssertNotNull(tokenRefresher, nameof(tokenRefresher));
35+
3436
RefreshProactively = refreshProactively;
3537
TokenRefresher = tokenRefresher;
36-
AsyncTokenRefresher = asyncTokenRefresher;
37-
Token = token;
38+
AsyncTokenRefresher = asyncTokenRefresher ?? (cancellationToken => new ValueTask<string>(tokenRefresher(cancellationToken)));
39+
InitialToken = initialToken;
3840
}
3941
}
4042
}

sdk/communication/Azure.Communication.Common/tests/Identity/CommunicationTokenCredentialTest.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,7 @@ private static AutoRefreshTokenCredential CreateTokenCredentialWithTestClock(
2525
string? initialToken = null)
2626
{
2727
return new AutoRefreshTokenCredential(
28-
tokenRefresher,
29-
asyncTokenRefresher ?? (cancellationToken => new ValueTask<string>(tokenRefresher(cancellationToken))),
30-
initialToken,
31-
refreshProactively,
28+
new CommunicationTokenRefreshOptions(refreshProactively, tokenRefresher, asyncTokenRefresher, initialToken),
3229
testClock.Schedule,
3330
() => testClock.UtcNow);
3431
}

0 commit comments

Comments
 (0)