Skip to content

Commit 103028a

Browse files
minnieliuMinnie Liu
andauthored
[Communication] - SMS - Managed Identity Support for SMS Client (Azure#17867)
* Managed Identity for SMS * Adding readme env variables detail * Fix changelog * Adding live tests for managed identity SMS * Resolve merge conflicts * Remove extra connection string variable * Remove endpoint and parse from connection string * Updating tsts Co-authored-by: Minnie Liu <peiliu@microsoft.com>
1 parent 4297de2 commit 103028a

File tree

9 files changed

+169
-0
lines changed

9 files changed

+169
-0
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
### Added
66
- Added support to create SmsClient with AzureKeyCredential.
7+
- Support for creating SmsClient with TokenCredential
78

89

910
## 1.0.0-beta.3 (2020-11-16)

sdk/communication/Azure.Communication.Sms/README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,15 @@ string connectionString = "YOUR_CONNECTION_STRING"; // Find your Communication S
3636
SmsClient client = new SmsClient(connectionString);
3737
```
3838

39+
Alternatively, SMS clients can also be authenticated using a valid token credential. With this option,
40+
`AZURE_CLIENT_SECRET`, `AZURE_CLIENT_ID` and `AZURE_TENANT_ID` environment variables need to be set up for authentication.
41+
42+
```C# Snippet:Azure_Communication_Sms_Tests_Samples_CreateSmsClientWithToken
43+
string endpoint = "<endpoint_url>";
44+
TokenCredential tokenCredential = new DefaultAzureCredential();
45+
SmsClient client = new SmsClient(new Uri(endpoint), tokenCredential);
46+
```
47+
3948
## Examples
4049
### Send a SMS Message
4150
To send a SMS message, call the `Send` or `SendAsync` function from the `SmsClient`.

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ public partial class SmsClient
1515
protected SmsClient() { }
1616
public SmsClient(string connectionString, Azure.Communication.Sms.SmsClientOptions? options = null) { }
1717
public SmsClient(System.Uri endpoint, Azure.AzureKeyCredential keyCredential, Azure.Communication.Sms.SmsClientOptions? options = null) { }
18+
public SmsClient(System.Uri endpoint, Azure.Core.TokenCredential tokenCredential, Azure.Communication.Sms.SmsClientOptions? options = null) { }
1819
public virtual Azure.Response<Azure.Communication.Sms.SendSmsResponse> Send(Azure.Communication.PhoneNumberIdentifier from, Azure.Communication.PhoneNumberIdentifier to, string message, Azure.Communication.Sms.SendSmsOptions? sendSmsOptions = null, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
1920
public virtual Azure.Response<Azure.Communication.Sms.SendSmsResponse> Send(Azure.Communication.PhoneNumberIdentifier from, System.Collections.Generic.IEnumerable<Azure.Communication.PhoneNumberIdentifier> to, string message, Azure.Communication.Sms.SendSmsOptions? sendSmsOptions = null, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
2021
public virtual System.Threading.Tasks.Task<Azure.Response<Azure.Communication.Sms.SendSmsResponse>> SendAsync(Azure.Communication.PhoneNumberIdentifier from, Azure.Communication.PhoneNumberIdentifier to, string message, Azure.Communication.Sms.SendSmsOptions? sendSmsOptions = null, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }

sdk/communication/Azure.Communication.Sms/src/SmsClient.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,17 @@ public SmsClient(string connectionString, SmsClientOptions? options = default)
4040
ConnectionString.Parse(AssertNotNullOrEmpty(connectionString, nameof(connectionString))))
4141
{ }
4242

43+
/// <summary> Initializes a new instance of <see cref="SmsClient"/>.</summary>
44+
/// <param name="endpoint">The URI of the Azure Communication Services resource.</param>
45+
/// <param name="tokenCredential">The TokenCredential used to authenticate requests, such as DefaultAzureCredential.</param>
46+
/// <param name="options">Client option exposing <see cref="ClientOptions.Diagnostics"/>, <see cref="ClientOptions.Retry"/>, <see cref="ClientOptions.Transport"/>, etc.</param>
47+
public SmsClient(Uri endpoint, TokenCredential tokenCredential, SmsClientOptions? options = default)
48+
: this(
49+
endpoint,
50+
options ?? new SmsClientOptions(),
51+
tokenCredential)
52+
{ }
53+
4354
/// <summary>Initializes a new instance of <see cref="SmsClient"/> for mocking.</summary>
4455
protected SmsClient()
4556
{
@@ -60,6 +71,18 @@ private SmsClient(SmsClientOptions options, ConnectionString connectionString)
6071
endpointUrl: connectionString.GetRequired("endpoint"))
6172
{ }
6273

74+
private SmsClient(Uri endpoint, SmsClientOptions options, TokenCredential tokenCredential)
75+
{
76+
Argument.AssertNotNull(endpoint, nameof(endpoint));
77+
Argument.AssertNotNull(tokenCredential, nameof(tokenCredential));
78+
79+
_clientDiagnostics = new ClientDiagnostics(options);
80+
RestClient = new SmsRestClient(
81+
_clientDiagnostics,
82+
options.BuildHttpPipeline(tokenCredential),
83+
endpoint.AbsoluteUri);
84+
}
85+
6386
private SmsClient(Uri endpoint, SmsClientOptions options, AzureKeyCredential credential)
6487
{
6588
_clientDiagnostics = new ClientDiagnostics(options);

sdk/communication/Azure.Communication.Sms/tests/SessionRecords/SmsClientLiveTests/SendingAnSmsMessageUsingTokenCredential.json

Lines changed: 41 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.Sms/tests/SessionRecords/SmsClientLiveTests/SendingAnSmsMessageUsingTokenCredentialAsync.json

Lines changed: 41 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.Sms/tests/SmsClientLiveTests.cs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66
//@@ using Azure.Communication.Sms;
77
#endregion Snippet:Azure_Communication_Sms_Tests_UsingStatements
88
using System.Threading.Tasks;
9+
using Azure.Core;
910
using Azure.Core.TestFramework;
11+
using Azure.Identity;
1012
using NUnit.Framework;
1113

1214
namespace Azure.Communication.Sms.Tests
@@ -48,5 +50,42 @@ public async Task SendingAnSmsMessage()
4850
Assert.Fail($"Unexpected error: {ex}");
4951
}
5052
}
53+
54+
[Test]
55+
public async Task SendingAnSmsMessageUsingTokenCredential()
56+
{
57+
TokenCredential tokenCredential;
58+
if (Mode == RecordedTestMode.Playback)
59+
{
60+
tokenCredential = new MockCredential();
61+
}
62+
else
63+
{
64+
tokenCredential = new DefaultAzureCredential();
65+
}
66+
SmsClient client = InstrumentClient(
67+
new SmsClient(
68+
new Uri(ConnectionString.Parse(TestEnvironment.ConnectionString, allowEmptyValues: true).GetRequired("endpoint")),
69+
tokenCredential,
70+
InstrumentClientOptions(new SmsClientOptions())));
71+
72+
try
73+
{
74+
SendSmsResponse result = await client.SendAsync(
75+
from: new PhoneNumberIdentifier(TestEnvironment.PhoneNumber),
76+
to: new PhoneNumberIdentifier(TestEnvironment.PhoneNumber),
77+
message: "Hi");
78+
Console.WriteLine($"Sms id: {result.MessageId}");
79+
Assert.IsFalse(string.IsNullOrWhiteSpace(result.MessageId));
80+
}
81+
catch (RequestFailedException ex)
82+
{
83+
Console.WriteLine(ex.Message);
84+
}
85+
catch (Exception ex)
86+
{
87+
Assert.Fail($"Unexpected error: {ex}");
88+
}
89+
}
5190
}
5291
}

sdk/communication/Azure.Communication.Sms/tests/SmsClientTestEnvironment.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ public class SmsClientTestEnvironment : TestEnvironment
1111
public string ConnectionString => GetRecordedVariable(CommunicationRecordedTestSanitizer.ConnectionStringEnvironmentVariableName);
1212

1313
internal const string PhoneNumberEnvironmentVariableName = "AZURE_PHONE_NUMBER";
14+
1415
public string PhoneNumber => GetRecordedVariable(PhoneNumberEnvironmentVariableName);
1516
}
1617
}

sdk/communication/Azure.Communication.Sms/tests/samples/Sample1_SmsClient.cs

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

4+
using System;
5+
using Azure.Core;
6+
using Azure.Identity;
7+
48
namespace Azure.Communication.Sms.Tests.samples
59
{
610
/// <summary>
@@ -16,5 +20,14 @@ public SmsClient CreateSmsClient()
1620
#endregion Snippet:Azure_Communication_Sms_Tests_Samples_CreateSmsClient
1721
return client;
1822
}
23+
public SmsClient CreateSmsClientWithToken()
24+
{
25+
#region Snippet:Azure_Communication_Sms_Tests_Samples_CreateSmsClientWithToken
26+
string endpoint = "<endpoint_url>";
27+
TokenCredential tokenCredential = new DefaultAzureCredential();
28+
SmsClient client = new SmsClient(new Uri(endpoint), tokenCredential);
29+
#endregion Snippet:Azure_Communication_Sms_Tests_Samples_CreateSmsClientWithToken
30+
return client;
31+
}
1932
}
2033
}

0 commit comments

Comments
 (0)