com.azure.communication.identity.CommunicationIdentityAsyncClient Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of azure-communication-identity Show documentation
Show all versions of azure-communication-identity Show documentation
This package contains APIs for application identity in Microsoft Azure Communication Services.
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.
Microsoft Azure Communication Identity quickstart - https://docs.microsoft.com/azure/communication-services/quickstarts/access-tokens?pivots=programming-language-java
The newest version!
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
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.converters.IdentityErrorConverter;
import com.azure.communication.identity.implementation.models.CommunicationErrorResponseException;
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.models.CommunicationTokenScope;
import com.azure.communication.identity.models.CommunicationUserIdentifierAndToken;
import com.azure.communication.identity.models.GetTokenForTeamsUserOptions;
import com.azure.communication.common.CommunicationUserIdentifier;
import com.azure.core.annotation.ReturnType;
import com.azure.core.annotation.ServiceClient;
import com.azure.core.annotation.ServiceMethod;
import com.azure.core.credential.AccessToken;
import com.azure.core.http.rest.Response;
import com.azure.core.http.rest.SimpleResponse;
import com.azure.core.util.logging.ClientLogger;
import java.time.Duration;
import java.util.Objects;
import reactor.core.publisher.Mono;
import static com.azure.core.util.FluxUtil.monoError;
/**
* Asynchronous client interface for Azure Communication Services Identity
* operations
*
* Instantiating an asynchronous Azure Communication Service Identity Client
*
*
*
* // You can find your endpoint and access key from your resource in the Azure Portal
* String endpoint = "https://<RESOURCE_NAME>.communication.azure.com";
* AzureKeyCredential keyCredential = new AzureKeyCredential("<access-key>");
*
* CommunicationIdentityAsyncClient communicationIdentityAsyncClient = new CommunicationIdentityClientBuilder()
* .endpoint(endpoint)
* .credential(keyCredential)
* .buildAsyncClient();
*
*
*
*View {@link CommunicationIdentityClientBuilder this} for additional ways to construct the client.
*
* @see CommunicationIdentityClientBuilder
*/
@ServiceClient(builder = CommunicationIdentityClientBuilder.class, isAsync = true)
public final class CommunicationIdentityAsyncClient {
private final CommunicationIdentitiesImpl client;
private final ClientLogger logger = new ClientLogger(CommunicationIdentityAsyncClient.class);
CommunicationIdentityAsyncClient(CommunicationIdentityClientImpl communicationIdentityServiceClient) {
client = communicationIdentityServiceClient.getCommunicationIdentities();
}
/**
* Creates a new CommunicationUserIdentifier.
*
* @return The created communication user.
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public Mono createUser() {
try {
return client.createAsync(new CommunicationIdentityCreateRequest())
.onErrorMap(CommunicationErrorResponseException.class, IdentityErrorConverter::translateException)
.flatMap(
(CommunicationIdentityAccessTokenResult result) -> {
return Mono.just(new CommunicationUserIdentifier(result.getIdentity().getId()));
});
} catch (RuntimeException ex) {
return monoError(logger, ex);
}
}
/**
* Creates a new CommunicationUserIdentifier with response.
*
* @return The created communication user with response.
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public Mono> createUserWithResponse() {
try {
return client.createWithResponseAsync(new CommunicationIdentityCreateRequest())
.onErrorMap(CommunicationErrorResponseException.class, IdentityErrorConverter::translateException)
.flatMap(
(Response response) -> {
String id = response.getValue().getIdentity().getId();
return Mono.just(
new SimpleResponse(
response,
new CommunicationUserIdentifier(id)));
});
} catch (RuntimeException ex) {
return monoError(logger, ex);
}
}
/**
* Creates a new CommunicationUserIdentifier with token.
*
* @param scopes The list of scopes for the token.
* @param tokenExpiresIn Custom validity period of the Communication Identity access token within [1,24]
* hours range. If not provided, the default value of 24 hours will be used.
* @return The created communication user and token.
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public Mono
createUserAndToken(Iterable scopes, Duration tokenExpiresIn) {
try {
Objects.requireNonNull(scopes);
CommunicationIdentityCreateRequest communicationIdentityCreateRequest =
CommunicationIdentityClientUtils.createCommunicationIdentityCreateRequest(scopes, tokenExpiresIn, logger);
return client.createAsync(communicationIdentityCreateRequest)
.onErrorMap(CommunicationErrorResponseException.class, IdentityErrorConverter::translateException)
.flatMap(
(CommunicationIdentityAccessTokenResult result) -> {
return Mono.just(userWithAccessTokenResultConverter(result));
});
} catch (RuntimeException ex) {
return monoError(logger, ex);
}
}
/**
* Creates a new CommunicationUserIdentifier with token.
*
* @param scopes The list of scopes for the token.
* @return The created communication user and token.
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public Mono
createUserAndToken(Iterable scopes) {
return createUserAndToken(scopes, null);
}
/**
* Creates a new CommunicationUserIdentifier with token with response.
*
* @param scopes The list of scopes for the token.
* @param tokenExpiresIn Custom validity period of the Communication Identity access token within [1,24]
* hours range. If not provided, the default value of 24 hours will be used.
* @return The result with created communication user and token with response.
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public Mono>
createUserAndTokenWithResponse(Iterable scopes, Duration tokenExpiresIn) {
try {
Objects.requireNonNull(scopes);
CommunicationIdentityCreateRequest communicationIdentityCreateRequest =
CommunicationIdentityClientUtils.createCommunicationIdentityCreateRequest(scopes, tokenExpiresIn, logger);
return client.createWithResponseAsync(communicationIdentityCreateRequest)
.onErrorMap(CommunicationErrorResponseException.class, IdentityErrorConverter::translateException)
.flatMap(
(Response response) -> {
return Mono.just(new SimpleResponse(response,
userWithAccessTokenResultConverter(response.getValue())));
});
} catch (RuntimeException ex) {
return monoError(logger, ex);
}
}
/**
* Creates a new CommunicationUserIdentifier with token with response.
*
* @param scopes The list of scopes for the token.
* @return The result with created communication user and token with response.
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public Mono>
createUserAndTokenWithResponse(Iterable scopes) {
return createUserAndTokenWithResponse(scopes, null);
}
/**
* Deletes a CommunicationUserIdentifier, revokes its tokens and deletes its
* data.
*
* @param communicationUser The user to be deleted.
* @return A reactive response signalling completion.
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public Mono deleteUser(CommunicationUserIdentifier communicationUser) {
try {
Objects.requireNonNull(communicationUser);
return client.deleteAsync(communicationUser.getId())
.onErrorMap(CommunicationErrorResponseException.class, IdentityErrorConverter::translateException);
} catch (RuntimeException ex) {
return monoError(logger, ex);
}
}
/**
* Deletes a CommunicationUserIdentifier, revokes its tokens and deletes its
* data with response.
*
* @param communicationUser The user to be deleted.
* @return The response with void.
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public Mono> deleteUserWithResponse(CommunicationUserIdentifier communicationUser) {
try {
Objects.requireNonNull(communicationUser);
return client.deleteWithResponseAsync(communicationUser.getId())
.onErrorMap(CommunicationErrorResponseException.class, IdentityErrorConverter::translateException);
} catch (RuntimeException ex) {
return monoError(logger, ex);
}
}
/**
* Revokes all the tokens created for an identifier.
*
* @param communicationUser The user to be revoked access tokens.
* @return A reactive response signalling completion.
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public Mono revokeTokens(CommunicationUserIdentifier communicationUser) {
try {
Objects.requireNonNull(communicationUser);
return client.revokeAccessTokensAsync(communicationUser.getId())
.onErrorMap(CommunicationErrorResponseException.class, IdentityErrorConverter::translateException);
} catch (RuntimeException ex) {
return monoError(logger, ex);
}
}
/**
* Revokes all the tokens created for an identifier with response.
*
* @param communicationUser The user to be revoked tokens.
* @return The response with void.
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public Mono> revokeTokensWithResponse(CommunicationUserIdentifier communicationUser) {
try {
Objects.requireNonNull(communicationUser);
return client.revokeAccessTokensWithResponseAsync(communicationUser.getId())
.onErrorMap(CommunicationErrorResponseException.class, IdentityErrorConverter::translateException);
} catch (RuntimeException ex) {
return monoError(logger, ex);
}
}
/**
* Gets a Communication Identity access token for a {@link CommunicationUserIdentifier}.
*
* @param communicationUser A {@link CommunicationUserIdentifier} from whom to issue a Communication Identity
* access token.
* @param scopes List of {@link CommunicationTokenScope} scopes for the Communication Identity access token.
* @param tokenExpiresIn Custom validity period of the Communication Identity access token within [1,24]
* hours range. If not provided, the default value of 24 hours will be used.
* @return the Communication Identity access token.
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public Mono getToken(CommunicationUserIdentifier communicationUser,
Iterable scopes, Duration tokenExpiresIn) {
try {
Objects.requireNonNull(communicationUser);
Objects.requireNonNull(scopes);
CommunicationIdentityAccessTokenRequest tokenRequest =
CommunicationIdentityClientUtils.createCommunicationIdentityAccessTokenRequest(scopes, tokenExpiresIn, logger);
return client.issueAccessTokenAsync(communicationUser.getId(),
tokenRequest
)
.onErrorMap(CommunicationErrorResponseException.class, IdentityErrorConverter::translateException)
.flatMap((CommunicationIdentityAccessToken rawToken) -> {
return Mono.just(new AccessToken(rawToken.getToken(), rawToken.getExpiresOn()));
});
} catch (RuntimeException ex) {
return monoError(logger, ex);
}
}
/**
* Gets a Communication Identity access token for a {@link CommunicationUserIdentifier}.
*
* @param communicationUser A {@link CommunicationUserIdentifier} from whom to issue a Communication Identity
* access token.
* @param scopes List of {@link CommunicationTokenScope} scopes for the Communication Identity access token.
* @return the Communication Identity access token.
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public Mono getToken(CommunicationUserIdentifier communicationUser,
Iterable scopes) {
return getToken(communicationUser, scopes, null);
}
/**
* Gets a Communication Identity access token for a {@link CommunicationUserIdentifier}.
*
* @param communicationUser A {@link CommunicationUserIdentifier} from whom to issue a Communication Identity
* access token.
* @param scopes List of {@link CommunicationTokenScope} scopes for the Communication Identity access token.
* @param tokenExpiresIn Custom validity period of the Communication Identity access token within [1,24]
* hours range. If not provided, the default value of 24 hours will be used.
* @return the Communication Identity access token with response.
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public Mono> getTokenWithResponse(CommunicationUserIdentifier communicationUser,
Iterable scopes, Duration tokenExpiresIn) {
try {
Objects.requireNonNull(communicationUser);
Objects.requireNonNull(scopes);
CommunicationIdentityAccessTokenRequest tokenRequest =
CommunicationIdentityClientUtils.createCommunicationIdentityAccessTokenRequest(scopes, tokenExpiresIn, logger);
return client.issueAccessTokenWithResponseAsync(communicationUser.getId(),
tokenRequest
)
.onErrorMap(CommunicationErrorResponseException.class, IdentityErrorConverter::translateException)
.flatMap((Response response) -> {
AccessToken token = new AccessToken(response.getValue().getToken(), response.getValue().getExpiresOn());
return Mono.just(new SimpleResponse(response, token));
});
} catch (RuntimeException ex) {
return monoError(logger, ex);
}
}
/**
* Gets a Communication Identity access token for a {@link CommunicationUserIdentifier}.
*
* @param communicationUser A {@link CommunicationUserIdentifier} from whom to issue a Communication Identity
* access token.
* @param scopes List of {@link CommunicationTokenScope} scopes for the Communication Identity access token.
* @return the Communication Identity access token with response.
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public Mono> getTokenWithResponse(CommunicationUserIdentifier communicationUser,
Iterable scopes) {
return getTokenWithResponse(communicationUser, scopes, null);
}
/**
* Converts CommunicationIdentityAccessTokenResult to CommunicationUserIdentifierAndToken
*
* @param identityAccessTokenResult The result input.
* @return The result converted to CommunicationUserIdentifierAndToken type
*/
private CommunicationUserIdentifierAndToken userWithAccessTokenResultConverter(
CommunicationIdentityAccessTokenResult identityAccessTokenResult) {
CommunicationUserIdentifier user =
new CommunicationUserIdentifier(identityAccessTokenResult.getIdentity().getId());
AccessToken token = new AccessToken(
identityAccessTokenResult.getAccessToken().getToken(),
identityAccessTokenResult.getAccessToken().getExpiresOn());
return new CommunicationUserIdentifierAndToken(user, token);
}
/**
* Exchanges an Azure AD access token of a Teams User for a new Communication Identity access token.
*
* @param options {@link GetTokenForTeamsUserOptions} request options used to exchange an Azure AD access token of a Teams User for a new Communication Identity access token.
* @return Communication Identity access token.
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public Mono getTokenForTeamsUser(GetTokenForTeamsUserOptions options) {
try {
return client.exchangeTeamsUserAccessTokenAsync(options)
.onErrorMap(CommunicationErrorResponseException.class, IdentityErrorConverter::translateException)
.flatMap((CommunicationIdentityAccessToken rawToken) -> {
return Mono.just(new AccessToken(rawToken.getToken(), rawToken.getExpiresOn()));
});
} catch (RuntimeException ex) {
return monoError(logger, ex);
}
}
/**
* Exchanges an Azure AD access token of a Teams User for a new Communication Identity access token.
*
* @param options {@link GetTokenForTeamsUserOptions} request options used to exchange an Azure AD access token of a Teams User for a new Communication Identity access token.
* @return Communication Identity access token with response.
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public Mono> getTokenForTeamsUserWithResponse(GetTokenForTeamsUserOptions options) {
try {
return client.exchangeTeamsUserAccessTokenWithResponseAsync(options)
.onErrorMap(CommunicationErrorResponseException.class, IdentityErrorConverter::translateException)
.flatMap((Response response) -> {
AccessToken token = new AccessToken(response.getValue().getToken(), response.getValue().getExpiresOn());
return Mono.just(new SimpleResponse(response, token));
});
} catch (RuntimeException ex) {
return monoError(logger, ex);
}
}
}