Skip to content

Commit fed4969

Browse files
committed
feat(middleware-signing): support SigV4 for non AWS services
There is no regionInfo to determine signingService. The signingName will instead directly come from input. And region will be used as signingRegion.
1 parent 3cf3b32 commit fed4969

File tree

2 files changed

+66
-0
lines changed

2 files changed

+66
-0
lines changed

packages/middleware-signing/src/configurations.ts

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,29 @@ export interface AwsAuthInputConfig {
3232
*/
3333
signingRegion?: string;
3434
}
35+
36+
export interface CustomAwsAuthInputConfig {
37+
/**
38+
* The credentials used to sign requests.
39+
*/
40+
credentials?: Credentials | Provider<Credentials>;
41+
42+
/**
43+
* The signer to use when signing requests.
44+
*/
45+
signer?: RequestSigner | Provider<RequestSigner>;
46+
47+
/**
48+
* Whether to escape request path when signing the request.
49+
*/
50+
signingEscapePath?: boolean;
51+
52+
/**
53+
* An offset value in milliseconds to apply to all signing times.
54+
*/
55+
systemClockOffset?: number;
56+
}
57+
3558
interface PreviouslyResolved {
3659
credentialDefaultProvider: (input: any) => Provider<Credentials>;
3760
region: string | Provider<string>;
@@ -40,13 +63,23 @@ interface PreviouslyResolved {
4063
serviceId: string;
4164
sha256: HashConstructor;
4265
}
66+
67+
interface CustomPreviouslyResolved {
68+
credentialDefaultProvider: (input: any) => Provider<Credentials>;
69+
region: string | Provider<string>;
70+
signingName: string;
71+
sha256: HashConstructor;
72+
}
73+
4374
export interface AwsAuthResolvedConfig {
4475
credentials: Provider<Credentials>;
4576
signer: Provider<RequestSigner>;
4677
signingEscapePath: boolean;
4778
systemClockOffset: number;
4879
}
4980

81+
export interface CustomAwsAuthResolvedConfig extends AwsAuthResolvedConfig {}
82+
5083
export const resolveAwsAuthConfig = <T>(
5184
input: T & AwsAuthInputConfig & PreviouslyResolved
5285
): T & AwsAuthResolvedConfig => {
@@ -91,6 +124,37 @@ export const resolveAwsAuthConfig = <T>(
91124
};
92125
};
93126

127+
// TODO: reduce code duplication
128+
export const resolveCustomAwsAuthConfig = <T>(
129+
input: T & CustomAwsAuthInputConfig & CustomPreviouslyResolved
130+
): T & CustomAwsAuthResolvedConfig => {
131+
const normalizedCreds = input.credentials
132+
? normalizeCredentialProvider(input.credentials)
133+
: input.credentialDefaultProvider(input as any);
134+
const { signingEscapePath = true, systemClockOffset = input.systemClockOffset || 0, sha256 } = input;
135+
let signer: Provider<RequestSigner>;
136+
if (input.signer) {
137+
//if signer is supplied by user, normalize it to a function returning a promise for signer.
138+
signer = normalizeProvider(input.signer);
139+
} else {
140+
signer = normalizeProvider(new SignatureV4({
141+
credentials: normalizedCreds,
142+
region: input.region,
143+
service: input.signingName,
144+
sha256,
145+
uriEscapePath: signingEscapePath,
146+
}));
147+
}
148+
149+
return {
150+
...input,
151+
systemClockOffset,
152+
signingEscapePath,
153+
credentials: normalizedCreds,
154+
signer,
155+
};
156+
};
157+
94158
const normalizeProvider = <T>(input: T | Provider<T>): Provider<T> => {
95159
if (typeof input === "object") {
96160
const promisified = Promise.resolve(input);

packages/middleware-signing/src/middleware.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,3 +58,5 @@ export const getAwsAuthPlugin = (options: AwsAuthResolvedConfig): Pluggable<any,
5858
clientStack.addRelativeTo(awsAuthMiddleware(options), awsAuthMiddlewareOptions);
5959
},
6060
});
61+
62+
export const getCustomAwsAuthPlugin = getAwsAuthPlugin;

0 commit comments

Comments
 (0)