Skip to content

Commit 81d37fa

Browse files
[communication] switch to bearerTokenAuthenticationPolicy (Azure#13220)
1 parent 9b37d20 commit 81d37fa

File tree

3 files changed

+11
-55
lines changed

3 files changed

+11
-55
lines changed

sdk/communication/communication-chat/src/chatClient.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ import {
1616
TypingIndicatorReceivedEvent
1717
} from "@azure/communication-signaling";
1818
import { getSignalingClient } from "./signaling/signalingClient";
19-
import { createCommunicationTokenCredentialPolicy } from "./credential/communicationTokenCredentialPolicy";
2019
import { ChatApiClient } from "./generated/src/chatApiClient";
2120
import {
2221
InternalPipelineOptions,
@@ -43,6 +42,7 @@ import {
4342
} from "./models/mappers";
4443
import { ChatThreadInfo } from "./generated/src/models";
4544
import { CreateChatThreadRequest } from "./models/requests";
45+
import { createCommunicationTokenCredentialPolicy } from "./credential/communicationTokenCredentialPolicy";
4646

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

sdk/communication/communication-chat/src/chatThreadClient.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import {
77
CommunicationUserIdentifier,
88
CommunicationTokenCredential
99
} from "@azure/communication-common";
10-
import { createCommunicationTokenCredentialPolicy } from "./credential/communicationTokenCredentialPolicy";
1110
import { ChatApiClient } from "./generated/src/chatApiClient";
1211
import {
1312
InternalPipelineOptions,
@@ -50,6 +49,7 @@ import {
5049
mapToChatThreadMemberSdkModel,
5150
mapToReadReceiptSdkModel
5251
} from "./models/mappers";
52+
import { createCommunicationTokenCredentialPolicy } from "./credential/communicationTokenCredentialPolicy";
5353

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

sdk/communication/communication-chat/src/credential/communicationTokenCredentialPolicy.ts

Lines changed: 9 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,7 @@
22
// Licensed under the MIT license.
33

44
import { CommunicationTokenCredential } from "@azure/communication-common";
5-
import {
6-
Constants,
7-
HttpOperationResponse,
8-
WebResourceLike,
9-
BaseRequestPolicy,
10-
RequestPolicy,
11-
RequestPolicyOptions,
12-
RequestPolicyFactory
13-
} from "@azure/core-http";
5+
import { RequestPolicyFactory, bearerTokenAuthenticationPolicy } from "@azure/core-http";
146

157
/**
168
* Creates a new CommunicationTokenCredentialPolicy factory.
@@ -19,47 +11,11 @@ import {
1911
*/
2012
export const createCommunicationTokenCredentialPolicy = (
2113
credential: CommunicationTokenCredential
22-
): RequestPolicyFactory => ({
23-
create: (nextPolicy: RequestPolicy, options: RequestPolicyOptions) => {
24-
return new CommunicationTokenCredentialPolicy(nextPolicy, options, credential);
25-
}
26-
});
27-
28-
/**
29-
*
30-
* Provides a RequestPolicy that can request a token from a CommunicationTokenCredential
31-
* implementation and then apply it to the Authorization header of a request.
32-
*
33-
* @internal
34-
*/
35-
export class CommunicationTokenCredentialPolicy extends BaseRequestPolicy {
36-
/**
37-
* Creates a new CommunicationTokenCredentialPolicy object.
38-
*
39-
* @param nextPolicy The next RequestPolicy in the request pipeline.
40-
* @param options Options for this RequestPolicy.
41-
* @param credential The CommunicationTokenCredential implementation that can supply the user credential.
42-
* @param tokenCache The cache for the most recent AccessToken returned from the CommunicationTokenCredential.
43-
*/
44-
constructor(
45-
nextPolicy: RequestPolicy,
46-
options: RequestPolicyOptions,
47-
private readonly credential: CommunicationTokenCredential
48-
) {
49-
super(nextPolicy, options);
50-
}
51-
52-
/**
53-
* Applies the user credential to the request through the Authorization header.
54-
* @param webResource
55-
*/
56-
public async sendRequest(webResource: WebResourceLike): Promise<HttpOperationResponse> {
57-
if (!webResource) {
58-
throw new Error("webResource cannot be null or undefined");
59-
}
60-
61-
const token = (await this.credential.getToken())?.token;
62-
webResource.headers.set(Constants.HeaderConstants.AUTHORIZATION, `Bearer ${token}`);
63-
return this._nextPolicy.sendRequest(webResource);
64-
}
65-
}
14+
): RequestPolicyFactory => {
15+
return bearerTokenAuthenticationPolicy(
16+
{
17+
getToken: (_scopes, options) => credential.getToken(options?.abortSignal)
18+
},
19+
[]
20+
);
21+
};

0 commit comments

Comments
 (0)