All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.azure.communication.identity.CommunicationIdentityClient Maven / Gradle / Ivy

Go to download

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.common.CommunicationUserIdentifier;
import com.azure.communication.identity.implementation.CommunicationIdentitiesImpl;
import com.azure.communication.identity.implementation.CommunicationIdentityClientImpl;
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.models.CommunicationTokenScope;
import com.azure.communication.identity.models.CommunicationUserIdentifierAndToken;
import com.azure.communication.identity.models.GetTokenForTeamsUserOptions;
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.Context;
import com.azure.core.util.logging.ClientLogger;

import java.time.Duration;
import java.util.Objects;

/**
 * Synchronous client interface for Azure Communication Service Identity operations
 *
 * 

Instantiating a synchronous 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>");
 *
 * CommunicationIdentityClient communicationIdentityClient = new CommunicationIdentityClientBuilder()
 *     .endpoint(endpoint)
 *     .credential(keyCredential)
 *     .buildClient();
 * 
* * *

View {@link CommunicationIdentityClientBuilder this} for additional ways to construct the client.

* * @see CommunicationIdentityClientBuilder */ @ServiceClient(builder = CommunicationIdentityClientBuilder.class, isAsync = false) public final class CommunicationIdentityClient { private final CommunicationIdentitiesImpl client; private final ClientLogger logger = new ClientLogger(CommunicationIdentityClient.class); CommunicationIdentityClient(CommunicationIdentityClientImpl communicationIdentityClient) { client = communicationIdentityClient.getCommunicationIdentities(); } /** * Creates a new CommunicationUserIdentifier. * * @return The created Communication User. */ @ServiceMethod(returns = ReturnType.SINGLE) public CommunicationUserIdentifier createUser() { CommunicationIdentityAccessTokenResult result = client.create(new CommunicationIdentityCreateRequest()); return new CommunicationUserIdentifier(result.getIdentity().getId()); } /** * Creates a new CommunicationUserIdentifier with response. * * @param context A {@link Context} representing the request context. * @return The created Communication User. */ @ServiceMethod(returns = ReturnType.SINGLE) public Response createUserWithResponse(Context context) { context = context == null ? Context.NONE : context; Response response = client.createWithResponse(new CommunicationIdentityCreateRequest(), context); if (response == null || response.getValue() == null) { throw logger.logExceptionAsError(new IllegalStateException("Service failed to return a response or expected value.")); } String id = response.getValue().getIdentity().getId(); return new SimpleResponse( response, new CommunicationUserIdentifier(id)); } /** * 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 CommunicationUserIdentifierAndToken createUserAndToken( Iterable scopes, Duration tokenExpiresIn) { Objects.requireNonNull(scopes); CommunicationIdentityCreateRequest communicationIdentityCreateRequest = CommunicationIdentityClientUtils.createCommunicationIdentityCreateRequest(scopes, tokenExpiresIn, logger); CommunicationIdentityAccessTokenResult result = client.create(communicationIdentityCreateRequest); return userWithAccessTokenResultConverter(result); } /** * 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 CommunicationUserIdentifierAndToken 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. * @param context A {@link Context} representing the request context. * @return The created communication user and token with response. */ @ServiceMethod(returns = ReturnType.SINGLE) public Response createUserAndTokenWithResponse( Iterable scopes, Duration tokenExpiresIn, Context context) { Objects.requireNonNull(scopes); context = context == null ? Context.NONE : context; CommunicationIdentityCreateRequest communicationIdentityCreateRequest = CommunicationIdentityClientUtils.createCommunicationIdentityCreateRequest(scopes, tokenExpiresIn, logger); Response response = client.createWithResponse( communicationIdentityCreateRequest, context); if (response == null || response.getValue() == null) { throw logger.logExceptionAsError(new IllegalStateException("Service failed to return a response or expected value.")); } return new SimpleResponse( response, userWithAccessTokenResultConverter(response.getValue())); } /** * Creates a new CommunicationUserIdentifier with token with response. * * @param scopes The list of scopes for the token. * @param context A {@link Context} representing the request context. * @return The created communication user and token with response. */ @ServiceMethod(returns = ReturnType.SINGLE) public Response createUserAndTokenWithResponse( Iterable scopes, Context context) { return createUserAndTokenWithResponse(scopes, null, context); } /** * Deletes a CommunicationUserIdentifier, revokes its tokens and deletes its * data. * * @param communicationUser The user to be deleted. */ @ServiceMethod(returns = ReturnType.SINGLE) public void deleteUser(CommunicationUserIdentifier communicationUser) { Objects.requireNonNull(communicationUser); client.delete(communicationUser.getId()); } /** * Deletes a CommunicationUserIdentifier, revokes its tokens and deletes its * data with response. * * @param communicationUser The user to be deleted. * @param context A {@link Context} representing the request context. * @return The response with void. */ @ServiceMethod(returns = ReturnType.SINGLE) public Response deleteUserWithResponse(CommunicationUserIdentifier communicationUser, Context context) { Objects.requireNonNull(communicationUser); context = context == null ? Context.NONE : context; return client.deleteWithResponse(communicationUser.getId(), context); } /** * Revokes all the tokens created for an identifier. * * @param communicationUser The user to be revoked token. */ @ServiceMethod(returns = ReturnType.SINGLE) public void revokeTokens(CommunicationUserIdentifier communicationUser) { Objects.requireNonNull(communicationUser); client.revokeAccessTokens(communicationUser.getId()); } /** * Revokes all the tokens created for a user before a specific date. * * @param communicationUser The user to be revoked token. * @param context the context of the request. Can also be null or * Context.NONE. * @return The response with void. */ @ServiceMethod(returns = ReturnType.SINGLE) public Response revokeTokensWithResponse(CommunicationUserIdentifier communicationUser, Context context) { Objects.requireNonNull(communicationUser); context = context == null ? Context.NONE : context; return client.revokeAccessTokensWithResponse(communicationUser.getId(), context); } /** * 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 AccessToken getToken( CommunicationUserIdentifier communicationUser, Iterable scopes, Duration tokenExpiresIn) { Objects.requireNonNull(communicationUser); Objects.requireNonNull(scopes); CommunicationIdentityAccessTokenRequest tokenRequest = CommunicationIdentityClientUtils.createCommunicationIdentityAccessTokenRequest(scopes, tokenExpiresIn, logger); CommunicationIdentityAccessToken rawToken = client.issueAccessToken( communicationUser.getId(), tokenRequest); return new AccessToken(rawToken.getToken(), rawToken.getExpiresOn()); } /** * 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 AccessToken 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. * @param context the context of the request. Can also be null or Context.NONE. * @return the Communication Identity access token with response. */ @ServiceMethod(returns = ReturnType.SINGLE) public Response getTokenWithResponse( CommunicationUserIdentifier communicationUser, Iterable scopes, Duration tokenExpiresIn, Context context) { Objects.requireNonNull(communicationUser); Objects.requireNonNull(scopes); context = context == null ? Context.NONE : context; CommunicationIdentityAccessTokenRequest tokenRequest = CommunicationIdentityClientUtils.createCommunicationIdentityAccessTokenRequest(scopes, tokenExpiresIn, logger); Response response = client.issueAccessTokenWithResponse( communicationUser.getId(), tokenRequest, context); if (response == null || response.getValue() == null) { throw logger.logExceptionAsError(new IllegalStateException("Service failed to return a response or expected value.")); } return new SimpleResponse( response, new AccessToken(response.getValue().getToken(), response.getValue().getExpiresOn())); } /** * 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 context the context of the request. Can also be null or Context.NONE. * @return the Communication Identity access token with response. */ @ServiceMethod(returns = ReturnType.SINGLE) public Response getTokenWithResponse(CommunicationUserIdentifier communicationUser, Iterable scopes, Context context) { return getTokenWithResponse(communicationUser, scopes, null, context); } 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 AccessToken getTokenForTeamsUser(GetTokenForTeamsUserOptions options) { CommunicationIdentityAccessToken rawToken = client.exchangeTeamsUserAccessToken(options); return new AccessToken(rawToken.getToken(), rawToken.getExpiresOn()); } /** * 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. * @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 getTokenForTeamsUserWithResponse(GetTokenForTeamsUserOptions options, Context context) { context = context == null ? Context.NONE : context; Response response = client.exchangeTeamsUserAccessTokenWithResponse(options, context); if (response == null || response.getValue() == null) { throw logger.logExceptionAsError(new IllegalStateException("Service failed to return a response or expected value.")); } return new SimpleResponse( response, new AccessToken(response.getValue().getToken(), response.getValue().getExpiresOn())); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy