From 2783f628165b1f6ee1d77288b2d15a45ae4852b1 Mon Sep 17 00:00:00 2001 From: Minnie Liu Date: Tue, 15 Dec 2020 12:29:03 -0800 Subject: [PATCH 01/12] Rename CommunicationUserCredential to CommunicationTokenCredential --- .../Azure.Communication.Chat/README.md | 4 +- .../src/Azure.Communication.Chat.csproj | 2 +- .../src/ChatClient.cs | 26 ++--- .../src/ChatThreadClient.cs | 14 +-- .../tests/ChatClients/ChatClientsTests.cs | 8 +- .../tests/Infrastructure/ChatLiveTestBase.cs | 4 +- .../tests/samples/Sample1_ThreadOperations.cs | 2 +- .../samples/Sample2_MessagingOperations.cs | 2 +- .../tests/samples/Sample3_MemberOperations.cs | 2 +- .../Azure.Communication.Common/README.md | 24 ++-- ...ntial.cs => AutoRefreshTokenCredential.cs} | 6 +- ...ial.cs => CommunicationTokenCredential.cs} | 36 +++--- ...IUserCredential.cs => ITokenCredential.cs} | 2 +- ...Credential.cs => StaticTokenCredential.cs} | 6 +- .../Azure.Communication.Common.Tests.csproj | 2 +- ...cs => CommunicationTokenCredentialTest.cs} | 104 +++++++++--------- .../CommunicationTokenCredentialTests.cs | 48 ++++---- ... => CommunicationBearerTokenCredential.cs} | 6 +- 18 files changed, 149 insertions(+), 149 deletions(-) rename sdk/communication/Azure.Communication.Common/src/Identity/{AutoRefreshUserCredential.cs => AutoRefreshTokenCredential.cs} (93%) rename sdk/communication/Azure.Communication.Common/src/Identity/{CommunicationUserCredential.cs => CommunicationTokenCredential.cs} (68%) rename sdk/communication/Azure.Communication.Common/src/Identity/{IUserCredential.cs => ITokenCredential.cs} (87%) rename sdk/communication/Azure.Communication.Common/src/Identity/{StaticUserCredential.cs => StaticTokenCredential.cs} (78%) rename sdk/communication/Azure.Communication.Common/tests/Identity/{CommunicationUserCredentialTest.cs => CommunicationTokenCredentialTest.cs} (74%) rename sdk/communication/Shared/src/{CommunicationTokenCredential.cs => CommunicationBearerTokenCredential.cs} (70%) diff --git a/sdk/communication/Azure.Communication.Chat/README.md b/sdk/communication/Azure.Communication.Chat/README.md index a848f2f2f4528..f7210dd5af8e6 100644 --- a/sdk/communication/Azure.Communication.Chat/README.md +++ b/sdk/communication/Azure.Communication.Chat/README.md @@ -49,7 +49,7 @@ This will allow you to create, get, or delete chat threads. ```C# Snippet:Azure_Communication_Chat_Tests_Samples_CreateChatClient ChatClient chatClient = new ChatClient( new Uri(endpoint), - new CommunicationUserCredential(userToken)); + new CommunicationTokenCredential(userToken)); ``` ### Create a ChatThreadClient @@ -165,7 +165,7 @@ Use `CreateChatThread` to create a chat thread client object. ```C# Snippet:Azure_Communication_Chat_Tests_Samples_CreateChatClient ChatClient chatClient = new ChatClient( new Uri(endpoint), - new CommunicationUserCredential(userToken)); + new CommunicationTokenCredential(userToken)); ``` ```C# Snippet:Azure_Communication_Chat_Tests_Samples_CreateThread var chatThreadMember = new ChatThreadMember(new CommunicationUser(threadCreatorId)) diff --git a/sdk/communication/Azure.Communication.Chat/src/Azure.Communication.Chat.csproj b/sdk/communication/Azure.Communication.Chat/src/Azure.Communication.Chat.csproj index d2d60dd947f6b..b20d768ef6e79 100644 --- a/sdk/communication/Azure.Communication.Chat/src/Azure.Communication.Chat.csproj +++ b/sdk/communication/Azure.Communication.Chat/src/Azure.Communication.Chat.csproj @@ -18,7 +18,7 @@ - + diff --git a/sdk/communication/Azure.Communication.Chat/src/ChatClient.cs b/sdk/communication/Azure.Communication.Chat/src/ChatClient.cs index d042ef2c06761..ab44d49667e38 100644 --- a/sdk/communication/Azure.Communication.Chat/src/ChatClient.cs +++ b/sdk/communication/Azure.Communication.Chat/src/ChatClient.cs @@ -21,23 +21,23 @@ public class ChatClient private readonly ClientDiagnostics _clientDiagnostics; private readonly ChatRestClient _chatRestClient; private readonly Uri _endpointUrl; - private readonly CommunicationUserCredential _communicationUserCredential; + private readonly CommunicationTokenCredential _communicationTokenCredential; private readonly ChatClientOptions _chatClientOptions; private const string MultiStatusThreadResourceType = "THREAD"; /// Initializes a new instance of . /// The uri for the Azure Communication Services Chat. - /// Instance of . + /// Instance of . /// Chat client options exposing , , , etc. - public ChatClient(Uri endpointUrl, CommunicationUserCredential communicationUserCredential, ChatClientOptions? options = default) + public ChatClient(Uri endpointUrl, CommunicationTokenCredential communicationTokenCredential, ChatClientOptions? options = default) { - Argument.AssertNotNull(communicationUserCredential, nameof(communicationUserCredential)); + Argument.AssertNotNull(communicationTokenCredential, nameof(communicationTokenCredential)); Argument.AssertNotNull(endpointUrl, nameof(endpointUrl)); _chatClientOptions = options ?? new ChatClientOptions(); - _communicationUserCredential = communicationUserCredential; + _communicationTokenCredential = communicationTokenCredential; _endpointUrl = endpointUrl; _clientDiagnostics = new ClientDiagnostics(_chatClientOptions); - HttpPipeline pipeline = CreatePipelineFromOptions(_chatClientOptions, communicationUserCredential); + HttpPipeline pipeline = CreatePipelineFromOptions(_chatClientOptions, communicationTokenCredential); _chatRestClient = new ChatRestClient(_clientDiagnostics, pipeline, endpointUrl.AbsoluteUri, _chatClientOptions.ApiVersion); } @@ -47,7 +47,7 @@ protected ChatClient() _clientDiagnostics = null!; _chatRestClient = null!; _endpointUrl = null!; - _communicationUserCredential = null!; + _communicationTokenCredential = null!; _chatClientOptions = null!; } @@ -66,7 +66,7 @@ public virtual async Task CreateChatThreadAsync(string topic, { Response threadResponse = await _chatRestClient.CreateChatThreadAsync(topic, members.Select(x => x.ToChatThreadMemberInternal()), cancellationToken).ConfigureAwait(false); string threadId = threadResponse.Value.MultipleStatus.First(x => x.Type.ToUpperInvariant() == MultiStatusThreadResourceType).Id; - return new ChatThreadClient(threadId, _endpointUrl, _communicationUserCredential, _chatClientOptions); + return new ChatThreadClient(threadId, _endpointUrl, _communicationTokenCredential, _chatClientOptions); } catch (Exception ex) { @@ -88,7 +88,7 @@ public virtual ChatThreadClient CreateChatThread(string topic, IEnumerable threadResponse = _chatRestClient.CreateChatThread(topic, members.Select(x=>x.ToChatThreadMemberInternal()), cancellationToken); string threadId = threadResponse.Value.MultipleStatus.First(x => x.Type.ToUpperInvariant() == MultiStatusThreadResourceType).Id; - return new ChatThreadClient(threadId, _endpointUrl, _communicationUserCredential, _chatClientOptions); + return new ChatThreadClient(threadId, _endpointUrl, _communicationTokenCredential, _chatClientOptions); } catch (Exception ex) { @@ -105,7 +105,7 @@ public virtual ChatThreadClient GetChatThreadClient(string threadId) scope.Start(); try { - return new ChatThreadClient(threadId, _endpointUrl, _communicationUserCredential, _chatClientOptions); + return new ChatThreadClient(threadId, _endpointUrl, _communicationTokenCredential, _chatClientOptions); } catch (Exception ex) { @@ -279,10 +279,10 @@ public virtual Response DeleteChatThread(string threadId, CancellationToken canc #endregion - private static HttpPipeline CreatePipelineFromOptions(ChatClientOptions options, CommunicationUserCredential communicationUserCredential) + private static HttpPipeline CreatePipelineFromOptions(ChatClientOptions options, CommunicationTokenCredential communicationTokenCredential) { - var tokenCredential = new CommunicationTokenCredential(communicationUserCredential); - var authenticationPolicy = new BearerTokenAuthenticationPolicy(tokenCredential, ""); + var bearerTokenCredential = new CommunicationBearerTokenCredential(communicationTokenCredential); + var authenticationPolicy = new BearerTokenAuthenticationPolicy(bearerTokenCredential, ""); return HttpPipelineBuilder.Build(options, authenticationPolicy); } } diff --git a/sdk/communication/Azure.Communication.Chat/src/ChatThreadClient.cs b/sdk/communication/Azure.Communication.Chat/src/ChatThreadClient.cs index 3d2cced25f9e7..e2ace112e8c79 100644 --- a/sdk/communication/Azure.Communication.Chat/src/ChatThreadClient.cs +++ b/sdk/communication/Azure.Communication.Chat/src/ChatThreadClient.cs @@ -29,18 +29,18 @@ public class ChatThreadClient /// Initializes a new instance of . /// /// The uri for the Azure Communication Services Chat. - /// Instance of . + /// Instance of . /// Chat client options exposing , , , etc. /// This occurs when one of the required arguments is null. - internal ChatThreadClient(string threadId, Uri endpointUrl, CommunicationUserCredential communicationUserCredential, ChatClientOptions? options = default) + internal ChatThreadClient(string threadId, Uri endpointUrl, CommunicationTokenCredential communicationTokenCredential, ChatClientOptions? options = default) { Argument.AssertNotNull(threadId, nameof(threadId)); - Argument.AssertNotNull(communicationUserCredential, nameof(communicationUserCredential)); + Argument.AssertNotNull(communicationTokenCredential, nameof(communicationTokenCredential)); Argument.AssertNotNull(endpointUrl, nameof(endpointUrl)); options ??= new ChatClientOptions(); Id = threadId; _clientDiagnostics = new ClientDiagnostics(options); - HttpPipeline pipeline = CreatePipelineFromOptions(options, communicationUserCredential); + HttpPipeline pipeline = CreatePipelineFromOptions(options, communicationTokenCredential); _chatRestClient = new ChatRestClient(_clientDiagnostics, pipeline, endpointUrl.AbsoluteUri, options.ApiVersion); } @@ -586,10 +586,10 @@ Page FirstPageFunc(int? pageSizeHint) } #endregion - private static HttpPipeline CreatePipelineFromOptions(ChatClientOptions options, CommunicationUserCredential communicationUserCredential) + private static HttpPipeline CreatePipelineFromOptions(ChatClientOptions options, CommunicationTokenCredential communicationTokenCredential) { - var tokenCredential = new CommunicationTokenCredential(communicationUserCredential); - var authenticationPolicy = new BearerTokenAuthenticationPolicy(tokenCredential, ""); + var bearerTokenCredential = new CommunicationBearerTokenCredential(communicationTokenCredential); + var authenticationPolicy = new BearerTokenAuthenticationPolicy(bearerTokenCredential, ""); return HttpPipelineBuilder.Build(options, authenticationPolicy); } } diff --git a/sdk/communication/Azure.Communication.Chat/tests/ChatClients/ChatClientsTests.cs b/sdk/communication/Azure.Communication.Chat/tests/ChatClients/ChatClientsTests.cs index 65a3243c7ac4c..b383d5c124f34 100644 --- a/sdk/communication/Azure.Communication.Chat/tests/ChatClients/ChatClientsTests.cs +++ b/sdk/communication/Azure.Communication.Chat/tests/ChatClients/ChatClientsTests.cs @@ -35,8 +35,8 @@ public async Task OrderInGetMessagesIteratorIsNotAltered() }; //act - var communicationUserCredential = new CommunicationUserCredential(ChatRecordedTestSanitizer.SanitizedUnsignedUserTokenValue); - var chatThreadClient = new ChatThreadClient(threadId, uri, communicationUserCredential, chatClientOptions); + var communicationTokenCredential = new CommunicationTokenCredential(ChatRecordedTestSanitizer.SanitizedUnsignedUserTokenValue); + var chatThreadClient = new ChatThreadClient(threadId, uri, communicationTokenCredential, chatClientOptions); AsyncPageable allMessages = chatThreadClient.GetMessagesAsync(); //assert @@ -76,8 +76,8 @@ public async Task OrderInGetMessagesIteratorIsNotAlteredByPaging() }; //act - var communicationUserCredential = new CommunicationUserCredential(ChatRecordedTestSanitizer.SanitizedUnsignedUserTokenValue); - var chatThreadClient = new ChatThreadClient(threadId, uri, communicationUserCredential, chatClientOptions); + var communicationTokenCredential = new CommunicationTokenCredential(ChatRecordedTestSanitizer.SanitizedUnsignedUserTokenValue); + var chatThreadClient = new ChatThreadClient(threadId, uri, communicationTokenCredential, chatClientOptions); AsyncPageable allMessages = chatThreadClient.GetMessagesAsync(); //assert diff --git a/sdk/communication/Azure.Communication.Chat/tests/Infrastructure/ChatLiveTestBase.cs b/sdk/communication/Azure.Communication.Chat/tests/Infrastructure/ChatLiveTestBase.cs index b5a89ff6ecced..6c6b8c57c3e4f 100644 --- a/sdk/communication/Azure.Communication.Chat/tests/Infrastructure/ChatLiveTestBase.cs +++ b/sdk/communication/Azure.Communication.Chat/tests/Infrastructure/ChatLiveTestBase.cs @@ -38,8 +38,8 @@ protected ChatClient CreateInstrumentedChatClient(string token) token = ChatRecordedTestSanitizer.SanitizedUnsignedUserTokenValue; } - CommunicationUserCredential communicationUserCredential = new CommunicationUserCredential(token); - return InstrumentClient(new ChatClient(new Uri(TestEnvironment.ChatApiUrl()), communicationUserCredential, + CommunicationTokenCredential communicationTokenCredential = new CommunicationTokenCredential(token); + return InstrumentClient(new ChatClient(new Uri(TestEnvironment.ChatApiUrl()), communicationTokenCredential, InstrumentClientOptions(new ChatClientOptions()))); } diff --git a/sdk/communication/Azure.Communication.Chat/tests/samples/Sample1_ThreadOperations.cs b/sdk/communication/Azure.Communication.Chat/tests/samples/Sample1_ThreadOperations.cs index eae03526feb75..e40a0f1eff482 100644 --- a/sdk/communication/Azure.Communication.Chat/tests/samples/Sample1_ThreadOperations.cs +++ b/sdk/communication/Azure.Communication.Chat/tests/samples/Sample1_ThreadOperations.cs @@ -27,7 +27,7 @@ public async Task CreateGetUpdateDeleteThreadAsync() #region Snippet:Azure_Communication_Chat_Tests_Samples_CreateChatClient ChatClient chatClient = new ChatClient( new Uri(endpoint), - new CommunicationUserCredential(userToken)); + new CommunicationTokenCredential(userToken)); #endregion Snippet:Azure_Communication_Chat_Tests_Samples_CreateChatClient #region Snippet:Azure_Communication_Chat_Tests_Samples_CreateThread diff --git a/sdk/communication/Azure.Communication.Chat/tests/samples/Sample2_MessagingOperations.cs b/sdk/communication/Azure.Communication.Chat/tests/samples/Sample2_MessagingOperations.cs index 21479c0c622ad..585c068222dbf 100644 --- a/sdk/communication/Azure.Communication.Chat/tests/samples/Sample2_MessagingOperations.cs +++ b/sdk/communication/Azure.Communication.Chat/tests/samples/Sample2_MessagingOperations.cs @@ -26,7 +26,7 @@ public async Task SendGetUpdateDeleteMessagesSendNotificationReadReceiptsAsync() ChatClient chatClient = new ChatClient( new Uri(endpoint), - new CommunicationUserCredential(userToken)); + new CommunicationTokenCredential(userToken)); var chatThreadMember = new ChatThreadMember(new CommunicationUser(theadCreatorMemberId)) { diff --git a/sdk/communication/Azure.Communication.Chat/tests/samples/Sample3_MemberOperations.cs b/sdk/communication/Azure.Communication.Chat/tests/samples/Sample3_MemberOperations.cs index b6cee4913b1dd..b9037ce688b56 100644 --- a/sdk/communication/Azure.Communication.Chat/tests/samples/Sample3_MemberOperations.cs +++ b/sdk/communication/Azure.Communication.Chat/tests/samples/Sample3_MemberOperations.cs @@ -31,7 +31,7 @@ public async Task GetAddRemoveMembersAsync() ChatClient chatClient = new ChatClient( new Uri(endpoint), - new CommunicationUserCredential(userToken)); + new CommunicationTokenCredential(userToken)); var chatThreadMember = new ChatThreadMember(new CommunicationUser(theadCreatorMemberId)) { diff --git a/sdk/communication/Azure.Communication.Common/README.md b/sdk/communication/Azure.Communication.Common/README.md index 5337e5f9e144d..efd23ac05449b 100644 --- a/sdk/communication/Azure.Communication.Common/README.md +++ b/sdk/communication/Azure.Communication.Common/README.md @@ -30,33 +30,33 @@ This module does not contain a client and instead libraries that help other Azur ### Key concepts -### CommunicationUserCredential +### CommunicationTokenCredential -`CommunicationUserCredential` authenticates a user with Communication Services, such as Chat or Calling. It optionally provides an auto-refresh mechanism to ensure a continuously stable authentication state during communications. +`CommunicationTokenCredential` authenticates a user with Communication Services, such as Chat or Calling. It optionally provides an auto-refresh mechanism to ensure a continuously stable authentication state during communications. -It is up to you the developer to first create valid user tokens with the Communication Administration SDK. Then you use these tokens with the `CommunicationUserCredential`. +It is up to you the developer to first create valid user tokens with the Communication Administration SDK. Then you use these tokens with the `CommunicationTokenCredential`. ## Examples ### Create a credential with a static token -For a short-lived clents when refreshing token upon expiry is not needed, `CommunicationUserCredential` can be instantited with a static token. +For a short-lived clents when refreshing token upon expiry is not needed, `CommunicationTokenCredential` can be instantited with a static token. -```C# Snippet:CommunicationUserCredential_CreateWithStaticToken +```C# Snippet:CommunicationTokenCredential_CreateWithStaticToken string token = Environment.GetEnvironmentVariable("COMMUNICATION_SERVICES_USER_TOKEN"); -using var userCredential = new CommunicationUserCredential(token); +using var tokenCredential = new CommunicationTokenCredential(token); ``` -Alternatively, you can create a `CommunicationUserCredential` with callback to renew tokens if expired. +Alternatively, you can create a `CommunicationTokenCredential` with callback to renew tokens if expired. Here we pass two imagined functions that make network requests to retrieve token strings for user Bob. -If callbacks are passed, upon requests (sending a chat message), `CommunicationUserCredential` ensures +If callbacks are passed, upon requests (sending a chat message), `CommunicationTokenCredential` ensures that a valid token is acquired prior to executing the request. Optionally, you can enable proactive token refreshing where a fresh token will be acquired as soon as the previous token approaches expiry. Using this method, your requests are less likely to be blocked to acquire a fresh token: -```C# Snippet:CommunicationUserCredential_CreateRefreshableWithoutInitialToken -using var userCredential = new CommunicationUserCredential( +```C# Snippet:CommunicationTokenCredential_CreateRefreshableWithoutInitialToken +using var userCredential = new CommunicationTokenCredential( refreshProactively: true, // Indicates if the token should be proactively refreshed in the background or only on-demand tokenRefresher: cancellationToken => FetchTokenForUserFromMyServer("bob@contoso.com", cancellationToken), asyncTokenRefresher: cancellationToken => FetchTokenForUserFromMyServerAsync("bob@contoso.com", cancellationToken)); @@ -64,9 +64,9 @@ using var userCredential = new CommunicationUserCredential( If you already have a token, you can optimize the token refreshing even further by passing that initial token: -```C# Snippet:CommunicationUserCredential_CreateRefreshableWithInitialToken +```C# Snippet:CommunicationTokenCredential_CreateRefreshableWithInitialToken string initialToken = Environment.GetEnvironmentVariable("COMMUNICATION_SERVICES_USER_TOKEN"); -using var userCredential = new CommunicationUserCredential( +using var userCredential = new CommunicationTokenCredential( refreshProactively: true, // Indicates if the token should be proactively refreshed in the background or only on-demand tokenRefresher: cancellationToken => FetchTokenForUserFromMyServer("bob@contoso.com", cancellationToken), asyncTokenRefresher: cancellationToken => FetchTokenForUserFromMyServerAsync("bob@contoso.com", cancellationToken), diff --git a/sdk/communication/Azure.Communication.Common/src/Identity/AutoRefreshUserCredential.cs b/sdk/communication/Azure.Communication.Common/src/Identity/AutoRefreshTokenCredential.cs similarity index 93% rename from sdk/communication/Azure.Communication.Common/src/Identity/AutoRefreshUserCredential.cs rename to sdk/communication/Azure.Communication.Common/src/Identity/AutoRefreshTokenCredential.cs index c36fd62a49693..777f389723c4f 100644 --- a/sdk/communication/Azure.Communication.Common/src/Identity/AutoRefreshUserCredential.cs +++ b/sdk/communication/Azure.Communication.Common/src/Identity/AutoRefreshTokenCredential.cs @@ -8,11 +8,11 @@ namespace Azure.Communication.Identity { - internal sealed class AutoRefreshUserCredential : IUserCredential + internal sealed class AutoRefreshTokenCredential : ITokenCredential { private readonly ThreadSafeRefreshableAccessTokenCache _accessTokenCache; - public AutoRefreshUserCredential( + public AutoRefreshTokenCredential( Func tokenRefresher, Func> asyncTokenRefresher, string? initialToken, @@ -20,7 +20,7 @@ public AutoRefreshUserCredential( : this(tokenRefresher, asyncTokenRefresher, initialToken, refreshProactively, null, null) { } - internal AutoRefreshUserCredential( + internal AutoRefreshTokenCredential( Func tokenRefresher, Func> asyncTokenRefresher, string? initialToken, diff --git a/sdk/communication/Azure.Communication.Common/src/Identity/CommunicationUserCredential.cs b/sdk/communication/Azure.Communication.Common/src/Identity/CommunicationTokenCredential.cs similarity index 68% rename from sdk/communication/Azure.Communication.Common/src/Identity/CommunicationUserCredential.cs rename to sdk/communication/Azure.Communication.Common/src/Identity/CommunicationTokenCredential.cs index a5da03d44c77b..e91cd2a8624f1 100644 --- a/sdk/communication/Azure.Communication.Common/src/Identity/CommunicationUserCredential.cs +++ b/sdk/communication/Azure.Communication.Common/src/Identity/CommunicationTokenCredential.cs @@ -9,33 +9,33 @@ namespace Azure.Communication.Identity { /// - /// The Azure Communication Services User token credential. + /// The Azure Communication Services Token Credential. /// - public sealed class CommunicationUserCredential : IDisposable + public sealed class CommunicationTokenCredential : IDisposable { - private readonly IUserCredential _userTokenCredential; + private readonly ITokenCredential _tokenCredential; private bool _isDisposed; /// - /// Initializes a new instance of . + /// Initializes a new instance of . /// /// User token acquired from Azure.Communication.Administration package. - public CommunicationUserCredential(string userToken) - => _userTokenCredential = new StaticUserCredential(userToken); + public CommunicationTokenCredential(string userToken) + => _tokenCredential = new StaticTokenCredential(userToken); /// - /// Initializes a new instance of that automatically renews the token upon expiry or proactively prior to expiration to speed up the requests. + /// Initializes a new instance of that automatically renews the token upon expiry or proactively prior to expiration to speed up the requests. /// - /// Indicates wheter user token should be proactively renewed prior to expiry or renew on demand. - /// The function that provides the user token acquired from the configurtaion SDK. - /// The async function that provides the user token acquired from the configurtaion SDK. - /// Optional user token to initialize. - public CommunicationUserCredential( + /// 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 to initialize. + public CommunicationTokenCredential( bool refreshProactively, Func tokenRefresher, Func>? asyncTokenRefresher = null, string? initialToken = null) - => _userTokenCredential = new AutoRefreshUserCredential( + => _tokenCredential = new AutoRefreshTokenCredential( tokenRefresher, asyncTokenRefresher ?? (cancellationToken => new ValueTask(tokenRefresher(cancellationToken))), initialToken, @@ -51,9 +51,9 @@ public CommunicationUserCredential( public ValueTask GetTokenAsync(CancellationToken cancellationToken = default) { if (_isDisposed) - throw new ObjectDisposedException(nameof(CommunicationUserCredential)); + throw new ObjectDisposedException(nameof(CommunicationTokenCredential)); - return _userTokenCredential.GetTokenAsync(cancellationToken); + return _tokenCredential.GetTokenAsync(cancellationToken); } /// @@ -64,9 +64,9 @@ public ValueTask GetTokenAsync(CancellationToken cancellationToken public AccessToken GetToken(CancellationToken cancellationToken = default) { if (_isDisposed) - throw new ObjectDisposedException(nameof(CommunicationUserCredential)); + throw new ObjectDisposedException(nameof(CommunicationTokenCredential)); - return _userTokenCredential.GetToken(cancellationToken); + return _tokenCredential.GetToken(cancellationToken); } /// @@ -75,7 +75,7 @@ public void Dispose() if (_isDisposed) return; - _userTokenCredential.Dispose(); + _tokenCredential.Dispose(); _isDisposed = true; } } diff --git a/sdk/communication/Azure.Communication.Common/src/Identity/IUserCredential.cs b/sdk/communication/Azure.Communication.Common/src/Identity/ITokenCredential.cs similarity index 87% rename from sdk/communication/Azure.Communication.Common/src/Identity/IUserCredential.cs rename to sdk/communication/Azure.Communication.Common/src/Identity/ITokenCredential.cs index b0c05ed2a2d5a..1622b66cef9fd 100644 --- a/sdk/communication/Azure.Communication.Common/src/Identity/IUserCredential.cs +++ b/sdk/communication/Azure.Communication.Common/src/Identity/ITokenCredential.cs @@ -8,7 +8,7 @@ namespace Azure.Communication.Identity { - internal interface IUserCredential : IDisposable + internal interface ITokenCredential : IDisposable { AccessToken GetToken(CancellationToken cancellationToken); ValueTask GetTokenAsync(CancellationToken cancellationToken); diff --git a/sdk/communication/Azure.Communication.Common/src/Identity/StaticUserCredential.cs b/sdk/communication/Azure.Communication.Common/src/Identity/StaticTokenCredential.cs similarity index 78% rename from sdk/communication/Azure.Communication.Common/src/Identity/StaticUserCredential.cs rename to sdk/communication/Azure.Communication.Common/src/Identity/StaticTokenCredential.cs index 0dbae9573b5ff..1f807e597e629 100644 --- a/sdk/communication/Azure.Communication.Common/src/Identity/StaticUserCredential.cs +++ b/sdk/communication/Azure.Communication.Common/src/Identity/StaticTokenCredential.cs @@ -1,4 +1,4 @@ -// Copyright (c) Microsoft Corporation. All rights reserved. +// Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT License. using System.Threading; @@ -7,11 +7,11 @@ namespace Azure.Communication.Identity { - internal sealed class StaticUserCredential : IUserCredential + internal sealed class StaticTokenCredential : ITokenCredential { private readonly AccessToken _accessToken; - public StaticUserCredential(string token) + public StaticTokenCredential(string token) { Argument.AssertNotNull(token, nameof(token)); diff --git a/sdk/communication/Azure.Communication.Common/tests/Azure.Communication.Common.Tests.csproj b/sdk/communication/Azure.Communication.Common/tests/Azure.Communication.Common.Tests.csproj index a2c25cdb4cd02..07db38cb6bc7f 100644 --- a/sdk/communication/Azure.Communication.Common/tests/Azure.Communication.Common.Tests.csproj +++ b/sdk/communication/Azure.Communication.Common/tests/Azure.Communication.Common.Tests.csproj @@ -13,7 +13,7 @@ - + diff --git a/sdk/communication/Azure.Communication.Common/tests/Identity/CommunicationUserCredentialTest.cs b/sdk/communication/Azure.Communication.Common/tests/Identity/CommunicationTokenCredentialTest.cs similarity index 74% rename from sdk/communication/Azure.Communication.Common/tests/Identity/CommunicationUserCredentialTest.cs rename to sdk/communication/Azure.Communication.Common/tests/Identity/CommunicationTokenCredentialTest.cs index f1114d21fea20..1b19dc8e0d641 100644 --- a/sdk/communication/Azure.Communication.Common/tests/Identity/CommunicationUserCredentialTest.cs +++ b/sdk/communication/Azure.Communication.Common/tests/Identity/CommunicationTokenCredentialTest.cs @@ -11,20 +11,20 @@ namespace Azure.Communication.Identity { - public class CommunicationUserCredentialTest + public class CommunicationTokenCredentialTest { private const string SampleToken = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjMyNTAzNjgwMDAwfQ.9i7FNNHHJT8cOzo-yrAUJyBSfJ-tPPk2emcHavOEpWc"; private const long SampleTokenExpiry = 32503680000; private const string ExpiredToken = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjEwMH0.1h_scYkNp-G98-O4cW6KvfJZwiz54uJMyeDACE4nypg"; - private static AutoRefreshUserCredential CreateUserCredentialWithTestClock( + private static AutoRefreshTokenCredential CreateTokenCredentialWithTestClock( TestClock testClock, bool refreshProactively, Func tokenRefresher, Func>? asyncTokenRefresher = null, string? initialToken = null) { - return new AutoRefreshUserCredential( + return new AutoRefreshTokenCredential( tokenRefresher, asyncTokenRefresher ?? (cancellationToken => new ValueTask(tokenRefresher(cancellationToken))), initialToken, @@ -34,41 +34,41 @@ private static AutoRefreshUserCredential CreateUserCredentialWithTestClock( } [Test] - public async Task CommunicationUserCredential_CreateStaticToken() + public async Task CommunicationTokenCredential_CreateStaticToken() { var token = ExpiredToken; - #region Snippet:CommunicationUserCredential_CreateWithStaticToken + #region Snippet:CommunicationTokenCredential_CreateWithStaticToken //@@string token = Environment.GetEnvironmentVariable("COMMUNICATION_SERVICES_USER_TOKEN"); - using var userCredential = new CommunicationUserCredential(token); + using var tokenCredential = new CommunicationTokenCredential(token); #endregion - await userCredential.GetTokenAsync(); + await tokenCredential.GetTokenAsync(); } [Test] - public async Task CommunicationUserCredential_CreateRefreshableWithoutInitialToken() + public async Task CommunicationTokenCredential_CreateRefreshableWithoutInitialToken() { - #region Snippet:CommunicationUserCredential_CreateRefreshableWithoutInitialToken - using var userCredential = new CommunicationUserCredential( + #region Snippet:CommunicationTokenCredential_CreateRefreshableWithoutInitialToken + using var tokenCredential = new CommunicationTokenCredential( refreshProactively: true, // Indicates if the token should be proactively refreshed in the background or only on-demand tokenRefresher: cancellationToken => FetchTokenForUserFromMyServer("bob@contoso.com", cancellationToken), asyncTokenRefresher: cancellationToken => FetchTokenForUserFromMyServerAsync("bob@contoso.com", cancellationToken)); #endregion - await userCredential.GetTokenAsync(); + await tokenCredential.GetTokenAsync(); } [Test] - public async Task CommunicationUserCredential_CreateRefreshableWithInitialToken() + public async Task CommunicationTokenCredential_CreateRefreshableWithInitialToken() { var initialToken = ExpiredToken; - #region Snippet:CommunicationUserCredential_CreateRefreshableWithInitialToken + #region Snippet:CommunicationTokenCredential_CreateRefreshableWithInitialToken //@@string initialToken = Environment.GetEnvironmentVariable("COMMUNICATION_SERVICES_USER_TOKEN"); - using var userCredential = new CommunicationUserCredential( + using var tokenCredential = new CommunicationTokenCredential( refreshProactively: true, // Indicates if the token should be proactively refreshed in the background or only on-demand tokenRefresher: cancellationToken => FetchTokenForUserFromMyServer("bob@contoso.com", cancellationToken), asyncTokenRefresher: cancellationToken => FetchTokenForUserFromMyServerAsync("bob@contoso.com", cancellationToken), initialToken); #endregion - await userCredential.GetTokenAsync(); + await tokenCredential.GetTokenAsync(); } #pragma warning disable IDE0060 // Remove unused parameter @@ -79,10 +79,10 @@ public async Task CommunicationUserCredential_CreateRefreshableWithInitialToken( [Test] [TestCase(SampleToken, SampleTokenExpiry)] - public async Task CommunicationUserCredential_DecodesToken(string token, long expectedExpiryUnixTimeSeconds) + public async Task CommunicationTokenCredential_DecodesToken(string token, long expectedExpiryUnixTimeSeconds) { - using var userCredential = new CommunicationUserCredential(token); - AccessToken accessToken = await userCredential.GetTokenAsync(); + using var tokenCredential = new CommunicationTokenCredential(token); + AccessToken accessToken = await tokenCredential.GetTokenAsync(); Assert.AreEqual(token, accessToken.Token); Assert.AreEqual(expectedExpiryUnixTimeSeconds, accessToken.ExpiresOn.ToUnixTimeSeconds()); @@ -92,21 +92,21 @@ public async Task CommunicationUserCredential_DecodesToken(string token, long ex [TestCase("foo")] [TestCase("foo.bar")] [TestCase("foo.bar.foobar")] - public void CommunicationUserCredential_ThrowsIfInvalidToken(string token) - => Assert.Throws(() => new CommunicationUserCredential(token)); + public void CommunicationTokenCredential_ThrowsIfInvalidToken(string token) + => Assert.Throws(() => new CommunicationTokenCredential(token)); [Test] - public void CommunicationUserCredential_ThrowsIfTokenIsNull() - => Assert.Throws(() => new CommunicationUserCredential(null!)); + public void CommunicationTokenCredential_ThrowsIfTokenIsNull() + => Assert.Throws(() => new CommunicationTokenCredential(null!)); [Test] [TestCase(false)] [TestCase(true)] - public async Task CommunicationUserCredential_StaticToken_ReturnsExpiredToken(bool async) + public async Task CommunicationTokenCredential_StaticToken_ReturnsExpiredToken(bool async) { - using var userCredential = new CommunicationUserCredential(ExpiredToken); + using var tokenCredential = new CommunicationTokenCredential(ExpiredToken); - var token = async ? await userCredential.GetTokenAsync() : userCredential.GetToken(); + var token = async ? await tokenCredential.GetTokenAsync() : tokenCredential.GetToken(); Assert.AreEqual(ExpiredToken, token.Token); } @@ -115,18 +115,18 @@ public async Task CommunicationUserCredential_StaticToken_ReturnsExpiredToken(bo [TestCase(false, true)] [TestCase(true, false)] [TestCase(true, true)] - public async Task CommunicationUserCredential_PassesCancellationToken(bool refreshProactively, bool async) + public async Task CommunicationTokenCredential_PassesCancellationToken(bool refreshProactively, bool async) { var cancellationToken = new CancellationToken(); CancellationToken? actualCancellationToken = null; - using var userCredential = new CommunicationUserCredential( + using var tokenCredential = new CommunicationTokenCredential( refreshProactively, RefreshToken, c => new ValueTask(RefreshToken(c)), ExpiredToken); - var token = async ? await userCredential.GetTokenAsync(cancellationToken) : userCredential.GetToken(cancellationToken); + var token = async ? await tokenCredential.GetTokenAsync(cancellationToken) : tokenCredential.GetToken(cancellationToken); Assert.AreEqual(cancellationToken, actualCancellationToken); string RefreshToken(CancellationToken token) @@ -137,12 +137,12 @@ string RefreshToken(CancellationToken token) } [Test] - public void CommunicationUserCredential_RefreshsTokenProactively_ImmediateWhenExpired() + public void CommunicationTokenCredential_RefreshsTokenProactively_ImmediateWhenExpired() { var refreshCallCount = 0; var testClock = new TestClock(); - using var userCredential = CreateUserCredentialWithTestClock( + using var tokenCredential = CreateTokenCredentialWithTestClock( testClock, true, RefreshToken, @@ -166,7 +166,7 @@ string RefreshToken(CancellationToken _) public async Task GetTokenSeries_RefreshTokenOnDemandIfNeeded(string token, long expectedExpiryUnixTimeSeconds, bool async) { var testClock = new TestClock(); - using var userCredential = CreateUserCredentialWithTestClock( + using var tokenCredential = CreateTokenCredentialWithTestClock( testClock, false, _ => token, @@ -176,8 +176,8 @@ public async Task GetTokenSeries_RefreshTokenOnDemandIfNeeded(string token, long Assert.AreEqual(0, testClock.ScheduledActions.Count()); AccessToken accessToken = async - ? await userCredential.GetTokenAsync() - : userCredential.GetToken(); + ? await tokenCredential.GetTokenAsync() + : tokenCredential.GetToken(); Assert.AreEqual(token, accessToken.Token); Assert.AreEqual(expectedExpiryUnixTimeSeconds, accessToken.ExpiresOn.ToUnixTimeSeconds()); @@ -190,17 +190,17 @@ public async Task GetTokenSeries_RefreshTokenOnDemandIfNeeded(string token, long [TestCase(SampleToken, true, true)] public void GetTokenSeries_Throws_IfTokenRequestedWhileDisposed(string token, bool refreshProactively, bool async) { - using var userCredential = new CommunicationUserCredential( + using var tokenCredential = new CommunicationTokenCredential( refreshProactively, _ => token, _ => new ValueTask(token)); - userCredential.Dispose(); + tokenCredential.Dispose(); if (async) - Assert.ThrowsAsync(async () => await userCredential.GetTokenAsync()); + Assert.ThrowsAsync(async () => await tokenCredential.GetTokenAsync()); else - Assert.Throws(() => userCredential.GetToken()); + Assert.Throws(() => tokenCredential.GetToken()); } [Test] @@ -208,28 +208,28 @@ public void GetTokenSeries_Throws_IfTokenRequestedWhileDisposed(string token, bo [TestCase(true)] public void GetTokenSeries_StaticToken_Throws_IfTokenRequestedWhileDisposed(bool async) { - using var userCredential = new CommunicationUserCredential(SampleToken); + using var tokenCredential = new CommunicationTokenCredential(SampleToken); - userCredential.Dispose(); + tokenCredential.Dispose(); if (async) - Assert.ThrowsAsync(async () => await userCredential.GetTokenAsync()); + Assert.ThrowsAsync(async () => await tokenCredential.GetTokenAsync()); else - Assert.Throws(() => userCredential.GetToken()); + Assert.Throws(() => tokenCredential.GetToken()); } [Test] public void Dispose_CancelsTimer() { var testClock = new TestClock(); - using var userCredential = CreateUserCredentialWithTestClock( + using var tokenCredential = CreateTokenCredentialWithTestClock( testClock, true, _ => SampleToken, _ => new ValueTask(SampleToken)); Assert.AreEqual(1, testClock.ScheduledActions.Count()); - userCredential.Dispose(); + tokenCredential.Dispose(); Assert.AreEqual(0, testClock.ScheduledActions.Count()); } @@ -248,20 +248,20 @@ public void CurrentTokenExpiringSoon_TokenRefreshed(bool inCriticalExpiryWindow) var newToken = GenerateTokenValidForMinutes(testClock.UtcNow, 55); var refreshCallCount = 0; - using var userCredential = CreateUserCredentialWithTestClock( + using var tokenCredential = CreateTokenCredentialWithTestClock( testClock, refreshProactively: true, RefreshToken, _ => throw new NotImplementedException(), initialToken); - var token = userCredential.GetToken(); + var token = tokenCredential.GetToken(); testClock.Tick(TimeSpan.FromMinutes(tokenValidForMinutes - ThreadSafeRefreshableAccessTokenCache.ProactiveRefreshIntervalInMinutes + 0.5)); Assert.AreEqual(1, refreshCallCount); - var afterRefreshToken = userCredential.GetToken(); + var afterRefreshToken = tokenCredential.GetToken(); Assert.AreEqual(inCriticalExpiryWindow ? newToken : initialToken, token.Token); Assert.AreEqual(newToken, afterRefreshToken.Token); @@ -278,13 +278,13 @@ public void CurrentTokenExpired_TokenRefreshFails_Throws() { var expiredToken = GenerateTokenValidForMinutes(DateTimeOffset.UtcNow, -10); - using var userCredential = new CommunicationUserCredential( + using var tokenCredential = new CommunicationTokenCredential( refreshProactively: true, RefreshToken, _ => throw new NotImplementedException(), expiredToken); - Assert.Throws(() => userCredential.GetToken()); + Assert.Throws(() => tokenCredential.GetToken()); string RefreshToken(CancellationToken _) => throw new ArithmeticException("Refresh token failed"); } @@ -295,7 +295,7 @@ public void ProactiveRefreshingEnabled_KeepsSchedulingNewTimers() var testClock = new TestClock(); var twentyMinToken = GenerateTokenValidForMinutes(testClock.UtcNow, 20); - using var userCredential = CreateUserCredentialWithTestClock( + using var tokenCredential = CreateTokenCredentialWithTestClock( testClock, refreshProactively: true, _ => GenerateTokenValidForMinutes(testClock.UtcNow, 20), @@ -321,7 +321,7 @@ public async Task GetTokenSeries_CallsRefreshTokenOnlyOnce(bool async) var testClock = new TestClock(); var twentyMinToken = GenerateTokenValidForMinutes(testClock.UtcNow, 20); - using var userCredential = CreateUserCredentialWithTestClock( + using var tokenCredential = CreateTokenCredentialWithTestClock( testClock, refreshProactively: true, RefreshToken, @@ -334,9 +334,9 @@ public async Task GetTokenSeries_CallsRefreshTokenOnlyOnce(bool async) for (var i = 0; i < 10; i++) { if (async) - await userCredential.GetTokenAsync(); + await tokenCredential.GetTokenAsync(); else - userCredential.GetToken(); + tokenCredential.GetToken(); } Assert.AreEqual(1, refreshCallCount); diff --git a/sdk/communication/Azure.Communication.Common/tests/Pipeline/CommunicationTokenCredentialTests.cs b/sdk/communication/Azure.Communication.Common/tests/Pipeline/CommunicationTokenCredentialTests.cs index d48b319790895..c994172457d34 100644 --- a/sdk/communication/Azure.Communication.Common/tests/Pipeline/CommunicationTokenCredentialTests.cs +++ b/sdk/communication/Azure.Communication.Common/tests/Pipeline/CommunicationTokenCredentialTests.cs @@ -28,46 +28,46 @@ public async Task CommunicationTokenCredential_CreateStaticToken() { var token = ExpiredToken; - using var userCredential = new CommunicationUserCredential(token); - var communicationTokenCredential = new CommunicationTokenCredential(userCredential); + using var tokenCredential = new CommunicationTokenCredential(token); + var communicationBearerTokenCredential = new CommunicationBearerTokenCredential(tokenCredential); - await communicationTokenCredential.GetTokenAsync(MockTokenRequestContext(), CancellationToken.None); + await communicationBearerTokenCredential.GetTokenAsync(MockTokenRequestContext(), CancellationToken.None); } [Test] public async Task CommunicationTokenCredential_CreateRefreshableWithoutInitialToken() { - var userCredential = new CommunicationUserCredential( + var tokenCredential = new CommunicationTokenCredential( refreshProactively: true, // Indicates if the token should be proactively refreshed in the background or only on-demand tokenRefresher: cancellationToken => FetchTokenForUserFromMyServer("bob@contoso.com", cancellationToken), asyncTokenRefresher: cancellationToken => FetchTokenForUserFromMyServerAsync("bob@contoso.com", cancellationToken)); - var communicationTokenCredential = new CommunicationTokenCredential(userCredential); + var communicationBearerTokenCredential = new CommunicationBearerTokenCredential(tokenCredential); - await communicationTokenCredential.GetTokenAsync(MockTokenRequestContext(), CancellationToken.None); + await communicationBearerTokenCredential.GetTokenAsync(MockTokenRequestContext(), CancellationToken.None); } [Test] public async Task CommunicationTokenCredential_CreateRefreshableWithInitialToken() { var initialToken = ExpiredToken; - var userCredential = new CommunicationUserCredential( + var tokenCredential = new CommunicationTokenCredential( refreshProactively: true, // Indicates if the token should be proactively refreshed in the background or only on-demand tokenRefresher: cancellationToken => FetchTokenForUserFromMyServer("bob@contoso.com", cancellationToken), asyncTokenRefresher: cancellationToken => FetchTokenForUserFromMyServerAsync("bob@contoso.com", cancellationToken), initialToken); - var communicationTokenCredential = new CommunicationTokenCredential(userCredential); + var communicationBearerTokenCredential = new CommunicationBearerTokenCredential(tokenCredential); - await communicationTokenCredential.GetTokenAsync(MockTokenRequestContext(), CancellationToken.None); + await communicationBearerTokenCredential.GetTokenAsync(MockTokenRequestContext(), CancellationToken.None); } [Test] public async Task CommunicationTokenCredential_DecodesToken() { var initialToken = SampleToken; - var userCredential = new CommunicationUserCredential(initialToken); - var communicationTokenCredential = new CommunicationTokenCredential(userCredential); + var tokenCredential = new CommunicationTokenCredential(initialToken); + var communicationBearerTokenCredential = new CommunicationBearerTokenCredential(tokenCredential); - var accessToken = await communicationTokenCredential.GetTokenAsync(MockTokenRequestContext(), CancellationToken.None); + var accessToken = await communicationBearerTokenCredential.GetTokenAsync(MockTokenRequestContext(), CancellationToken.None); Assert.AreEqual(initialToken, accessToken.Token); Assert.AreEqual(SampleTokenExpiry, accessToken.ExpiresOn.ToUnixTimeSeconds()); @@ -76,20 +76,20 @@ public async Task CommunicationTokenCredential_DecodesToken() [Test] public void CommunicationTokenCredential_StaticTokenReturnsExpiredToken() { - var userCredential = new CommunicationUserCredential(ExpiredToken); - var communicationTokenCredential = new CommunicationTokenCredential(userCredential); + var tokenCredential = new CommunicationTokenCredential(ExpiredToken); + var communicationBearerTokenCredential = new CommunicationBearerTokenCredential(tokenCredential); - var accessToken = communicationTokenCredential.GetToken(MockTokenRequestContext(), CancellationToken.None); + var accessToken = communicationBearerTokenCredential.GetToken(MockTokenRequestContext(), CancellationToken.None); Assert.AreEqual(ExpiredToken, accessToken.Token); } [Test] public async Task CommunicationTokenCredential_StaticTokenAsyncReturnsExpiredToken() { - var userCredential = new CommunicationUserCredential(ExpiredToken); - var communicationTokenCredential = new CommunicationTokenCredential(userCredential); + var tokenCredential = new CommunicationTokenCredential(ExpiredToken); + var communicationBearerTokenCredential = new CommunicationBearerTokenCredential(tokenCredential); - var accessToken = await communicationTokenCredential.GetTokenAsync(MockTokenRequestContext(), CancellationToken.None); + var accessToken = await communicationBearerTokenCredential.GetTokenAsync(MockTokenRequestContext(), CancellationToken.None); Assert.AreEqual(ExpiredToken, accessToken.Token); } @@ -101,14 +101,14 @@ public void CommunicationTokenCredential_PassesCancelToken(bool refreshProactive var cancellationToken = new CancellationToken(); CancellationToken? actualCancellationToken = null; - var userCredential = new CommunicationUserCredential( + var tokenCredential = new CommunicationTokenCredential( refreshProactively, RefreshToken, c => new ValueTask(RefreshToken(c)), ExpiredToken); - var communicationTokenCredential = new CommunicationTokenCredential(userCredential); - var accessToken = communicationTokenCredential.GetToken(MockTokenRequestContext(), cancellationToken); + var communicationBearerTokenCredential = new CommunicationBearerTokenCredential(tokenCredential); + var accessToken = communicationBearerTokenCredential.GetToken(MockTokenRequestContext(), cancellationToken); Assert.AreEqual(cancellationToken.GetHashCode(), actualCancellationToken.GetHashCode()); string RefreshToken(CancellationToken token) @@ -126,14 +126,14 @@ public async Task CommunicationTokenCredential_PassesAsyncCancelToken(bool refre var cancellationToken = new CancellationToken(); CancellationToken? actualCancellationToken = null; - var userCredential = new CommunicationUserCredential( + var tokenCredential = new CommunicationTokenCredential( refreshProactively, RefreshToken, c => new ValueTask(RefreshToken(c)), ExpiredToken); - var communicationTokenCredential = new CommunicationTokenCredential(userCredential); - var accessToken = await communicationTokenCredential.GetTokenAsync(MockTokenRequestContext(), cancellationToken); + var communicationBearerTokenCredential = new CommunicationBearerTokenCredential(tokenCredential); + var accessToken = await communicationBearerTokenCredential.GetTokenAsync(MockTokenRequestContext(), cancellationToken); Assert.AreEqual(cancellationToken.GetHashCode(), actualCancellationToken.GetHashCode()); string RefreshToken(CancellationToken token) diff --git a/sdk/communication/Shared/src/CommunicationTokenCredential.cs b/sdk/communication/Shared/src/CommunicationBearerTokenCredential.cs similarity index 70% rename from sdk/communication/Shared/src/CommunicationTokenCredential.cs rename to sdk/communication/Shared/src/CommunicationBearerTokenCredential.cs index c93034fdb9793..f4fc608cf9554 100644 --- a/sdk/communication/Shared/src/CommunicationTokenCredential.cs +++ b/sdk/communication/Shared/src/CommunicationBearerTokenCredential.cs @@ -8,11 +8,11 @@ namespace Azure.Communication.Pipeline { - internal class CommunicationTokenCredential : TokenCredential + internal class CommunicationBearerTokenCredential : TokenCredential { - private readonly CommunicationUserCredential _credential; + private readonly CommunicationTokenCredential _credential; - public CommunicationTokenCredential(CommunicationUserCredential credential) => _credential = credential; + public CommunicationBearerTokenCredential(CommunicationTokenCredential credential) => _credential = credential; public override AccessToken GetToken(TokenRequestContext requestContext, CancellationToken cancellationToken) => _credential.GetToken(cancellationToken); public override ValueTask GetTokenAsync(TokenRequestContext requestContext, CancellationToken cancellationToken) => _credential.GetTokenAsync(cancellationToken); From df47bd580488414060c8bb435b2036eb52150376 Mon Sep 17 00:00:00 2001 From: Minnie Liu Date: Tue, 15 Dec 2020 12:33:18 -0800 Subject: [PATCH 02/12] Update changelog --- sdk/communication/Azure.Communication.Common/CHANGELOG.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/sdk/communication/Azure.Communication.Common/CHANGELOG.md b/sdk/communication/Azure.Communication.Common/CHANGELOG.md index 8af1478bd606b..7c66ca8e74464 100644 --- a/sdk/communication/Azure.Communication.Common/CHANGELOG.md +++ b/sdk/communication/Azure.Communication.Common/CHANGELOG.md @@ -1,7 +1,8 @@ # Release History ## 1.0.0-beta.4 (Unreleased) - +### Breaking Changes +- Renamed `CommunicationUserCredential` to `CommunicationTokenCredential` ## 1.0.0-beta.3 (2020-11-16) Updated `Azure.Communication.Common` version. From dfed2b09e5ee2983a1d3b0dc7c66920fbed1890c Mon Sep 17 00:00:00 2001 From: Minnie Liu Date: Tue, 15 Dec 2020 13:53:03 -0800 Subject: [PATCH 03/12] Update readme --- sdk/communication/Azure.Communication.Common/CHANGELOG.md | 2 +- sdk/communication/Azure.Communication.Common/README.md | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/sdk/communication/Azure.Communication.Common/CHANGELOG.md b/sdk/communication/Azure.Communication.Common/CHANGELOG.md index 7c66ca8e74464..1d9d2f20cc8b2 100644 --- a/sdk/communication/Azure.Communication.Common/CHANGELOG.md +++ b/sdk/communication/Azure.Communication.Common/CHANGELOG.md @@ -2,7 +2,7 @@ ## 1.0.0-beta.4 (Unreleased) ### Breaking Changes -- Renamed `CommunicationUserCredential` to `CommunicationTokenCredential` +- Renamed `CommunicationUserCredential` to `CommunicationTokenCredential`. ## 1.0.0-beta.3 (2020-11-16) Updated `Azure.Communication.Common` version. diff --git a/sdk/communication/Azure.Communication.Common/README.md b/sdk/communication/Azure.Communication.Common/README.md index efd23ac05449b..8a5056f208ab3 100644 --- a/sdk/communication/Azure.Communication.Common/README.md +++ b/sdk/communication/Azure.Communication.Common/README.md @@ -56,7 +56,7 @@ Optionally, you can enable proactive token refreshing where a fresh token will b previous token approaches expiry. Using this method, your requests are less likely to be blocked to acquire a fresh token: ```C# Snippet:CommunicationTokenCredential_CreateRefreshableWithoutInitialToken -using var userCredential = new CommunicationTokenCredential( +using var tokenCredential = new CommunicationTokenCredential( refreshProactively: true, // Indicates if the token should be proactively refreshed in the background or only on-demand tokenRefresher: cancellationToken => FetchTokenForUserFromMyServer("bob@contoso.com", cancellationToken), asyncTokenRefresher: cancellationToken => FetchTokenForUserFromMyServerAsync("bob@contoso.com", cancellationToken)); @@ -66,7 +66,7 @@ If you already have a token, you can optimize the token refreshing even further ```C# Snippet:CommunicationTokenCredential_CreateRefreshableWithInitialToken string initialToken = Environment.GetEnvironmentVariable("COMMUNICATION_SERVICES_USER_TOKEN"); -using var userCredential = new CommunicationTokenCredential( +using var tokenCredential = new CommunicationTokenCredential( refreshProactively: true, // Indicates if the token should be proactively refreshed in the background or only on-demand tokenRefresher: cancellationToken => FetchTokenForUserFromMyServer("bob@contoso.com", cancellationToken), asyncTokenRefresher: cancellationToken => FetchTokenForUserFromMyServerAsync("bob@contoso.com", cancellationToken), From b12a209eeb1065e31893a16ed98f6a5fe76b2eb4 Mon Sep 17 00:00:00 2001 From: Minnie Liu Date: Tue, 15 Dec 2020 14:22:00 -0800 Subject: [PATCH 04/12] Update readme --- .../api/Azure.Communication.Chat.netstandard2.0.cs | 2 +- .../api/Azure.Communication.Common.netstandard2.0.cs | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/sdk/communication/Azure.Communication.Chat/api/Azure.Communication.Chat.netstandard2.0.cs b/sdk/communication/Azure.Communication.Chat/api/Azure.Communication.Chat.netstandard2.0.cs index a4d4bac2c53ad..e4e82be8af3fe 100644 --- a/sdk/communication/Azure.Communication.Chat/api/Azure.Communication.Chat.netstandard2.0.cs +++ b/sdk/communication/Azure.Communication.Chat/api/Azure.Communication.Chat.netstandard2.0.cs @@ -3,7 +3,7 @@ namespace Azure.Communication.Chat public partial class ChatClient { protected ChatClient() { } - public ChatClient(System.Uri endpointUrl, Azure.Communication.Identity.CommunicationUserCredential communicationUserCredential, Azure.Communication.Chat.ChatClientOptions? options = null) { } + public ChatClient(System.Uri endpointUrl, Azure.Communication.Identity.CommunicationTokenCredential communicationTokenCredential, Azure.Communication.Chat.ChatClientOptions? options = null) { } public virtual Azure.Communication.Chat.ChatThreadClient CreateChatThread(string topic, System.Collections.Generic.IEnumerable members, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; } public virtual System.Threading.Tasks.Task CreateChatThreadAsync(string topic, System.Collections.Generic.IEnumerable members, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; } public virtual Azure.Response DeleteChatThread(string threadId, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; } 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 352ed2fc2ab3f..353354f5c5a69 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 @@ -28,10 +28,10 @@ public UnknownIdentifier(string id) { } } namespace Azure.Communication.Identity { - public sealed partial class CommunicationUserCredential : System.IDisposable + public sealed partial class CommunicationTokenCredential : System.IDisposable { - public CommunicationUserCredential(bool refreshProactively, System.Func tokenRefresher, System.Func>? asyncTokenRefresher = null, string? initialToken = null) { } - public CommunicationUserCredential(string userToken) { } + public CommunicationTokenCredential(bool refreshProactively, System.Func tokenRefresher, System.Func>? asyncTokenRefresher = null, string? initialToken = null) { } + public CommunicationTokenCredential(string userToken) { } public void Dispose() { } public Azure.Core.AccessToken GetToken(System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; } public System.Threading.Tasks.ValueTask GetTokenAsync(System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; } From b3f754fe9fba6917622cec0130d1f80e3b4bfd33 Mon Sep 17 00:00:00 2001 From: Minnie Liu Date: Tue, 15 Dec 2020 16:10:07 -0800 Subject: [PATCH 05/12] Encapsulate CommunicationUserCredential params into TokenRefreshOptions --- .../Azure.Communication.Common/CHANGELOG.md | 2 + .../Azure.Communication.Common/README.md | 19 ++++--- ...ure.Communication.Common.netstandard2.0.cs | 6 ++- .../Identity/CommunicationTokenCredential.cs | 24 ++++----- .../CommunicationTokenRefreshOptions.cs | 40 +++++++++++++++ .../CommunicationTokenCredentialTest.cs | 49 ++++++++++++------- .../CommunicationTokenCredentialTests.cs | 38 ++++++++------ 7 files changed, 122 insertions(+), 56 deletions(-) create mode 100644 sdk/communication/Azure.Communication.Common/src/Identity/CommunicationTokenRefreshOptions.cs diff --git a/sdk/communication/Azure.Communication.Common/CHANGELOG.md b/sdk/communication/Azure.Communication.Common/CHANGELOG.md index 1d9d2f20cc8b2..7ed38d57369f4 100644 --- a/sdk/communication/Azure.Communication.Common/CHANGELOG.md +++ b/sdk/communication/Azure.Communication.Common/CHANGELOG.md @@ -3,6 +3,8 @@ ## 1.0.0-beta.4 (Unreleased) ### Breaking Changes - Renamed `CommunicationUserCredential` to `CommunicationTokenCredential`. +- Replace `CommunicationTokenCredential(bool refreshProactively, Func tokenRefresher,Func>? asyncTokenRefresher = null, string? initialToken = null)` +- with `CommunicationTokenCredential(CommunicationTokenRefreshOptions tokenRefreshOptions)`. ## 1.0.0-beta.3 (2020-11-16) Updated `Azure.Communication.Common` version. diff --git a/sdk/communication/Azure.Communication.Common/README.md b/sdk/communication/Azure.Communication.Common/README.md index 8a5056f208ab3..86e928c368aef 100644 --- a/sdk/communication/Azure.Communication.Common/README.md +++ b/sdk/communication/Azure.Communication.Common/README.md @@ -57,9 +57,12 @@ previous token approaches expiry. Using this method, your requests are less like ```C# Snippet:CommunicationTokenCredential_CreateRefreshableWithoutInitialToken using var tokenCredential = new CommunicationTokenCredential( - refreshProactively: true, // Indicates if the token should be proactively refreshed in the background or only on-demand - tokenRefresher: cancellationToken => FetchTokenForUserFromMyServer("bob@contoso.com", cancellationToken), - asyncTokenRefresher: cancellationToken => FetchTokenForUserFromMyServerAsync("bob@contoso.com", cancellationToken)); + new CommunicationTokenRefreshOptions( + refreshProactively: true, // Indicates if the token should be proactively refreshed in the background or only on-demand + tokenRefresher: cancellationToken => FetchTokenForUserFromMyServer("bob@contoso.com", cancellationToken), + asyncTokenRefresher: cancellationToken => FetchTokenForUserFromMyServerAsync("bob@contoso.com", cancellationToken) + ) + ); ``` If you already have a token, you can optimize the token refreshing even further by passing that initial token: @@ -67,10 +70,12 @@ If you already have a token, you can optimize the token refreshing even further ```C# Snippet:CommunicationTokenCredential_CreateRefreshableWithInitialToken string initialToken = Environment.GetEnvironmentVariable("COMMUNICATION_SERVICES_USER_TOKEN"); using var tokenCredential = new CommunicationTokenCredential( - refreshProactively: true, // Indicates if the token should be proactively refreshed in the background or only on-demand - tokenRefresher: cancellationToken => FetchTokenForUserFromMyServer("bob@contoso.com", cancellationToken), - asyncTokenRefresher: cancellationToken => FetchTokenForUserFromMyServerAsync("bob@contoso.com", cancellationToken), - initialToken); + new CommunicationTokenRefreshOptions( + refreshProactively: true, // Indicates if the token should be proactively refreshed in the background or only on-demand + tokenRefresher: cancellationToken => FetchTokenForUserFromMyServer("bob@contoso.com", cancellationToken), + asyncTokenRefresher: cancellationToken => FetchTokenForUserFromMyServerAsync("bob@contoso.com", cancellationToken), + initialToken) + ); ``` ## Troubleshooting 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 353354f5c5a69..2c7e33e9a3c46 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 @@ -30,10 +30,14 @@ namespace Azure.Communication.Identity { public sealed partial class CommunicationTokenCredential : System.IDisposable { - public CommunicationTokenCredential(bool refreshProactively, System.Func tokenRefresher, System.Func>? asyncTokenRefresher = null, string? initialToken = null) { } + public CommunicationTokenCredential(Azure.Communication.Identity.CommunicationTokenRefreshOptions tokenRefreshOptions) { } public CommunicationTokenCredential(string userToken) { } public void Dispose() { } public Azure.Core.AccessToken GetToken(System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; } public System.Threading.Tasks.ValueTask GetTokenAsync(System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; } } + public partial class CommunicationTokenRefreshOptions + { + public CommunicationTokenRefreshOptions(bool refreshProactively, System.Func tokenRefresher, System.Func>? asyncTokenRefresher, string? token = null) { } + } } diff --git a/sdk/communication/Azure.Communication.Common/src/Identity/CommunicationTokenCredential.cs b/sdk/communication/Azure.Communication.Common/src/Identity/CommunicationTokenCredential.cs index e91cd2a8624f1..922d5bbc29231 100644 --- a/sdk/communication/Azure.Communication.Common/src/Identity/CommunicationTokenCredential.cs +++ b/sdk/communication/Azure.Communication.Common/src/Identity/CommunicationTokenCredential.cs @@ -26,20 +26,16 @@ public CommunicationTokenCredential(string userToken) /// /// Initializes a new instance of that automatically renews the token upon expiry or proactively prior to expiration to speed up the requests. /// - /// 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 to initialize. - public CommunicationTokenCredential( - bool refreshProactively, - Func tokenRefresher, - Func>? asyncTokenRefresher = null, - string? initialToken = null) - => _tokenCredential = new AutoRefreshTokenCredential( - tokenRefresher, - asyncTokenRefresher ?? (cancellationToken => new ValueTask(tokenRefresher(cancellationToken))), - initialToken, - refreshProactively); + /// Options for how the token will be refreshed + public CommunicationTokenCredential(CommunicationTokenRefreshOptions tokenRefreshOptions) + { + Argument.AssertNotNull(tokenRefreshOptions, nameof(tokenRefreshOptions)); + _tokenCredential = new AutoRefreshTokenCredential( + tokenRefreshOptions.TokenRefresher, + tokenRefreshOptions.AsyncTokenRefresher ?? (cancellationToken => new ValueTask(tokenRefreshOptions.TokenRefresher(cancellationToken))), + tokenRefreshOptions.Token, + tokenRefreshOptions.RefreshProactively); + } /// /// Gets an for the user. diff --git a/sdk/communication/Azure.Communication.Common/src/Identity/CommunicationTokenRefreshOptions.cs b/sdk/communication/Azure.Communication.Common/src/Identity/CommunicationTokenRefreshOptions.cs new file mode 100644 index 0000000000000..6eb09b764d967 --- /dev/null +++ b/sdk/communication/Azure.Communication.Common/src/Identity/CommunicationTokenRefreshOptions.cs @@ -0,0 +1,40 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +using System; +using System.Threading; +using System.Threading.Tasks; +using Azure.Core; + +namespace Azure.Communication.Identity +{ + /// + /// The Communication Token Refresh Options + /// + public class CommunicationTokenRefreshOptions + { + internal bool RefreshProactively { get; } + internal Func TokenRefresher { get; } + internal Func>? AsyncTokenRefresher { get; } + internal string? UserToken { 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. + public CommunicationTokenRefreshOptions( + bool refreshProactively, + Func tokenRefresher, + Func>? asyncTokenRefresher, + string? userToken = null) + { + RefreshProactively = refreshProactively; + TokenRefresher = tokenRefresher; + AsyncTokenRefresher = asyncTokenRefresher; + UserToken = userToken; + } + } +} diff --git a/sdk/communication/Azure.Communication.Common/tests/Identity/CommunicationTokenCredentialTest.cs b/sdk/communication/Azure.Communication.Common/tests/Identity/CommunicationTokenCredentialTest.cs index 1b19dc8e0d641..1cfca2537d32a 100644 --- a/sdk/communication/Azure.Communication.Common/tests/Identity/CommunicationTokenCredentialTest.cs +++ b/sdk/communication/Azure.Communication.Common/tests/Identity/CommunicationTokenCredentialTest.cs @@ -49,9 +49,12 @@ public async Task CommunicationTokenCredential_CreateRefreshableWithoutInitialTo { #region Snippet:CommunicationTokenCredential_CreateRefreshableWithoutInitialToken using var tokenCredential = new CommunicationTokenCredential( - refreshProactively: true, // Indicates if the token should be proactively refreshed in the background or only on-demand - tokenRefresher: cancellationToken => FetchTokenForUserFromMyServer("bob@contoso.com", cancellationToken), - asyncTokenRefresher: cancellationToken => FetchTokenForUserFromMyServerAsync("bob@contoso.com", cancellationToken)); + new CommunicationTokenRefreshOptions( + refreshProactively: true, // Indicates if the token should be proactively refreshed in the background or only on-demand + tokenRefresher: cancellationToken => FetchTokenForUserFromMyServer("bob@contoso.com", cancellationToken), + asyncTokenRefresher: cancellationToken => FetchTokenForUserFromMyServerAsync("bob@contoso.com", cancellationToken) + ) + ); #endregion await tokenCredential.GetTokenAsync(); } @@ -63,10 +66,12 @@ public async Task CommunicationTokenCredential_CreateRefreshableWithInitialToken #region Snippet:CommunicationTokenCredential_CreateRefreshableWithInitialToken //@@string initialToken = Environment.GetEnvironmentVariable("COMMUNICATION_SERVICES_USER_TOKEN"); using var tokenCredential = new CommunicationTokenCredential( - refreshProactively: true, // Indicates if the token should be proactively refreshed in the background or only on-demand - tokenRefresher: cancellationToken => FetchTokenForUserFromMyServer("bob@contoso.com", cancellationToken), - asyncTokenRefresher: cancellationToken => FetchTokenForUserFromMyServerAsync("bob@contoso.com", cancellationToken), - initialToken); + new CommunicationTokenRefreshOptions( + refreshProactively: true, // Indicates if the token should be proactively refreshed in the background or only on-demand + tokenRefresher: cancellationToken => FetchTokenForUserFromMyServer("bob@contoso.com", cancellationToken), + asyncTokenRefresher: cancellationToken => FetchTokenForUserFromMyServerAsync("bob@contoso.com", cancellationToken), + initialToken) + ); #endregion await tokenCredential.GetTokenAsync(); } @@ -97,7 +102,7 @@ public void CommunicationTokenCredential_ThrowsIfInvalidToken(string token) [Test] public void CommunicationTokenCredential_ThrowsIfTokenIsNull() - => Assert.Throws(() => new CommunicationTokenCredential(null!)); + => Assert.Throws(() => new CommunicationTokenCredential(userToken: null!)); [Test] [TestCase(false)] @@ -121,10 +126,12 @@ public async Task CommunicationTokenCredential_PassesCancellationToken(bool refr CancellationToken? actualCancellationToken = null; using var tokenCredential = new CommunicationTokenCredential( - refreshProactively, - RefreshToken, - c => new ValueTask(RefreshToken(c)), - ExpiredToken); + new CommunicationTokenRefreshOptions( + refreshProactively, + RefreshToken, + c => new ValueTask(RefreshToken(c)), + ExpiredToken) + ); var token = async ? await tokenCredential.GetTokenAsync(cancellationToken) : tokenCredential.GetToken(cancellationToken); Assert.AreEqual(cancellationToken, actualCancellationToken); @@ -191,9 +198,11 @@ public async Task GetTokenSeries_RefreshTokenOnDemandIfNeeded(string token, long public void GetTokenSeries_Throws_IfTokenRequestedWhileDisposed(string token, bool refreshProactively, bool async) { using var tokenCredential = new CommunicationTokenCredential( - refreshProactively, - _ => token, - _ => new ValueTask(token)); + new CommunicationTokenRefreshOptions( + refreshProactively, + _ => token, + _ => new ValueTask(token)) + ); tokenCredential.Dispose(); @@ -279,10 +288,12 @@ public void CurrentTokenExpired_TokenRefreshFails_Throws() var expiredToken = GenerateTokenValidForMinutes(DateTimeOffset.UtcNow, -10); using var tokenCredential = new CommunicationTokenCredential( - refreshProactively: true, - RefreshToken, - _ => throw new NotImplementedException(), - expiredToken); + new CommunicationTokenRefreshOptions( + refreshProactively: true, + RefreshToken, + _ => throw new NotImplementedException(), + expiredToken) + ); Assert.Throws(() => tokenCredential.GetToken()); diff --git a/sdk/communication/Azure.Communication.Common/tests/Pipeline/CommunicationTokenCredentialTests.cs b/sdk/communication/Azure.Communication.Common/tests/Pipeline/CommunicationTokenCredentialTests.cs index c994172457d34..f513bf462167d 100644 --- a/sdk/communication/Azure.Communication.Common/tests/Pipeline/CommunicationTokenCredentialTests.cs +++ b/sdk/communication/Azure.Communication.Common/tests/Pipeline/CommunicationTokenCredentialTests.cs @@ -38,9 +38,11 @@ public async Task CommunicationTokenCredential_CreateStaticToken() public async Task CommunicationTokenCredential_CreateRefreshableWithoutInitialToken() { var tokenCredential = new CommunicationTokenCredential( - refreshProactively: true, // Indicates if the token should be proactively refreshed in the background or only on-demand - tokenRefresher: cancellationToken => FetchTokenForUserFromMyServer("bob@contoso.com", cancellationToken), - asyncTokenRefresher: cancellationToken => FetchTokenForUserFromMyServerAsync("bob@contoso.com", cancellationToken)); + new CommunicationTokenRefreshOptions( + refreshProactively: true, // Indicates if the token should be proactively refreshed in the background or only on-demand + tokenRefresher: cancellationToken => FetchTokenForUserFromMyServer("bob@contoso.com", cancellationToken), + asyncTokenRefresher: cancellationToken => FetchTokenForUserFromMyServerAsync("bob@contoso.com", cancellationToken)) + ); var communicationBearerTokenCredential = new CommunicationBearerTokenCredential(tokenCredential); await communicationBearerTokenCredential.GetTokenAsync(MockTokenRequestContext(), CancellationToken.None); @@ -51,10 +53,12 @@ public async Task CommunicationTokenCredential_CreateRefreshableWithInitialToken { var initialToken = ExpiredToken; var tokenCredential = new CommunicationTokenCredential( - refreshProactively: true, // Indicates if the token should be proactively refreshed in the background or only on-demand - tokenRefresher: cancellationToken => FetchTokenForUserFromMyServer("bob@contoso.com", cancellationToken), - asyncTokenRefresher: cancellationToken => FetchTokenForUserFromMyServerAsync("bob@contoso.com", cancellationToken), - initialToken); + new CommunicationTokenRefreshOptions( + refreshProactively: true, // Indicates if the token should be proactively refreshed in the background or only on-demand + tokenRefresher: cancellationToken => FetchTokenForUserFromMyServer("bob@contoso.com", cancellationToken), + asyncTokenRefresher: cancellationToken => FetchTokenForUserFromMyServerAsync("bob@contoso.com", cancellationToken), + initialToken) + ); var communicationBearerTokenCredential = new CommunicationBearerTokenCredential(tokenCredential); await communicationBearerTokenCredential.GetTokenAsync(MockTokenRequestContext(), CancellationToken.None); @@ -102,10 +106,12 @@ public void CommunicationTokenCredential_PassesCancelToken(bool refreshProactive CancellationToken? actualCancellationToken = null; var tokenCredential = new CommunicationTokenCredential( - refreshProactively, - RefreshToken, - c => new ValueTask(RefreshToken(c)), - ExpiredToken); + new CommunicationTokenRefreshOptions( + refreshProactively, + RefreshToken, + c => new ValueTask(RefreshToken(c)), + ExpiredToken) + ); var communicationBearerTokenCredential = new CommunicationBearerTokenCredential(tokenCredential); var accessToken = communicationBearerTokenCredential.GetToken(MockTokenRequestContext(), cancellationToken); @@ -127,10 +133,12 @@ public async Task CommunicationTokenCredential_PassesAsyncCancelToken(bool refre CancellationToken? actualCancellationToken = null; var tokenCredential = new CommunicationTokenCredential( - refreshProactively, - RefreshToken, - c => new ValueTask(RefreshToken(c)), - ExpiredToken); + new CommunicationTokenRefreshOptions( + refreshProactively, + RefreshToken, + c => new ValueTask(RefreshToken(c)), + ExpiredToken) + ); var communicationBearerTokenCredential = new CommunicationBearerTokenCredential(tokenCredential); var accessToken = await communicationBearerTokenCredential.GetTokenAsync(MockTokenRequestContext(), cancellationToken); From fc2eae6c8f10f9614a8245f21bb17ec24c0ad92d Mon Sep 17 00:00:00 2001 From: Minnie Liu Date: Tue, 15 Dec 2020 16:21:09 -0800 Subject: [PATCH 06/12] Renaming bearer test file --- ...CommunicationBearerTokenCredentialTests.cs} | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) rename sdk/communication/Azure.Communication.Common/tests/Pipeline/{CommunicationTokenCredentialTests.cs => CommunicationBearerTokenCredentialTests.cs} (88%) diff --git a/sdk/communication/Azure.Communication.Common/tests/Pipeline/CommunicationTokenCredentialTests.cs b/sdk/communication/Azure.Communication.Common/tests/Pipeline/CommunicationBearerTokenCredentialTests.cs similarity index 88% rename from sdk/communication/Azure.Communication.Common/tests/Pipeline/CommunicationTokenCredentialTests.cs rename to sdk/communication/Azure.Communication.Common/tests/Pipeline/CommunicationBearerTokenCredentialTests.cs index c994172457d34..0fba69e74580e 100644 --- a/sdk/communication/Azure.Communication.Common/tests/Pipeline/CommunicationTokenCredentialTests.cs +++ b/sdk/communication/Azure.Communication.Common/tests/Pipeline/CommunicationBearerTokenCredentialTests.cs @@ -9,7 +9,7 @@ namespace Azure.Communication.Pipeline { - public class CommunicationTokenCredentialTests + public class CommunicationBearerTokenCredentialTests { private const string SampleToken = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjMyNTAzNjgwMDAwfQ.9i7FNNHHJT8cOzo-yrAUJyBSfJ-tPPk2emcHavOEpWc"; private const long SampleTokenExpiry = 32503680000; @@ -24,7 +24,7 @@ public TokenRequestContext MockTokenRequestContext() } [Test] - public async Task CommunicationTokenCredential_CreateStaticToken() + public async Task CommunicationBearerTokenCredential_CreateStaticToken() { var token = ExpiredToken; @@ -35,7 +35,7 @@ public async Task CommunicationTokenCredential_CreateStaticToken() } [Test] - public async Task CommunicationTokenCredential_CreateRefreshableWithoutInitialToken() + public async Task CommunicationBearerTokenCredential_CreateRefreshableWithoutInitialToken() { var tokenCredential = new CommunicationTokenCredential( refreshProactively: true, // Indicates if the token should be proactively refreshed in the background or only on-demand @@ -47,7 +47,7 @@ public async Task CommunicationTokenCredential_CreateRefreshableWithoutInitialTo } [Test] - public async Task CommunicationTokenCredential_CreateRefreshableWithInitialToken() + public async Task CommunicationBearerTokenCredential_CreateRefreshableWithInitialToken() { var initialToken = ExpiredToken; var tokenCredential = new CommunicationTokenCredential( @@ -61,7 +61,7 @@ public async Task CommunicationTokenCredential_CreateRefreshableWithInitialToken } [Test] - public async Task CommunicationTokenCredential_DecodesToken() + public async Task CommunicationBearerTokenCredential_DecodesToken() { var initialToken = SampleToken; var tokenCredential = new CommunicationTokenCredential(initialToken); @@ -74,7 +74,7 @@ public async Task CommunicationTokenCredential_DecodesToken() } [Test] - public void CommunicationTokenCredential_StaticTokenReturnsExpiredToken() + public void CommunicationBearerTokenCredential_StaticTokenReturnsExpiredToken() { var tokenCredential = new CommunicationTokenCredential(ExpiredToken); var communicationBearerTokenCredential = new CommunicationBearerTokenCredential(tokenCredential); @@ -84,7 +84,7 @@ public void CommunicationTokenCredential_StaticTokenReturnsExpiredToken() } [Test] - public async Task CommunicationTokenCredential_StaticTokenAsyncReturnsExpiredToken() + public async Task CommunicationBearerTokenCredential_StaticTokenAsyncReturnsExpiredToken() { var tokenCredential = new CommunicationTokenCredential(ExpiredToken); var communicationBearerTokenCredential = new CommunicationBearerTokenCredential(tokenCredential); @@ -96,7 +96,7 @@ public async Task CommunicationTokenCredential_StaticTokenAsyncReturnsExpiredTok [Test] [TestCase(true)] [TestCase(false)] - public void CommunicationTokenCredential_PassesCancelToken(bool refreshProactively) + public void CommunicationBearerTokenCredential_PassesCancelToken(bool refreshProactively) { var cancellationToken = new CancellationToken(); CancellationToken? actualCancellationToken = null; @@ -121,7 +121,7 @@ string RefreshToken(CancellationToken token) [Test] [TestCase(true)] [TestCase(false)] - public async Task CommunicationTokenCredential_PassesAsyncCancelToken(bool refreshProactively) + public async Task CommunicationBearerTokenCredential_PassesAsyncCancelToken(bool refreshProactively) { var cancellationToken = new CancellationToken(); CancellationToken? actualCancellationToken = null; From 153122f4cc1fabe7c83ea9bc39694f660f1b3333 Mon Sep 17 00:00:00 2001 From: Minnie Liu Date: Wed, 16 Dec 2020 12:07:22 -0800 Subject: [PATCH 07/12] Address previous PR's comments --- .../src/Identity/AutoRefreshTokenCredential.cs | 2 +- .../src/Identity/CommunicationTokenCredential.cs | 5 +++-- ...{ITokenCredential.cs => ICommunicationTokenCredential.cs} | 2 +- .../src/Identity/StaticTokenCredential.cs | 2 +- 4 files changed, 6 insertions(+), 5 deletions(-) rename sdk/communication/Azure.Communication.Common/src/Identity/{ITokenCredential.cs => ICommunicationTokenCredential.cs} (85%) diff --git a/sdk/communication/Azure.Communication.Common/src/Identity/AutoRefreshTokenCredential.cs b/sdk/communication/Azure.Communication.Common/src/Identity/AutoRefreshTokenCredential.cs index 777f389723c4f..cdf7da858ac0a 100644 --- a/sdk/communication/Azure.Communication.Common/src/Identity/AutoRefreshTokenCredential.cs +++ b/sdk/communication/Azure.Communication.Common/src/Identity/AutoRefreshTokenCredential.cs @@ -8,7 +8,7 @@ namespace Azure.Communication.Identity { - internal sealed class AutoRefreshTokenCredential : ITokenCredential + internal sealed class AutoRefreshTokenCredential : ICommunicationTokenCredential { private readonly ThreadSafeRefreshableAccessTokenCache _accessTokenCache; diff --git a/sdk/communication/Azure.Communication.Common/src/Identity/CommunicationTokenCredential.cs b/sdk/communication/Azure.Communication.Common/src/Identity/CommunicationTokenCredential.cs index e53c9bbce719f..ee0d927c00f25 100644 --- a/sdk/communication/Azure.Communication.Common/src/Identity/CommunicationTokenCredential.cs +++ b/sdk/communication/Azure.Communication.Common/src/Identity/CommunicationTokenCredential.cs @@ -4,16 +4,17 @@ using System; using System.Threading; using System.Threading.Tasks; +using Azure.Communication.Identity; using Azure.Core; -namespace Azure.Communication.Identity +namespace Azure.Communication { /// /// The Azure Communication Services Token Credential. /// public sealed class CommunicationTokenCredential : IDisposable { - private readonly ITokenCredential _tokenCredential; + private readonly ICommunicationTokenCredential _tokenCredential; private bool _isDisposed; /// diff --git a/sdk/communication/Azure.Communication.Common/src/Identity/ITokenCredential.cs b/sdk/communication/Azure.Communication.Common/src/Identity/ICommunicationTokenCredential.cs similarity index 85% rename from sdk/communication/Azure.Communication.Common/src/Identity/ITokenCredential.cs rename to sdk/communication/Azure.Communication.Common/src/Identity/ICommunicationTokenCredential.cs index 1622b66cef9fd..9a87867c494e1 100644 --- a/sdk/communication/Azure.Communication.Common/src/Identity/ITokenCredential.cs +++ b/sdk/communication/Azure.Communication.Common/src/Identity/ICommunicationTokenCredential.cs @@ -8,7 +8,7 @@ namespace Azure.Communication.Identity { - internal interface ITokenCredential : IDisposable + internal interface ICommunicationTokenCredential : IDisposable { AccessToken GetToken(CancellationToken cancellationToken); ValueTask GetTokenAsync(CancellationToken cancellationToken); diff --git a/sdk/communication/Azure.Communication.Common/src/Identity/StaticTokenCredential.cs b/sdk/communication/Azure.Communication.Common/src/Identity/StaticTokenCredential.cs index 1f807e597e629..3370611ede6c3 100644 --- a/sdk/communication/Azure.Communication.Common/src/Identity/StaticTokenCredential.cs +++ b/sdk/communication/Azure.Communication.Common/src/Identity/StaticTokenCredential.cs @@ -7,7 +7,7 @@ namespace Azure.Communication.Identity { - internal sealed class StaticTokenCredential : ITokenCredential + internal sealed class StaticTokenCredential : ICommunicationTokenCredential { private readonly AccessToken _accessToken; From dc49f2bf9158723dd5a482298d6f520d8bbbaf53 Mon Sep 17 00:00:00 2001 From: Minnie Liu Date: Wed, 16 Dec 2020 12:17:15 -0800 Subject: [PATCH 08/12] Update generated files --- ...Azure.Communication.Chat.netstandard2.0.cs | 2 +- ...ure.Communication.Common.netstandard2.0.cs | 22 ++++++++----------- 2 files changed, 10 insertions(+), 14 deletions(-) diff --git a/sdk/communication/Azure.Communication.Chat/api/Azure.Communication.Chat.netstandard2.0.cs b/sdk/communication/Azure.Communication.Chat/api/Azure.Communication.Chat.netstandard2.0.cs index e4e82be8af3fe..a8764ab604e44 100644 --- a/sdk/communication/Azure.Communication.Chat/api/Azure.Communication.Chat.netstandard2.0.cs +++ b/sdk/communication/Azure.Communication.Chat/api/Azure.Communication.Chat.netstandard2.0.cs @@ -3,7 +3,7 @@ namespace Azure.Communication.Chat public partial class ChatClient { protected ChatClient() { } - public ChatClient(System.Uri endpointUrl, Azure.Communication.Identity.CommunicationTokenCredential communicationTokenCredential, Azure.Communication.Chat.ChatClientOptions? options = null) { } + public ChatClient(System.Uri endpointUrl, Azure.Communication.CommunicationTokenCredential communicationTokenCredential, Azure.Communication.Chat.ChatClientOptions? options = null) { } public virtual Azure.Communication.Chat.ChatThreadClient CreateChatThread(string topic, System.Collections.Generic.IEnumerable members, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; } public virtual System.Threading.Tasks.Task CreateChatThreadAsync(string topic, System.Collections.Generic.IEnumerable members, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; } public virtual Azure.Response DeleteChatThread(string threadId, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; } 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 908565a9d9435..5b297e74d673a 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 @@ -9,6 +9,14 @@ public abstract partial class CommunicationIdentifier { protected CommunicationIdentifier() { } } + public sealed partial class CommunicationTokenCredential : System.IDisposable + { + public CommunicationTokenCredential(Azure.Communication.Identity.CommunicationTokenRefreshOptions tokenRefreshOptions) { } + public CommunicationTokenCredential(string userToken) { } + public void Dispose() { } + public Azure.Core.AccessToken GetToken(System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; } + public System.Threading.Tasks.ValueTask GetTokenAsync(System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; } + } public partial class CommunicationUser : Azure.Communication.CommunicationIdentifier { public CommunicationUser(string id) { } @@ -28,20 +36,8 @@ public UnknownIdentifier(string id) { } } namespace Azure.Communication.Identity { - public sealed partial class CommunicationTokenCredential : System.IDisposable - { -<<<<<<< HEAD - public CommunicationTokenCredential(Azure.Communication.Identity.CommunicationTokenRefreshOptions tokenRefreshOptions) { } -======= - public CommunicationTokenCredential(bool refreshProactively, System.Func tokenRefresher, System.Func>? asyncTokenRefresher = null, string? initialToken = null) { } ->>>>>>> 9e93e136d1dd5ee5cb24adb1ded988a45acf79c3 - public CommunicationTokenCredential(string userToken) { } - public void Dispose() { } - public Azure.Core.AccessToken GetToken(System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; } - public System.Threading.Tasks.ValueTask GetTokenAsync(System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; } - } 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, string? userToken = null) { } } } From 624cab72cd7c2d69f29bb69622555a3ac8b2d180 Mon Sep 17 00:00:00 2001 From: Minnie Liu Date: Wed, 16 Dec 2020 14:37:38 -0800 Subject: [PATCH 09/12] Removing Identity namespace --- .../CommunicationIdentityClientLiveTests.cs | 2 +- .../Azure.Communication.Chat/README.md | 2 +- .../Azure.Communication.Chat/src/ChatClient.cs | 2 +- .../src/ChatThreadClient.cs | 2 +- .../tests/ChatClients/ChatClientsLiveTest.cs | 2 +- .../tests/ChatClients/ChatClientsTests.cs | 2 +- .../tests/Infrastructure/ChatLiveTestBase.cs | 2 +- .../tests/samples/Sample1_ThreadOperations.cs | 2 +- .../tests/samples/Sample2_MessagingOperations.cs | 2 +- .../tests/samples/Sample3_MemberOperations.cs | 2 +- .../Azure.Communication.Common.netstandard2.0.cs | 15 ++++++--------- .../{Identity => }/AutoRefreshTokenCredential.cs | 2 +- .../CommunicationTokenCredential.cs | 10 +++++----- .../CommunicationTokenRefreshOptions.cs | 10 +++++----- .../ICommunicationTokenCredential.cs | 2 +- .../src/{Identity => }/JwtTokenParser.cs | 2 +- .../src/{Identity => }/StaticTokenCredential.cs | 2 +- .../ThreadSafeRefreshableAccessTokenCache.cs | 2 +- .../Identity/CommunicationTokenCredentialTest.cs | 2 +- .../tests/Identity/TestClock.cs | 2 +- .../src/CommunicationBearerTokenCredential.cs | 2 +- 21 files changed, 34 insertions(+), 37 deletions(-) rename sdk/communication/Azure.Communication.Common/src/{Identity => }/AutoRefreshTokenCredential.cs (98%) rename sdk/communication/Azure.Communication.Common/src/{Identity => }/CommunicationTokenCredential.cs (90%) rename sdk/communication/Azure.Communication.Common/src/{Identity => }/CommunicationTokenRefreshOptions.cs (87%) rename sdk/communication/Azure.Communication.Common/src/{Identity => }/ICommunicationTokenCredential.cs (91%) rename sdk/communication/Azure.Communication.Common/src/{Identity => }/JwtTokenParser.cs (97%) rename sdk/communication/Azure.Communication.Common/src/{Identity => }/StaticTokenCredential.cs (95%) rename sdk/communication/Azure.Communication.Common/src/{Identity => }/ThreadSafeRefreshableAccessTokenCache.cs (99%) diff --git a/sdk/communication/Azure.Communication.Administration/tests/CommunicationIdentityClient/CommunicationIdentityClientLiveTests.cs b/sdk/communication/Azure.Communication.Administration/tests/CommunicationIdentityClient/CommunicationIdentityClientLiveTests.cs index 6bc7583846a0b..c23082dc6c56f 100644 --- a/sdk/communication/Azure.Communication.Administration/tests/CommunicationIdentityClient/CommunicationIdentityClientLiveTests.cs +++ b/sdk/communication/Azure.Communication.Administration/tests/CommunicationIdentityClient/CommunicationIdentityClientLiveTests.cs @@ -4,7 +4,7 @@ using System.Linq; using System.Threading.Tasks; using Azure.Communication.Administration.Models; -using Azure.Communication.Identity; +using Azure.Communication; using Azure.Core.TestFramework; using NUnit.Framework; diff --git a/sdk/communication/Azure.Communication.Chat/README.md b/sdk/communication/Azure.Communication.Chat/README.md index f7210dd5af8e6..d593c570747e6 100644 --- a/sdk/communication/Azure.Communication.Chat/README.md +++ b/sdk/communication/Azure.Communication.Chat/README.md @@ -39,7 +39,7 @@ For the generation of user access tokens, refer to [User Access Tokens][useracce ```C# Snippet:Azure_Communication_Chat_Tests_E2E_UsingStatements using Azure.Communication.Administration; using Azure.Communication.Administration.Models; -using Azure.Communication.Identity; +using Azure.Communication; using Azure.Communication.Chat; ``` diff --git a/sdk/communication/Azure.Communication.Chat/src/ChatClient.cs b/sdk/communication/Azure.Communication.Chat/src/ChatClient.cs index ab44d49667e38..96fb7e189b89b 100644 --- a/sdk/communication/Azure.Communication.Chat/src/ChatClient.cs +++ b/sdk/communication/Azure.Communication.Chat/src/ChatClient.cs @@ -6,7 +6,7 @@ using System.Linq; using System.Threading; using System.Threading.Tasks; -using Azure.Communication.Identity; +using Azure.Communication; using Azure.Communication.Pipeline; using Azure.Core; using Azure.Core.Pipeline; diff --git a/sdk/communication/Azure.Communication.Chat/src/ChatThreadClient.cs b/sdk/communication/Azure.Communication.Chat/src/ChatThreadClient.cs index e2ace112e8c79..c7a68edaa90c6 100644 --- a/sdk/communication/Azure.Communication.Chat/src/ChatThreadClient.cs +++ b/sdk/communication/Azure.Communication.Chat/src/ChatThreadClient.cs @@ -6,7 +6,7 @@ using System.Linq; using System.Threading; using System.Threading.Tasks; -using Azure.Communication.Identity; +using Azure.Communication; using Azure.Communication.Pipeline; using Azure.Core; using Azure.Core.Pipeline; diff --git a/sdk/communication/Azure.Communication.Chat/tests/ChatClients/ChatClientsLiveTest.cs b/sdk/communication/Azure.Communication.Chat/tests/ChatClients/ChatClientsLiveTest.cs index ec2b395e084dd..84652eaf5206c 100644 --- a/sdk/communication/Azure.Communication.Chat/tests/ChatClients/ChatClientsLiveTest.cs +++ b/sdk/communication/Azure.Communication.Chat/tests/ChatClients/ChatClientsLiveTest.cs @@ -9,7 +9,7 @@ #region Snippet:Azure_Communication_Chat_Tests_E2E_UsingStatements using Azure.Communication.Administration; using Azure.Communication.Administration.Models; -using Azure.Communication.Identity; +using Azure.Communication; //@@ using Azure.Communication.Chat; #endregion Snippet:Azure_Communication_Chat_Tests_E2E_UsingStatements diff --git a/sdk/communication/Azure.Communication.Chat/tests/ChatClients/ChatClientsTests.cs b/sdk/communication/Azure.Communication.Chat/tests/ChatClients/ChatClientsTests.cs index b383d5c124f34..a0c94029d483c 100644 --- a/sdk/communication/Azure.Communication.Chat/tests/ChatClients/ChatClientsTests.cs +++ b/sdk/communication/Azure.Communication.Chat/tests/ChatClients/ChatClientsTests.cs @@ -3,7 +3,7 @@ using System; using System.Threading.Tasks; -using Azure.Communication.Identity; +using Azure.Communication; using Azure.Core.TestFramework; using NUnit.Framework; diff --git a/sdk/communication/Azure.Communication.Chat/tests/Infrastructure/ChatLiveTestBase.cs b/sdk/communication/Azure.Communication.Chat/tests/Infrastructure/ChatLiveTestBase.cs index 6c6b8c57c3e4f..1023b1e4f8246 100644 --- a/sdk/communication/Azure.Communication.Chat/tests/Infrastructure/ChatLiveTestBase.cs +++ b/sdk/communication/Azure.Communication.Chat/tests/Infrastructure/ChatLiveTestBase.cs @@ -5,7 +5,7 @@ using System.Collections.Generic; using System.Threading.Tasks; using Azure.Communication.Administration; -using Azure.Communication.Identity; +using Azure.Communication; using Azure.Core.TestFramework; namespace Azure.Communication.Chat.Tests diff --git a/sdk/communication/Azure.Communication.Chat/tests/samples/Sample1_ThreadOperations.cs b/sdk/communication/Azure.Communication.Chat/tests/samples/Sample1_ThreadOperations.cs index e40a0f1eff482..b2524ab313841 100644 --- a/sdk/communication/Azure.Communication.Chat/tests/samples/Sample1_ThreadOperations.cs +++ b/sdk/communication/Azure.Communication.Chat/tests/samples/Sample1_ThreadOperations.cs @@ -5,7 +5,7 @@ using System.Threading.Tasks; using Azure.Communication.Administration; using Azure.Communication.Administration.Models; -using Azure.Communication.Identity; +using Azure.Communication; using Azure.Core.TestFramework; using NUnit.Framework; diff --git a/sdk/communication/Azure.Communication.Chat/tests/samples/Sample2_MessagingOperations.cs b/sdk/communication/Azure.Communication.Chat/tests/samples/Sample2_MessagingOperations.cs index 585c068222dbf..1f0e589d8879b 100644 --- a/sdk/communication/Azure.Communication.Chat/tests/samples/Sample2_MessagingOperations.cs +++ b/sdk/communication/Azure.Communication.Chat/tests/samples/Sample2_MessagingOperations.cs @@ -5,7 +5,7 @@ using System.Threading.Tasks; using Azure.Communication.Administration; using Azure.Communication.Administration.Models; -using Azure.Communication.Identity; +using Azure.Communication; using Azure.Core.TestFramework; using NUnit.Framework; diff --git a/sdk/communication/Azure.Communication.Chat/tests/samples/Sample3_MemberOperations.cs b/sdk/communication/Azure.Communication.Chat/tests/samples/Sample3_MemberOperations.cs index b9037ce688b56..d8b49c194500e 100644 --- a/sdk/communication/Azure.Communication.Chat/tests/samples/Sample3_MemberOperations.cs +++ b/sdk/communication/Azure.Communication.Chat/tests/samples/Sample3_MemberOperations.cs @@ -5,7 +5,7 @@ using System.Threading.Tasks; using Azure.Communication.Administration; using Azure.Communication.Administration.Models; -using Azure.Communication.Identity; +using Azure.Communication; using Azure.Core.TestFramework; using NUnit.Framework; 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 5b297e74d673a..ae4f1fd40bf11 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 @@ -11,12 +11,16 @@ protected CommunicationIdentifier() { } } public sealed partial class CommunicationTokenCredential : System.IDisposable { - public CommunicationTokenCredential(Azure.Communication.Identity.CommunicationTokenRefreshOptions tokenRefreshOptions) { } - public CommunicationTokenCredential(string userToken) { } + public CommunicationTokenCredential(Azure.Communication.CommunicationTokenRefreshOptions tokenRefreshOptions) { } + 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 GetTokenAsync(System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; } } + public partial class CommunicationTokenRefreshOptions + { + public CommunicationTokenRefreshOptions(bool refreshProactively, System.Func tokenRefresher, System.Func>? asyncTokenRefresher, string? token = null) { } + } public partial class CommunicationUser : Azure.Communication.CommunicationIdentifier { public CommunicationUser(string id) { } @@ -34,10 +38,3 @@ public UnknownIdentifier(string id) { } public string Id { get { throw null; } } } } -namespace Azure.Communication.Identity -{ - public partial class CommunicationTokenRefreshOptions - { - public CommunicationTokenRefreshOptions(bool refreshProactively, System.Func tokenRefresher, System.Func>? asyncTokenRefresher, string? userToken = null) { } - } -} diff --git a/sdk/communication/Azure.Communication.Common/src/Identity/AutoRefreshTokenCredential.cs b/sdk/communication/Azure.Communication.Common/src/AutoRefreshTokenCredential.cs similarity index 98% rename from sdk/communication/Azure.Communication.Common/src/Identity/AutoRefreshTokenCredential.cs rename to sdk/communication/Azure.Communication.Common/src/AutoRefreshTokenCredential.cs index cdf7da858ac0a..48775305d6fc7 100644 --- a/sdk/communication/Azure.Communication.Common/src/Identity/AutoRefreshTokenCredential.cs +++ b/sdk/communication/Azure.Communication.Common/src/AutoRefreshTokenCredential.cs @@ -6,7 +6,7 @@ using System.Threading.Tasks; using Azure.Core; -namespace Azure.Communication.Identity +namespace Azure.Communication { internal sealed class AutoRefreshTokenCredential : ICommunicationTokenCredential { diff --git a/sdk/communication/Azure.Communication.Common/src/Identity/CommunicationTokenCredential.cs b/sdk/communication/Azure.Communication.Common/src/CommunicationTokenCredential.cs similarity index 90% rename from sdk/communication/Azure.Communication.Common/src/Identity/CommunicationTokenCredential.cs rename to sdk/communication/Azure.Communication.Common/src/CommunicationTokenCredential.cs index ee0d927c00f25..e5ab207b0d462 100644 --- a/sdk/communication/Azure.Communication.Common/src/Identity/CommunicationTokenCredential.cs +++ b/sdk/communication/Azure.Communication.Common/src/CommunicationTokenCredential.cs @@ -4,7 +4,7 @@ using System; using System.Threading; using System.Threading.Tasks; -using Azure.Communication.Identity; +using Azure.Communication; using Azure.Core; namespace Azure.Communication @@ -20,9 +20,9 @@ public sealed class CommunicationTokenCredential : IDisposable /// /// Initializes a new instance of . /// - /// User token acquired from Azure.Communication.Administration package. - public CommunicationTokenCredential(string userToken) - => _tokenCredential = new StaticTokenCredential(userToken); + /// User token acquired from Azure.Communication.Administration package. + public CommunicationTokenCredential(string token) + => _tokenCredential = new StaticTokenCredential(token); /// /// Initializes a new instance of that automatically renews the token upon expiry or proactively prior to expiration to speed up the requests. @@ -34,7 +34,7 @@ public CommunicationTokenCredential(CommunicationTokenRefreshOptions tokenRefres _tokenCredential = new AutoRefreshTokenCredential( tokenRefreshOptions.TokenRefresher, tokenRefreshOptions.AsyncTokenRefresher ?? (cancellationToken => new ValueTask(tokenRefreshOptions.TokenRefresher(cancellationToken))), - tokenRefreshOptions.UserToken, + tokenRefreshOptions.Token, tokenRefreshOptions.RefreshProactively); } diff --git a/sdk/communication/Azure.Communication.Common/src/Identity/CommunicationTokenRefreshOptions.cs b/sdk/communication/Azure.Communication.Common/src/CommunicationTokenRefreshOptions.cs similarity index 87% rename from sdk/communication/Azure.Communication.Common/src/Identity/CommunicationTokenRefreshOptions.cs rename to sdk/communication/Azure.Communication.Common/src/CommunicationTokenRefreshOptions.cs index 6eb09b764d967..8231a071b8c20 100644 --- a/sdk/communication/Azure.Communication.Common/src/Identity/CommunicationTokenRefreshOptions.cs +++ b/sdk/communication/Azure.Communication.Common/src/CommunicationTokenRefreshOptions.cs @@ -6,7 +6,7 @@ using System.Threading.Tasks; using Azure.Core; -namespace Azure.Communication.Identity +namespace Azure.Communication { /// /// The Communication Token Refresh Options @@ -16,7 +16,7 @@ public class CommunicationTokenRefreshOptions internal bool RefreshProactively { get; } internal Func TokenRefresher { get; } internal Func>? AsyncTokenRefresher { get; } - internal string? UserToken { get; } + internal string? Token { get; } /// /// Initializes a new instance of . @@ -24,17 +24,17 @@ public class CommunicationTokenRefreshOptions /// 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. + /// Optional token value. public CommunicationTokenRefreshOptions( bool refreshProactively, Func tokenRefresher, Func>? asyncTokenRefresher, - string? userToken = null) + string? token = null) { RefreshProactively = refreshProactively; TokenRefresher = tokenRefresher; AsyncTokenRefresher = asyncTokenRefresher; - UserToken = userToken; + Token = token; } } } diff --git a/sdk/communication/Azure.Communication.Common/src/Identity/ICommunicationTokenCredential.cs b/sdk/communication/Azure.Communication.Common/src/ICommunicationTokenCredential.cs similarity index 91% rename from sdk/communication/Azure.Communication.Common/src/Identity/ICommunicationTokenCredential.cs rename to sdk/communication/Azure.Communication.Common/src/ICommunicationTokenCredential.cs index 9a87867c494e1..a59a9de7b91ca 100644 --- a/sdk/communication/Azure.Communication.Common/src/Identity/ICommunicationTokenCredential.cs +++ b/sdk/communication/Azure.Communication.Common/src/ICommunicationTokenCredential.cs @@ -6,7 +6,7 @@ using System.Threading.Tasks; using Azure.Core; -namespace Azure.Communication.Identity +namespace Azure.Communication { internal interface ICommunicationTokenCredential : IDisposable { diff --git a/sdk/communication/Azure.Communication.Common/src/Identity/JwtTokenParser.cs b/sdk/communication/Azure.Communication.Common/src/JwtTokenParser.cs similarity index 97% rename from sdk/communication/Azure.Communication.Common/src/Identity/JwtTokenParser.cs rename to sdk/communication/Azure.Communication.Common/src/JwtTokenParser.cs index 0c5983e5908d7..dd9a062039f08 100644 --- a/sdk/communication/Azure.Communication.Common/src/Identity/JwtTokenParser.cs +++ b/sdk/communication/Azure.Communication.Common/src/JwtTokenParser.cs @@ -7,7 +7,7 @@ using System.Text.Json.Serialization; using Azure.Core; -namespace Azure.Communication.Identity +namespace Azure.Communication { internal class JwtTokenParser { diff --git a/sdk/communication/Azure.Communication.Common/src/Identity/StaticTokenCredential.cs b/sdk/communication/Azure.Communication.Common/src/StaticTokenCredential.cs similarity index 95% rename from sdk/communication/Azure.Communication.Common/src/Identity/StaticTokenCredential.cs rename to sdk/communication/Azure.Communication.Common/src/StaticTokenCredential.cs index 3370611ede6c3..5ca99adce8b5f 100644 --- a/sdk/communication/Azure.Communication.Common/src/Identity/StaticTokenCredential.cs +++ b/sdk/communication/Azure.Communication.Common/src/StaticTokenCredential.cs @@ -5,7 +5,7 @@ using System.Threading.Tasks; using Azure.Core; -namespace Azure.Communication.Identity +namespace Azure.Communication { internal sealed class StaticTokenCredential : ICommunicationTokenCredential { diff --git a/sdk/communication/Azure.Communication.Common/src/Identity/ThreadSafeRefreshableAccessTokenCache.cs b/sdk/communication/Azure.Communication.Common/src/ThreadSafeRefreshableAccessTokenCache.cs similarity index 99% rename from sdk/communication/Azure.Communication.Common/src/Identity/ThreadSafeRefreshableAccessTokenCache.cs rename to sdk/communication/Azure.Communication.Common/src/ThreadSafeRefreshableAccessTokenCache.cs index 7c30dede8734c..3cfb2d42f8f2f 100644 --- a/sdk/communication/Azure.Communication.Common/src/Identity/ThreadSafeRefreshableAccessTokenCache.cs +++ b/sdk/communication/Azure.Communication.Common/src/ThreadSafeRefreshableAccessTokenCache.cs @@ -7,7 +7,7 @@ using Azure.Core; using Azure.Core.Pipeline; -namespace Azure.Communication.Identity +namespace Azure.Communication { /// /// Represents a type that caches an access token, diff --git a/sdk/communication/Azure.Communication.Common/tests/Identity/CommunicationTokenCredentialTest.cs b/sdk/communication/Azure.Communication.Common/tests/Identity/CommunicationTokenCredentialTest.cs index 1cfca2537d32a..4cd7696304b34 100644 --- a/sdk/communication/Azure.Communication.Common/tests/Identity/CommunicationTokenCredentialTest.cs +++ b/sdk/communication/Azure.Communication.Common/tests/Identity/CommunicationTokenCredentialTest.cs @@ -102,7 +102,7 @@ public void CommunicationTokenCredential_ThrowsIfInvalidToken(string token) [Test] public void CommunicationTokenCredential_ThrowsIfTokenIsNull() - => Assert.Throws(() => new CommunicationTokenCredential(userToken: null!)); + => Assert.Throws(() => new CommunicationTokenCredential(token: null!)); [Test] [TestCase(false)] diff --git a/sdk/communication/Azure.Communication.Common/tests/Identity/TestClock.cs b/sdk/communication/Azure.Communication.Common/tests/Identity/TestClock.cs index b1fd3163bcb49..62294260d7779 100644 --- a/sdk/communication/Azure.Communication.Common/tests/Identity/TestClock.cs +++ b/sdk/communication/Azure.Communication.Common/tests/Identity/TestClock.cs @@ -4,7 +4,7 @@ using System; using System.Collections.Generic; using System.Linq; -using IScheduledAction = Azure.Communication.Identity.ThreadSafeRefreshableAccessTokenCache.IScheduledAction; +using IScheduledAction = Azure.Communication.ThreadSafeRefreshableAccessTokenCache.IScheduledAction; namespace Azure.Communication.Identity { diff --git a/sdk/communication/Shared/src/CommunicationBearerTokenCredential.cs b/sdk/communication/Shared/src/CommunicationBearerTokenCredential.cs index f4fc608cf9554..4216e7726422f 100644 --- a/sdk/communication/Shared/src/CommunicationBearerTokenCredential.cs +++ b/sdk/communication/Shared/src/CommunicationBearerTokenCredential.cs @@ -1,7 +1,7 @@ // Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT License. -using Azure.Communication.Identity; +using Azure.Communication; using Azure.Core; using System.Threading; using System.Threading.Tasks; From 569f1bf3debf4ba076cefce8d7d93ef468b4742c Mon Sep 17 00:00:00 2001 From: Minnie Liu Date: Wed, 16 Dec 2020 15:08:38 -0800 Subject: [PATCH 10/12] Fix format --- .../CommunicationBearerTokenCredentialTests.cs | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/sdk/communication/Azure.Communication.Common/tests/Pipeline/CommunicationBearerTokenCredentialTests.cs b/sdk/communication/Azure.Communication.Common/tests/Pipeline/CommunicationBearerTokenCredentialTests.cs index a189ac6fd012a..4c711a2829b03 100644 --- a/sdk/communication/Azure.Communication.Common/tests/Pipeline/CommunicationBearerTokenCredentialTests.cs +++ b/sdk/communication/Azure.Communication.Common/tests/Pipeline/CommunicationBearerTokenCredentialTests.cs @@ -41,8 +41,7 @@ public async Task CommunicationBearerTokenCredential_CreateRefreshableWithoutIni new CommunicationTokenRefreshOptions( refreshProactively: true, // Indicates if the token should be proactively refreshed in the background or only on-demand tokenRefresher: cancellationToken => FetchTokenForUserFromMyServer("bob@contoso.com", cancellationToken), - asyncTokenRefresher: cancellationToken => FetchTokenForUserFromMyServerAsync("bob@contoso.com", cancellationToken) - ) + asyncTokenRefresher: cancellationToken => FetchTokenForUserFromMyServerAsync("bob@contoso.com", cancellationToken)) ); var communicationBearerTokenCredential = new CommunicationBearerTokenCredential(tokenCredential); @@ -58,8 +57,7 @@ public async Task CommunicationBearerTokenCredential_CreateRefreshableWithInitia refreshProactively: true, // Indicates if the token should be proactively refreshed in the background or only on-demand tokenRefresher: cancellationToken => FetchTokenForUserFromMyServer("bob@contoso.com", cancellationToken), asyncTokenRefresher: cancellationToken => FetchTokenForUserFromMyServerAsync("bob@contoso.com", cancellationToken), - initialToken - ) + initialToken) ); var communicationBearerTokenCredential = new CommunicationBearerTokenCredential(tokenCredential); @@ -112,8 +110,7 @@ public void CommunicationBearerTokenCredential_PassesCancelToken(bool refreshPro refreshProactively, RefreshToken, c => new ValueTask(RefreshToken(c)), - ExpiredToken - ) + ExpiredToken) ); var communicationBearerTokenCredential = new CommunicationBearerTokenCredential(tokenCredential); @@ -140,8 +137,7 @@ public async Task CommunicationBearerTokenCredential_PassesAsyncCancelToken(bool refreshProactively, RefreshToken, c => new ValueTask(RefreshToken(c)), - ExpiredToken - ) + ExpiredToken) ); var communicationBearerTokenCredential = new CommunicationBearerTokenCredential(tokenCredential); From a4288d4bc626819408493e279f22074625ecb3f2 Mon Sep 17 00:00:00 2001 From: Minnie Liu Date: Wed, 16 Dec 2020 15:56:54 -0800 Subject: [PATCH 11/12] Update changelog --- sdk/communication/Azure.Communication.Common/CHANGELOG.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/sdk/communication/Azure.Communication.Common/CHANGELOG.md b/sdk/communication/Azure.Communication.Common/CHANGELOG.md index 7ed38d57369f4..e766106b5c05a 100644 --- a/sdk/communication/Azure.Communication.Common/CHANGELOG.md +++ b/sdk/communication/Azure.Communication.Common/CHANGELOG.md @@ -3,8 +3,8 @@ ## 1.0.0-beta.4 (Unreleased) ### Breaking Changes - Renamed `CommunicationUserCredential` to `CommunicationTokenCredential`. -- Replace `CommunicationTokenCredential(bool refreshProactively, Func tokenRefresher,Func>? asyncTokenRefresher = null, string? initialToken = null)` -- with `CommunicationTokenCredential(CommunicationTokenRefreshOptions tokenRefreshOptions)`. +- Replace `CommunicationTokenCredential(bool refreshProactively, Func tokenRefresher,Func>? asyncTokenRefresher = null, string? initialToken = null)` +with `CommunicationTokenCredential(CommunicationTokenRefreshOptions tokenRefreshOptions)`. ## 1.0.0-beta.3 (2020-11-16) Updated `Azure.Communication.Common` version. From c90c821a3c185b041a22e3c99d5d3f8fd36cf55c Mon Sep 17 00:00:00 2001 From: Minnie Liu Date: Thu, 17 Dec 2020 11:50:09 -0800 Subject: [PATCH 12/12] Update Merge conflict --- .../CommunicationIdentityClientLiveTests.cs | 5 ----- 1 file changed, 5 deletions(-) diff --git a/sdk/communication/Azure.Communication.Administration/tests/CommunicationIdentityClient/CommunicationIdentityClientLiveTests.cs b/sdk/communication/Azure.Communication.Administration/tests/CommunicationIdentityClient/CommunicationIdentityClientLiveTests.cs index 1848c4845d42b..b6c80518e3a4f 100644 --- a/sdk/communication/Azure.Communication.Administration/tests/CommunicationIdentityClient/CommunicationIdentityClientLiveTests.cs +++ b/sdk/communication/Azure.Communication.Administration/tests/CommunicationIdentityClient/CommunicationIdentityClientLiveTests.cs @@ -5,16 +5,11 @@ using System.Linq; using System.Threading.Tasks; using Azure.Communication.Administration.Models; -<<<<<<< HEAD using Azure.Communication; using Azure.Core; -======= ->>>>>>> e12f3e0f673cc1b234451f9b89a45d7cb8d1f2c6 using Azure.Core.TestFramework; using Azure.Identity; using NUnit.Framework; -using Azure.Core; -using Azure.Communication.Identity; namespace Azure.Communication.Administration.Tests {