Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Communcation] - Common - Renaming CommunicationUserCredential to CommunicationTokenCredential #17989

Merged
merged 6 commits into from
Dec 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions sdk/communication/azure-communication-chat/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ HttpClient httpClient = httpClientBuilder.build();

// Your user access token retrieved from your trusted service
String token = "SECRET";
CommunicationUserCredential credential = new CommunicationUserCredential(token);
CommunicationTokenCredential credential = new CommunicationTokenCredential(token);
RezaJooyandeh marked this conversation as resolved.
Show resolved Hide resolved

// Initialize the chat client
final ChatClientBuilder builder = new ChatClientBuilder();
Expand All @@ -94,7 +94,7 @@ ChatClient chatClient = builder.buildClient();

#### Create a chat thread

To create a chat client, you will use the Communications Service endpoint and the access token that was generated as part of pre-requisite steps. User access tokens enable you to build client applications that directly authenticate to Azure Communication Services. Once you generate these tokens on your server, pass them back to a client device. You need to use the CommunicationUserCredential class from the Common SDK to pass the token to your chat client.
To create a chat client, you will use the Communications Service endpoint and the access token that was generated as part of pre-requisite steps. User access tokens enable you to build client applications that directly authenticate to Azure Communication Services. Once you generate these tokens on your server, pass them back to a client device. You need to use the CommunicationTokenCredential class from the Common SDK to pass the token to your chat client.

Use the `createChatThread` method to create a chat thread.
`createChatThreadOptions` is used to describe the thread request, an example is shown in the code snippet below.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@
package com.azure.communication.chat;

import com.azure.communication.chat.implementation.AzureCommunicationChatServiceImplBuilder;
import com.azure.communication.chat.implementation.CommunicationBearerTokenCredential;

import com.azure.communication.common.CommunicationTokenCredential;
import com.azure.communication.common.CommunicationUserCredential;

