Skip to content

Commit

Permalink
Add event stream signing after initial http request signing
Browse files Browse the repository at this point in the history
  • Loading branch information
syall committed Dec 14, 2023
1 parent 7748297 commit 2f8c3a4
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/angry-carpets-own.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@smithy/core": minor
---

Add event stream signing after initial http request signing if supported.
42 changes: 40 additions & 2 deletions packages/core/src/middleware-http-signing/httpSigningMiddleware.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,40 @@
import { HttpRequest } from "@smithy/protocol-http";
import {
ErrorHandler,
EventStreamPayloadHandler,
FinalizeHandler,
FinalizeHandlerArguments,
FinalizeHandlerOutput,
FinalizeRequestMiddleware,
HandlerExecutionContext,
MetadataBearer,
SelectedHttpAuthScheme,
SMITHY_CONTEXT_KEY,
SuccessHandler,
} from "@smithy/types";
import { getSmithyContext } from "@smithy/util-middleware";

/**
* @internal
*/
interface HttpSigningMiddlewareConfig {
/**
* @internal
*/
eventStreamPayloadHandler?: EventStreamPayloadHandler;
}

/**
* @internal
*/
interface HttpSigningMiddlewareSmithyContext extends Record<string, unknown> {
selectedHttpAuthScheme?: SelectedHttpAuthScheme;
/**
* @internal
*/
eventStream?: {
input?: boolean;
};
}

/**
Expand All @@ -26,20 +44,34 @@ interface HttpSigningMiddlewareHandlerExecutionContext extends HandlerExecutionC
[SMITHY_CONTEXT_KEY]?: HttpSigningMiddlewareSmithyContext;
}

/**
* @internal
*/
const defaultErrorHandler: ErrorHandler = (signingProperties) => (error) => {
throw error;
};

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

/**
* @internal
*/
const isEventStreamInputSupported = (
smithyContext: HttpSigningMiddlewareSmithyContext,
config: HttpSigningMiddlewareConfig
) => smithyContext?.eventStream?.input && config.eventStreamPayloadHandler;

/**
* @internal
*/
export const httpSigningMiddleware = <Input extends object, Output extends object>(
config: object
config: HttpSigningMiddlewareConfig
): FinalizeRequestMiddleware<Input, Output> => (
next: FinalizeHandler<Input, Output>,
context: HttpSigningMiddlewareHandlerExecutionContext
Expand All @@ -60,7 +92,13 @@ export const httpSigningMiddleware = <Input extends object, Output extends objec
identity,
signer,
} = scheme;
const output = await next({
// If the input is an event stream and has a handler, use the eventStreamPayloadHandler
// right after signing the initial request.
const wrappedNext: FinalizeHandler<Input, Output> = isEventStreamInputSupported(smithyContext, config)
? async (args) =>
config.eventStreamPayloadHandler!.handle(next as FinalizeHandler<Input, Output & MetadataBearer>, args, context)
: next;
const output = await wrappedNext({
...args,
request: await signer.sign(args.request, identity, signingProperties),
}).catch((signer.errorHandler || defaultErrorHandler)(signingProperties));
Expand Down

0 comments on commit 2f8c3a4

Please sign in to comment.