Skip to content

Commit

Permalink
[Communication] - Common - Renaming CommunicationUserCredential to Co…
Browse files Browse the repository at this point in the history
…mmunicationTokenCredential (#12992)
  • Loading branch information
minnieliu authored Dec 22, 2020
1 parent 6ee8de4 commit 7e2f5e2
Show file tree
Hide file tree
Showing 19 changed files with 137 additions and 134 deletions.
6 changes: 3 additions & 3 deletions sdk/communication/communication-chat/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,13 @@ Use resource url and user access token to initialize chat client.

```JavaScript
import { ChatClient } from '@azure/communication-chat';
import { AzureCommunicationUserCredential } from "@azure/communication-common";
import { AzureCommunicationTokenCredential } from "@azure/communication-common";

// Your unique Azure Communication service endpoint
let endpointUrl = '<ENDPOINT>';
let userAccessToken = '<USER_ACCESS_TOKEN>';
let userCredential = new AzureCommunicationUserCredential(userAccessToken);
let chatClient = new ChatClient(endpointUrl, userCredential);
let tokenCredential = new AzureCommunicationTokenCredential(userAccessToken);
let chatClient = new ChatClient(endpointUrl, tokenCredential);

```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
import { ChatMessageDeletedEvent } from '@azure/communication-signaling';
import { ChatMessageEditedEvent } from '@azure/communication-signaling';
import { ChatMessageReceivedEvent } from '@azure/communication-signaling';
import { CommunicationTokenCredential } from '@azure/communication-common';
import { CommunicationUser } from '@azure/communication-common';
import { CommunicationUserCredential } from '@azure/communication-common';
import * as coreHttp from '@azure/core-http';
import { HttpResponse } from '@azure/core-http';
import { OperationOptions } from '@azure/core-http';
Expand All @@ -27,7 +27,7 @@ export interface AddMembersRequest extends Omit<RestAddMembersRequest, "members"

// @public
export class ChatClient {
constructor(url: string, credential: CommunicationUserCredential, options?: ChatClientOptions);
constructor(url: string, credential: CommunicationTokenCredential, options?: ChatClientOptions);
createChatThread(request: CreateChatThreadRequest, options?: CreateChatThreadOptions): Promise<ChatThreadClient>;
deleteChatThread(threadId: string, options?: DeleteChatThreadOptions): Promise<OperationResponse>;
getChatThread(threadId: string, options?: GetChatThreadOptions): Promise<GetChatThreadResponse>;
Expand Down Expand Up @@ -67,7 +67,7 @@ export interface ChatThread extends Omit<RestChatThread, "createdBy" | "members"

// @public
export class ChatThreadClient {
constructor(threadId: string, url: string, credential: CommunicationUserCredential, options?: ChatThreadClientOptions);
constructor(threadId: string, url: string, credential: CommunicationTokenCredential, options?: ChatThreadClientOptions);
addMembers(request: AddMembersRequest, options?: AddMembersOptions): Promise<OperationResponse>;
deleteMessage(messageId: string, options?: DeleteMessageOptions): Promise<OperationResponse>;
dispose(): void;
Expand Down
12 changes: 6 additions & 6 deletions sdk/communication/communication-chat/src/chatClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import { logger } from "./models/logger";
import { EventEmitter } from "events";
import { SDK_VERSION } from "./constants";
import { CommunicationUserCredential } from "@azure/communication-common";
import { CommunicationTokenCredential } from "@azure/communication-common";
import {
SignalingClient,
ChatEventId,
Expand All @@ -16,7 +16,7 @@ import {
TypingIndicatorReceivedEvent
} from "@azure/communication-signaling";
import { getSignalingClient } from "./signaling/signalingClient";
import { createCommunicationUserCredentialPolicy } from "./credential/communicationUserCredentialPolicy";
import { createCommunicationTokenCredentialPolicy } from "./credential/communicationTokenCredentialPolicy";
import { ChatApiClient } from "./generated/src/chatApiClient";
import {
InternalPipelineOptions,
Expand Down Expand Up @@ -50,7 +50,7 @@ export { ChatThreadInfo } from "./generated/src/models";
* The client to do chat operations
*/
export class ChatClient {
private readonly tokenCredential: CommunicationUserCredential;
private readonly tokenCredential: CommunicationTokenCredential;
private readonly clientOptions: ChatClientOptions;
private readonly api: ChatApiClient;
private readonly signalingClient: SignalingClient | undefined = undefined;
Expand All @@ -61,12 +61,12 @@ export class ChatClient {
* Creates an instance of the ChatClient for a given resource and user.
*
* @param url The url of the Communication Services resouce.
* @param credential The user credential. Use AzureCommunicationUserCredential from @azure/communication-common to create a credential.
* @param credential The token credential. Use AzureCommunicationTokenCredential from @azure/communication-common to create a credential.
* @param options Additional client options.
*/
constructor(
private readonly url: string,
credential: CommunicationUserCredential,
credential: CommunicationTokenCredential,
options: ChatClientOptions = {}
) {
this.tokenCredential = credential;
Expand Down Expand Up @@ -94,7 +94,7 @@ export class ChatClient {
}
};

const authPolicy = createCommunicationUserCredentialPolicy(this.tokenCredential);
const authPolicy = createCommunicationTokenCredentialPolicy(this.tokenCredential);
const pipeline = createPipelineFromOptions(internalPipelineOptions, authPolicy);

this.api = new ChatApiClient(this.url, pipeline);
Expand Down
10 changes: 5 additions & 5 deletions sdk/communication/communication-chat/src/chatThreadClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@

import { logger } from "./models/logger";
import { SDK_VERSION } from "./constants";
import { CommunicationUser, CommunicationUserCredential } from "@azure/communication-common";
import { createCommunicationUserCredentialPolicy } from "./credential/communicationUserCredentialPolicy";
import { CommunicationUser, CommunicationTokenCredential } from "@azure/communication-common";
import { createCommunicationTokenCredentialPolicy } from "./credential/communicationTokenCredentialPolicy";
import { ChatApiClient } from "./generated/src/chatApiClient";
import {
InternalPipelineOptions,
Expand Down Expand Up @@ -61,7 +61,7 @@ export class ChatThreadClient {
*/
readonly threadId: string;

private readonly tokenCredential: CommunicationUserCredential;
private readonly tokenCredential: CommunicationTokenCredential;
private readonly api: ChatApiClient;
private disposed = false;

Expand All @@ -70,7 +70,7 @@ export class ChatThreadClient {
constructor(
threadId: string,
private readonly url: string,
credential: CommunicationUserCredential,
credential: CommunicationTokenCredential,
options: ChatThreadClientOptions = {}
) {
this.threadId = threadId;
Expand Down Expand Up @@ -98,7 +98,7 @@ export class ChatThreadClient {
}
};

const authPolicy = createCommunicationUserCredentialPolicy(this.tokenCredential);
const authPolicy = createCommunicationTokenCredentialPolicy(this.tokenCredential);
const pipeline = createPipelineFromOptions(internalPipelineOptions, authPolicy);

this.api = new ChatApiClient(this.url, pipeline);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

import { CommunicationUserCredential } from "@azure/communication-common";
import { CommunicationTokenCredential } from "@azure/communication-common";
import {
Constants,
HttpOperationResponse,
Expand All @@ -13,38 +13,38 @@ import {
} from "@azure/core-http";

/**
* Creates a new CommunicationUserCredentialPolicy factory.
* Creates a new CommunicationTokenCredentialPolicy factory.
*
* @param credential The CommunicationUserCredential implementation that can supply the user credential.
* @param credential The CommunicationTokenCredential implementation that can supply the user credential.
*/
export const createCommunicationUserCredentialPolicy = (
credential: CommunicationUserCredential
export const createCommunicationTokenCredentialPolicy = (
credential: CommunicationTokenCredential
): RequestPolicyFactory => ({
create: (nextPolicy: RequestPolicy, options: RequestPolicyOptions) => {
return new CommunicationUserCredentialPolicy(nextPolicy, options, credential);
return new CommunicationTokenCredentialPolicy(nextPolicy, options, credential);
}
});

/**
*
* Provides a RequestPolicy that can request a token from a CommunicationUserCredential
* Provides a RequestPolicy that can request a token from a CommunicationTokenCredential
* implementation and then apply it to the Authorization header of a request.
*
* @internal
*/
export class CommunicationUserCredentialPolicy extends BaseRequestPolicy {
export class CommunicationTokenCredentialPolicy extends BaseRequestPolicy {
/**
* Creates a new CommunicationUserCredentialPolicy object.
* Creates a new CommunicationTokenCredentialPolicy object.
*
* @param nextPolicy The next RequestPolicy in the request pipeline.
* @param options Options for this RequestPolicy.
* @param credential The CommunicationUserCredential implementation that can supply the user credential.
* @param tokenCache The cache for the most recent AccessToken returned from the CommunicationUserCredential.
* @param credential The CommunicationTokenCredential implementation that can supply the user credential.
* @param tokenCache The cache for the most recent AccessToken returned from the CommunicationTokenCredential.
*/
constructor(
nextPolicy: RequestPolicy,
options: RequestPolicyOptions,
private readonly credential: CommunicationUserCredential
private readonly credential: CommunicationTokenCredential
) {
super(nextPolicy, options);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
// Licensed under the MIT license.

import { CommunicationSignalingClient, SignalingClient } from "@azure/communication-signaling";
import { CommunicationUserCredential } from "@azure/communication-common";
import { CommunicationTokenCredential } from "@azure/communication-common";
import { AzureLogger } from "@azure/logger";

export const getSignalingClient = (
credential: CommunicationUserCredential,
credential: CommunicationTokenCredential,
logger: AzureLogger
): SignalingClient | undefined => {
return new CommunicationSignalingClient(credential, logger);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
// Licensed under the MIT license.

import { SignalingClient } from "@azure/communication-signaling";
import { CommunicationUserCredential } from "@azure/communication-common";
import { CommunicationTokenCredential } from "@azure/communication-common";
import { AzureLogger } from "@azure/logger";

export const getSignalingClient = (
_credential: CommunicationUserCredential,
_credential: CommunicationTokenCredential,
_logger: AzureLogger
): SignalingClient | undefined => {
return undefined;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { ChatClient, CreateChatThreadRequest } from "../src";
import * as RestModel from "../src/generated/src/models";
import { apiVersion } from "../src/generated/src/models/parameters";
import { baseUri, generateToken } from "./utils/connectionUtils";
import { AzureCommunicationUserCredential } from "@azure/communication-common";
import { AzureCommunicationTokenCredential } from "@azure/communication-common";
import {
mockThread,
generateHttpClient,
Expand All @@ -25,7 +25,7 @@ describe("[Mocked] ChatClient", async () => {
});

it("can instantiate", async () => {
new ChatClient(baseUri, new AzureCommunicationUserCredential(generateToken()));
new ChatClient(baseUri, new AzureCommunicationTokenCredential(generateToken()));
});

it("makes successful create thread request", async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

import sinon from "sinon";
import { assert } from "chai";
import { AzureCommunicationUserCredential } from "@azure/communication-common";
import { AzureCommunicationTokenCredential } from "@azure/communication-common";
import {
ChatThreadClient,
UpdateThreadOptions,
Expand Down Expand Up @@ -35,7 +35,7 @@ describe("[Mocked] ChatThreadClient", async () => {
});

it("can instantiate", async () => {
new ChatThreadClient(threadId, baseUri, new AzureCommunicationUserCredential(generateToken()));
new ChatThreadClient(threadId, baseUri, new AzureCommunicationTokenCredential(generateToken()));
});

it("makes successful update thread request", async () => {
Expand Down
6 changes: 3 additions & 3 deletions sdk/communication/communication-chat/test/utils/mockClient.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

import { AzureCommunicationUserCredential } from "@azure/communication-common";
import { AzureCommunicationTokenCredential } from "@azure/communication-common";
import { HttpClient, HttpHeaders, WebResourceLike, HttpOperationResponse } from "@azure/core-http";
import { ChatClient, ChatThreadMember } from "../../src";
import * as RestModel from "../../src/generated/src/models";
Expand Down Expand Up @@ -75,7 +75,7 @@ export const generateHttpClient = (status: number, parsedBody?: any): HttpClient
};

export const createChatClient = (mockHttpClient: HttpClient): ChatClient => {
return new ChatClient(baseUri, new AzureCommunicationUserCredential(generateToken()), {
return new ChatClient(baseUri, new AzureCommunicationTokenCredential(generateToken()), {
httpClient: mockHttpClient
});
};
Expand All @@ -87,7 +87,7 @@ export const createChatThreadClient = (
return new ChatThreadClient(
threadId,
baseUri,
new AzureCommunicationUserCredential(generateToken()),
new AzureCommunicationTokenCredential(generateToken()),
{
httpClient: mockHttpClient
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { isNode } from "@azure/core-http";
import { ChatClient } from "../../src";
import {
CommunicationUser,
AzureCommunicationUserCredential,
AzureCommunicationTokenCredential,
parseClientArguments
} from "@azure/communication-common";
import {
Expand Down Expand Up @@ -64,5 +64,5 @@ export function createChatClient(userToken: string): ChatClient {
userToken = generateToken();
}
const { url } = parseClientArguments(env.COMMUNICATION_CONNECTION_STRING);
return new ChatClient(url, new AzureCommunicationUserCredential(userToken));
return new ChatClient(url, new AzureCommunicationTokenCredential(userToken));
}
3 changes: 3 additions & 0 deletions sdk/communication/communication-common/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

## 1.0.0-beta.4 (Unreleased)

### Breaking Changes

- Renamed `communicationUserCredential` to `communicationTokenCredential`.

## 1.0.0-beta.3 (2020-11-16)

Expand Down
18 changes: 9 additions & 9 deletions sdk/communication/communication-common/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,20 @@ npm install @azure/communication-common

## Key concepts

### CommunicationUserCredential and AzureCommunicationUserCredential
### CommunicationTokenCredential and AzureCommunicationTokenCredential

A `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.
A `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.

It is up to you the developer to first create valid user tokens with the Azure Communication Administration library. Then you use these tokens to create a `AzureCommunicationUserCredential`.
It is up to you the developer to first create valid user tokens with the Azure Communication Administration library. Then you use these tokens to create a `AzureCommunicationTokenCredential`.

`CommunicationUserCredential` is only the interface, please always use the `AzureCommunicationUserCredential` constructor to create a credential and take advantage of the built-in refresh logic.
`CommunicationTokenCredential` is only the interface, please always use the `AzureCommunicationTokenCredential` constructor to create a credential and take advantage of the built-in refresh logic.

## Examples

### Create a credential with a static token

```typescript
const userCredential = new AzureCommunicationUserCredential(
const tokenCredential = new AzureCommunicationTokenCredential(
"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjM2MDB9.adM-ddBZZlQ1WlN3pdPBOF5G4Wh9iZpxNP_fSvpF4cWs"
);
```
Expand All @@ -40,7 +40,7 @@ const userCredential = new AzureCommunicationUserCredential(
Here we assume that we have a function `fetchTokenFromMyServerForUser` that makes a network request to retrieve a token string for a user. We pass it into the credential to fetch a token for Bob from our own server. Our server would use the Azure Communication Administration library to issue tokens.

```typescript
const userCredential = new AzureCommunicationUserCredential({
const tokenCredential = new AzureCommunicationTokenCredential({
tokenRefresher: async () => fetchTokenFromMyServerForUser("[email protected]")
});
```
Expand All @@ -50,7 +50,7 @@ const userCredential = new AzureCommunicationUserCredential({
Setting `refreshProactively` to true will call your `tokenRefresher` function when the token is close to expiry.

```typescript
const userCredential = new AzureCommunicationUserCredential({
const tokenCredential = new AzureCommunicationTokenCredential({
tokenRefresher: async () => fetchTokenFromMyServerForUser("[email protected]"),
refreshProactively: true
});
Expand All @@ -61,10 +61,10 @@ const userCredential = new AzureCommunicationUserCredential({
Passing `initialToken` is an optional optimization to skip the first call to `tokenRefresher`. You can use this to separate the boot from your application from subsequent token refresh cycles.

```typescript
const userCredential = new AzureCommunicationUserCredential({
const tokenCredential = new AzureCommunicationTokenCredential({
tokenRefresher: async () => fetchTokenFromMyServerForUser("[email protected]"),
refreshProactively: true,
initialToken:
token:
"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjM2MDB9.adM-ddBZZlQ1WlN3pdPBOF5G4Wh9iZpxNP_fSvpF4cWs"
});
```
Expand Down
Loading

0 comments on commit 7e2f5e2

Please sign in to comment.