Skip to content

Commit

Permalink
feat(experimentalIdentityAndAuth): add handlers to HttpSigner
Browse files Browse the repository at this point in the history
  • Loading branch information
Steven Yuan authored and syall committed Nov 21, 2023
1 parent 8c674e7 commit 9579a9a
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 5 deletions.
6 changes: 6 additions & 0 deletions .changeset/rotten-islands-brake.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@smithy/types": patch
"@smithy/core": patch
---

Add internal error and success handlers to `HttpSigner`.
21 changes: 17 additions & 4 deletions packages/core/src/middleware-http-signing/httpSigningMiddleware.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import { HttpRequest } from "@smithy/protocol-http";
import {
ErrorHandler,
FinalizeHandler,
FinalizeHandlerArguments,
FinalizeHandlerOutput,
FinalizeRequestMiddleware,
HandlerExecutionContext,
SelectedHttpAuthScheme,
SMITHY_CONTEXT_KEY,
SuccessHandler,
} from "@smithy/types";
import { getSmithyContext } from "@smithy/util-middleware";

Expand All @@ -24,6 +26,15 @@ interface HttpSigningMiddlewareHandlerExecutionContext extends HandlerExecutionC
[SMITHY_CONTEXT_KEY]?: HttpSigningMiddlewareSmithyContext;
}

const defaultErrorHandler: ErrorHandler = (signingProperties) => (error) => {
throw error;
};

const defaultSuccessHandler: SuccessHandler = (
httpResponse: unknown,
signingProperties: Record<string, unknown>
): void => {};

/**
* @internal
*/
Expand All @@ -45,12 +56,14 @@ export const httpSigningMiddleware = <Input extends object, Output extends objec
throw new Error(`No HttpAuthScheme was selected: unable to sign request`);
}
const {
httpAuthOption: { signingProperties },
httpAuthOption: { signingProperties = {} },
identity,
signer,
} = scheme;
return next({
const output = await next({
...args,
request: await signer.sign(args.request, identity, signingProperties || {}),
});
request: await signer.sign(args.request, identity, signingProperties),
}).catch((signer.errorHandler || defaultErrorHandler)(signingProperties));
(signer.successHandler || defaultSuccessHandler)(output.response, signingProperties);
return output;
};
29 changes: 28 additions & 1 deletion packages/types/src/auth/HttpSigner.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,20 @@
import { HttpRequest } from "../http";
import { HttpRequest, HttpResponse } from "../http";
import { Identity } from "../identity/identity";

/**
* @internal
*/
export interface ErrorHandler {
(signingProperties: Record<string, unknown>): <E extends Error>(error: E) => never;
}

/**
* @internal
*/
export interface SuccessHandler {
(httpResponse: HttpResponse | unknown, signingProperties: Record<string, unknown>): void;
}

/**
* Interface to sign identity and signing properties.
* @internal
Expand All @@ -14,4 +28,17 @@ export interface HttpSigner {
* @returns signed request in a promise
*/
sign(httpRequest: HttpRequest, identity: Identity, signingProperties: Record<string, unknown>): Promise<HttpRequest>;
/**
* Handler that executes after the {@link HttpSigner.sign} invocation and corresponding
* middleware throws an error.
* The error handler is expected to throw the error it receives, so the return type of the error handler is `never`.
* @internal
*/
errorHandler?: ErrorHandler;
/**
* Handler that executes after the {@link HttpSigner.sign} invocation and corresponding
* middleware succeeds.
* @internal
*/
successHandler?: SuccessHandler;
}

0 comments on commit 9579a9a

Please sign in to comment.