22// Licensed under the MIT license.
33
44import { 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 */
2012export 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