Skip to content

Commit 1595039

Browse files
minnieliuMinnie Liu
andauthored
[Communication] - Identity - Redesign Fixes and Address Comments (Azure#18885)
* Address previous code review comments * Fix ubild * Update exception message * Address review comments * Update README Co-authored-by: Minnie Liu <peiliu@microsoft.com>
1 parent 14cd741 commit 1595039

File tree

14 files changed

+159
-160
lines changed

14 files changed

+159
-160
lines changed

sdk/communication/azure-communication-chat/src/test/java/com/azure/communication/chat/ChatAsyncClientTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ private void setupTest(HttpClient httpClient, String testName) {
5858
firstThreadMember = communicationClient.createUser();
5959
secondThreadMember = communicationClient.createUser();
6060

61-
List<CommunicationTokenScope> scopes = new ArrayList<CommunicationTokenScope>(Arrays.asList(CommunicationTokenScope.fromString("chat")));
61+
List<CommunicationTokenScope> scopes = Arrays.asList(CommunicationTokenScope.CHAT);
6262
AccessToken response = communicationClient.issueToken(firstThreadMember, scopes);
6363

6464
ChatClientBuilder chatBuilder = getChatClientBuilder(response.getToken(), httpClient);

sdk/communication/azure-communication-chat/src/test/java/com/azure/communication/chat/ChatClientTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ private void setupTest(HttpClient httpClient, String testName) {
5353
firstThreadMember = communicationClient.createUser();
5454
secondThreadMember = communicationClient.createUser();
5555

56-
List<CommunicationTokenScope> scopes = new ArrayList<CommunicationTokenScope>(Arrays.asList(CommunicationTokenScope.fromString("chat")));
56+
List<CommunicationTokenScope> scopes = Arrays.asList(CommunicationTokenScope.CHAT);
5757
AccessToken response = communicationClient.issueToken(firstThreadMember, scopes);
5858

5959
ChatClientBuilder chatBuilder = getChatClientBuilder(response.getToken(), httpClient);

sdk/communication/azure-communication-chat/src/test/java/com/azure/communication/chat/ChatThreadAsyncClientTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ private void setupTest(HttpClient httpClient, String testName) {
7171
firstThreadMember = communicationClient.createUser();
7272
secondThreadMember = communicationClient.createUser();
7373

74-
List<CommunicationTokenScope> scopes = new ArrayList<CommunicationTokenScope>(Arrays.asList(CommunicationTokenScope.fromString("chat")));
74+
List<CommunicationTokenScope> scopes = Arrays.asList(CommunicationTokenScope.CHAT);
7575
AccessToken response = communicationClient.issueToken(firstThreadMember, scopes);
7676

7777
ChatClientBuilder chatBuilder = getChatClientBuilder(response.getToken(), httpClient);

sdk/communication/azure-communication-chat/src/test/java/com/azure/communication/chat/ChatThreadClientTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ private void setupTest(HttpClient httpClient, String testName) {
6868
firstAddedThreadMember = communicationClient.createUser();
6969
secondAddedThreadMember = communicationClient.createUser();
7070

71-
List<CommunicationTokenScope> scopes = new ArrayList<CommunicationTokenScope>(Arrays.asList(CommunicationTokenScope.fromString("chat")));
71+
List<CommunicationTokenScope> scopes = Arrays.asList(CommunicationTokenScope.CHAT);
7272
AccessToken response = communicationClient.issueToken(firstThreadMember, scopes);
7373

7474
ChatClientBuilder chatBuilder = getChatClientBuilder(response.getToken(), httpClient);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
## 1.0.0-beta.4 (Unreleased)
44
### Added
5-
- Added CommunicationIdentityClient and CommunicationIdentityClientAsync (originally was part of the azure-communication-aministration package).
5+
- Added CommunicationIdentityClient and CommunicationIdentityAsyncClient (originally was part of the azure-communication-aministration package).
66
- Added support for Azure Active Directory Authentication.
77
- Added ability to create a user and issue token for it at the same time.
88

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

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@ The identity package is used for managing users and tokens for Azure Communicati
1010

1111
### Prerequisites
1212

13-
- An Azure account with an active subscription. [Create an account for free](https://azure.microsoft.com/free/?WT.mc_id=A261C142F).
13+
- An Azure subscription. [Create an account for free](https://azure.microsoft.com/free/?WT.mc_id=A261C142F).
1414
- [Java Development Kit (JDK)](https://docs.microsoft.com/java/azure/jdk/?view=azure-java-stable) version 8 or above.
1515
- [Apache Maven](https://maven.apache.org/download.cgi).
16-
- A deployed Communication Services resource. You can use the [Azure Portal](https://docs.microsoft.com/azure/communication-services/quickstarts/create-communication-resource?tabs=windows&pivots=platform-azp) or the [Azure PowerShell](https://docs.microsoft.com/powershell/module/az.communication/new-azcommunicationservice) to set it up.
16+
- A Communication Services resource. You can use the [Azure Portal](https://docs.microsoft.com/azure/communication-services/quickstarts/create-communication-resource?tabs=windows&pivots=platform-azp) or the [Azure PowerShell](https://docs.microsoft.com/powershell/module/az.communication/new-azcommunicationservice) to set it up.
1717

1818
### Include the package
1919

@@ -26,7 +26,7 @@ The identity package is used for managing users and tokens for Azure Communicati
2626
</dependency>
2727
```
2828

29-
## Key concepts
29+
## Authenticate the client
3030

3131
There are two forms of authentication to use the Identity SDK:
3232

@@ -82,6 +82,9 @@ CommunicationIdentityClient communicationIdentityClient = new CommunicationIdent
8282
.buildClient();
8383
```
8484

85+
## Key concepts
86+
`CommunicationIdentityClient` and `CommunicationIdentityAsyncClient` provide the functionalities to manage users and user tokens.
87+
8588
## Examples
8689

8790
### Creating a new user
@@ -113,7 +116,7 @@ also takes in a list of `CommunicationIdentityTokenScope`. Scope options include
113116
- `chat` (Chat)
114117
- `voip` (Voice over IP)
115118

116-
<!-- embedme ./src/samples/java/com/azure/communication/identity/ReadmeSamples.java#L120-L125 -->
119+
<!-- embedme ./src/samples/java/com/azure/communication/identity/ReadmeSamples.java#L119-L124 -->
117120
```java
118121
List<CommunicationTokenScope> scopes =
119122
new ArrayList<>(Arrays.asList(CommunicationTokenScope.CHAT));
@@ -126,7 +129,7 @@ System.out.println("Expires at: " + userToken.getExpiresAt());
126129
### Revoking all tokens for an existing user
127130
Use the `revokeTokens` function to revoke all the issued tokens of a user.
128131

129-
<!-- embedme ./src/samples/java/com/azure/communication/identity/ReadmeSamples.java#L139-L140 -->
132+
<!-- embedme ./src/samples/java/com/azure/communication/identity/ReadmeSamples.java#L137-L138 -->
130133
```java
131134
// revoke tokens issued for the specified user
132135
communicationIdentityClient.revokeTokens(user);
@@ -135,37 +138,44 @@ communicationIdentityClient.revokeTokens(user);
135138
### Deleting a user
136139
Use the `deleteUser` function to delete a user.
137140

138-
<!-- embedme ./src/samples/java/com/azure/communication/identity/ReadmeSamples.java#L149-L150 -->
141+
<!-- embedme ./src/samples/java/com/azure/communication/identity/ReadmeSamples.java#L147-L148 -->
139142
```java
140143
// delete a previously created user
141144
communicationIdentityClient.deleteUser(user);
142145
```
143146

144-
## Contributing
145-
146-
This project welcomes contributions and suggestions. Most contributions require you to agree to a [Contributor License Agreement (CLA)][cla] declaring that you have the right to, and actually do, grant us the rights to use your contribution.
147+
## Troubleshooting
147148

148-
When you submit a pull request, a CLA-bot will automatically determine whether you need to provide a CLA and decorate the PR appropriately (e.g., label, comment). Simply follow the instructions provided by the bot. You will only need to do this once across all repos using our CLA.
149+
All user token service operations will throw an exception on failure.
150+
<!-- embedme ./src/samples/java/com/azure/communication/identity/ReadmeSamples.java#L156-L160 -->
151+
```java
152+
try {
153+
CommunicationUserIdentifier user = communicationIdentityClient.createUser();
154+
} catch (RuntimeException ex) {
155+
System.out.println(ex.getMessage());
156+
}
157+
```
149158

150-
This project has adopted the [Microsoft Open Source Code of Conduct][coc]. For more information see the [Code of Conduct FAQ][coc_faq] or contact [opencode@microsoft.com][coc_contact] with any additional questions or comments.
159+
## Next steps
151160

161+
Check out other client libraries for Azure communication service
152162

153-
## Troubleshooting
163+
## Contributing
154164

155-
In progress.
165+
This project welcomes contributions and suggestions. Most contributions require you to agree to a [Contributor License Agreement (CLA)][cla] declaring that you have the right to, and actually do, grant us the rights to use your contribution.
156166

157-
## Next steps
167+
When you submit a pull request, a CLA-bot will automatically determine whether you need to provide a CLA and decorate the PR appropriately (e.g., label, comment). Simply follow the instructions provided by the bot. You will only need to do this once across all repos using our CLA.
158168

159-
Check out other client libraries for Azure communication service
169+
This project has adopted the [Microsoft Open Source Code of Conduct][coc]. For more information see the [Code of Conduct FAQ][coc_faq] or contact [opencode@microsoft.com][coc_contact] with any additional questions or comments.
160170

161171
<!-- LINKS -->
162172
[cla]: https://cla.microsoft.com
163173
[coc]: https://opensource.microsoft.com/codeofconduct/
164174
[coc_faq]: https://opensource.microsoft.com/codeofconduct/faq/
165175
[coc_contact]: mailto:opencode@microsoft.com
166176
[product_docs]: https://docs.microsoft.com/azure/communication-services/
167-
[package]: https://search.maven.org/artifact/com.azure/azure-communication-administration
177+
[package]: https://search.maven.org/artifact/com.azure/azure-communication-identity
168178
[api_documentation]: https://aka.ms/java-docs
169-
[source]: https://github.com/Azure/azure-sdk-for-java/tree/master/sdk/communication/azure-communication-administration/src
179+
[source]: https://github.com/Azure/azure-sdk-for-java/tree/master/sdk/communication/azure-communication-identity/src
170180

171-
![Impressions](https://azure-sdk-impressions.azurewebsites.net/api/impressions/azure-sdk-for-java%2Fsdk%2Fcommunication%2Fazure-communication-administration%2FREADME.png)
181+
![Impressions](https://azure-sdk-impressions.azurewebsites.net/api/impressions/azure-sdk-for-java%2Fsdk%2Fcommunication%2Fazure-communication-identity%2FREADME.png)

sdk/communication/azure-communication-identity/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
<packaging>jar</packaging>
1919
<version>1.0.0-beta.4</version> <!-- {x-version-update;com.azure:azure-communication-identity;current} -->
2020

21-
<name>Microsoft Azure client identity library for communication</name>
21+
<name>Microsoft Azure identity client library for communication</name>
2222
<description>
2323
This package contains APIs for application identity in Microsoft Azure Communication Services.
2424
For this release, see notes - https://github.com/Azure/azure-sdk-for-java/blob/master/sdk/communication/azure-communication-identity/README.md and https://github.com/Azure/azure-sdk-for-java/blob/master/sdk/communication/azure-communication-identity/CHANGELOG.md.

sdk/communication/azure-communication-identity/src/main/java/com/azure/communication/identity/CommunicationIdentityAsyncClient.java

Lines changed: 41 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222

2323
import java.util.List;
2424
import java.util.Objects;
25+
import java.util.stream.Collectors;
26+
import java.util.stream.StreamSupport;
2527

2628
import reactor.core.publisher.Mono;
2729

@@ -52,10 +54,7 @@ public Mono<CommunicationUserIdentifier> createUser() {
5254
return client.createAsync(new CommunicationIdentityCreateRequest())
5355
.flatMap(
5456
(CommunicationIdentityAccessTokenResult result) -> {
55-
if (result.getIdentity() != null) {
56-
return Mono.just(new CommunicationUserIdentifier(result.getIdentity().getId()));
57-
}
58-
return Mono.empty();
57+
return Mono.just(new CommunicationUserIdentifier(result.getIdentity().getId()));
5958
});
6059
} catch (RuntimeException ex) {
6160
return monoError(logger, ex);
@@ -73,14 +72,11 @@ public Mono<Response<CommunicationUserIdentifier>> createUserWithResponse() {
7372
return client.createWithResponseAsync(new CommunicationIdentityCreateRequest())
7473
.flatMap(
7574
(Response<CommunicationIdentityAccessTokenResult> response) -> {
76-
if (response != null && response.getValue() != null && response.getValue().getIdentity() != null) {
77-
String id = response.getValue().getIdentity().getId();
78-
return Mono.just(
79-
new SimpleResponse<CommunicationUserIdentifier>(
80-
response,
81-
new CommunicationUserIdentifier(id)));
82-
}
83-
return Mono.empty();
75+
String id = response.getValue().getIdentity().getId();
76+
return Mono.just(
77+
new SimpleResponse<CommunicationUserIdentifier>(
78+
response,
79+
new CommunicationUserIdentifier(id)));
8480
});
8581
} catch (RuntimeException ex) {
8682
return monoError(logger, ex);
@@ -95,21 +91,14 @@ public Mono<Response<CommunicationUserIdentifier>> createUserWithResponse() {
9591
*/
9692
@ServiceMethod(returns = ReturnType.SINGLE)
9793
public Mono<CommunicationUserIdentifierWithTokenResult>
98-
createUserWithToken(List<CommunicationTokenScope> scopes) {
94+
createUserWithToken(Iterable<CommunicationTokenScope> scopes) {
9995
try {
10096
Objects.requireNonNull(scopes);
101-
return client.createAsync(new CommunicationIdentityCreateRequest().setCreateTokenWithScopes(scopes))
97+
final List<CommunicationTokenScope> scopesInput = StreamSupport.stream(scopes.spliterator(), false).collect(Collectors.toList());
98+
return client.createAsync(new CommunicationIdentityCreateRequest().setCreateTokenWithScopes(scopesInput))
10299
.flatMap(
103100
(CommunicationIdentityAccessTokenResult result) -> {
104-
if (result != null && result.getIdentity() != null && result.getAccessToken() != null) {
105-
CommunicationUserIdentifier user =
106-
new CommunicationUserIdentifier(result.getIdentity().getId());
107-
AccessToken token =
108-
new AccessToken(result.getAccessToken().getToken(), result.getAccessToken().getExpiresOn());
109-
return Mono.just(
110-
new CommunicationUserIdentifierWithTokenResult(user, token));
111-
}
112-
return Mono.empty();
101+
return Mono.just(userWithAccessTokenResultConverter(result));
113102
});
114103
} catch (RuntimeException ex) {
115104
return monoError(logger, ex);
@@ -124,23 +113,16 @@ public Mono<Response<CommunicationUserIdentifier>> createUserWithResponse() {
124113
*/
125114
@ServiceMethod(returns = ReturnType.SINGLE)
126115
public Mono<Response<CommunicationUserIdentifierWithTokenResult>>
127-
createUserWithTokenWithResponse(List<CommunicationTokenScope> scopes) {
116+
createUserWithTokenWithResponse(Iterable<CommunicationTokenScope> scopes) {
128117
try {
129118
Objects.requireNonNull(scopes);
119+
final List<CommunicationTokenScope> scopesInput = StreamSupport.stream(scopes.spliterator(), false).collect(Collectors.toList());
130120
return client.createWithResponseAsync(
131-
new CommunicationIdentityCreateRequest().setCreateTokenWithScopes(scopes))
121+
new CommunicationIdentityCreateRequest().setCreateTokenWithScopes(scopesInput))
132122
.flatMap(
133123
(Response<CommunicationIdentityAccessTokenResult> response) -> {
134-
if (response != null && response.getValue() != null
135-
&& response.getValue().getIdentity() != null && response.getValue().getAccessToken() != null) {
136-
String id = response.getValue().getIdentity().getId();
137-
CommunicationUserIdentifier user = new CommunicationUserIdentifier(id);
138-
CommunicationIdentityAccessToken rawToken = response.getValue().getAccessToken();
139-
AccessToken token = new AccessToken(rawToken.getToken(), rawToken.getExpiresOn());
140-
return Mono.just(new SimpleResponse<CommunicationUserIdentifierWithTokenResult>(response,
141-
new CommunicationUserIdentifierWithTokenResult(user, token)));
142-
}
143-
return Mono.empty();
124+
return Mono.just(new SimpleResponse<CommunicationUserIdentifierWithTokenResult>(response,
125+
userWithAccessTokenResultConverter(response.getValue())));
144126
});
145127
} catch (RuntimeException ex) {
146128
return monoError(logger, ex);
@@ -222,12 +204,13 @@ public Mono<Response<Void>> revokeTokensWithResponse(CommunicationUserIdentifier
222204
*/
223205
@ServiceMethod(returns = ReturnType.SINGLE)
224206
public Mono<AccessToken> issueToken(CommunicationUserIdentifier communicationUser,
225-
List<CommunicationTokenScope> scopes) {
207+
Iterable<CommunicationTokenScope> scopes) {
226208
try {
227209
Objects.requireNonNull(communicationUser);
228210
Objects.requireNonNull(scopes);
211+
final List<CommunicationTokenScope> scopesInput = StreamSupport.stream(scopes.spliterator(), false).collect(Collectors.toList());
229212
return client.issueAccessTokenAsync(communicationUser.getId(),
230-
new CommunicationIdentityAccessTokenRequest().setScopes(scopes))
213+
new CommunicationIdentityAccessTokenRequest().setScopes(scopesInput))
231214
.flatMap((CommunicationIdentityAccessToken rawToken) -> {
232215
return Mono.just(new AccessToken(rawToken.getToken(), rawToken.getExpiresOn()));
233216
});
@@ -245,21 +228,35 @@ public Mono<AccessToken> issueToken(CommunicationUserIdentifier communicationUse
245228
*/
246229
@ServiceMethod(returns = ReturnType.SINGLE)
247230
public Mono<Response<AccessToken>> issueTokenWithResponse(CommunicationUserIdentifier communicationUser,
248-
List<CommunicationTokenScope> scopes) {
231+
Iterable<CommunicationTokenScope> scopes) {
249232
try {
250233
Objects.requireNonNull(communicationUser);
251234
Objects.requireNonNull(scopes);
235+
final List<CommunicationTokenScope> scopesInput = StreamSupport.stream(scopes.spliterator(), false).collect(Collectors.toList());
252236
return client.issueAccessTokenWithResponseAsync(communicationUser.getId(),
253-
new CommunicationIdentityAccessTokenRequest().setScopes(scopes))
237+
new CommunicationIdentityAccessTokenRequest().setScopes(scopesInput))
254238
.flatMap((Response<CommunicationIdentityAccessToken> response) -> {
255-
if (response != null && response.getValue() != null) {
256-
AccessToken token = new AccessToken(response.getValue().getToken(), response.getValue().getExpiresOn());
257-
return Mono.just(new SimpleResponse<AccessToken>(response, token));
258-
}
259-
return Mono.empty();
239+
AccessToken token = new AccessToken(response.getValue().getToken(), response.getValue().getExpiresOn());
240+
return Mono.just(new SimpleResponse<AccessToken>(response, token));
260241
});
261242
} catch (RuntimeException ex) {
262243
return monoError(logger, ex);
263244
}
264245
}
246+
247+
/**
248+
* Converts CommunicationIdentityAccessTokenResult to CommunicationUserIdentifierWithTokenResult
249+
*
250+
* @param identityAccessTokenResult The result input.
251+
* @return the result converted to CommunicationUserIdentifierWithTokenResult type
252+
*/
253+
private CommunicationUserIdentifierWithTokenResult userWithAccessTokenResultConverter(
254+
CommunicationIdentityAccessTokenResult identityAccessTokenResult) {
255+
CommunicationUserIdentifier user =
256+
new CommunicationUserIdentifier(identityAccessTokenResult.getIdentity().getId());
257+
AccessToken token = new AccessToken(
258+
identityAccessTokenResult.getAccessToken().getToken(),
259+
identityAccessTokenResult.getAccessToken().getExpiresOn());
260+
return new CommunicationUserIdentifierWithTokenResult(user, token);
261+
}
265262
}

0 commit comments

Comments
 (0)