Skip to content

Instantly share code, notes, and snippets.

@syall
Last active September 5, 2023 22:53
Show Gist options
  • Save syall/03a85aa0578d37a78413d0689cdfb1ad to your computer and use it in GitHub Desktop.
Save syall/03a85aa0578d37a78413d0689cdfb1ad to your computer and use it in GitHub Desktop.
pr-#907.diff
diff --color -Nur smithy-typescript-codegen-test/build/smithyprojections/smithy-typescript-codegen-test/control-experimental-identity-and-auth/typescript-codegen/package.json smithy-typescript-codegen-test/build/smithyprojections/smithy-typescript-codegen-test/client-experimental-identity-and-auth/typescript-codegen/package.json
--- smithy-typescript-codegen-test/build/smithyprojections/smithy-typescript-codegen-test/control-experimental-identity-and-auth/typescript-codegen/package.json 2023-09-05 15:50:34
+++ smithy-typescript-codegen-test/build/smithyprojections/smithy-typescript-codegen-test/client-experimental-identity-and-auth/typescript-codegen/package.json 2023-09-05 15:50:34
@@ -10,8 +10,7 @@
"build:types": "tsc -p tsconfig.types.json",
"build:types:downlevel": "downlevel-dts dist-types dist-types/ts3.4",
"clean": "rimraf ./dist-* && rimraf *.tsbuildinfo",
- "prepack": "yarn run clean && yarn run build",
- "test": "vitest run"
+ "prepack": "yarn run clean && yarn run build"
},
"main": "./dist-cjs/index.js",
"types": "./dist-types/index.d.ts",
@@ -26,6 +25,7 @@
"@smithy/eventstream-serde-browser": "^2.0.5",
"@smithy/eventstream-serde-config-resolver": "^2.0.5",
"@smithy/eventstream-serde-node": "^2.0.5",
+ "@smithy/experimental-identity-and-auth": "~0.0.1",
"@smithy/fetch-http-handler": "^2.0.5",
"@smithy/hash-blob-browser": "^2.0.5",
"@smithy/hash-node": "^2.0.5",
@@ -62,8 +62,7 @@
"typedoc": "0.23.23",
"typescript": "~4.9.5",
"@smithy/service-client-documentation-generator": "^2.0.0",
- "@types/node": "^14.14.31",
- "vitest": "^0.33.0"
+ "@types/node": "^14.14.31"
},
"engines": {
"node": ">=14.0.0"
diff --color -Nur smithy-typescript-codegen-test/build/smithyprojections/smithy-typescript-codegen-test/control-experimental-identity-and-auth/typescript-codegen/src/WeatherClient.ts smithy-typescript-codegen-test/build/smithyprojections/smithy-typescript-codegen-test/client-experimental-identity-and-auth/typescript-codegen/src/WeatherClient.ts
--- smithy-typescript-codegen-test/build/smithyprojections/smithy-typescript-codegen-test/control-experimental-identity-and-auth/typescript-codegen/src/WeatherClient.ts 2023-09-05 15:50:34
+++ smithy-typescript-codegen-test/build/smithyprojections/smithy-typescript-codegen-test/client-experimental-identity-and-auth/typescript-codegen/src/WeatherClient.ts 2023-09-05 15:50:34
@@ -1,4 +1,5 @@
// smithy-typescript generated code
+import { WeatherHttpAuthSchemeProvider } from "./auth/httpAuthSchemeProvider";
import {
CreateCityCommandInput,
CreateCityCommandOutput,
@@ -75,11 +76,6 @@
SameAsServiceCommandInput,
SameAsServiceCommandOutput,
} from "./commands/SameAsServiceCommand";
-import {
- HttpApiKeyAuthInputConfig,
- HttpApiKeyAuthResolvedConfig,
- resolveHttpApiKeyAuthConfig,
-} from "./middleware/HttpApiKeyAuth";
import { getRuntimeConfig as __getRuntimeConfig } from "./runtimeConfig";
import {
RuntimeExtension,
@@ -96,6 +92,13 @@
EventStreamSerdeResolvedConfig,
resolveEventStreamSerdeConfig,
} from "@smithy/eventstream-serde-config-resolver";
+import {
+ ApiKeyIdentity,
+ ApiKeyIdentityProvider,
+ HttpAuthScheme,
+ TokenIdentity,
+ TokenIdentityProvider,
+} from "@smithy/experimental-identity-and-auth";
import { getContentLengthPlugin } from "@smithy/middleware-content-length";
import {
RetryInputConfig,
@@ -111,6 +114,8 @@
SmithyResolvedConfiguration as __SmithyResolvedConfiguration,
} from "@smithy/smithy-client";
import {
+ AwsCredentialIdentity,
+ AwsCredentialIdentityProvider,
BodyLengthCalculator as __BodyLengthCalculator,
CheckOptionalClientConfig as __CheckOptionalClientConfig,
Checksum as __Checksum,
@@ -252,6 +257,18 @@
disableHostPrefix?: boolean;
/**
+ * experimentalIdentityAndAuth: Configuration of HttpAuthSchemes for a client which provides default identity providers and signers per auth scheme.
+ * @internal
+ */
+ httpAuthSchemes?: HttpAuthScheme[];
+
+ /**
+ * experimentalIdentityAndAuth: Configuration of an HttpAuthSchemeProvider for a client which resolves which HttpAuthScheme to use.
+ * @internal
+ */
+ httpAuthSchemeProvider?: WeatherHttpAuthSchemeProvider;
+
+ /**
* Value for how many times a request will be made at most in case of retry.
*/
maxAttempts?: number | __Provider<number>;
@@ -296,6 +313,26 @@
defaultsMode?: __DefaultsMode | __Provider<__DefaultsMode>;
/**
+ * The API key to use when making requests.
+ */
+ apiKey?: ApiKeyIdentity | ApiKeyIdentityProvider;
+
+ /**
+ * The token used to authenticate requests.
+ */
+ token?: TokenIdentity | TokenIdentityProvider;
+
+ /**
+ * The AWS region to which this client will send requests.
+ */
+ region?: string | __Provider<string>;
+
+ /**
+ * The credentials used to sign requests.
+ */
+ credentials?: AwsCredentialIdentity | AwsCredentialIdentityProvider;
+
+ /**
* The internal function that inject utilities to runtime-specific stream to help users consume the data
* @internal
*/
@@ -311,7 +348,6 @@
& CustomEndpointsInputConfig
& RetryInputConfig
& EventStreamSerdeInputConfig
- & HttpApiKeyAuthInputConfig
/**
* @public
*
@@ -328,7 +364,6 @@
& CustomEndpointsResolvedConfig
& RetryResolvedConfig
& EventStreamSerdeResolvedConfig
- & HttpApiKeyAuthResolvedConfig
/**
* @public
*
@@ -356,10 +391,9 @@
let _config_1 = resolveCustomEndpointsConfig(_config_0);
let _config_2 = resolveRetryConfig(_config_1);
let _config_3 = resolveEventStreamSerdeConfig(_config_2);
- let _config_4 = resolveHttpApiKeyAuthConfig(_config_3);
- let _config_5 = resolveRuntimeExtensions(_config_4, configuration?.extensions || []);
- super(_config_5);
- this.config = _config_5;
+ let _config_4 = resolveRuntimeExtensions(_config_3, configuration?.extensions || []);
+ super(_config_4);
+ this.config = _config_4;
this.middlewareStack.use(getRetryPlugin(this.config));
this.middlewareStack.use(getContentLengthPlugin(this.config));
}
diff --color -Nur smithy-typescript-codegen-test/build/smithyprojections/smithy-typescript-codegen-test/control-experimental-identity-and-auth/typescript-codegen/src/auth/httpAuthSchemeProvider.ts smithy-typescript-codegen-test/build/smithyprojections/smithy-typescript-codegen-test/client-experimental-identity-and-auth/typescript-codegen/src/auth/httpAuthSchemeProvider.ts
--- smithy-typescript-codegen-test/build/smithyprojections/smithy-typescript-codegen-test/control-experimental-identity-and-auth/typescript-codegen/src/auth/httpAuthSchemeProvider.ts 1969-12-31 16:00:00
+++ smithy-typescript-codegen-test/build/smithyprojections/smithy-typescript-codegen-test/client-experimental-identity-and-auth/typescript-codegen/src/auth/httpAuthSchemeProvider.ts 2023-09-05 15:50:34
@@ -0,0 +1,132 @@
+// smithy-typescript generated code
+import { WeatherClientResolvedConfig } from "../WeatherClient";
+import {
+ HttpApiKeyAuthLocation,
+ HttpAuthOption,
+} from "@smithy/experimental-identity-and-auth";
+import { HandlerExecutionContext } from "@smithy/types";
+import { normalizeProvider } from "@smithy/util-middleware";
+
+/**
+ * @internal
+ */
+export interface WeatherHttpAuthSchemeParameters {
+ operation?: string;
+ region?: string;
+}
+
+/**
+ * @internal
+ */
+export async function defaultWeatherHttpAuthSchemeParametersProvider(
+ config: WeatherClientResolvedConfig,
+ context: HandlerExecutionContext
+): Promise<WeatherHttpAuthSchemeParameters> {
+ return {
+ operation: context.commandName,
+ region: await normalizeProvider(config.region)() || (() => {
+ throw new Error("expected `region` to be configured for `aws.auth#sigv4`");
+ })(),
+ };
+};
+
+function createAwsAuthSigv4HttpAuthOption(authParameters: WeatherHttpAuthSchemeParameters): HttpAuthOption {
+ return {
+ schemeId: "aws.auth#sigv4",
+ signingProperties: {
+ name: "weather",
+ region: authParameters.region,
+ },
+ };
+};
+
+function createExampleWeatherCustomAuthHttpAuthOption(authParameters: WeatherHttpAuthSchemeParameters): HttpAuthOption {
+ return {
+ schemeId: "example.weather#customAuth",
+ };
+};
+
+function createSmithyApiHttpApiKeyAuthHttpAuthOption(authParameters: WeatherHttpAuthSchemeParameters): HttpAuthOption {
+ return {
+ schemeId: "smithy.api#httpApiKeyAuth",
+ signingProperties: {
+ name: "X-Api-Key",
+ in: HttpApiKeyAuthLocation.HEADER,
+ scheme: undefined,
+ },
+ };
+};
+
+function createSmithyApiHttpBearerAuthHttpAuthOption(authParameters: WeatherHttpAuthSchemeParameters): HttpAuthOption {
+ return {
+ schemeId: "smithy.api#httpBearerAuth",
+ };
+};
+
+function createSmithyApiNoAuthHttpAuthOption(authParameters: WeatherHttpAuthSchemeParameters): HttpAuthOption {
+ return {
+ schemeId: "smithy.api#noAuth",
+ };
+};
+
+/**
+ * @internal
+ */
+export interface WeatherHttpAuthSchemeProvider {
+ (authParameters: WeatherHttpAuthSchemeParameters): HttpAuthOption[];
+}
+
+/**
+ * @internal
+ */
+export function defaultWeatherHttpAuthSchemeProvider(authParameters: WeatherHttpAuthSchemeParameters): HttpAuthOption[] {
+ const options: HttpAuthOption[] = [];
+ switch (authParameters.operation) {
+ case "OnlyHttpApiKeyAuth": {
+ options.push(createSmithyApiHttpApiKeyAuthHttpAuthOption(authParameters));
+ break;
+ };
+ case "OnlyHttpApiKeyAuthOptional": {
+ options.push(createSmithyApiHttpApiKeyAuthHttpAuthOption(authParameters));
+ options.push(createSmithyApiNoAuthHttpAuthOption(authParameters));
+ break;
+ };
+ case "OnlyHttpBearerAuth": {
+ options.push(createSmithyApiHttpBearerAuthHttpAuthOption(authParameters));
+ break;
+ };
+ case "OnlyHttpBearerAuthOptional": {
+ options.push(createSmithyApiHttpBearerAuthHttpAuthOption(authParameters));
+ options.push(createSmithyApiNoAuthHttpAuthOption(authParameters));
+ break;
+ };
+ case "OnlyHttpApiKeyAndBearerAuth": {
+ options.push(createSmithyApiHttpApiKeyAuthHttpAuthOption(authParameters));
+ options.push(createSmithyApiHttpBearerAuthHttpAuthOption(authParameters));
+ break;
+ };
+ case "OnlyHttpApiKeyAndBearerAuthReversed": {
+ options.push(createSmithyApiHttpBearerAuthHttpAuthOption(authParameters));
+ options.push(createSmithyApiHttpApiKeyAuthHttpAuthOption(authParameters));
+ break;
+ };
+ case "OnlySigv4AuthOptional": {
+ options.push(createAwsAuthSigv4HttpAuthOption(authParameters));
+ options.push(createSmithyApiNoAuthHttpAuthOption(authParameters));
+ break;
+ };
+ case "OnlyCustomAuth": {
+ options.push(createExampleWeatherCustomAuthHttpAuthOption(authParameters));
+ break;
+ };
+ case "OnlyCustomAuthOptional": {
+ options.push(createExampleWeatherCustomAuthHttpAuthOption(authParameters));
+ options.push(createSmithyApiNoAuthHttpAuthOption(authParameters));
+ break;
+ };
+ default: {
+ options.push(createAwsAuthSigv4HttpAuthOption(authParameters));
+ };
+ };
+ return options;
+};
diff --color -Nur smithy-typescript-codegen-test/build/smithyprojections/smithy-typescript-codegen-test/control-experimental-identity-and-auth/typescript-codegen/src/commands/OnlyHttpApiKeyAndBearerAuthCommand.ts smithy-typescript-codegen-test/build/smithyprojections/smithy-typescript-codegen-test/client-experimental-identity-and-auth/typescript-codegen/src/commands/OnlyHttpApiKeyAndBearerAuthCommand.ts
--- smithy-typescript-codegen-test/build/smithyprojections/smithy-typescript-codegen-test/control-experimental-identity-and-auth/typescript-codegen/src/commands/OnlyHttpApiKeyAndBearerAuthCommand.ts 2023-09-05 15:50:34
+++ smithy-typescript-codegen-test/build/smithyprojections/smithy-typescript-codegen-test/client-experimental-identity-and-auth/typescript-codegen/src/commands/OnlyHttpApiKeyAndBearerAuthCommand.ts 2023-09-05 15:50:34
@@ -4,7 +4,6 @@
ServiceOutputTypes,
WeatherClientResolvedConfig,
} from "../WeatherClient";
-import { getHttpApiKeyAuthPlugin } from "../middleware/HttpApiKeyAuth";
import {
de_OnlyHttpApiKeyAndBearerAuthCommand,
se_OnlyHttpApiKeyAndBearerAuthCommand,
@@ -64,7 +63,6 @@
options?: __HttpHandlerOptions
): Handler<OnlyHttpApiKeyAndBearerAuthCommandInput, OnlyHttpApiKeyAndBearerAuthCommandOutput> {
this.middlewareStack.use(getSerdePlugin(configuration, this.serialize, this.deserialize));
- this.middlewareStack.use(getHttpApiKeyAuthPlugin(configuration, { in: 'header', name: 'X-Api-Key'}));
const stack = clientStack.concat(this.middlewareStack);
diff --color -Nur smithy-typescript-codegen-test/build/smithyprojections/smithy-typescript-codegen-test/control-experimental-identity-and-auth/typescript-codegen/src/commands/OnlyHttpApiKeyAndBearerAuthReversedCommand.ts smithy-typescript-codegen-test/build/smithyprojections/smithy-typescript-codegen-test/client-experimental-identity-and-auth/typescript-codegen/src/commands/OnlyHttpApiKeyAndBearerAuthReversedCommand.ts
--- smithy-typescript-codegen-test/build/smithyprojections/smithy-typescript-codegen-test/control-experimental-identity-and-auth/typescript-codegen/src/commands/OnlyHttpApiKeyAndBearerAuthReversedCommand.ts 2023-09-05 15:50:34
+++ smithy-typescript-codegen-test/build/smithyprojections/smithy-typescript-codegen-test/client-experimental-identity-and-auth/typescript-codegen/src/commands/OnlyHttpApiKeyAndBearerAuthReversedCommand.ts 2023-09-05 15:50:34
@@ -4,7 +4,6 @@
ServiceOutputTypes,
WeatherClientResolvedConfig,
} from "../WeatherClient";
-import { getHttpApiKeyAuthPlugin } from "../middleware/HttpApiKeyAuth";
import {
de_OnlyHttpApiKeyAndBearerAuthReversedCommand,
se_OnlyHttpApiKeyAndBearerAuthReversedCommand,
@@ -64,7 +63,6 @@
options?: __HttpHandlerOptions
): Handler<OnlyHttpApiKeyAndBearerAuthReversedCommandInput, OnlyHttpApiKeyAndBearerAuthReversedCommandOutput> {
this.middlewareStack.use(getSerdePlugin(configuration, this.serialize, this.deserialize));
- this.middlewareStack.use(getHttpApiKeyAuthPlugin(configuration, { in: 'header', name: 'X-Api-Key'}));
const stack = clientStack.concat(this.middlewareStack);
diff --color -Nur smithy-typescript-codegen-test/build/smithyprojections/smithy-typescript-codegen-test/control-experimental-identity-and-auth/typescript-codegen/src/commands/OnlyHttpApiKeyAuthCommand.ts smithy-typescript-codegen-test/build/smithyprojections/smithy-typescript-codegen-test/client-experimental-identity-and-auth/typescript-codegen/src/commands/OnlyHttpApiKeyAuthCommand.ts
--- smithy-typescript-codegen-test/build/smithyprojections/smithy-typescript-codegen-test/control-experimental-identity-and-auth/typescript-codegen/src/commands/OnlyHttpApiKeyAuthCommand.ts 2023-09-05 15:50:34
+++ smithy-typescript-codegen-test/build/smithyprojections/smithy-typescript-codegen-test/client-experimental-identity-and-auth/typescript-codegen/src/commands/OnlyHttpApiKeyAuthCommand.ts 2023-09-05 15:50:34
@@ -4,7 +4,6 @@
ServiceOutputTypes,
WeatherClientResolvedConfig,
} from "../WeatherClient";
-import { getHttpApiKeyAuthPlugin } from "../middleware/HttpApiKeyAuth";
import {
de_OnlyHttpApiKeyAuthCommand,
se_OnlyHttpApiKeyAuthCommand,
@@ -64,7 +63,6 @@
options?: __HttpHandlerOptions
): Handler<OnlyHttpApiKeyAuthCommandInput, OnlyHttpApiKeyAuthCommandOutput> {
this.middlewareStack.use(getSerdePlugin(configuration, this.serialize, this.deserialize));
- this.middlewareStack.use(getHttpApiKeyAuthPlugin(configuration, { in: 'header', name: 'X-Api-Key'}));
const stack = clientStack.concat(this.middlewareStack);
diff --color -Nur smithy-typescript-codegen-test/build/smithyprojections/smithy-typescript-codegen-test/control-experimental-identity-and-auth/typescript-codegen/src/index.ts smithy-typescript-codegen-test/build/smithyprojections/smithy-typescript-codegen-test/client-experimental-identity-and-auth/typescript-codegen/src/index.ts
--- smithy-typescript-codegen-test/build/smithyprojections/smithy-typescript-codegen-test/control-experimental-identity-and-auth/typescript-codegen/src/index.ts 2023-09-05 15:50:34
+++ smithy-typescript-codegen-test/build/smithyprojections/smithy-typescript-codegen-test/client-experimental-identity-and-auth/typescript-codegen/src/index.ts 2023-09-05 15:50:34
@@ -12,6 +12,4 @@
export * from "./waiters";
export * from "./models";
-export * from "./middleware/HttpApiKeyAuth";
-
export { WeatherServiceException } from "./models/WeatherServiceException";
diff --color -Nur smithy-typescript-codegen-test/build/smithyprojections/smithy-typescript-codegen-test/control-experimental-identity-and-auth/typescript-codegen/src/middleware/HttpApiKeyAuth/index.spec.ts smithy-typescript-codegen-test/build/smithyprojections/smithy-typescript-codegen-test/client-experimental-identity-and-auth/typescript-codegen/src/middleware/HttpApiKeyAuth/index.spec.ts
--- smithy-typescript-codegen-test/build/smithyprojections/smithy-typescript-codegen-test/control-experimental-identity-and-auth/typescript-codegen/src/middleware/HttpApiKeyAuth/index.spec.ts 2023-09-05 15:50:34
+++ smithy-typescript-codegen-test/build/smithyprojections/smithy-typescript-codegen-test/client-experimental-identity-and-auth/typescript-codegen/src/middleware/HttpApiKeyAuth/index.spec.ts 1969-12-31 16:00:00
@@ -1,176 +0,0 @@
-// smithy-typescript generated code
-// Please do not touch this file. It's generated from a template in:
-// https://github.com/awslabs/smithy-typescript/blob/main/smithy-typescript-codegen/src/main/resources/software/amazon/smithy/aws/typescript/codegen/integration/http-api-key-auth.spec.ts
-import { HttpRequest } from "@smithy/protocol-http";
-import { MiddlewareStack } from "@smithy/types";
-import { vi } from "vitest";
-
-import {
- getHttpApiKeyAuthPlugin,
- httpApiKeyAuthMiddleware,
- resolveHttpApiKeyAuthConfig,
-} from "./index";
-
-describe("resolveHttpApiKeyAuthConfig", () => {
- it("should return the input unchanged", () => {
- const config = {
- apiKey: () => Promise.resolve("example-api-key"),
- };
- expect(resolveHttpApiKeyAuthConfig(config)).toEqual(config);
- });
-});
-
-describe("getHttpApiKeyAuthPlugin", () => {
- it("should apply the middleware to the stack", () => {
- const plugin = getHttpApiKeyAuthPlugin(
- {
- apiKey: () => Promise.resolve("example-api-key"),
- },
- {
- in: "query",
- name: "key",
- }
- );
-
- const mockApplied = vi.fn();
- const mockOther = vi.fn();
-
- // TODO there's got to be a better way to do this mocking
- plugin.applyToStack({
- addRelativeTo: mockApplied,
- // We don't expect any of these others to be called.
- add: mockOther,
- concat: mockOther,
- resolve: mockOther,
- applyToStack: mockOther,
- use: mockOther,
- clone: mockOther,
- remove: mockOther,
- removeByTag: mockOther,
- } as unknown as MiddlewareStack<any, any>);
-
- expect(mockApplied.mock.calls.length).toEqual(1);
- expect(mockOther.mock.calls.length).toEqual(0);
- });
-});
-
-describe("httpApiKeyAuthMiddleware", () => {
- describe("returned middleware function", () => {
- const mockNextHandler = vi.fn();
-
- beforeEach(() => {
- vi.clearAllMocks();
- });
-
- it("should set the query parameter if the location is `query`", async () => {
- const middleware = httpApiKeyAuthMiddleware(
- {
- apiKey: () => Promise.resolve("example-api-key"),
- },
- {
- in: "query",
- name: "key",
- }
- );
-
- const handler = middleware(mockNextHandler, {});
-
- await handler({
- input: {},
- request: new HttpRequest({}),
- });
-
- expect(mockNextHandler.mock.calls.length).toEqual(1);
- expect(
- mockNextHandler.mock.calls[0][0].request.query.key
- ).toBe("example-api-key");
- });
-
- it("should skip if the api key has not been set", async () => {
- const middleware = httpApiKeyAuthMiddleware(
- {},
- {
- in: "header",
- name: "auth",
- scheme: "scheme",
- }
- );
-
- const handler = middleware(mockNextHandler, {});
-
- await handler({
- input: {},
- request: new HttpRequest({}),
- });
-
- expect(mockNextHandler.mock.calls.length).toEqual(1);
- });
-
- it("should skip if the request is not an HttpRequest", async () => {
- const middleware = httpApiKeyAuthMiddleware(
- {},
- {
- in: "header",
- name: "Authorization",
- }
- );
-
- const handler = middleware(mockNextHandler, {});
-
- await handler({
- input: {},
- request: {},
- });
-
- expect(mockNextHandler.mock.calls.length).toEqual(1);
- });
-
- it("should set the API key in the lower-cased named header", async () => {
- const middleware = httpApiKeyAuthMiddleware(
- {
- apiKey: () => Promise.resolve("example-api-key"),
- },
- {
- in: "header",
- name: "Authorization",
- }
- );
-
- const handler = middleware(mockNextHandler, {});
-
- await handler({
- input: {},
- request: new HttpRequest({}),
- });
-
- expect(mockNextHandler.mock.calls.length).toEqual(1);
- expect(
- mockNextHandler.mock.calls[0][0].request.headers.authorization
- ).toBe("example-api-key");
- });
-
- it("should set the API key in the named header with the provided scheme", async () => {
- const middleware = httpApiKeyAuthMiddleware(
- {
- apiKey: () => Promise.resolve("example-api-key"),
- },
- {
- in: "header",
- name: "authorization",
- scheme: "exampleScheme",
- }
- );
- const handler = middleware(mockNextHandler, {});
-
- await handler({
- input: {},
- request: new HttpRequest({}),
- });
-
- expect(mockNextHandler.mock.calls.length).toEqual(1);
- expect(
- mockNextHandler.mock.calls[0][0].request.headers.authorization
- ).toBe("exampleScheme example-api-key");
- });
- });
-});
diff --color -Nur smithy-typescript-codegen-test/build/smithyprojections/smithy-typescript-codegen-test/control-experimental-identity-and-auth/typescript-codegen/src/middleware/HttpApiKeyAuth/index.ts smithy-typescript-codegen-test/build/smithyprojections/smithy-typescript-codegen-test/client-experimental-identity-and-auth/typescript-codegen/src/middleware/HttpApiKeyAuth/index.ts
--- smithy-typescript-codegen-test/build/smithyprojections/smithy-typescript-codegen-test/control-experimental-identity-and-auth/typescript-codegen/src/middleware/HttpApiKeyAuth/index.ts 2023-09-05 15:50:34
+++ smithy-typescript-codegen-test/build/smithyprojections/smithy-typescript-codegen-test/client-experimental-identity-and-auth/typescript-codegen/src/middleware/HttpApiKeyAuth/index.ts 1969-12-31 16:00:00
@@ -1,136 +0,0 @@
-// smithy-typescript generated code
-// Please do not touch this file. It's generated from a template in:
-// https://github.com/awslabs/smithy-typescript/blob/main/smithy-typescript-codegen/src/main/resources/software/amazon/smithy/aws/typescript/codegen/integration/http-api-key-auth.ts
-// derived from https://github.com/aws/aws-sdk-js-v3/blob/e35f78c97fa6710ff9c444351893f0f06755e771/packages/middleware-endpoint-discovery/src/endpointDiscoveryMiddleware.ts
-
-import { HttpRequest } from "@smithy/protocol-http";
-import { BuildMiddleware, Pluggable, Provider, RelativeMiddlewareOptions } from "@smithy/types";
-import { normalizeProvider } from "@smithy/util-middleware";
-
-interface HttpApiKeyAuthMiddlewareConfig {
- /**
- * Where to put the API key.
- *
- * If the value is `header`, the API key will be transported in the named header,
- * optionally prefixed with the provided scheme.
- *
- * If the value is `query`, the API key will be transported in the named query parameter.
- */
- in: "header" | "query";
-
- /**
- * The name of the header / query parameter to use for the transporting the API key.
- */
- name: string;
-
- /**
- * The scheme to use. Only supported when `in` is `header`.
- */
- scheme?: string;
-}
-
-export interface HttpApiKeyAuthInputConfig {
- /**
- * The API key to use when making requests.
- *
- * This is optional because some operations may not require an API key.
- */
- apiKey?: string | Provider<string>;
-}
-
-export interface ApiKeyPreviouslyResolved {}
-
-export interface HttpApiKeyAuthResolvedConfig {
- /**
- * The API key to use when making requests.
- *
- * This is optional because some operations may not require an API key.
- */
- apiKey?: Provider<string>;
-}
-
-// We have to provide a resolve function when we have config, even if it doesn't
-// actually do anything to the input value. "If any of inputConfig, resolvedConfig,
-// or resolveFunction are set, then all of inputConfig, resolvedConfig, and
-// resolveFunction must be set."
-export function resolveHttpApiKeyAuthConfig<T>(
- input: T & ApiKeyPreviouslyResolved & HttpApiKeyAuthInputConfig,
-): T & HttpApiKeyAuthResolvedConfig {
- return {
- ...input,
- apiKey: input.apiKey ? normalizeProvider(input.apiKey) : undefined,
- };
-}
-
-/**
- * Middleware to inject the API key into the HTTP request.
- *
- * The middleware will inject the client's configured API key into the
- * request as defined by the `@httpApiKeyAuth` trait. If the trait says to
- * put the API key into a named header, that header will be used, optionally
- * prefixed with a scheme. If the trait says to put the API key into a named
- * query parameter, that query parameter will be used.
- *
- * @param pluginConfig the client configuration. Includes the function that will return the API key value.
- * @param middlewareConfig the plugin options (location of the parameter, name, and optional scheme)
- * @returns a function that processes the HTTP request and passes it on to the next handler
- */
-export const httpApiKeyAuthMiddleware =
- <Input extends object, Output extends object>(
- pluginConfig: HttpApiKeyAuthResolvedConfig,
- middlewareConfig: HttpApiKeyAuthMiddlewareConfig,
- ): BuildMiddleware<Input, Output> =>
- (next) =>
- async (args) => {
- if (!HttpRequest.isInstance(args.request)) return next(args);
-
- const apiKey = pluginConfig.apiKey && (await pluginConfig.apiKey());
-
- // This middleware will not be injected if the operation has the @optionalAuth trait.
- // We don't know if we're the only auth middleware, so let the service deal with the
- // absence of the API key (or let other middleware do its job).
- if (!apiKey) {
- return next(args);
- }
-
- return next({
- ...args,
- request: {
- ...args.request,
- headers: {
- ...args.request.headers,
- ...(middlewareConfig.in === "header" && {
- // Set the header, even if it's already been set.
- [middlewareConfig.name.toLowerCase()]: middlewareConfig.scheme
- ? `${middlewareConfig.scheme} ${apiKey}`
- : apiKey,
- }),
- },
- query: {
- ...args.request.query,
- // Set the query parameter, even if it's already been set.
- ...(middlewareConfig.in === "query" && { [middlewareConfig.name]: apiKey }),
- },
- },
- });
- };
-
-export const httpApiKeyAuthMiddlewareOptions: RelativeMiddlewareOptions = {
- name: "httpApiKeyAuthMiddleware",
- tags: ["APIKEY", "AUTH"],
- relation: "after",
- toMiddleware: "retryMiddleware",
- override: true,
-};
-
-export const getHttpApiKeyAuthPlugin = (
- pluginConfig: HttpApiKeyAuthResolvedConfig,
- middlewareConfig: HttpApiKeyAuthMiddlewareConfig,
-): Pluggable<any, any> => ({
- applyToStack: (clientStack) => {
- clientStack.addRelativeTo(
- httpApiKeyAuthMiddleware(pluginConfig, middlewareConfig),
- httpApiKeyAuthMiddlewareOptions,
- );
- },
-});
diff --color -Nur smithy-typescript-codegen-test/build/smithyprojections/smithy-typescript-codegen-test/control-experimental-identity-and-auth/typescript-codegen/src/runtimeConfig.shared.ts smithy-typescript-codegen-test/build/smithyprojections/smithy-typescript-codegen-test/client-experimental-identity-and-auth/typescript-codegen/src/runtimeConfig.shared.ts
--- smithy-typescript-codegen-test/build/smithyprojections/smithy-typescript-codegen-test/control-experimental-identity-and-auth/typescript-codegen/src/runtimeConfig.shared.ts 2023-09-05 15:50:34
+++ smithy-typescript-codegen-test/build/smithyprojections/smithy-typescript-codegen-test/client-experimental-identity-and-auth/typescript-codegen/src/runtimeConfig.shared.ts 2023-09-05 15:50:34
@@ -1,4 +1,12 @@
// smithy-typescript generated code
+import { defaultWeatherHttpAuthSchemeProvider } from "./auth/httpAuthSchemeProvider";
+import {
+ HttpApiKeyAuthSigner,
+ HttpBearerAuthSigner,
+ IdentityProviderConfig,
+ NoAuthSigner,
+ SigV4Signer,
+} from "@smithy/experimental-identity-and-auth";
import { NoOpLogger } from "@smithy/smithy-client";
import { parseUrl } from "@smithy/url-parser";
import {
@@ -21,6 +29,28 @@
base64Encoder: config?.base64Encoder ?? toBase64,
disableHostPrefix: config?.disableHostPrefix ?? false,
extensions: config?.extensions ?? [],
+ httpAuthSchemeProvider: config?.httpAuthSchemeProvider ?? defaultWeatherHttpAuthSchemeProvider,
+ httpAuthSchemes: config?.httpAuthSchemes ?? [{
+ schemeId: "aws.auth#sigv4",
+ identityProvider: (config: IdentityProviderConfig) =>
+ config.getIdentityProvider("aws.auth#sigv4"),
+ signer: new SigV4Signer(),
+ }, {
+ schemeId: "smithy.api#httpApiKeyAuth",
+ identityProvider: (config: IdentityProviderConfig) =>
+ config.getIdentityProvider("smithy.api#httpApiKeyAuth"),
+ signer: new HttpApiKeyAuthSigner(),
+ }, {
+ schemeId: "smithy.api#httpBearerAuth",
+ identityProvider: (config: IdentityProviderConfig) =>
+ config.getIdentityProvider("smithy.api#httpBearerAuth"),
+ signer: new HttpBearerAuthSigner(),
+ }, {
+ schemeId: "smithy.api#noAuth",
+ identityProvider: (config: IdentityProviderConfig) =>
+ config.getIdentityProvider("smithy.api#noAuth") || (async () => ({})),
+ signer: new NoAuthSigner(),
+ }],
logger: config?.logger ?? new NoOpLogger(),
sdkStreamMixin: config?.sdkStreamMixin ?? sdkStreamMixin,
urlParser: config?.urlParser ?? parseUrl,
diff --color -Nur smithy-typescript-codegen-test/build/smithyprojections/smithy-typescript-codegen-test/control-experimental-identity-and-auth/typescript-codegen/vite.config.js smithy-typescript-codegen-test/build/smithyprojections/smithy-typescript-codegen-test/client-experimental-identity-and-auth/typescript-codegen/vite.config.js
--- smithy-typescript-codegen-test/build/smithyprojections/smithy-typescript-codegen-test/control-experimental-identity-and-auth/typescript-codegen/vite.config.js 2023-09-05 15:50:34
+++ smithy-typescript-codegen-test/build/smithyprojections/smithy-typescript-codegen-test/client-experimental-identity-and-auth/typescript-codegen/vite.config.js 1969-12-31 16:00:00
@@ -1,8 +0,0 @@
-import { defineConfig } from 'vite'
-
-export default defineConfig({
- test: {
- include: ['**/*.spec.ts'],
- globals: true
- },
-})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment