@aws-lambda-powertools/signer 2.35.0 → 2.37.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (41) hide show
  1. package/lib/cjs/SigV4Signer.d.ts +11 -0
  2. package/lib/cjs/SigV4Signer.d.ts.map +1 -1
  3. package/lib/cjs/SigV4Signer.js +26 -104
  4. package/lib/cjs/SigV4aSigner.d.ts +34 -0
  5. package/lib/cjs/SigV4aSigner.d.ts.map +1 -0
  6. package/lib/cjs/SigV4aSigner.js +73 -0
  7. package/lib/cjs/constants.d.ts +8 -0
  8. package/lib/cjs/constants.d.ts.map +1 -0
  9. package/lib/cjs/constants.js +10 -0
  10. package/lib/cjs/errors.d.ts +3 -2
  11. package/lib/cjs/errors.d.ts.map +1 -1
  12. package/lib/cjs/errors.js +3 -2
  13. package/lib/cjs/helpers.d.ts +40 -0
  14. package/lib/cjs/helpers.d.ts.map +1 -0
  15. package/lib/cjs/helpers.js +145 -0
  16. package/lib/cjs/sigv4a.d.ts +3 -0
  17. package/lib/cjs/sigv4a.d.ts.map +1 -0
  18. package/lib/cjs/sigv4a.js +5 -0
  19. package/lib/cjs/types/index.d.ts +28 -11
  20. package/lib/cjs/types/index.d.ts.map +1 -1
  21. package/lib/esm/SigV4Signer.d.ts +11 -0
  22. package/lib/esm/SigV4Signer.d.ts.map +1 -1
  23. package/lib/esm/SigV4Signer.js +25 -103
  24. package/lib/esm/SigV4aSigner.d.ts +34 -0
  25. package/lib/esm/SigV4aSigner.d.ts.map +1 -0
  26. package/lib/esm/SigV4aSigner.js +70 -0
  27. package/lib/esm/constants.d.ts +8 -0
  28. package/lib/esm/constants.d.ts.map +1 -0
  29. package/lib/esm/constants.js +7 -0
  30. package/lib/esm/errors.d.ts +3 -2
  31. package/lib/esm/errors.d.ts.map +1 -1
  32. package/lib/esm/errors.js +3 -2
  33. package/lib/esm/helpers.d.ts +40 -0
  34. package/lib/esm/helpers.d.ts.map +1 -0
  35. package/lib/esm/helpers.js +138 -0
  36. package/lib/esm/sigv4a.d.ts +3 -0
  37. package/lib/esm/sigv4a.d.ts.map +1 -0
  38. package/lib/esm/sigv4a.js +1 -0
  39. package/lib/esm/types/index.d.ts +28 -11
  40. package/lib/esm/types/index.d.ts.map +1 -1
  41. package/package.json +22 -4
@@ -17,7 +17,18 @@ import type { Signer, SigV4SignerOptions } from './types/index.js';
17
17
  */
18
18
  declare class SigV4Signer implements Signer {
19
19
  #private;
20
+ /**
21
+ * Creates a signer for the specified service and AWS region.
22
+ *
23
+ * @param options - The service and optional region and credentials.
24
+ */
20
25
  constructor(options: SigV4SignerOptions);
26
+ /**
27
+ * Signs a request and preserves its fetch options in the returned request.
28
+ *
29
+ * @param input - The URL string, URL, or request to sign.
30
+ * @param init - Optional request options matching the `fetch` init argument.
31
+ */
21
32
  sign(input: string | URL | Request, init?: RequestInit): Promise<Request>;
22
33
  }
23
34
  export { SigV4Signer };
@@ -1 +1 @@
1
- {"version":3,"file":"SigV4Signer.d.ts","sourceRoot":"","sources":["../../src/SigV4Signer.ts"],"names":[],"mappings":"AASA,OAAO,KAAK,EAAE,MAAM,EAAE,kBAAkB,EAAE,MAAM,kBAAkB,CAAC;AAyBnE;;;;;;;;;;;;;;;GAeG;AACH,cAAM,WAAY,YAAW,MAAM;;gBAKd,OAAO,EAAE,kBAAkB;IAcjC,IAAI,CACf,KAAK,EAAE,MAAM,GAAG,GAAG,GAAG,OAAO,EAC7B,IAAI,CAAC,EAAE,WAAW,GACjB,OAAO,CAAC,OAAO,CAAC;CAgHpB;AAED,OAAO,EAAE,WAAW,EAAE,CAAC"}
1
+ {"version":3,"file":"SigV4Signer.d.ts","sourceRoot":"","sources":["../../src/SigV4Signer.ts"],"names":[],"mappings":"AAiBA,OAAO,KAAK,EAAE,MAAM,EAAE,kBAAkB,EAAE,MAAM,kBAAkB,CAAC;AAEnE;;;;;;;;;;;;;;;GAeG;AACH,cAAM,WAAY,YAAW,MAAM;;IAKjC;;;;OAIG;gBACgB,OAAO,EAAE,kBAAkB;IAgB9C;;;;;OAKG;IACU,IAAI,CACf,KAAK,EAAE,MAAM,GAAG,GAAG,GAAG,OAAO,EAC7B,IAAI,CAAC,EAAE,WAAW,GACjB,OAAO,CAAC,OAAO,CAAC;CA0BpB;AAED,OAAO,EAAE,WAAW,EAAE,CAAC"}
@@ -1,25 +1,9 @@
1
+ import { getStringFromEnv } from '@aws-lambda-powertools/commons/utils/env';
1
2
  import { SignatureV4 } from '@smithy/signature-v4';
3
+ import { SERVICE_S3 } from './constants.js';
2
4
  import { RequestSigningError, SignerConfigError } from './errors.js';
5
+ import { credentialsFromEnv, resolveCredentials, toHttpRequest, toRequest, toSigningRequest, } from './helpers.js';
3
6
  import { Sha256 } from './Sha256.js';
4
- /**
5
- * Credential provider that reads AWS credentials from the standard environment
6
- * variables that the AWS Lambda runtime always injects.
7
- *
8
- * Throws a {@link SignerConfigError | `SignerConfigError`} when the required
9
- * variables are not present, e.g. when running outside of Lambda.
10
- */
11
- const credentialsFromEnv = async () => {
12
- const accessKeyId = process.env.AWS_ACCESS_KEY_ID;
13
- const secretAccessKey = process.env.AWS_SECRET_ACCESS_KEY;
14
- if (accessKeyId === undefined || secretAccessKey === undefined) {
15
- throw new SignerConfigError('Unable to resolve AWS credentials to sign the request with. Ensure the standard AWS credentials environment variables are set, or pass the `credentials` option.');
16
- }
17
- return {
18
- accessKeyId,
19
- secretAccessKey,
20
- sessionToken: process.env.AWS_SESSION_TOKEN,
21
- };
22
- };
23
7
  /**
24
8
  * A {@link Signer | `Signer`} that signs requests using the AWS Signature
25
9
  * Version 4 (SigV4) signing process.
@@ -40,112 +24,50 @@ class SigV4Signer {
40
24
  #service;
41
25
  #region;
42
26
  #credentials;
27
+ /**
28
+ * Creates a signer for the specified service and AWS region.
29
+ *
30
+ * @param options - The service and optional region and credentials.
31
+ */
43
32
  constructor(options) {
44
33
  this.#service = options.service;
45
- const region = options.region ?? process.env.AWS_REGION;
46
- if (region === undefined || region === '') {
34
+ const region = options.region ??
35
+ getStringFromEnv({ key: 'AWS_REGION', defaultValue: '' });
36
+ if (region === '') {
47
37
  throw new SignerConfigError('Unable to determine the AWS region to sign the request with. Set the `region` option or the `AWS_REGION` environment variable.');
48
38
  }
49
39
  this.#region = region;
50
40
  this.#credentials = options.credentials ?? credentialsFromEnv;
51
41
  }
42
+ /**
43
+ * Signs a request and preserves its fetch options in the returned request.
44
+ *
45
+ * @param input - The URL string, URL, or request to sign.
46
+ * @param init - Optional request options matching the `fetch` init argument.
47
+ */
52
48
  async sign(input, init) {
53
- const request = new Request(input, init);
54
- const credentials = await this.#resolveCredentials();
55
- const httpRequest = await this.#toHttpRequest(request);
49
+ const request = toSigningRequest(input, init);
50
+ const [credentials, httpRequest] = await Promise.all([
51
+ resolveCredentials(this.#credentials),
52
+ toHttpRequest(request, this.#service),
53
+ ]);
56
54
  const signer = new SignatureV4({
57
55
  service: this.#service,
58
56
  region: this.#region,
59
57
  credentials,
60
58
  sha256: Sha256,
59
+ uriEscapePath: this.#service !== SERVICE_S3,
61
60
  });
62
61
  let signed;
63
62
  try {
64
- signed = (await signer.sign(httpRequest));
63
+ signed = await signer.sign(httpRequest);
65
64
  }
66
65
  catch (error) {
67
66
  throw new RequestSigningError('Failed to sign the request', {
68
67
  cause: error,
69
68
  });
70
69
  }
71
- return this.#toRequest(signed, request);
72
- }
73
- /**
74
- * Resolve the configured credentials, supporting both static values and
75
- * async providers. The default env-var provider throws a configuration error
76
- * when no credentials are available.
77
- */
78
- async #resolveCredentials() {
79
- return typeof this.#credentials === 'function'
80
- ? await this.#credentials()
81
- : this.#credentials;
82
- }
83
- /**
84
- * Adapt a web-standard `Request` into the `HttpRequest` shape expected by the
85
- * underlying signing process, buffering the body so it can be hashed.
86
- *
87
- * The signing process accepts a plain object matching the `HttpRequest`
88
- * interface, so we avoid depending on `@smithy/protocol-http` just to
89
- * construct one.
90
- */
91
- async #toHttpRequest(request) {
92
- const url = new URL(request.url);
93
- const headers = {};
94
- for (const [key, value] of request.headers) {
95
- headers[key] = value;
96
- }
97
- headers.host = url.host;
98
- const query = {};
99
- for (const [key, value] of url.searchParams) {
100
- query[key] = value;
101
- }
102
- const body = await this.#readBody(request);
103
- return {
104
- method: request.method,
105
- protocol: url.protocol,
106
- hostname: url.hostname,
107
- port: url.port ? Number(url.port) : undefined,
108
- path: url.pathname,
109
- query,
110
- headers,
111
- body,
112
- };
113
- }
114
- /**
115
- * Buffer the request body so it can be both hashed for signing and replayed
116
- * when the signed request is sent.
117
- *
118
- * Most bodies (strings, buffers, and finite streams) are buffered
119
- * transparently. A body that cannot be read or replayed — for example a
120
- * stream that errors mid-read — cannot be signed and results in a
121
- * {@link RequestSigningError | `RequestSigningError`}.
122
- */
123
- async #readBody(request) {
124
- if (request.body === null || request.body === undefined) {
125
- return undefined;
126
- }
127
- try {
128
- const buffer = await request.clone().arrayBuffer();
129
- return buffer.byteLength === 0 ? undefined : new Uint8Array(buffer);
130
- }
131
- catch (error) {
132
- throw new RequestSigningError('Unable to read the request body to sign it. The body could not be buffered, for example because it is a stream that cannot be replayed.', { cause: error });
133
- }
134
- }
135
- /**
136
- * Adapt a signed `HttpRequest` back into a web-standard `Request`, carrying
137
- * over the signed headers and the original (buffered) body.
138
- */
139
- #toRequest(signed, original) {
140
- const headers = new Headers();
141
- for (const [key, value] of Object.entries(signed.headers)) {
142
- headers.set(key, value);
143
- }
144
- return new Request(original.url, {
145
- method: signed.method,
146
- headers,
147
- body: signed.body,
148
- });
70
+ return toRequest(signed, request);
149
71
  }
