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

[communication] switch to bearerTokenAuthenticationPolicy #13220

Merged
merged 2 commits into from
Jan 18, 2021
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
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)
},
[]
);
};