|
| 1 | +// Copyright (c) Microsoft Corporation. |
| 2 | +// Licensed under the MIT license. |
| 3 | + |
| 4 | +import { |
| 5 | + AuthorizeRequestOnChallengeOptions, |
| 6 | + PipelineRequest, |
| 7 | + PipelineResponse, |
| 8 | +} from "@azure/core-rest-pipeline"; |
| 9 | + |
| 10 | +import { GetTokenOptions } from "@azure/core-auth"; |
| 11 | + |
| 12 | +/** |
| 13 | + * A set of constants used internally when processing requests. |
| 14 | + */ |
| 15 | +const Constants = { |
| 16 | + DefaultScope: "/.default", |
| 17 | + /** |
| 18 | + * Defines constants for use with HTTP headers. |
| 19 | + */ |
| 20 | + HeaderConstants: { |
| 21 | + /** |
| 22 | + * The Authorization header. |
| 23 | + */ |
| 24 | + AUTHORIZATION: "authorization", |
| 25 | + }, |
| 26 | +}; |
| 27 | + |
| 28 | +/** |
| 29 | + * Defines a callback to handle auth challenge for Storage APIs. |
| 30 | + * This implements the bearer challenge process described here: https://docs.microsoft.com/rest/api/storageservices/authorize-with-azure-active-directory#bearer-challenge |
| 31 | + * Handling has specific features for storage that departs to the general AAD challenge docs. |
| 32 | + **/ |
| 33 | +export const authorizeRequestOnTenantChallenge: ( |
| 34 | + challengeOptions: AuthorizeRequestOnChallengeOptions |
| 35 | +) => Promise<boolean> = async (challengeOptions) => { |
| 36 | + const requestOptions = requestToOptions(challengeOptions.request); |
| 37 | + const challenge = getChallenge(challengeOptions.response); |
| 38 | + if (challenge) { |
| 39 | + const challengeInfo: Challenge = parseChallenge(challenge); |
| 40 | + const challengeScopes = buildScopes(challengeOptions, challengeInfo); |
| 41 | + const tenantId = extractTenantId(challengeInfo); |
| 42 | + const accessToken = await challengeOptions.getAccessToken(challengeScopes, { |
| 43 | + ...requestOptions, |
| 44 | + tenantId, |
| 45 | + }); |
| 46 | + |
| 47 | + if (!accessToken) { |
| 48 | + return false; |
| 49 | + } |
| 50 | + |
| 51 | + challengeOptions.request.headers.set( |
| 52 | + Constants.HeaderConstants.AUTHORIZATION, |
| 53 | + `Bearer ${accessToken.token}` |
| 54 | + ); |
| 55 | + return true; |
| 56 | + } |
| 57 | + return false; |
| 58 | +}; |
| 59 | + |
| 60 | +/** |
| 61 | + * Extracts the tenant id from the challenge information |
| 62 | + * The tenant id is contained in the authorization_uri as the first |
| 63 | + * path part. |
| 64 | + */ |
| 65 | +function extractTenantId(challengeInfo: Challenge): string { |
| 66 | + const parsedAuthUri = new URL(challengeInfo.authorization_uri); |
| 67 | + const pathSegments = parsedAuthUri.pathname.split("/"); |
| 68 | + const tenantId = pathSegments[1]; |
| 69 | + |
| 70 | + return tenantId; |
| 71 | +} |
| 72 | + |
| 73 | +/** |
| 74 | + * Builds the authentication scopes based on the information that comes in the |
| 75 | + * challenge information. Scopes url is present in the resource_id, if it is empty |
| 76 | + * we keep using the original scopes. |
| 77 | + */ |
| 78 | +function buildScopes( |
| 79 | + challengeOptions: AuthorizeRequestOnChallengeOptions, |
| 80 | + challengeInfo: Challenge |
| 81 | +): string[] { |
| 82 | + if (!challengeInfo.resource_uri) { |
| 83 | + return challengeOptions.scopes; |
| 84 | + } |
| 85 | + |
| 86 | + const challengeScopes = new URL(challengeInfo.resource_uri); |
| 87 | + challengeScopes.pathname = Constants.DefaultScope; |
| 88 | + return [challengeScopes.toString()]; |
| 89 | +} |
| 90 | + |
| 91 | +/** |
| 92 | + * We will retrieve the challenge only if the response status code was 401, |
| 93 | + * and if the response contained the header "WWW-Authenticate" with a non-empty value. |
| 94 | + */ |
| 95 | +function getChallenge(response: PipelineResponse): string | undefined { |
| 96 | + const challenge = response.headers.get("WWW-Authenticate"); |
| 97 | + if (response.status === 401 && challenge) { |
| 98 | + return challenge; |
| 99 | + } |
| 100 | + return; |
| 101 | +} |
| 102 | + |
| 103 | +/** |
| 104 | + * Challenge structure |
| 105 | + */ |
| 106 | +interface Challenge { |
| 107 | + authorization_uri: string; |
| 108 | + resource_uri?: string; |
| 109 | +} |
| 110 | + |
| 111 | +/** |
| 112 | + * Converts: `Bearer a="b" c="d"`. |
| 113 | + * Into: `[ { a: 'b', c: 'd' }]`. |
| 114 | + * |
| 115 | + * @internal |
| 116 | + */ |
| 117 | +function parseChallenge(challenge: string): Challenge { |
| 118 | + const bearerChallenge = challenge.slice("Bearer ".length); |
| 119 | + const challengeParts = `${bearerChallenge.trim()} `.split(" ").filter((x) => x); |
| 120 | + const keyValuePairs = challengeParts.map((keyValue) => |
| 121 | + (([key, value]) => ({ [key]: value }))(keyValue.trim().split("=")) |
| 122 | + ); |
| 123 | + // Key-value pairs to plain object: |
| 124 | + return keyValuePairs.reduce((a, b) => ({ ...a, ...b }), {} as Challenge); |
| 125 | +} |
| 126 | + |
| 127 | +/** |
| 128 | + * Extracts the options form a Pipeline Request for later re-use |
| 129 | + */ |
| 130 | +function requestToOptions(request: PipelineRequest): GetTokenOptions { |
| 131 | + return { |
| 132 | + abortSignal: request.abortSignal, |
| 133 | + requestOptions: { |
| 134 | + timeout: request.timeout, |
| 135 | + }, |
| 136 | + tracingOptions: request.tracingOptions, |
| 137 | + }; |
| 138 | +} |
0 commit comments