diff --git a/sdk/communication/communication-chat/src/chatClient.ts b/sdk/communication/communication-chat/src/chatClient.ts index 724fa3c1ccaa..c8c8d81b7e73 100644 --- a/sdk/communication/communication-chat/src/chatClient.ts +++ b/sdk/communication/communication-chat/src/chatClient.ts @@ -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, @@ -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"; diff --git a/sdk/communication/communication-chat/src/chatThreadClient.ts b/sdk/communication/communication-chat/src/chatThreadClient.ts index 464bcce1db5a..40c96b3ff828 100644 --- a/sdk/communication/communication-chat/src/chatThreadClient.ts +++ b/sdk/communication/communication-chat/src/chatThreadClient.ts @@ -7,7 +7,6 @@ import { CommunicationUserIdentifier, CommunicationTokenCredential } from "@azure/communication-common"; -import { createCommunicationTokenCredentialPolicy } from "./credential/communicationTokenCredentialPolicy"; import { ChatApiClient } from "./generated/src/chatApiClient"; import { InternalPipelineOptions, @@ -50,6 +49,7 @@ import { mapToChatThreadMemberSdkModel, mapToReadReceiptSdkModel } from "./models/mappers"; +import { createCommunicationTokenCredentialPolicy } from "./credential/communicationTokenCredentialPolicy"; export { ChatMessagePriority, SendReadReceiptRequest } from "./generated/src/models"; diff --git a/sdk/communication/communication-chat/src/credential/communicationTokenCredentialPolicy.ts b/sdk/communication/communication-chat/src/credential/communicationTokenCredentialPolicy.ts index 2b5cd6c97039..fe9a499261e9 100644 --- a/sdk/communication/communication-chat/src/credential/communicationTokenCredentialPolicy.ts +++ b/sdk/communication/communication-chat/src/credential/communicationTokenCredentialPolicy.ts @@ -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. @@ -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 { - 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) + }, + [] + ); +};