150
72
  }
151
73
  export { SigV4Signer };
@@ -0,0 +1,34 @@
1
+ import type { Signer, SigV4aSignerOptions } from './types/index.js';
2
+ /**
3
+ * Signs web-standard requests using AWS Signature Version 4A (SigV4a).
4
+ *
5
+ * SigV4a uses ECDSA signatures valid in a set of AWS regions, for services
6
+ * such as Amazon S3 Multi-Region Access Points. The signer performs no network
7
+ * I/O and works with `createSignedFetcher` from the `/fetch` entry point.
8
+ *
9
+ * @example
10
+ * ```ts
11
+ * import { SigV4aSigner } from '@aws-lambda-powertools/signer/sigv4a';
12
+ *
13
+ * const signer = new SigV4aSigner({ service: 's3', regionSet: ['*'] });
14
+ * const signed = await signer.sign('https://example.accesspoint.s3-global.amazonaws.com/object');
15
+ * ```
16
+ */
17
+ declare class SigV4aSigner implements Signer {
18
+ #private;
19
+ /**
20
+ * Creates a signer for the specified service and region set.
21
+ *
22
+ * @param options - The service, region set, and optional credentials.
23
+ */
24
+ constructor(options: SigV4aSignerOptions);
25
+ /**
26
+ * Signs a request and returns a new request with the SigV4a headers.
27
+ *
28
+ * @param input - The URL string, URL, or request to sign.
29
+ * @param init - Optional request options matching the `fetch` init argument.
30
+ */
31
+ sign(input: string | URL | Request, init?: RequestInit): Promise<Request>;
32
+ }
33
+ export { SigV4aSigner };
34
+ //# sourceMappingURL=SigV4aSigner.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"SigV4aSigner.d.ts","sourceRoot":"","sources":["../../src/SigV4aSigner.ts"],"names":[],"mappings":"AAgBA,OAAO,KAAK,EAAE,MAAM,EAAE,mBAAmB,EAAE,MAAM,kBAAkB,CAAC;AAEpE;;;;;;;;;;;;;;GAcG;AACH,cAAM,YAAa,YAAW,MAAM;;IAKlC;;;;OAIG;gBACgB,OAAO,EAAE,mBAAmB;IAiB/C;;;;;OAKG;IACU,IAAI,CACf,KAAK,EAAE,MAAM,GAAG,GAAG,GAAG,OAAO,EAC7B,IAAI,CAAC,EAAE,WAAW,GACjB,OAAO,CAAC,OAAO,CAAC;CAyBpB;AAED,OAAO,EAAE,YAAY,EAAE,CAAC"}
@@ -0,0 +1,70 @@
1
+ import { SignatureV4a } from '@smithy/signature-v4a';
2
+ import { SERVICE_S3 } from './constants.js';
3
+ import { RequestSigningError, SignerConfigError } from './errors.js';
4
+ import { credentialsFromEnv, resolveCredentials, toHttpRequest, toRequest, toSigningRequest, } from './helpers.js';
5
+ import { Sha256 } from './Sha256.js';
6
+ /**
7
+ * Signs web-standard requests using AWS Signature Version 4A (SigV4a).
8
+ *
9
+ * SigV4a uses ECDSA signatures valid in a set of AWS regions, for services
10
+ * such as Amazon S3 Multi-Region Access Points. The signer performs no network
11
+ * I/O and works with `createSignedFetcher` from the `/fetch` entry point.
12
+ *
13
+ * @example
14
+ * ```ts
15
+ * import { SigV4aSigner } from '@aws-lambda-powertools/signer/sigv4a';
16
+ *
17
+ * const signer = new SigV4aSigner({ service: 's3', regionSet: ['*'] });
18
+ * const signed = await signer.sign('https://example.accesspoint.s3-global.amazonaws.com/object');
19
+ * ```
20
+ */
21
+ class SigV4aSigner {
22
+ #service;
23
+ #regionSet;
24
+ #credentials;
25
+ /**
26
+ * Creates a signer for the specified service and region set.
27
+ *
28
+ * @param options - The service, region set, and optional credentials.
29
+ */
30
+ constructor(options) {
31
+ if (options.regionSet.length === 0 ||
32
+ options.regionSet.some((region) => !/^[a-z0-9*]+(-[a-z0-9*]+)*$/.test(region))) {
33
+ throw new SignerConfigError('Set `regionSet` to a non-empty array of AWS regions or wildcard patterns, for example ["us-east-1", "us-west-2"] or ["*"].');
34
+ }
35
+ this.#service = options.service;
36
+ this.#regionSet = options.regionSet.join(',');
37
+ this.#credentials = options.credentials ?? credentialsFromEnv;
38
+ }
39
+ /**
40
+ * Signs a request and returns a new request with the SigV4a headers.
41
+ *
42
+ * @param input - The URL string, URL, or request to sign.
43
+ * @param init - Optional request options matching the `fetch` init argument.
44
+ */
45
+ async sign(input, init) {
46
+ const request = toSigningRequest(input, init);
47
+ const [credentials, httpRequest] = await Promise.all([
48
+ resolveCredentials(this.#credentials),
49
+ toHttpRequest(request, this.#service),
50
+ ]);
51
+ const signer = new SignatureV4a({
52
+ service: this.#service,
53
+ region: this.#regionSet,
54
+ credentials,
55
+ sha256: Sha256,
56
+ uriEscapePath: this.#service !== SERVICE_S3,
57
+ });
58
+ let signed;
59
+ try {
60
+ signed = await signer.sign(httpRequest, {});
61
+ }
62
+ catch (error) {
63
+ throw new RequestSigningError('Failed to sign the request', {
64
+ cause: error,
65
+ });
66
+ }
67
+ return toRequest(signed, request);
68
+ }
69
+ }
70
+ export { SigV4aSigner };
@@ -0,0 +1,8 @@
1
+ /**
2
+ * Identifies the S3 signing service.
3
+ *
4
+ * @internal
5
+ */
6
+ declare const SERVICE_S3 = "s3";
7
+ export { SERVICE_S3 };
8
+ //# sourceMappingURL=constants.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"constants.d.ts","sourceRoot":"","sources":["../../src/constants.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AACH,QAAA,MAAM,UAAU,OAAO,CAAC;AAExB,OAAO,EAAE,UAAU,EAAE,CAAC"}
@@ -0,0 +1,7 @@
1
+ /**
2
+ * Identifies the S3 signing service.
3
+ *
4
+ * @internal
5
+ */
6
+ const SERVICE_S3 = 's3';
7
+ export { SERVICE_S3 };
@@ -11,8 +11,9 @@ declare class SignerError extends Error {
11
11
  /**
12
12
  * Error thrown when the signer is misconfigured.
13
13
  *
14
- * This is thrown eagerly at construction when the region cannot be determined,
15
- * and lazily during signing when credentials are missing or cannot be resolved.
14
+ * This is thrown eagerly at construction when the region cannot be determined
15
+ * or the SigV4a region set is invalid, and lazily during signing when the
16
+ * required AWS credentials environment variables are missing.
16
17
  */
17
18
  declare class SignerConfigError extends SignerError {
18
19
  constructor(message: string, options?: ErrorOptions);
@@ -1 +1 @@
1
- {"version":3,"file":"errors.d.ts","sourceRoot":"","sources":["../../src/errors.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AACH,cAAM,WAAY,SAAQ,KAAK;gBACV,OAAO,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,YAAY;CAI3D;AAED;;;;;GAKG;AACH,cAAM,iBAAkB,SAAQ,WAAW;gBACtB,OAAO,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,YAAY;CAI3D;AAED;;;;;GAKG;AACH,cAAM,mBAAoB,SAAQ,WAAW;gBACxB,OAAO,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,YAAY;CAQ3D;AAED,OAAO,EAAE,mBAAmB,EAAE,iBAAiB,EAAE,WAAW,EAAE,CAAC"}
1
+ {"version":3,"file":"errors.d.ts","sourceRoot":"","sources":["../../src/errors.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AACH,cAAM,WAAY,SAAQ,KAAK;gBACV,OAAO,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,YAAY;CAI3D;AAED;;;;;;GAMG;AACH,cAAM,iBAAkB,SAAQ,WAAW;gBACtB,OAAO,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,YAAY;CAI3D;AAED;;;;;GAKG;AACH,cAAM,mBAAoB,SAAQ,WAAW;gBACxB,OAAO,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,YAAY;CAQ3D;AAED,OAAO,EAAE,mBAAmB,EAAE,iBAAiB,EAAE,WAAW,EAAE,CAAC"}
package/lib/esm/errors.js CHANGED
@@ -14,8 +14,9 @@ class SignerError extends Error {
14
14
  /**
15
15
  * Error thrown when the signer is misconfigured.
16
16
  *
17
- * This is thrown eagerly at construction when the region cannot be determined,
18
- * and lazily during signing when credentials are missing or cannot be resolved.
17
+ * This is thrown eagerly at construction when the region cannot be determined
18
+ * or the SigV4a region set is invalid, and lazily during signing when the
19
+ * required AWS credentials environment variables are missing.
19
20
  */
20
21
  class SignerConfigError extends SignerError {
21
22
  constructor(message, options) {
@@ -0,0 +1,40 @@
1
+ import type { AwsCredentialIdentity, AwsCredentialIdentityProvider, HttpRequest } from '@smithy/types';
2
+ /**
3
+ * Reads credentials from the standard AWS Lambda environment variables.
4
+ *
5
+ * @internal
6
+ */
7
+ declare const credentialsFromEnv: AwsCredentialIdentityProvider;
8
+ /**
9
+ * Resolves static credentials or invokes the configured credential provider.
10
+ *
11
+ * @param credentials - The configured credentials or provider.
12
+ * @internal
13
+ */
14
+ declare const resolveCredentials: (credentials: AwsCredentialIdentity | AwsCredentialIdentityProvider) => Promise<AwsCredentialIdentity>;
15
+ /**
16
+ * Creates a request and preserves its referrer settings across init overrides.
17
+ *
18
+ * @param input - The URL or request to sign.
19
+ * @param init - The request options to apply.
20
+ * @internal
21
+ */
22
+ declare const toSigningRequest: (input: string | URL | Request, init?: RequestInit) => Request;
23
+ /**
24
+ * Adapts a web-standard request into the structural Smithy HTTP request shape.
25
+ *
26
+ * @param request - The request to adapt, buffering its body for signing.
27
+ * @param service - The signing service that determines canonical-path rules.
28
+ * @internal
29
+ */
30
+ declare const toHttpRequest: (request: Request, service: string) => Promise<HttpRequest>;
31
+ /**
32
+ * Creates a web-standard request with the signed headers and buffered body.
33
+ *
34
+ * @param signed - The signed Smithy HTTP request.
35
+ * @param original - The original request supplying the URL.
36
+ * @internal
37
+ */
38
+ declare const toRequest: (signed: HttpRequest, original: Request) => Request;
39
+ export { credentialsFromEnv, resolveCredentials, toHttpRequest, toRequest, toSigningRequest, };
40
+ //# sourceMappingURL=helpers.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"helpers.d.ts","sourceRoot":"","sources":["../../src/helpers.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EACV,qBAAqB,EACrB,6BAA6B,EAC7B,WAAW,EACZ,MAAM,eAAe,CAAC;AA4BvB;;;;GAIG;AACH,QAAA,MAAM,kBAAkB,EAAE,6BAsBzB,CAAC;AAEF;;;;;GAKG;AACH,QAAA,MAAM,kBAAkB,GACtB,aAAa,qBAAqB,GAAG,6BAA6B,KACjE,OAAO,CAAC,qBAAqB,CACuC,CAAC;AAExE;;;;;;GAMG;AACH,QAAA,MAAM,gBAAgB,GACpB,OAAO,MAAM,GAAG,GAAG,GAAG,OAAO,EAC7B,OAAO,WAAW,KACjB,OAUA,CAAC;AAwBJ;;;;;;GAMG;AACH,QAAA,MAAM,aAAa,GACjB,SAAS,OAAO,EAChB,SAAS,MAAM,KACd,OAAO,CAAC,WAAW,CAyBrB,CAAC;AAEF;;;;;;GAMG;AACH,QAAA,MAAM,SAAS,GAAI,QAAQ,WAAW,EAAE,UAAU,OAAO,KAAG,OAe3D,CAAC;AAEF,OAAO,EACL,kBAAkB,EAClB,kBAAkB,EAClB,aAAa,EACb,SAAS,EACT,gBAAgB,GACjB,CAAC"}
@@ -0,0 +1,138 @@
1
+ import { getStringFromEnv } from '@aws-lambda-powertools/commons/utils/env';
2
+ import { SERVICE_S3 } from './constants.js';
3
+ import { RequestSigningError, SignerConfigError } from './errors.js';
4
+ /**
5
+ * Builds the canonical S3 URI for an already parsed request URL.
6
+ *
7
+ * S3 treats raw plus signs as spaces and decodes escaped slashes in object
8
+ * keys. Preserve other escapes without double-encoding their percent signs.
9
+ *
10
+ * @param path - The URL pathname as it will be sent to S3.
11
+ * @internal
12
+ */
13
+ const canonicalS3Path = (path) => encodeURIComponent(path.replace(/\+/g, ' '))
14
+ .replace(/[!'()*]/g, (character) => `%${character.charCodeAt(0).toString(16).toUpperCase()}`)
15
+ .replace(/%2F/g, '/')
16
+ .replace(/%25([a-f0-9]{2})/gi, (_match, byte) => {
17
+ const character = String.fromCharCode(Number.parseInt(byte, 16));
18
+ return /^[a-z0-9._~/-]$/i.test(character)
19
+ ? character
20
+ : `%${byte.toUpperCase()}`;
21
+ });
22
+ /**
23
+ * Reads credentials from the standard AWS Lambda environment variables.
24
+ *
25
+ * @internal
26
+ */
27
+ const credentialsFromEnv = async () => {
28
+ const accessKeyId = getStringFromEnv({
29
+ key: 'AWS_ACCESS_KEY_ID',
30
+ defaultValue: '',
31
+ });
32
+ const secretAccessKey = getStringFromEnv({
33
+ key: 'AWS_SECRET_ACCESS_KEY',
34
+ defaultValue: '',
35
+ });
36
+ if (accessKeyId === '' || secretAccessKey === '') {
37
+ throw new SignerConfigError('Unable to resolve AWS credentials to sign the request with. Ensure the standard AWS credentials environment variables are set, or pass the `credentials` option.');
38
+ }
39
+ return {
40
+ accessKeyId,
41
+ secretAccessKey,
42
+ sessionToken: getStringFromEnv({ key: 'AWS_SESSION_TOKEN', defaultValue: '' }) ||
43
+ undefined,
44
+ };
45
+ };
46
+ /**
47
+ * Resolves static credentials or invokes the configured credential provider.
48
+ *
49
+ * @param credentials - The configured credentials or provider.
50
+ * @internal
51
+ */
52
+ const resolveCredentials = async (credentials) => typeof credentials === 'function' ? await credentials() : credentials;
53
+ /**
54
+ * Creates a request and preserves its referrer settings across init overrides.
55
+ *
56
+ * @param input - The URL or request to sign.
57
+ * @param init - The request options to apply.
58
+ * @internal
59
+ */
60
+ const toSigningRequest = (input, init) => new Request(input, input instanceof Request
61
+ ? {
62
+ ...init,
63
+ referrer: init?.referrer ?? input.referrer,
64
+ referrerPolicy: init?.referrerPolicy ?? input.referrerPolicy,
65
+ }
66
+ : init);
67
+ /**
68
+ * Buffers the request body so it can be hashed and replayed after signing.
69
+ *
70
+ * @param request - The request whose body to read.
71
+ * @internal
72
+ */
73
+ const readBody = async (request) => {
74
+ if (request.body === null) {
75
+ return undefined;
76
+ }
77
+ try {
78
+ const buffer = await request.clone().arrayBuffer();
79
+ return buffer.byteLength === 0 ? undefined : new Uint8Array(buffer);
80
+ }
81
+ catch (error) {
82
+ throw new RequestSigningError('Unable to read the request body to sign it. The body could not be buffered, for example because it is a stream that cannot be replayed.', { cause: error });
83
+ }
84
+ };
85
+ /**
86
+ * Adapts a web-standard request into the structural Smithy HTTP request shape.
87
+ *
88
+ * @param request - The request to adapt, buffering its body for signing.
89
+ * @param service - The signing service that determines canonical-path rules.
90
+ * @internal
91
+ */
92
+ const toHttpRequest = async (request, service) => {
93
+ const url = new URL(request.url);
94
+ const headers = {};
95
+ for (const [key, value] of request.headers) {
96
+ headers[key] = value;
97
+ }
98
+ headers.host = url.host;
99
+ // A repeated query parameter keeps all its values, as SigV4 signs each one.
100
+ const query = {};
101
+ for (const [key, value] of url.searchParams) {
102
+ query[key] ??= [];
103
+ query[key].push(value);
104
+ }
105
+ return {
106
+ method: request.method,
107
+ protocol: url.protocol,
108
+ hostname: url.hostname,
109
+ port: url.port ? Number(url.port) : undefined,
110
+ path: service === SERVICE_S3 ? canonicalS3Path(url.pathname) : url.pathname,
111
+ query,
112
+ headers,
113
+ body: await readBody(request),
114
+ };
115
+ };
116
+ /**
117
+ * Creates a web-standard request with the signed headers and buffered body.
118
+ *
119
+ * @param signed - The signed Smithy HTTP request.
120
+ * @param original - The original request supplying the URL.
121
+ * @internal
122
+ */
123
+ const toRequest = (signed, original) => {
124
+ const headers = new Headers();
125
+ for (const [key, value] of Object.entries(signed.headers)) {
126
+ headers.set(key, value);
127
+ }
128
+ return new Request(original, {
129
+ method: signed.method,
130
+ headers,
131
+ // Request resets referrer settings when init contains overrides.
132
+ referrer: original.referrer,
133
+ referrerPolicy: original.referrerPolicy,
134
+ // Smithy's body type is broader than fetch's; our adapter supplies only bytes.
135
+ body: signed.body,
136
+ });
137
+ };
138
+ export { credentialsFromEnv, resolveCredentials, toHttpRequest, toRequest, toSigningRequest, };
@@ -0,0 +1,3 @@
1
+ export { SigV4aSigner } from './SigV4aSigner.js';
2
+ export type { Signer, SigV4aSignerOptions } from './types/index.js';
3
+ //# sourceMappingURL=sigv4a.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"sigv4a.d.ts","sourceRoot":"","sources":["../../src/sigv4a.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAC;AACjD,YAAY,EAAE,MAAM,EAAE,mBAAmB,EAAE,MAAM,kBAAkB,CAAC"}
@@ -0,0 +1 @@
1
+ export { SigV4aSigner } from './SigV4aSigner.js';
@@ -2,9 +2,9 @@ import type { AwsCredentialIdentity, AwsCredentialIdentityProvider } from '@smit
2
2
  /**
3
3
  * A request signer.
4
4
  *
5
- * This is the structural contract that every signing variant (e.g. SigV4, and
6
- * future variants such as SigV4a) implements, and the contract that the
7
- * `createSignedFetcher` factory depends on.
5
+ * This is the structural contract that every signing variant (SigV4 and
6
+ * SigV4a) implements, and the contract that the `createSignedFetcher` factory
7
+ * depends on.
8
8
  *
9
9
  * Implementations take a web-standard request and return a signed
10
10
  * web-standard {@link Request | `Request`}, performing no network I/O.
@@ -19,10 +19,9 @@ interface Signer {
19
19
  sign(input: string | URL | Request, init?: RequestInit): Promise<Request>;
20
20
  }
21
21
  /**
22
- * Options for constructing a {@link Signer | `Signer`} that uses the AWS
23
- * Signature Version 4 signing process.
22
+ * Configures the service and credentials shared by AWS request signers.
24
23
  */
25
- interface SigV4SignerOptions {
24
+ interface AwsSignerOptions {
26
25
  /**
27
26
  * The service name to use when signing the request, e.g. `execute-api`,
28
27
  * `lambda`, or `appsync`.
@@ -31,19 +30,37 @@ interface SigV4SignerOptions {
31
30
  * CloudFront hide the underlying service), so it is required.
32
31
  */
33
32
  service: string;
33
+ /**
34
+ * The credentials to use when signing the request, either as a static
35
+ * value or as a provider that resolves them.
36
+ *
37
+ * @default credentials read from the standard Lambda environment variables
38
+ */
39
+ credentials?: AwsCredentialIdentity | AwsCredentialIdentityProvider;
40
+ }
41
+ /**
42
+ * Configures a signer that uses AWS Signature Version 4.
43
+ */
44
+ interface SigV4SignerOptions extends AwsSignerOptions {
34
45
  /**
35
46
  * The AWS region to use when signing the request.
36
47
  *
37
48
  * @default process.env.AWS_REGION
38
49
  */
39
50
  region?: string;
51
+ }
52
+ /**
53
+ * Configures a signer that uses AWS Signature Version 4A.
54
+ */
55
+ interface SigV4aSignerOptions extends AwsSignerOptions {
40
56
  /**
41
- * The credentials to use when signing the request, either as a static
42
- * value or as a provider that resolves them.
57
+ * The AWS regions in which the signature is valid.
43
58
  *
44
- * @default credentials read from the standard Lambda environment variables
59
+ * Provide at least one region or wildcard pattern, such as
60
+ * `['us-east-1', 'us-west-2']`, `['us-west-*']`, or `['*']` for all regions.
61
+ * This option is required and does not default to `AWS_REGION`.
45
62
  */
46
- credentials?: AwsCredentialIdentity | AwsCredentialIdentityProvider;
63
+ regionSet: readonly string[];
47
64
  }
48
65
  /**
49
66
  * Options for the `createSignedFetcher` factory.
@@ -56,5 +73,5 @@ interface SignedFetcherOptions {
56
73
  */
57
74
  fetch?: typeof fetch;
58
75
  }
59
- export type { SignedFetcherOptions, Signer, SigV4SignerOptions };
76
+ export type { AwsSignerOptions, SignedFetcherOptions, Signer, SigV4aSignerOptions, SigV4SignerOptions, };
60
77
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/types/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,qBAAqB,EACrB,6BAA6B,EAC9B,MAAM,eAAe,CAAC;AAEvB;;;;;;;;;GASG;AACH,UAAU,MAAM;IACd;;;;;OAKG;IACH,IAAI,CAAC,KAAK,EAAE,MAAM,GAAG,GAAG,GAAG,OAAO,EAAE,IAAI,CAAC,EAAE,WAAW,GAAG,OAAO,CAAC,OAAO,CAAC,CAAC;CAC3E;AAED;;;GAGG;AACH,UAAU,kBAAkB;IAC1B;;;;;;OAMG;IACH,OAAO,EAAE,MAAM,CAAC;IAChB;;;;OAIG;IACH,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB;;;;;OAKG;IACH,WAAW,CAAC,EAAE,qBAAqB,GAAG,6BAA6B,CAAC;CACrE;AAED;;GAEG;AACH,UAAU,oBAAoB;IAC5B;;;;OAIG;IACH,KAAK,CAAC,EAAE,OAAO,KAAK,CAAC;CACtB;AAED,YAAY,EAAE,oBAAoB,EAAE,MAAM,EAAE,kBAAkB,EAAE,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/types/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,qBAAqB,EACrB,6BAA6B,EAC9B,MAAM,eAAe,CAAC;AAEvB;;;;;;;;;GASG;AACH,UAAU,MAAM;IACd;;;;;OAKG;IACH,IAAI,CAAC,KAAK,EAAE,MAAM,GAAG,GAAG,GAAG,OAAO,EAAE,IAAI,CAAC,EAAE,WAAW,GAAG,OAAO,CAAC,OAAO,CAAC,CAAC;CAC3E;AAED;;GAEG;AACH,UAAU,gBAAgB;IACxB;;;;;;OAMG;IACH,OAAO,EAAE,MAAM,CAAC;IAChB;;;;;OAKG;IACH,WAAW,CAAC,EAAE,qBAAqB,GAAG,6BAA6B,CAAC;CACrE;AAED;;GAEG;AACH,UAAU,kBAAmB,SAAQ,gBAAgB;IACnD;;;;OAIG;IACH,MAAM,CAAC,EAAE,MAAM,CAAC;CACjB;AAED;;GAEG;AACH,UAAU,mBAAoB,SAAQ,gBAAgB;IACpD;;;;;;OAMG;IACH,SAAS,EAAE,SAAS,MAAM,EAAE,CAAC;CAC9B;AAED;;GAEG;AACH,UAAU,oBAAoB;IAC5B;;;;OAIG;IACH,KAAK,CAAC,EAAE,OAAO,KAAK,CAAC;CACtB;AAED,YAAY,EACV,gBAAgB,EAChB,oBAAoB,EACpB,MAAM,EACN,mBAAmB,EACnB,kBAAkB,GACnB,CAAC"}