Skip to content

Commit 211dbd4

Browse files
melneubertMelissa Neubert
andauthored
Add support for TokenCredential auth in CallingServerClientBuilder (Azure#23831)
* Add support for TokenCredential auth in CallingServerClientBuilder * Changelog update Co-authored-by: Melissa Neubert <[email protected]>
1 parent ec046fa commit 211dbd4

File tree

25 files changed

+1290
-513
lines changed

25 files changed

+1290
-513
lines changed

sdk/communication/azure-communication-callingserver/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Release History
22

3+
## 1.0.0-beta.6 (Unreleased)
4+
### Features Added
5+
- Add support for TokenCredential authentication with CallingServerClientBuilder.
6+
37
## 1.0.0-beta.5 (2021-08-30)
48
### Dependency updates
59
- Upgraded `azure-communication-common` to 1.0.4

sdk/communication/azure-communication-callingserver/README.md

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ Based on if the Contoso app join a call or not, APIs can be divided into two cat
4646

4747

4848
You can provide the connection string using the connectionString() function of `CallingServerClientBuilder`. Once you initialized a `CallingServerClient` class, you can do the different server calling operations.
49-
<!-- embedme src/samples/java/com/azure/communication/callingserver/ReadmeSamples.java#L31-L37 -->
49+
<!-- embedme src/samples/java/com/azure/communication/callingserver/ReadmeSamples.java#L33-L39 -->
5050
```java
5151
// Your connectionString retrieved from your Azure Communication Service
5252
String connectionString = "endpoint=https://<resource-name>.communication.azure.com/;accesskey=<access-key>";
@@ -57,10 +57,28 @@ builder.connectionString(connectionString);
5757
CallingServerClient callingServerClient = builder.buildClient();
5858
```
5959

60+
Alternatively, calling clients can also be authenticated using a valid token credential. With this option,
61+
`AZURE_CLIENT_SECRET`, `AZURE_CLIENT_ID` and `AZURE_TENANT_ID` environment variables need to be set up for authentication.
62+
<!-- embedme src/samples/java/com/azure/communication/callingserver/ReadmeSample.java#L117-128 -->
63+
```java
64+
// Your endpoint retrieved from your Azure Communication Service
65+
String endpoint = "https://<resource-name>.communication.azure.com";
66+
67+
// Token credential used for managed identity authentication. Depends on `AZURE_CLIENT_SECRET`,
68+
// `AZURE_CLIENT_ID`, and `AZURE_TENANT_ID` environment variables to be set up.
69+
TokenCredential tokenCredential = new DefaultAzureCredentialBuilder().build();
70+
71+
// Initialize the calling server client
72+
CallingServerClient callingServerClient = new CallingServerClientBuilder()
73+
.endpoint(endpoint)
74+
.credential(tokenCredential)
75+
.buildClient();
76+
```
77+
6078
### Create call, Add participant and Hangup a call
6179

6280
#### Create a Call:
63-
<!-- embedme src/samples/java/com/azure/communication/callingserver/ReadmeSamples.java#L49-L68 -->
81+
<!-- embedme src/samples/java/com/azure/communication/callingserver/ReadmeSamples.java#L51-L70 -->
6482
```java
6583
CommunicationIdentifier source = new CommunicationUserIdentifier("<acs-user-identity>");
6684
CommunicationIdentifier firstCallee = new CommunicationUserIdentifier("<acs-user-identity-1>");
@@ -85,22 +103,22 @@ CallConnection callConnection = callingServerClient.createCallConnection(source,
85103
```
86104

87105
#### Add a participant to a Call:
88-
<!-- embedme src/samples/java/com/azure/communication/callingserver/ReadmeSamples.java#L88-L89 -->
106+
<!-- embedme src/samples/java/com/azure/communication/callingserver/ReadmeSamples.java#L90-L91 -->
89107
```java
90108
CommunicationIdentifier thirdCallee = new CommunicationUserIdentifier("<acs-user-identity-3>");
91109
callConnection.addParticipant(thirdCallee, "ACS User 3", "<string-for-tracing-responses>");
92110
```
93111

94112
#### Hangup a Call:
95-
<!-- embedme src/samples/java/com/azure/communication/callingserver/ReadmeSamples.java#L78-L78 -->
113+
<!-- embedme src/samples/java/com/azure/communication/callingserver/ReadmeSamples.java#L80-L80 -->
96114
```java
97115
callConnection.hangup();
98116
```
99117

100118
### Start, Pause, Resume, Stop and Get a recording
101119

102120
#### Start a Recording:
103-
<!-- embedme src/samples/java/com/azure/communication/callingserver/ConversationClientReadmeSamples.java#L47-L51 -->
121+
<!-- embedme src/samples/java/com/azure/communication/callingserver/ConversationClientReadmeSamples.java#L49-L53 -->
104122
```java
105123
String serverCallId = "<serverCallId received from starting call>";
106124
String recordingStateCallbackUri = "<webhook endpoint to which calling service can report status>";
@@ -110,31 +128,31 @@ String recordingId = response.getRecordingId();
110128
```
111129

112130
#### Pause a Recording:
113-
<!-- embedme src/samples/java/com/azure/communication/callingserver/ConversationClientReadmeSamples.java#L65-L65 -->
131+
<!-- embedme src/samples/java/com/azure/communication/callingserver/ConversationClientReadmeSamples.java#L67-L67 -->
114132
```java
115133
serverCall.pauseRecording(recordingId);
116134
```
117135

118136
#### Resume a Recording:
119-
<!-- embedme src/samples/java/com/azure/communication/callingserver/ConversationClientReadmeSamples.java#L78-L78 -->
137+
<!-- embedme src/samples/java/com/azure/communication/callingserver/ConversationClientReadmeSamples.java#L80-L80 -->
120138
```java
121139
serverCall.resumeRecording(recordingId);
122140
```
123141

124142
#### Stop a Recording:
125-
<!-- embedme src/samples/java/com/azure/communication/callingserver/ConversationClientReadmeSamples.java#L91-L91 -->
143+
<!-- embedme src/samples/java/com/azure/communication/callingserver/ConversationClientReadmeSamples.java#L93-L93 -->
126144
```java
127145
serverCall.stopRecording(recordingId);
128146
```
129147

130148
#### Get the Recording State:
131-
<!-- embedme src/samples/java/com/azure/communication/callingserver/ConversationClientReadmeSamples.java#L105-L105 -->
149+
<!-- embedme src/samples/java/com/azure/communication/callingserver/ConversationClientReadmeSamples.java#L107-L107 -->
132150
```java
133151
CallRecordingStateResult callRecordingStateResult = serverCall.getRecordingState(recordingId);
134152
```
135153

136154
#### Download a Recording into a file:
137-
<!-- embedme src/samples/java/com/azure/communication/callingserver/ReadmeSamples.java#L100-L100 -->
155+
<!-- embedme src/samples/java/com/azure/communication/callingserver/ReadmeSamples.java#L102-L102 -->
138156
```java
139157
callingServerClient.downloadTo(
140158
recordingUrl,
@@ -146,7 +164,7 @@ callingServerClient.downloadTo(
146164
### Play Audio in Call
147165

148166
#### Play Audio:
149-
<!-- embedme src/samples/java/com/azure/communication/callingserver/ConversationClientReadmeSamples.java#L122-L127 -->
167+
<!-- embedme src/samples/java/com/azure/communication/callingserver/ConversationClientReadmeSamples.java#L124-L129 -->
150168
```java
151169
String audioFileUri = "<uri of the file to play>";
152170
String audioFileId = "<a name to use for caching the audio file>";

sdk/communication/azure-communication-callingserver/src/main/java/com/azure/communication/callingserver/CallingServerClientBuilder.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ public CallingServerClientBuilder pipeline(HttpPipeline pipeline) {
9191
* @return Updated {@link CallingServerClientBuilder} object.
9292
* @throws NullPointerException If {@code tokenCredential} is null.
9393
*/
94-
CallingServerClientBuilder credential(TokenCredential tokenCredential) {
94+
public CallingServerClientBuilder credential(TokenCredential tokenCredential) {
9595
this.tokenCredential = Objects.requireNonNull(tokenCredential, "'tokenCredential' cannot be null.");
9696
return this;
9797
}

sdk/communication/azure-communication-callingserver/src/samples/java/com/azure/communication/callingserver/ReadmeSamples.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
import com.azure.communication.callingserver.models.MediaType;
99
import com.azure.communication.common.CommunicationIdentifier;
1010
import com.azure.communication.common.CommunicationUserIdentifier;
11+
import com.azure.core.credential.TokenCredential;
12+
import com.azure.identity.DefaultAzureCredentialBuilder;
1113

1214
import java.nio.file.Paths;
1315
import java.util.Arrays;
@@ -104,4 +106,26 @@ public void getRecordingStream() {
104106
true
105107
);
106108
}
109+
110+
/**
111+
* Sample code for creating async calling server client with token credential.
112+
*
113+
* @return the calling server client.
114+
*/
115+
public CallingServerClient createCallingServerClientWithTokenCredential() {
116+
// Your endpoint retrieved from your Azure Communication Service
117+
String endpoint = "https://<resource-name>.communication.azure.com";
118+
119+
// Token credential used for managed identity authentication. Depends on `AZURE_CLIENT_SECRET`,
120+
// `AZURE_CLIENT_ID`, and `AZURE_TENANT_ID` environment variables to be set up.
121+
TokenCredential tokenCredential = new DefaultAzureCredentialBuilder().build();
122+
123+
// Initialize the calling server client
124+
CallingServerClient callingServerClient = new CallingServerClientBuilder()
125+
.endpoint(endpoint)
126+
.credential(tokenCredential)
127+
.buildClient();
128+
129+
return callingServerClient;
130+
}
107131
}

sdk/communication/azure-communication-callingserver/src/test/java/com/azure/communication/callingserver/CallConnectionAsyncLiveTests.java

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public class CallConnectionAsyncLiveTests extends CallingServerTestBase {
3434
matches = "(?i)(true)",
3535
disabledReason = "Requires human intervention")
3636
public void runCreatePlayCancelHangupScenarioAsync(HttpClient httpClient) {
37-
CallingServerClientBuilder builder = getCallClientUsingConnectionString(httpClient);
37+
CallingServerClientBuilder builder = getCallingServerClientUsingConnectionString(httpClient);
3838
CallingServerAsyncClient callingServerAsyncClient =
3939
setupAsyncClient(builder, "runCreatePlayCancelHangupScenarioAsync");
4040

@@ -86,7 +86,7 @@ public void runCreatePlayCancelHangupScenarioAsync(HttpClient httpClient) {
8686
matches = "(?i)(true)",
8787
disabledReason = "Requires human intervention")
8888
public void runCreatePlayCancelHangupScenarioWithResponseAsync(HttpClient httpClient) {
89-
CallingServerClientBuilder builder = getCallClientUsingConnectionString(httpClient);
89+
CallingServerClientBuilder builder = getCallingServerClientUsingConnectionString(httpClient);
9090
CallingServerAsyncClient callingServerAsyncClient =
9191
setupAsyncClient(builder, "runCreatePlayCancelHangupScenarioWithResponseAsync");
9292

@@ -143,7 +143,7 @@ public void runCreatePlayCancelHangupScenarioWithResponseAsync(HttpClient httpCl
143143
matches = "(?i)(true)",
144144
disabledReason = "Requires human intervention")
145145
public void runCreateAddRemoveHangupScenarioAsync(HttpClient httpClient) {
146-
CallingServerClientBuilder builder = getCallClientUsingConnectionString(httpClient);
146+
CallingServerClientBuilder builder = getCallingServerClientUsingConnectionString(httpClient);
147147
CallingServerAsyncClient callingServerAsyncClient =
148148
setupAsyncClient(builder, "runCreateAddRemoveHangupScenarioAsync");
149149

@@ -190,7 +190,7 @@ public void runCreateAddRemoveHangupScenarioAsync(HttpClient httpClient) {
190190
matches = "(?i)(true)",
191191
disabledReason = "Requires human intervention")
192192
public void runCreateAddRemoveHangupScenarioWithResponseAsync(HttpClient httpClient) {
193-
CallingServerClientBuilder builder = getCallClientUsingConnectionString(httpClient);
193+
CallingServerClientBuilder builder = getCallingServerClientUsingConnectionString(httpClient);
194194
CallingServerAsyncClient callingServerAsyncClient =
195195
setupAsyncClient(builder, "runCreateAddRemoveHangupScenarioWithResponseAsync");
196196

@@ -243,11 +243,27 @@ public void runCreateAddRemoveHangupScenarioWithResponseAsync(HttpClient httpCli
243243
named = "SKIP_LIVE_TEST",
244244
matches = "(?i)(true)",
245245
disabledReason = "Requires human intervention")
246-
public void runCreateJoinHangupScenarioAsync(HttpClient httpClient) {
247-
CallingServerClientBuilder builder = getCallClientUsingConnectionString(httpClient);
246+
public void runCreateJoinHangupScenarioWithConnectionStringAsyncClient(HttpClient httpClient) {
247+
CallingServerClientBuilder builder = getCallingServerClientUsingConnectionString(httpClient);
248248
CallingServerAsyncClient callingServerAsyncClient =
249-
setupAsyncClient(builder, "runCreateJoinHangupScenarioAsync");
249+
setupAsyncClient(builder, "runCreateJoinHangupScenarioWithConnectionStringAsyncClient");
250+
runCreateJoinHangupScenarioAsync(callingServerAsyncClient);
251+
}
252+
253+
@ParameterizedTest
254+
@MethodSource("com.azure.core.test.TestBase#getHttpClients")
255+
@DisabledIfEnvironmentVariable(
256+
named = "SKIP_LIVE_TEST",
257+
matches = "(?i)(true)",
258+
disabledReason = "Requires human intervention")
259+
public void runCreateJoinHangupScenarioWithTokenCredentialAsyncClient(HttpClient httpClient) {
260+
CallingServerClientBuilder builder = getCallingServerClientUsingTokenCredential(httpClient);
261+
CallingServerAsyncClient callingServerAsyncClient =
262+
setupAsyncClient(builder, "runCreateJoinHangupScenarioWithTokenCredentialAsyncClient");
263+
runCreateJoinHangupScenarioAsync(callingServerAsyncClient);
264+
}
250265

266+
private void runCreateJoinHangupScenarioAsync(CallingServerAsyncClient callingServerAsyncClient) {
251267
try {
252268
// Establish a call
253269
CreateCallOptions options = new CreateCallOptions(
@@ -299,7 +315,7 @@ public void runCreateJoinHangupScenarioAsync(HttpClient httpClient) {
299315
matches = "(?i)(true)",
300316
disabledReason = "Requires human intervention")
301317
public void runCreateJoinHangupScenarioWithResponseAsync(HttpClient httpClient) {
302-
CallingServerClientBuilder builder = getCallClientUsingConnectionString(httpClient);
318+
CallingServerClientBuilder builder = getCallingServerClientUsingConnectionString(httpClient);
303319
CallingServerAsyncClient callingServerAsyncClient =
304320
setupAsyncClient(builder, "runCreateJoinHangupScenarioWithResponseAsync");
305321

sdk/communication/azure-communication-callingserver/src/test/java/com/azure/communication/callingserver/CallConnectionLiveTests.java

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public class CallConnectionLiveTests extends CallingServerTestBase {
3333
matches = "(?i)(true)",
3434
disabledReason = "Requires human intervention")
3535
public void runCreatePlayCancelHangupScenario(HttpClient httpClient) {
36-
CallingServerClientBuilder builder = getCallClientUsingConnectionString(httpClient);
36+
CallingServerClientBuilder builder = getCallingServerClientUsingConnectionString(httpClient);
3737
CallingServerClient callingServerClient = setupClient(builder, "runCreatePlayCancelHangupScenario");
3838

3939
try {
@@ -83,7 +83,7 @@ public void runCreatePlayCancelHangupScenario(HttpClient httpClient) {
8383
matches = "(?i)(true)",
8484
disabledReason = "Requires human intervention")
8585
public void runCreatePlayCancelHangupScenarioWithResponse(HttpClient httpClient) {
86-
CallingServerClientBuilder builder = getCallClientUsingConnectionString(httpClient);
86+
CallingServerClientBuilder builder = getCallingServerClientUsingConnectionString(httpClient);
8787
CallingServerClient callingServerClient =
8888
setupClient(builder, "runCreatePlayCancelHangupScenarioWithResponse");
8989

@@ -140,7 +140,7 @@ public void runCreatePlayCancelHangupScenarioWithResponse(HttpClient httpClient)
140140
matches = "(?i)(true)",
141141
disabledReason = "Requires human intervention")
142142
public void runCreateAddRemoveHangupScenario(HttpClient httpClient) {
143-
CallingServerClientBuilder builder = getCallClientUsingConnectionString(httpClient);
143+
CallingServerClientBuilder builder = getCallingServerClientUsingConnectionString(httpClient);
144144
CallingServerClient callingServerClient = setupClient(builder, "runCreateAddRemoveHangupScenario");
145145

146146
try {
@@ -181,7 +181,7 @@ public void runCreateAddRemoveHangupScenario(HttpClient httpClient) {
181181
matches = "(?i)(true)",
182182
disabledReason = "Requires human intervention")
183183
public void runCreateAddRemoveHangupScenarioWithResponse(HttpClient httpClient) {
184-
CallingServerClientBuilder builder = getCallClientUsingConnectionString(httpClient);
184+
CallingServerClientBuilder builder = getCallingServerClientUsingConnectionString(httpClient);
185185
CallingServerClient callingServerClient =
186186
setupClient(builder, "runCreateAddRemoveHangupScenarioWithResponse");
187187

@@ -234,9 +234,25 @@ public void runCreateAddRemoveHangupScenarioWithResponse(HttpClient httpClient)
234234
named = "SKIP_LIVE_TEST",
235235
matches = "(?i)(true)",
236236
disabledReason = "Requires human intervention")
237-
public void runCreateJoinHangupScenario(HttpClient httpClient) {
238-
CallingServerClientBuilder builder = getCallClientUsingConnectionString(httpClient);
239-
CallingServerClient callingServerClient = setupClient(builder, "runCreateJoinHangupScenario");
237+
public void runCreateJoinHangupScenarioWithConnectionStringClient(HttpClient httpClient) {
238+
CallingServerClientBuilder builder = getCallingServerClientUsingConnectionString(httpClient);
239+
CallingServerClient callingServerClient = setupClient(builder, "runCreateJoinHangupScenarioWithConnectionStringClient");
240+
runCreateJoinHangupScenario(callingServerClient);
241+
}
242+
243+
@ParameterizedTest
244+
@MethodSource("com.azure.core.test.TestBase#getHttpClients")
245+
@DisabledIfEnvironmentVariable(
246+
named = "SKIP_LIVE_TEST",
247+
matches = "(?i)(true)",
248+
disabledReason = "Requires human intervention")
249+
public void runCreateJoinHangupScenarioWithTokenCredentialClient(HttpClient httpClient) {
250+
CallingServerClientBuilder builder = getCallingServerClientUsingTokenCredential(httpClient);
251+
CallingServerClient callingServerClient = setupClient(builder, "runCreateJoinHangupScenarioWithTokenCredentialClient");
252+
runCreateJoinHangupScenario(callingServerClient);
253+
}
254+
255+
private void runCreateJoinHangupScenario(CallingServerClient callingServerClient) {
240256

241257
try {
242258
// Establish a call
@@ -284,7 +300,7 @@ public void runCreateJoinHangupScenario(HttpClient httpClient) {
284300
matches = "(?i)(true)",
285301
disabledReason = "Requires human intervention")
286302
public void runCreateJoinHangupScenarioWithResponse(HttpClient httpClient) {
287-
CallingServerClientBuilder builder = getCallClientUsingConnectionString(httpClient);
303+
CallingServerClientBuilder builder = getCallingServerClientUsingConnectionString(httpClient);
288304
CallingServerClient callingServerClient =
289305
setupClient(builder, "runCreateJoinHangupScenarioWithResponse");
290306

0 commit comments

Comments
 (0)