From 94a0c46257f5505332262142a286aa2e7cec7cbc Mon Sep 17 00:00:00 2001 From: Steven Yuan Date: Wed, 13 Sep 2023 12:01:49 -0700 Subject: [PATCH 1/2] Add additional `HttpAuthScheme` interfaces for auth scheme resolution --- .changeset/gorgeous-rabbits-collect.md | 5 ++++ .../src/HttpAuthScheme.ts | 9 +++++++ .../src/HttpAuthSchemeProvider.ts | 27 +++++++++++++++++++ .../src/index.ts | 1 + 4 files changed, 42 insertions(+) create mode 100644 .changeset/gorgeous-rabbits-collect.md create mode 100644 packages/experimental-identity-and-auth/src/HttpAuthSchemeProvider.ts diff --git a/.changeset/gorgeous-rabbits-collect.md b/.changeset/gorgeous-rabbits-collect.md new file mode 100644 index 00000000000..39f6fded7e6 --- /dev/null +++ b/.changeset/gorgeous-rabbits-collect.md @@ -0,0 +1,5 @@ +--- +"@smithy/experimental-identity-and-auth": patch +--- + +Add additional `HttpAuthScheme` interfaces for auth scheme resolution diff --git a/packages/experimental-identity-and-auth/src/HttpAuthScheme.ts b/packages/experimental-identity-and-auth/src/HttpAuthScheme.ts index ad70e632eb6..110f3404244 100644 --- a/packages/experimental-identity-and-auth/src/HttpAuthScheme.ts +++ b/packages/experimental-identity-and-auth/src/HttpAuthScheme.ts @@ -38,3 +38,12 @@ export interface HttpAuthOption { identityProperties?: Record; signingProperties?: Record; } + +/** + * @internal + */ +export interface SelectedHttpAuthScheme { + httpAuthOption: HttpAuthOption; + identity: Identity; + signer: HttpSigner; +} diff --git a/packages/experimental-identity-and-auth/src/HttpAuthSchemeProvider.ts b/packages/experimental-identity-and-auth/src/HttpAuthSchemeProvider.ts new file mode 100644 index 00000000000..4f6c42ecca6 --- /dev/null +++ b/packages/experimental-identity-and-auth/src/HttpAuthSchemeProvider.ts @@ -0,0 +1,27 @@ +import { HandlerExecutionContext } from "@smithy/types"; + +import { HttpAuthOption } from "./HttpAuthScheme"; + +/** + * @internal + */ +export interface HttpAuthSchemeParameters { + operation?: string; +} + +/** + * @internal + */ +export interface HttpAuthSchemeProvider { + (authParameters: TParameters): HttpAuthOption[]; +} + +/** + * @internal + */ +export interface HttpAuthSchemeParametersProvider< + C extends object = object, + T extends HttpAuthSchemeParameters = HttpAuthSchemeParameters +> { + (config: C, context: HandlerExecutionContext): Promise; +} diff --git a/packages/experimental-identity-and-auth/src/index.ts b/packages/experimental-identity-and-auth/src/index.ts index 19886670bcb..510f45b8608 100644 --- a/packages/experimental-identity-and-auth/src/index.ts +++ b/packages/experimental-identity-and-auth/src/index.ts @@ -1,4 +1,5 @@ export * from "./HttpAuthScheme"; +export * from "./HttpAuthSchemeProvider"; export * from "./HttpSigner"; export * from "./IdentityProviderConfig"; export * from "./SigV4Signer"; From ca2a77f1f847e934e19b749dcdffc02f340d5108 Mon Sep 17 00:00:00 2001 From: Steven Yuan Date: Wed, 13 Sep 2023 12:08:37 -0700 Subject: [PATCH 2/2] Update `HttpAuthSchemeProviderGenerator` to use new interfaces --- .../http/HttpAuthSchemeProviderGenerator.java | 60 ++++++++++--------- 1 file changed, 33 insertions(+), 27 deletions(-) diff --git a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/auth/http/HttpAuthSchemeProviderGenerator.java b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/auth/http/HttpAuthSchemeProviderGenerator.java index eb2157ee638..c41dc51a1ee 100644 --- a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/auth/http/HttpAuthSchemeProviderGenerator.java +++ b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/auth/http/HttpAuthSchemeProviderGenerator.java @@ -85,20 +85,24 @@ public void run() { } /* - export interface WeatherHttpAuthSchemeParameters { - operation?: string; + import { HttpAuthSchemeParameters } from "@smithy/types"; + + // ... + + export interface WeatherHttpAuthSchemeParameters extends HttpAuthSchemeParameters { } */ private void generateHttpAuthSchemeParametersInterface() { delegator.useFileWriter(AuthUtils.HTTP_AUTH_SCHEME_PROVIDER_PATH, w -> { + w.addDependency(TypeScriptDependency.EXPERIMENTAL_IDENTITY_AND_AUTH); + w.addImport("HttpAuthSchemeParameters", null, TypeScriptDependency.EXPERIMENTAL_IDENTITY_AND_AUTH); w.openBlock(""" /** * @internal */ - export interface $LHttpAuthSchemeParameters {""", "}", + export interface $LHttpAuthSchemeParameters extends HttpAuthSchemeParameters {""", "}", serviceName, () -> { - w.write("operation?: string;"); for (HttpAuthScheme authScheme : authIndex.getSupportedHttpAuthSchemes().values()) { for (HttpAuthSchemeParameter parameter : authScheme.getHttpAuthSchemeParameters()) { w.write("$L?: $C;", parameter.name(), parameter.type()); @@ -110,32 +114,31 @@ export interface $LHttpAuthSchemeParameters {""", "}", /* import { WeatherClientResolvedConfig } from "../WeatherClient"; - import { HandlerExecutionContext } from "@smithy/types"; + import { HttpAuthSchemeParametersProvider } from "@smithy/types"; // ... - export async function defaultWeatherHttpAuthSchemeParametersProvider( - config: WeatherClientResolvedConfig, - context: HandlerExecutionContext - ): Promise { - return { - operation: context.commandName, - }; + export const defaultWeatherHttpAuthSchemeParametersProvider: + HttpAuthSchemeParametersProvider = + async (config, context) => { + return { + operation: context.commandName, + }; }; */ private void generateDefaultHttpAuthSchemeParametersProviderFunction() { delegator.useFileWriter(AuthUtils.HTTP_AUTH_SCHEME_PROVIDER_PATH, w -> { w.addRelativeImport(serviceSymbol.getName() + "ResolvedConfig", null, Paths.get(".", serviceSymbol.getNamespace())); - w.addImport("HandlerExecutionContext", null, TypeScriptDependency.SMITHY_TYPES); + w.addDependency(TypeScriptDependency.EXPERIMENTAL_IDENTITY_AND_AUTH); + w.addImport("HttpAuthSchemeParametersProvider", null, TypeScriptDependency.EXPERIMENTAL_IDENTITY_AND_AUTH); w.openBlock(""" /** * @internal */ - export async function default$LHttpAuthSchemeParametersProvider( - config: $LResolvedConfig, - context: HandlerExecutionContext - ): Promise<$LHttpAuthSchemeParameters> {""", "};", + export const default$LHttpAuthSchemeParametersProvider: \ + HttpAuthSchemeParametersProvider<$LResolvedConfig, $LHttpAuthSchemeParameters> = \ + async (config, context) => {""", "};", serviceName, serviceSymbol.getName(), serviceName, () -> { w.openBlock("return {", "};", () -> { @@ -223,26 +226,28 @@ private static String normalizeAuthSchemeName(ShapeId shapeId) { } /* - export interface WeatherHttpAuthSchemeProvider { - (authParameters: WeatherHttpAuthSchemeParameters): HttpAuthOption[]; - } + import { HttpAuthSchemeProvider } from "@smithy/types"; + + // ... + + export interface WeatherHttpAuthSchemeProvider extends HttpAuthSchemeProvider {} */ private void generateHttpAuthSchemeProviderInterface() { delegator.useFileWriter(AuthUtils.HTTP_AUTH_SCHEME_PROVIDER_PATH, w -> { + w.addDependency(TypeScriptDependency.EXPERIMENTAL_IDENTITY_AND_AUTH); + w.addImport("HttpAuthSchemeProvider", null, TypeScriptDependency.EXPERIMENTAL_IDENTITY_AND_AUTH); w.write(""" /** * @internal */ - export interface $LHttpAuthSchemeProvider { - (authParameters: $LHttpAuthSchemeParameters): HttpAuthOption[]; - } + export interface $LHttpAuthSchemeProvider extends HttpAuthSchemeProvider<$LHttpAuthSchemeParameters> {} """, serviceName, serviceName); }); } /* - export function defaultWeatherHttpAuthSchemeProvider(authParameters: WeatherHttpAuthSchemeParameters): - HttpAuthOption[] { + export const defaultWeatherHttpAuthSchemeProvider: WeatherHttpAuthSchemeProvider = + (authParameters) => { const options: HttpAuthOption[] = []; switch (authParameters.operation) { default: { @@ -258,8 +263,9 @@ private void generateHttpAuthSchemeProviderDefaultFunction() { /** * @internal */ - export function default$LHttpAuthSchemeProvider(authParameters: $LHttpAuthSchemeParameters): \ - HttpAuthOption[] {""", "};", serviceName, serviceName, () -> { + export const default$LHttpAuthSchemeProvider: $LHttpAuthSchemeProvider = \ + (authParameters) => {""", "};", + serviceName, serviceName, () -> { w.write("const options: HttpAuthOption[] = [];"); w.openBlock("switch (authParameters.operation) {", "};", () -> { var serviceAuthSchemes = serviceIndex.getEffectiveAuthSchemes(