Skip to content

Commit

Permalink
[communication] switch to bearerTokenAuthenticationPolicy (#13220)
Browse files Browse the repository at this point in the history
  • Loading branch information
0rland0Wats0n authored Jan 18, 2021
1 parent 9b37d20 commit 81d37fa
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 55 deletions.
2 changes: 1 addition & 1 deletion sdk/communication/communication-chat/src/chatClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import {
TypingIndicatorReceivedEvent
} from "@azure/communication-signaling";
import { getSignalingClient } from "./signaling/signalingClient";
import { createCommunicationTokenCredentialPolicy } from "./credential/communicationTokenCredentialPolicy";
import { ChatApiClient } from "./generated/src/chatApiClient";
import {
InternalPipelineOptions,
Expand All @@ -43,6 +42,7 @@ import {
} from "./models/mappers";
import { ChatThreadInfo } from "./generated/src/models";
import { CreateChatThreadRequest } from "./models/requests";
import { createCommunicationTokenCredentialPolicy } from "./credential/communicationTokenCredentialPolicy";

export { ChatThreadInfo } from "./generated/src/models";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import {
CommunicationUserIdentifier,
CommunicationTokenCredential
} from "@azure/communication-common";
import { createCommunicationTokenCredentialPolicy } from "./credential/communicationTokenCredentialPolicy";
import { ChatApiClient } from "./generated/src/chatApiClient";
import {
InternalPipelineOptions,
Expand Down Expand Up @@ -50,6 +49,7 @@ import {
mapToChatThreadMemberSdkModel,
mapToReadReceiptSdkModel
} from "./models/mappers";
import { createCommunicationTokenCredentialPolicy } from "./credential/communicationTokenCredentialPolicy";

export { ChatMessagePriority, SendReadReceiptRequest } from "./generated/src/models";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,7 @@
// Licensed under the MIT license.

import { CommunicationTokenCredential } from "@azure/communication-common";
import {
Constants,
HttpOperationResponse,
WebResourceLike,
BaseRequestPolicy,
RequestPolicy,
RequestPolicyOptions,
RequestPolicyFactory
} from "@azure/core-http";
import { RequestPolicyFactory, bearerTokenAuthenticationPolicy } from "@azure/core-http";

/**
* Creates a new CommunicationTokenCredentialPolicy factory.
Expand All @@ -19,47 +11,11 @@ import {
*/
export const createCommunicationTokenCredentialPolicy = (
credential: CommunicationTokenCredential
): RequestPolicyFactory => ({
create: (nextPolicy: RequestPolicy, options: RequestPolicyOptions) => {
return new CommunicationTokenCredentialPolicy(nextPolicy, options, credential);
}
});

/**
*
* 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 CommunicationTokenCredentialPolicy extends BaseRequestPolicy {
/**
* Creates a new CommunicationTokenCredentialPolicy object.
*
* @param nextPolicy The next RequestPolicy in the request pipeline.
* @param options Options for this RequestPolicy.
* @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: CommunicationTokenCredential
) {
super(nextPolicy, options);
}

/**
* Applies the user credential to the request through the Authorization header.
* @param webResource
*/
public async sendRequest(webResource: WebResourceLike): Promise<HttpOperationResponse> {
if (!webResource) {
throw new Error("webResource cannot be null or undefined");
}

const token = (await this.credential.getToken())?.token;
webResource.headers.set(Constants.HeaderConstants.AUTHORIZATION, `Bearer ${token}`);
return this._nextPolicy.sendRequest(webResource);
}
}
): RequestPolicyFactory => {
return bearerTokenAuthenticationPolicy(
{
getToken: (_scopes, options) => credential.getToken(options?.abortSignal)
},
[]
);
};

0 comments on commit 81d37fa

Please sign in to comment.