import java.util.ArrayList;
import java.util.Map;
Expand Down Expand Up @@ -33,7 +34,7 @@ public final class ChatClientBuilder {

private String endpoint;
private HttpClient httpClient;
private CommunicationUserCredential communicationUserCredential;
private CommunicationTokenCredential communicationTokenCredential;
private final List<HttpPipelinePolicy> customPolicies = new ArrayList<HttpPipelinePolicy>();
private HttpLogOptions logOptions = new HttpLogOptions();
private HttpPipeline httpPipeline;
Expand Down Expand Up @@ -68,12 +69,12 @@ public ChatClientBuilder httpClient(HttpClient httpClient) {
/**
* Set a token credential for authorization
*
* @param communicationUserCredential valid token credential as a string
* @param communicationTokenCredential valid token credential as a string
* @return the updated ChatClientBuilder object
*/
public ChatClientBuilder credential(CommunicationUserCredential communicationUserCredential) {
this.communicationUserCredential = Objects.requireNonNull(
communicationUserCredential, "'communicationUserCredential' cannot be null.");
public ChatClientBuilder credential(CommunicationTokenCredential communicationTokenCredential) {
this.communicationTokenCredential = Objects.requireNonNull(
communicationTokenCredential, "'communicationTokenCredential' cannot be null.");
return this;
}

Expand Down Expand Up @@ -166,10 +167,10 @@ public ChatAsyncClient buildAsyncClient() {
if (httpPipeline != null) {
pipeline = httpPipeline;
} else {
Objects.requireNonNull(communicationUserCredential);
Objects.requireNonNull(communicationTokenCredential);
Objects.requireNonNull(httpClient);
CommunicationTokenCredential tokenCredential =
new CommunicationTokenCredential(communicationUserCredential);
CommunicationBearerTokenCredential tokenCredential =
new CommunicationBearerTokenCredential(communicationTokenCredential);

pipeline = createHttpPipeline(httpClient,
new BearerTokenAuthenticationPolicy(tokenCredential, ""),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

package com.azure.communication.chat.implementation;

import java.util.concurrent.ExecutionException;

import com.azure.communication.common.CommunicationTokenCredential;
import com.azure.core.credential.AccessToken;
import com.azure.core.credential.TokenCredential;
import com.azure.core.credential.TokenRequestContext;

import reactor.core.publisher.Mono;

/**
* This class serves as a CommunicationTokenCredential wrapper that
* allows using BearerAuthenticationPolicy in different clients
*/
public class CommunicationBearerTokenCredential implements TokenCredential {
private final CommunicationTokenCredential credential;

/**
* Creates a CommunicationTokenCredential
*
* @param communicationTokenCredential The {@link CommunicationTokenCredential} to use
* in the BearerAuthenticationPolicy.
*/
public CommunicationBearerTokenCredential(CommunicationTokenCredential communicationTokenCredential) {
credential = communicationTokenCredential;
}

@Override
public Mono<AccessToken> getToken(TokenRequestContext request) {
try {
return Mono.just(credential.getToken().get());
RezaJooyandeh marked this conversation as resolved.
Show resolved Hide resolved
} catch (InterruptedException ex) {
return Mono.error(ex);
} catch (ExecutionException ex) {
return Mono.error(ex);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import com.azure.communication.chat.models.UpdateChatMessageOptions;
import com.azure.communication.chat.models.UpdateChatThreadOptions;
import com.azure.communication.common.CommunicationUser;
import com.azure.communication.common.CommunicationUserCredential;
import com.azure.communication.common.CommunicationTokenCredential;
import com.azure.core.http.HttpClient;
import com.azure.core.http.netty.NettyAsyncHttpClientBuilder;
import com.azure.core.http.rest.PagedIterable;
Expand Down Expand Up @@ -47,7 +47,7 @@ public ChatClient createChatClient() {

// Your user access token retrieved from your trusted service
String token = "SECRET";
CommunicationUserCredential credential = new CommunicationUserCredential(token);
CommunicationTokenCredential credential = new CommunicationTokenCredential(token);

// Initialize the chat client
final ChatClientBuilder builder = new ChatClientBuilder();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import com.azure.communication.administration.CommunicationIdentityClientBuilder;
import com.azure.communication.chat.models.ErrorException;
import com.azure.communication.chat.models.*;
import com.azure.communication.common.CommunicationUserCredential;
import com.azure.communication.common.CommunicationTokenCredential;
import com.azure.core.exception.HttpResponseException;
import com.azure.core.http.HttpClient;
import com.azure.core.test.TestBase;
Expand Down Expand Up @@ -48,10 +48,10 @@ protected ChatClientBuilder getChatClientBuilder(String token, HttpClient httpCl
.httpClient(httpClient == null ? interceptorManager.getPlaybackClient() : httpClient);

if (interceptorManager.isPlaybackMode()) {
builder.credential(new CommunicationUserCredential(generateRawToken()));
builder.credential(new CommunicationTokenCredential(generateRawToken()));
return builder;
} else {
builder.credential(new CommunicationUserCredential(token));
builder.credential(new CommunicationTokenCredential(token));
}

if (getTestMode() == TestMode.RECORD) {
Expand Down
3 changes: 2 additions & 1 deletion sdk/communication/azure-communication-common/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
# Release History

## 1.0.0-beta.4 (Unreleased)

### Breaking Changes
- Renamed `CommunicationUserCredential` to `CommunicationTokenCredential`

## 1.0.0-beta.3 (2020-11-16)
Updated `azure-communication-common` version
Expand Down
6 changes: 3 additions & 3 deletions sdk/communication/azure-communication-common/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@ Azure Communication Service supports HMAC authentication with resource access ke
apply HMAC authentication, construct CommunicationClientCredential with the access key and instantiate
a CommunicationIdentityClient to manage users and tokens.

### CommunicationUserCredential
### CommunicationTokenCredential

It is up to you the developer to first create valid user tokens with the Communication Administration SDK. Then you use these tokens with the `CommunicationUserCredential`.
It is up to you the developer to first create valid user tokens with the Communication Administration SDK. Then you use these tokens with the `CommunicationTokenCredential`.

`CommunicationUserCredential` authenticates a user with Communication Services, such as Chat or Calling. It optionally provides an auto-refresh mechanism to ensure a continuously stable authentication state during communications.
`CommunicationTokenCredential` authenticates a user with Communication Services, such as Chat or Calling. It optionally provides an auto-refresh mechanism to ensure a continuously stable authentication state during communications.

## Contributing

Expand Down
Loading