Skip to content

Commit c6dc029

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 597e689 commit c6dc029

File tree

2 files changed

+70
-0
lines changed

2 files changed

+70
-0
lines changed

packages/middleware-signing/src/configurations.ts

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ import { Credentials, HashConstructor, Provider, RegionInfo, RegionInfoProvider,
55
// 5 minutes buffer time the refresh the credential before it really expires
66
const CREDENTIAL_EXPIRE_WINDOW = 300000;
77

8+
// AwsAuth v/s SigV4Auth
9+
// AwsAuth: specific to SigV4 auth for AWS services
10+
// SigV4Auth: SigV4 auth for non-AWS services
11+
812
export interface AwsAuthInputConfig {
913
/**
1014
* The credentials used to sign requests.
@@ -32,6 +36,29 @@ export interface AwsAuthInputConfig {
3236
*/
3337
signingRegion?: string;
3438
}
39+
40+
export interface SigV4AuthInputConfig {
41+
/**
42+
* The credentials used to sign requests.
43+
*/
44+
credentials?: Credentials | Provider<Credentials>;
45+
46+
/**
47+
* The signer to use when signing requests.
48+
*/
49+
signer?: RequestSigner | Provider<RequestSigner>;
50+
51+
/**
52+
* Whether to escape request path when signing the request.
53+
*/
54+
signingEscapePath?: boolean;
55+
56+
/**
57+
* An offset value in milliseconds to apply to all signing times.
58+
*/
59+
systemClockOffset?: number;
60+
}
61+
3562
interface PreviouslyResolved {
3663
credentialDefaultProvider: (input: any) => Provider<Credentials>;
3764
region: string | Provider<string>;
@@ -40,13 +67,23 @@ interface PreviouslyResolved {
4067
serviceId: string;
4168
sha256: HashConstructor;
4269
}
70+
71+
interface SigV4PreviouslyResolved {
72+
credentialDefaultProvider: (input: any) => Provider<Credentials>;
73+
region: string | Provider<string>;
74+
signingName: string;
75+
sha256: HashConstructor;
76+
}
77+
4378
export interface AwsAuthResolvedConfig {
4479
credentials: Provider<Credentials>;
4580
signer: Provider<RequestSigner>;
4681
signingEscapePath: boolean;
4782
systemClockOffset: number;
4883
}
4984

85+
export interface SigV4AuthResolvedConfig extends AwsAuthResolvedConfig {}
86+
5087
export const resolveAwsAuthConfig = <T>(
5188
input: T & AwsAuthInputConfig & PreviouslyResolved
5289
): T & AwsAuthResolvedConfig => {
@@ -91,6 +128,37 @@ export const resolveAwsAuthConfig = <T>(
91128
};
92129
};
93130

131+
// TODO: reduce code duplication
132+
export const resolveSigV4AuthConfig = <T>(
133+
input: T & SigV4AuthInputConfig & SigV4PreviouslyResolved
134+
): T & SigV4AuthResolvedConfig => {
135+
const normalizedCreds = input.credentials
136+
? normalizeCredentialProvider(input.credentials)
137+
: input.credentialDefaultProvider(input as any);
138+
const { signingEscapePath = true, systemClockOffset = input.systemClockOffset || 0, sha256 } = input;
139+
let signer: Provider<RequestSigner>;
140+
if (input.signer) {
141+
//if signer is supplied by user, normalize it to a function returning a promise for signer.
142+
signer = normalizeProvider(input.signer);
143+
} else {
144+
signer = normalizeProvider(new SignatureV4({
145+
credentials: normalizedCreds,
146+
region: input.region,
147+
service: input.signingName,
148+
sha256,
149+
uriEscapePath: signingEscapePath,
150+
}));
151+
}
152+
153+
return {
154+
...input,
155+
systemClockOffset,
156+
signingEscapePath,
157+
credentials: normalizedCreds,
158+
signer,
159+
};
160+
};
161+
94162
const normalizeProvider = <T>(input: T | Provider<T>): Provider<T> => {
95163
if (typeof input === "object") {
96164
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 getSigV4AuthPlugin = getAwsAuthPlugin;

0 commit comments

Comments
 (0)