-
Notifications
You must be signed in to change notification settings - Fork 1.3k
[core-client] authorizeRequestOnClaimChallenge #17315
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 10 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
dce7c17
[core-rest-pipeline] armChallengeAuthenticationPolicy
sadasant 90808dc
no policy, just the callbacks on core-client WIP
sadasant 955ec78
exporting just one of the challenge callbacks
sadasant 10bcbd8
testing the core-client authorizeRequestOnClaimChallenge
sadasant a678671
Merge remote-tracking branch 'Azure/main' into core-rest-pipeline/CAE…
sadasant 4b36f5a
unnecessary change undone
sadasant c8936eb
better changelog and code updates to the auth challenge
sadasant bcce127
tests on the core-client
sadasant e43f1b3
no more recursive dependencies
sadasant 72e8ed7
fixed browser tests
sadasant 6d42185
Node 10 fix
sadasant 4eab71d
Update sdk/core/core-client/src/base64.browser.ts
sadasant 346b812
feedback from Jeff and formatting
sadasant 4635468
escape wasnt needed
sadasant c99666b
decodeStringToString, feedback from Jeff
sadasant 912ccd6
Merge remote-tracking branch 'Azure/main' into core-rest-pipeline/CAE…
sadasant File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
96 changes: 96 additions & 0 deletions
96
sdk/core/core-client/src/authorizeRequestOnClaimChallenge.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,96 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT license. | ||
|
|
||
| import { GetTokenOptions } from "@azure/core-auth"; | ||
| import { AuthorizeRequestOnChallengeOptions } from "@azure/core-rest-pipeline"; | ||
| import { decodeString, uint8ArrayToString } from "./base64"; | ||
|
|
||
| /** | ||
| * Converts: `Bearer a="b", c="d", Bearer d="e", f="g"`. | ||
| * Into: `[ { a: 'b', c: 'd' }, { d: 'e', f: 'g' } ]`. | ||
| * | ||
| * @internal | ||
| */ | ||
| export function parseCAEChallenge(challenges: string): any[] { | ||
| return `, ${challenges.trim()}` | ||
| .split(", Bearer ") | ||
| .filter((x) => x) | ||
| .map((challenge) => | ||
| `${challenge.trim()}, ` | ||
| .split('", ') | ||
| .filter((x) => x) | ||
| .map((keyValue) => (([key, value]) => ({ [key]: value }))(keyValue.trim().split('="'))) | ||
| .reduce((a, b) => ({ ...a, ...b }), {}) | ||
| ); | ||
sadasant marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| /** | ||
| * CAE Challenge structure | ||
| */ | ||
| export interface CAEChallenge { | ||
| scope: string; | ||
| claims: string; | ||
| } | ||
|
|
||
| /** | ||
| * This function can be used as a callback for the `bearerTokenAuthenticationPolicy` of `@azure/core-rest-pipeline`, to support CAE challenges: | ||
| * [Continuous Access Evaluation](https://docs.microsoft.com/azure/active-directory/conditional-access/concept-continuous-access-evaluation). | ||
| * | ||
| * Call the `bearerTokenAuthenticationPolicy` with the following options: | ||
| * | ||
| * ```ts | ||
| * import { bearerTokenAuthenticationPolicy } from "@azure/core-rest-pipeline"; | ||
| * import { authorizeRequestOnClaimChallenge } from "@azure/core-client"; | ||
| * | ||
| * const bearerTokenAuthenticationPolicy = bearerTokenAuthenticationPolicy({ | ||
| * authorizeRequestOnChallenge: authorizeRequestOnClaimChallenge | ||
| * }); | ||
| * ``` | ||
| * | ||
| * Once provided, the `bearerTokenAuthenticationPolicy` policy will internally handle Continuous Access Evaluation (CAE) challenges. | ||
| * When it can't complete a challenge it will return the 401 (unauthorized) response from ARM. | ||
| * | ||
| * Example challenge with claims: | ||
| * | ||
| * ``` | ||
| * Bearer authorization_uri="https://login.windows-ppe.net/", error="invalid_token", | ||
| * error_description="User session has been revoked", | ||
| * claims="eyJhY2Nlc3NfdG9rZW4iOnsibmJmIjp7ImVzc2VudGlhbCI6dHJ1ZSwgInZhbHVlIjoiMTYwMzc0MjgwMCJ9fX0=" | ||
| * ``` | ||
| */ | ||
| export async function authorizeRequestOnClaimChallenge( | ||
| onChallengeOptions: AuthorizeRequestOnChallengeOptions | ||
| ): Promise<boolean> { | ||
| const { scopes, response } = onChallengeOptions; | ||
|
|
||
| const challenge = response.headers.get("WWW-Authenticate"); | ||
| if (!challenge) { | ||
| console.log( | ||
sadasant marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| `The WWW-Authenticate header was missing. Failed to perform the Continuous Access Evaluation authentication flow.` | ||
| ); | ||
| return false; | ||
| } | ||
| const challenges: CAEChallenge[] = parseCAEChallenge(challenge) || []; | ||
|
|
||
| const parsedChallenge = challenges.find((x) => x.claims); | ||
| if (!parsedChallenge) { | ||
| console.log( | ||
| `The WWW-Authenticate header was missing the necessary "claims" to perform the Continuous Access Evaluation authentication flow.` | ||
| ); | ||
| return false; | ||
| } | ||
|
|
||
| const accessToken = await onChallengeOptions.getAccessToken( | ||
| parsedChallenge.scope ? [parsedChallenge.scope] : scopes, | ||
| { | ||
| claims: uint8ArrayToString(decodeString(parsedChallenge.claims)) | ||
sadasant marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } as GetTokenOptions | ||
| ); | ||
|
|
||
| if (!accessToken) { | ||
| return false; | ||
| } | ||
|
|
||
| onChallengeOptions.request.headers.set("Authorization", `Bearer ${accessToken.token}`); | ||
| return true; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.