Skip to content

Commit 141f578

Browse files
JoshuaLaiMinnie Liu
andauthored
[Communication] - Identity - Auth with Token Credential (Azure#17438)
* [CommunicationIdentityClient] adding in new method to create beaer token. Fixing spelling mistake. * [CommunicationIdentityClient] adding new init passing in token credential * [TokenCredential] finalize the parameters to take in a token credential and an endpoint string * [ClientOptionsExtensions] removing the scope from the BearerTokenAuth policy * [CommunicationIdentityClientLiveTests] Adding new live test init with token credential * [PhoneNumberAdministrationClient] revert the init with token * [CommunicationIdentityClient] passing in endpoint as an uri * [CommunicationIdentityClient] assert on the endpoint * [CommunicationIdentityClient] reorder of the parameters * Adding working live tests * [Scripts] New generated code * [CommunicationIdentityClient] update the comment for tokenCredential to be accurate * [CommunicationIdentityClient] update the comment for endpoont to be more accurate * [CommunicationIdentityClientLiveTests] Nit on the the test name * Update CHANGELOG.md * [IdentityClientFromTokenWithMultipleScopes] rename the generated files * [CommunicationRecordedTestSanitizer] adding sanitizer to COMMUNICATION_ENDPOINT_STRING Co-authored-by: Minnie Liu <peiliu@microsoft.com>
1 parent ffbb276 commit 141f578

14 files changed

+361
-5
lines changed

sdk/communication/Azure.Communication.Administration/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
## 1.0.0-beta.4 (Unreleased)
44

5+
### Added
6+
- Added support to create CommunicationIdentityClient with TokenCredential
57
### Fixed
68
- Issue with paging results not pulling next pages
79

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ public partial class CommunicationIdentityClient
44
{
55
protected CommunicationIdentityClient() { }
66
public CommunicationIdentityClient(string connectionString, Azure.Communication.Administration.CommunicationIdentityClientOptions? options = null) { }
7+
public CommunicationIdentityClient(System.Uri endpoint, Azure.Core.TokenCredential tokenCredential, Azure.Communication.Administration.CommunicationIdentityClientOptions? options = null) { }
78
public virtual Azure.Response<Azure.Communication.CommunicationUser> CreateUser(System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
89
public virtual System.Threading.Tasks.Task<Azure.Response<Azure.Communication.CommunicationUser>> CreateUserAsync(System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
910
public virtual Azure.Response DeleteUser(Azure.Communication.CommunicationUser communicationUser, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }

sdk/communication/Azure.Communication.Administration/src/CommunicationIdentityClient.cs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,34 @@ public CommunicationIdentityClient(string connectionString, CommunicationIdentit
2929
options ?? new CommunicationIdentityClientOptions(),
3030
ConnectionString.Parse(AssertNotNull(connectionString, nameof(connectionString))))
3131
{ }
32-
32+
/// <summary> Initializes a new instance of <see cref="CommunicationIdentityClient"/>.</summary>
33+
/// <param name="tokenCredential">The TokenCredential used to authenticate requests, such as DefaultAzureCredential.</param>
34+
/// <param name="endpoint">The URI of the Azure Communication Services resource.</param>
35+
/// <param name="options">Client option exposing <see cref="ClientOptions.Diagnostics"/>, <see cref="ClientOptions.Retry"/>, <see cref="ClientOptions.Transport"/>, etc.</param>
36+
public CommunicationIdentityClient(Uri endpoint, TokenCredential tokenCredential, CommunicationIdentityClientOptions? options = default)
37+
: this(
38+
AssertNotNull(endpoint, nameof(endpoint)),
39+
options ?? new CommunicationIdentityClientOptions(),
40+
AssertNotNull(tokenCredential, nameof(tokenCredential)))
41+
{ }
3342
private CommunicationIdentityClient(CommunicationIdentityClientOptions options, ConnectionString connectionString)
3443
{
3544
_clientDiagnostics = new ClientDiagnostics(options);
3645
RestClient = new CommunicationIdentityRestClient(
3746
_clientDiagnostics,
38-
options.BuildHttpPipline(connectionString),
47+
options.BuildHttpPipeline(connectionString),
3948
connectionString.GetRequired("endpoint"));
4049
}
4150

51+
private CommunicationIdentityClient(Uri endpoint, CommunicationIdentityClientOptions options, TokenCredential tokenCredential)
52+
{
53+
_clientDiagnostics = new ClientDiagnostics(options);
54+
RestClient = new CommunicationIdentityRestClient(
55+
_clientDiagnostics,
56+
options.BuildHttpPipeline(tokenCredential),
57+
endpoint.AbsoluteUri);
58+
}
59+
4260
/// <summary>Initializes a new instance of <see cref="CommunicationIdentityClient"/> for mocking.</summary>
4361
protected CommunicationIdentityClient()
4462
{

sdk/communication/Azure.Communication.Administration/src/PhoneNumberAdministrationClient.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public PhoneNumberAdministrationClient(string connectionString, PhoneNumberAdmin
3838
{ }
3939

4040
internal PhoneNumberAdministrationClient(PhoneNumberAdministrationClientOptions options, ConnectionString connectionString)
41-
: this(new ClientDiagnostics(options), options.BuildHttpPipline(connectionString), connectionString.GetRequired("endpoint"))
41+
: this(new ClientDiagnostics(options), options.BuildHttpPipeline(connectionString), connectionString.GetRequired("endpoint"))
4242
{ }
4343

4444
internal PhoneNumberAdministrationClient(ClientDiagnostics clientDiagnostics, HttpPipeline pipeline, string endpointUrl)

sdk/communication/Azure.Communication.Administration/tests/CommunicationIdentityClient/CommunicationIdentityClientLiveTests.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
// Copyright (c) Microsoft Corporation. All rights reserved.
22
// Licensed under the MIT License.
33

4+
using System;
45
using System.Linq;
56
using System.Threading.Tasks;
67
using Azure.Communication.Administration.Models;
78
using Azure.Communication.Identity;
9+
using Azure.Core;
810
using Azure.Core.TestFramework;
11+
using Azure.Identity;
912
using NUnit.Framework;
1013

1114
namespace Azure.Communication.Administration.Tests
@@ -49,5 +52,28 @@ void ValidateScopesIfNotSanitized()
4952
}
5053
}
5154
}
55+
56+
[Test]
57+
[TestCase("chat", TestName = "IdentityClientFromTokenWithSingleScope")]
58+
[TestCase("chat", "pstn", TestName = "IdentityClientFromTokenWithMultipleScopes")]
59+
public async Task GeneratesIdentityUsingTokenCredentialWithScopes(params string[] scopes)
60+
{
61+
TokenCredential tokenCredential;
62+
if (Mode == RecordedTestMode.Playback)
63+
{
64+
tokenCredential = new MockCredential();
65+
}
66+
else
67+
{
68+
tokenCredential = new DefaultAzureCredential();
69+
}
70+
CommunicationIdentityClient client = CreateInstrumentedCommunicationIdentityClientWithToken(tokenCredential);
71+
Response<CommunicationUser> userResponse = await client.CreateUserAsync();
72+
Response<CommunicationUserToken> tokenResponse = await client.IssueTokenAsync(userResponse.Value, scopes: scopes.Select(x => new CommunicationTokenScope(x)));
73+
74+
Assert.IsNotNull(tokenResponse.Value);
75+
Assert.IsFalse(string.IsNullOrWhiteSpace(tokenResponse.Value.Token));
76+
Assert.IsFalse(string.IsNullOrWhiteSpace(tokenResponse.Value.User.Id));
77+
}
5278
}
5379
}

sdk/communication/Azure.Communication.Administration/tests/Infrastructure/CommunicationIdentityClientLiveTestBase.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
// Copyright (c) Microsoft Corporation. All rights reserved.
22
// Licensed under the MIT License.
33

4+
using System;
45
using Azure.Communication.Pipeline;
6+
using Azure.Core;
57
using Azure.Core.TestFramework;
68

79
namespace Azure.Communication.Administration.Tests
@@ -21,5 +23,12 @@ protected CommunicationIdentityClient CreateInstrumentedCommunicationIdentityCli
2123
new CommunicationIdentityClient(
2224
TestEnvironment.ConnectionString,
2325
InstrumentClientOptions(new CommunicationIdentityClientOptions())));
26+
27+
protected CommunicationIdentityClient CreateInstrumentedCommunicationIdentityClientWithToken(TokenCredential token)
28+
=> InstrumentClient(
29+
new CommunicationIdentityClient(
30+
new Uri(TestEnvironment.EndpointString),
31+
token,
32+
InstrumentClientOptions(new CommunicationIdentityClientOptions())));
2433
}
2534
}

