-
Notifications
You must be signed in to change notification settings - Fork 109
feat(core): resolve auth schemes based on the preference list #1571
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 all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
07bcfce
chore(core): add utility resolveAuthSchemes
trivikr 56bff6b
chore: yarn changeset
trivikr a61d087
chore: format resolveAuthSchemes.ts
trivikr 4e6fa23
test: resolveAuthSchemes.spec.ts
trivikr 22ed5f0
feat: resolve auth schemes based on the preference list
trivikr d0647d0
fix: formatting of resolveAuthSchemes.spec.ts
trivikr 740779c
fix: set authSchemePreference to empty for backward compatibility
trivikr ad8ab61
fix: resolve auth options instead of auth schemes
trivikr 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "@smithy/core": minor | ||
| --- | ||
|
|
||
| Resolve auth schemes based on the preference list |
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
42 changes: 42 additions & 0 deletions
42
packages/core/src/middleware-http-auth-scheme/resolveAuthOptions.spec.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,42 @@ | ||
| import { HttpAuthOption } from "@smithy/types"; | ||
| import { describe, expect, it } from "vitest"; | ||
|
|
||
| import { resolveAuthOptions } from "./resolveAuthOptions"; | ||
|
|
||
| describe("resolveAuthSchemes", () => { | ||
| const sigv4 = "sigv4"; | ||
| const sigv4a = "sigv4a"; | ||
|
|
||
| const mockSigV4AuthScheme = { schemeId: `aws.auth#${sigv4}` } as HttpAuthOption; | ||
| const mockSigV4aAuthScheme = { schemeId: `aws.auth#${sigv4a}` } as HttpAuthOption; | ||
|
|
||
| it("should return candidate auth schemes is preference list is not available", () => { | ||
| const candidateAuthSchemes = [mockSigV4AuthScheme, mockSigV4aAuthScheme]; | ||
| expect(resolveAuthOptions(candidateAuthSchemes, [])).toEqual(candidateAuthSchemes); | ||
|
|
||
| // @ts-expect-error case where callee incorrectly passes undefined | ||
| expect(resolveAuthOptions(candidateAuthSchemes)).toEqual(candidateAuthSchemes); | ||
| }); | ||
|
|
||
| it("should return auth scheme from preference if it's available", () => { | ||
| expect(resolveAuthOptions([mockSigV4AuthScheme, mockSigV4aAuthScheme], [sigv4a])).toEqual([ | ||
| mockSigV4aAuthScheme, | ||
| mockSigV4AuthScheme, | ||
| ]); | ||
|
|
||
| expect(resolveAuthOptions([mockSigV4AuthScheme, mockSigV4aAuthScheme], [sigv4a, sigv4])).toEqual([ | ||
| mockSigV4aAuthScheme, | ||
| mockSigV4AuthScheme, | ||
| ]); | ||
|
|
||
| expect(resolveAuthOptions([mockSigV4AuthScheme, mockSigV4aAuthScheme], [sigv4, sigv4a])).toEqual([ | ||
| mockSigV4AuthScheme, | ||
| mockSigV4aAuthScheme, | ||
| ]); | ||
| }); | ||
|
|
||
| it("should ignore auth scheme from preference if it's not available", () => { | ||
| expect(resolveAuthOptions([mockSigV4AuthScheme], [sigv4a])).toEqual([mockSigV4AuthScheme]); | ||
| expect(resolveAuthOptions([mockSigV4AuthScheme], ["sigv3"])).toEqual([mockSigV4AuthScheme]); | ||
| }); | ||
| }); |
39 changes: 39 additions & 0 deletions
39
packages/core/src/middleware-http-auth-scheme/resolveAuthOptions.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,39 @@ | ||
| import { HttpAuthOption } from "@smithy/types"; | ||
|
|
||
| /** | ||
| * Resolves list of auth options based on the supported ones, vs the preference list. | ||
| * | ||
| * @param candidateAuthOptions list of supported auth options selected by the standard | ||
| * resolution process (model-based, endpoints 2.0, etc.) | ||
| * @param authSchemePreference list of auth schemes preferred by user. | ||
| * @returns | ||
| */ | ||
| export const resolveAuthOptions = ( | ||
| candidateAuthOptions: HttpAuthOption[], | ||
| authSchemePreference: string[] | ||
| ): HttpAuthOption[] => { | ||
| if (!authSchemePreference || authSchemePreference.length === 0) { | ||
| return candidateAuthOptions; | ||
| } | ||
|
|
||
| // reprioritize candidates based on user's preference | ||
| const preferredAuthOptions = []; | ||
|
|
||
| for (const preferredSchemeName of authSchemePreference) { | ||
| for (const candidateAuthOption of candidateAuthOptions) { | ||
| const candidateAuthSchemeName = candidateAuthOption.schemeId.split("#")[1]; | ||
| if (candidateAuthSchemeName === preferredSchemeName) { | ||
| preferredAuthOptions.push(candidateAuthOption); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // add any remaining candidates that weren't in the preference list | ||
| for (const candidateAuthOption of candidateAuthOptions) { | ||
| if (!preferredAuthOptions.find(({ schemeId }) => schemeId === candidateAuthOption.schemeId)) { | ||
| preferredAuthOptions.push(candidateAuthOption); | ||
| } | ||
| } | ||
|
|
||
| return preferredAuthOptions; | ||
| }; | ||
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.