Skip to content

Commit

Permalink
Latest API(2021-10-31-preview) changes implementation in azure-commun…
Browse files Browse the repository at this point in the history
…ication-identity (#24574)

* exchange teams token added to the client classes

* exchange teams token live tests are added for sync&async clients

* README, CHANGELOG, and samples are updated

* Fixed Checkstyle violations

* http recordings are updated

* azure-core version fix

* decreased branchcoverage value

* PR comments fix

* live tests recordings are updated for 1.2.0-beta.1 version

* new V2021_10_31_PREVIEW version added, regenerated api implementation

* provided stable api version for CommunicationIdentityClientBuilder in networktraversal tests

* title for exchangeTeamsToken changed in terms of consistency in README.md

* updatet description of features added in CHANGELOG.md

* generating teams token is added to the samples

* fix checkstyle failure

Co-authored-by: Aigerim Beishenbekova <[email protected]>
  • Loading branch information
AikoBB and Aigerim Beishenbekova authored Oct 20, 2021
1 parent d7181b9 commit 3736b99
Show file tree
Hide file tree
Showing 82 changed files with 2,284 additions and 1,043 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.azure.communication.identity.CommunicationIdentityClientBuilder;
import com.azure.communication.chat.models.*;
import com.azure.communication.common.CommunicationTokenCredential;
import com.azure.communication.identity.CommunicationIdentityServiceVersion;
import com.azure.core.credential.AzureKeyCredential;
import com.azure.core.exception.HttpResponseException;
import com.azure.core.http.HttpClient;
Expand Down Expand Up @@ -105,6 +106,7 @@ protected CommunicationIdentityClientBuilder getCommunicationIdentityClientBuild
CommunicationIdentityClientBuilder builder = new CommunicationIdentityClientBuilder();
builder.endpoint(ENDPOINT)
.credential(new AzureKeyCredential(ACCESS_KEY))
.serviceVersion(CommunicationIdentityServiceVersion.V2021_03_07)
.httpClient(httpClient == null ? interceptorManager.getPlaybackClient() : httpClient);

if (getTestMode() == TestMode.RECORD) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## 1.2.0-beta.1 (Unreleased)

### Features Added
- Added support for Microsoft 365 Teams identities

### Breaking Changes

Expand Down
13 changes: 11 additions & 2 deletions sdk/communication/azure-communication-identity/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ System.out.println("User id: " + user.getId());

### Getting a token for an existing user
Use the `getToken` function to get a token for an existing user. The function
also takes in a list of `CommunicationIdentityTokenScope`. Scope options include:
also takes in a list of `CommunicationTokenScope`. Scope options include:
- `chat` (Chat)
- `voip` (Voice over IP)

Expand Down Expand Up @@ -170,10 +170,19 @@ Use the `deleteUser` function to delete a user.
communicationIdentityClient.deleteUser(user);
```

### Exchanging AAD access token of a Teams User for a Communication Identity access token
Use the `exchangeTeamsToken` function to exchanges an AAD access token of a Teams User for a new Communication Identity access token.

<!-- embedme ./src/samples/java/com/azure/communication/identity/ReadmeSamples.java#L139-L146 -->
```java
// exchanges an AAD access token of a Teams User for a new Communication Identity access token.
communicationIdentityClient.exchangeTeamsToken(accessTokenAAD);
```

## Troubleshooting

All user token service operations will throw an exception on failure.
<!-- embedme ./src/samples/java/com/azure/communication/identity/ReadmeSamples.java#L139-L143 -->
<!-- embedme ./src/samples/java/com/azure/communication/identity/ReadmeSamples.java#L151-L159 -->
```java
try {
CommunicationUserIdentifier user = communicationIdentityClient.createUser();
Expand Down
2 changes: 1 addition & 1 deletion sdk/communication/azure-communication-identity/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
<src.dir>src/main</src.dir>
<test.dir>src/test</test.dir>
<jacoco.min.linecoverage>0.88</jacoco.min.linecoverage>
<jacoco.min.branchcoverage>0.70</jacoco.min.branchcoverage>
<jacoco.min.branchcoverage>0.67</jacoco.min.branchcoverage>
<jacoco.skip.coverage.check>false</jacoco.skip.coverage.check>
<!-- Configures the Java 9+ run to perform the required module exports, opens, and reads that are necessary for testing but shouldn't be part of the module-info. -->
<javaModulesSurefireArgLine>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@

package com.azure.communication.identity;

import com.azure.communication.identity.implementation.CommunicationIdentitiesImpl;
import com.azure.communication.identity.implementation.CommunicationIdentityClientImpl;
import com.azure.communication.identity.implementation.CommunicationIdentityImpl;
import com.azure.communication.identity.implementation.converters.IdentityErrorConverter;
import com.azure.communication.identity.implementation.models.CommunicationErrorResponseException;
import com.azure.communication.identity.implementation.models.CommunicationIdentityAccessToken;
import com.azure.communication.identity.implementation.models.CommunicationIdentityAccessTokenRequest;
import com.azure.communication.identity.implementation.models.CommunicationIdentityAccessTokenResult;
import com.azure.communication.identity.implementation.models.CommunicationIdentityCreateRequest;
import com.azure.communication.identity.implementation.models.CommunicationIdentityAccessToken;
import com.azure.communication.identity.implementation.models.TeamsUserAccessTokenRequest;
import com.azure.communication.identity.models.CommunicationTokenScope;
import com.azure.communication.identity.models.CommunicationUserIdentifierAndToken;
import com.azure.communication.identity.models.IdentityError;
Expand Down Expand Up @@ -40,11 +41,11 @@
@ServiceClient(builder = CommunicationIdentityClientBuilder.class, isAsync = true)
public final class CommunicationIdentityAsyncClient {

private final CommunicationIdentityImpl client;
private final CommunicationIdentitiesImpl client;
private final ClientLogger logger = new ClientLogger(CommunicationIdentityAsyncClient.class);

CommunicationIdentityAsyncClient(CommunicationIdentityClientImpl communicationIdentityServiceClient) {
client = communicationIdentityServiceClient.getCommunicationIdentity();
client = communicationIdentityServiceClient.getCommunicationIdentities();
}

/**
Expand Down Expand Up @@ -281,4 +282,48 @@ private IdentityErrorResponseException translateException(CommunicationErrorResp
}
return new IdentityErrorResponseException(exception.getMessage(), exception.getResponse(), error);
}

/**
* Exchanges an AAD access token of a Teams User for a new Communication Identity access token.
*
* @param accessTokenAAD AAD access token of a Teams User to acquire Communication Identity access token.
* @return Communication Identity access token.
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public Mono<AccessToken> exchangeTeamsToken(String accessTokenAAD) {
try {
TeamsUserAccessTokenRequest requestBody = new TeamsUserAccessTokenRequest();
requestBody.setToken(accessTokenAAD);
return client.exchangeTeamsUserAccessTokenAsync(requestBody)
.onErrorMap(CommunicationErrorResponseException.class, e -> translateException(e))
.flatMap((CommunicationIdentityAccessToken rawToken) -> {
return Mono.just(new AccessToken(rawToken.getToken(), rawToken.getExpiresOn()));
});
} catch (RuntimeException ex) {
return monoError(logger, ex);
}
}

/**
* Exchanges an AAD access token of a Teams User for a new Communication Identity access token.
*
* @param accessTokenAAD AAD access token of a Teams User to acquire Communication Identity access token.
* @return Communication Identity access token with response.
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public Mono<Response<AccessToken>> exchangeTeamsTokenWithResponse(String accessTokenAAD) {
try {
TeamsUserAccessTokenRequest requestBody = new TeamsUserAccessTokenRequest();
requestBody.setToken(accessTokenAAD);
return client.exchangeTeamsUserAccessTokenWithResponseAsync(requestBody)
.onErrorMap(CommunicationErrorResponseException.class, e -> translateException(e))
.flatMap((Response<CommunicationIdentityAccessToken> response) -> {
AccessToken token = new AccessToken(response.getValue().getToken(), response.getValue().getExpiresOn());
return Mono.just(new SimpleResponse<AccessToken>(response, token));
});
} catch (RuntimeException ex) {
return monoError(logger, ex);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;

import com.azure.communication.identity.implementation.CommunicationIdentitiesImpl;
import com.azure.communication.identity.implementation.CommunicationIdentityClientImpl;
import com.azure.communication.identity.implementation.CommunicationIdentityImpl;
import com.azure.communication.identity.implementation.models.CommunicationIdentityAccessToken;
import com.azure.communication.identity.implementation.models.CommunicationIdentityAccessTokenRequest;
import com.azure.communication.identity.implementation.models.CommunicationIdentityAccessTokenResult;
import com.azure.communication.identity.implementation.models.CommunicationIdentityCreateRequest;
import com.azure.communication.identity.implementation.models.CommunicationIdentityAccessToken;
import com.azure.communication.identity.implementation.models.TeamsUserAccessTokenRequest;
import com.azure.communication.identity.models.CommunicationTokenScope;
import com.azure.communication.identity.models.CommunicationUserIdentifierAndToken;
import com.azure.communication.common.CommunicationUserIdentifier;
Expand All @@ -32,11 +33,11 @@
@ServiceClient(builder = CommunicationIdentityClientBuilder.class, isAsync = false)
public final class CommunicationIdentityClient {

private final CommunicationIdentityImpl client;
private final CommunicationIdentitiesImpl client;
private final ClientLogger logger = new ClientLogger(CommunicationIdentityClient.class);

CommunicationIdentityClient(CommunicationIdentityClientImpl communicationIdentityClient) {
client = communicationIdentityClient.getCommunicationIdentity();
client = communicationIdentityClient.getCommunicationIdentities();
}

/**
Expand Down Expand Up @@ -225,4 +226,45 @@ private CommunicationUserIdentifierAndToken userWithAccessTokenResultConverter(
return new CommunicationUserIdentifierAndToken(user, token);

}

/**
* Exchanges an AAD access token of a Teams User for a new Communication Identity access token.
*
* @param accessTokenAAD AAD access token of a Teams User to acquire Communication Identity access token.
* @return Communication Identity access token.
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public AccessToken exchangeTeamsToken(String accessTokenAAD) {
TeamsUserAccessTokenRequest requestBody = new TeamsUserAccessTokenRequest();
requestBody.setToken(accessTokenAAD);
CommunicationIdentityAccessToken rawToken = client.exchangeTeamsUserAccessToken(requestBody);
return new AccessToken(rawToken.getToken(), rawToken.getExpiresOn());
}

/**
* Exchanges an AAD access token of a Teams User for a new Communication Identity access token.
*
* @param accessTokenAAD AAD access token of a Teams User to acquire Communication Identity access token.
* @param context the context of the request. Can also be null or
* Context.NONE.
* @return Communication Identity access token with response.
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public Response<AccessToken> exchangeTeamsTokenWithResponse(String accessTokenAAD, Context context) {
context = context == null ? Context.NONE : context;
TeamsUserAccessTokenRequest requestBody = new TeamsUserAccessTokenRequest();
requestBody.setToken(accessTokenAAD);
Response<CommunicationIdentityAccessToken> response = client.exchangeTeamsUserAccessTokenWithResponseAsync(requestBody, context)
.block();
if (response == null || response.getValue() == null) {
throw logger.logExceptionAsError(new IllegalStateException("Service failed to return a response or expected value."));
}

return new SimpleResponse<AccessToken>(
response,
new AccessToken(response.getValue().getToken(), response.getValue().getExpiresOn()));
}



}
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public final class CommunicationIdentityClientBuilder {
private ClientOptions clientOptions;
private final Map<String, String> properties = CoreUtils.getProperties(COMMUNICATION_IDENTITY_PROPERTIES);
private final List<HttpPipelinePolicy> customPolicies = new ArrayList<HttpPipelinePolicy>();

private CommunicationIdentityServiceVersion serviceVersion;
/**
* Set endpoint of the service
*
Expand Down Expand Up @@ -202,6 +202,7 @@ public CommunicationIdentityClientBuilder retryPolicy(RetryPolicy retryPolicy) {
* @return the updated CommunicationIdentityClientBuilder object
*/
public CommunicationIdentityClientBuilder serviceVersion(CommunicationIdentityServiceVersion version) {
this.serviceVersion = version;
return this;
}

Expand Down Expand Up @@ -237,8 +238,11 @@ private CommunicationIdentityClientImpl createServiceImpl() {
customPolicies);
}

CommunicationIdentityServiceVersion apiVersion = serviceVersion != null ? serviceVersion : CommunicationIdentityServiceVersion.getLatest();

CommunicationIdentityClientImplBuilder clientBuilder = new CommunicationIdentityClientImplBuilder();
clientBuilder.endpoint(endpoint)
.apiVersion(apiVersion.getVersion())
.pipeline(builderPipeline);

return clientBuilder.buildClient();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
* The versions of Communication Identity Service supported by this client library.
*/
public enum CommunicationIdentityServiceVersion implements ServiceVersion {
V2021_03_07("2021-03-07");
V2021_03_07("2021-03-07"),
V2021_10_31_PREVIEW("2021-10-31-preview");

private final String version;

Expand All @@ -23,7 +24,6 @@ public enum CommunicationIdentityServiceVersion implements ServiceVersion {
*/
@Override
public String getVersion() {

return this.version;
}

Expand All @@ -33,7 +33,6 @@ public String getVersion() {
* @return the latest {@link CommunicationIdentityServiceVersion}
*/
public static CommunicationIdentityServiceVersion getLatest() {

return V2021_03_07;
return V2021_10_31_PREVIEW;
}
}
Loading

0 comments on commit 3736b99

Please sign in to comment.