sdk/communication/Azure.Communication.Administration/tests/Infrastructure/CommunicationIdentityClientTestEnvironment.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,9 @@ public class CommunicationIdentityClientTestEnvironment: TestEnvironment
1010
internal const string ConnectionStringEnvironmentVariableName = "COMMUNICATION_CONNECTION_STRING";
1111

1212
public string ConnectionString => GetRecordedVariable(ConnectionStringEnvironmentVariableName);
13+
14+
internal const string EndpointStringEnvironmentVariableName = "COMMUNICATION_ENDPOINT_STRING";
15+
16+
public string EndpointString => GetRecordedVariable(EndpointStringEnvironmentVariableName);
1317
}
1418
}

sdk/communication/Azure.Communication.Administration/tests/SessionRecords/CommunicationIdentityClientLiveTests/IdentityClientFromTokenWithMultipleScopes.json

Lines changed: 73 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

sdk/communication/Azure.Communication.Administration/tests/SessionRecords/CommunicationIdentityClientLiveTests/IdentityClientFromTokenWithMultipleScopesAsync.json

Lines changed: 73 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

sdk/communication/Azure.Communication.Administration/tests/SessionRecords/CommunicationIdentityClientLiveTests/IdentityClientFromTokenWithSingleScope.json

Lines changed: 72 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)