|
| 1 | +// Copyright (c) Microsoft Corporation. |
| 2 | +// Licensed under the MIT license. |
| 3 | + |
| 4 | +import type { TokenCredential, TracingContext } from "@azure/core-auth"; |
| 5 | +import { |
| 6 | + bearerTokenAuthenticationPolicy, |
| 7 | + createEmptyPipeline, |
| 8 | + createPipelineRequest, |
| 9 | +} from "@azure/core-rest-pipeline"; |
| 10 | + |
| 11 | +/** |
| 12 | + * The options to configure the token provider. |
| 13 | + */ |
| 14 | +export interface GetBearerTokenProviderOptions { |
| 15 | + /** The abort signal to abort requests to get tokens */ |
| 16 | + abortSignal?: AbortSignal; |
| 17 | + /** The tracing options for the requests to get tokens */ |
| 18 | + tracingOptions?: { |
| 19 | + /** |
| 20 | + * Tracing Context for the current request to get a token. |
| 21 | + */ |
| 22 | + tracingContext?: TracingContext; |
| 23 | + }; |
| 24 | +} |
| 25 | + |
| 26 | +/** |
| 27 | + * Returns a callback that provides a bearer token. |
| 28 | + * For example, the bearer token can be used to authenticate a request as follows: |
| 29 | + * ```js |
| 30 | + * import { DefaultAzureCredential } from "@azure/identity"; |
| 31 | + * |
| 32 | + * const credential = new DefaultAzureCredential(); |
| 33 | + * const scope = "https://cognitiveservices.azure.com/.default"; |
| 34 | + * const getAccessToken = getBearerTokenProvider(credential, scope); |
| 35 | + * const token = await getAccessToken(); |
| 36 | + * |
| 37 | + * // usage |
| 38 | + * const request = createPipelineRequest({ url: "https://example.com" }); |
| 39 | + * request.headers.set("Authorization", `Bearer ${token}`); |
| 40 | + * ``` |
| 41 | + * |
| 42 | + * @param credential - The credential used to authenticate the request. |
| 43 | + * @param scopes - The scopes required for the bearer token. |
| 44 | + * @param options - Options to configure the token provider. |
| 45 | + * @returns a callback that provides a bearer token. |
| 46 | + */ |
| 47 | +export function getBearerTokenProvider( |
| 48 | + credential: TokenCredential, |
| 49 | + scopes: string | string[], |
| 50 | + options?: GetBearerTokenProviderOptions, |
| 51 | +): () => Promise<string> { |
| 52 | + const { abortSignal, tracingOptions } = options || {}; |
| 53 | + const pipeline = createEmptyPipeline(); |
| 54 | + pipeline.addPolicy(bearerTokenAuthenticationPolicy({ credential, scopes })); |
| 55 | + async function getRefreshedToken(): Promise<string> { |
| 56 | + // Create a pipeline with just the bearer token policy |
| 57 | + // and run a dummy request through it to get the token |
| 58 | + const res = await pipeline.sendRequest( |
| 59 | + { |
| 60 | + sendRequest: (request) => |
| 61 | + Promise.resolve({ |
| 62 | + request, |
| 63 | + status: 200, |
| 64 | + headers: request.headers, |
| 65 | + }), |
| 66 | + }, |
| 67 | + createPipelineRequest({ |
| 68 | + url: "https://example.com", |
| 69 | + abortSignal, |
| 70 | + tracingOptions, |
| 71 | + }), |
| 72 | + ); |
| 73 | + const accessToken = res.headers.get("authorization")?.split(" ")[1]; |
| 74 | + if (!accessToken) { |
| 75 | + throw new Error("Failed to get access token"); |
| 76 | + } |
| 77 | + return accessToken; |
| 78 | + } |
| 79 | + return getRefreshedToken; |
| 80 | +} |
0 commit comments