@aws-amplify/core 5.2.1-unstable.eca5078.2 → 5.3.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 (183) hide show
  1. package/lib/Platform/version.d.ts +1 -1
  2. package/lib/Platform/version.js +1 -1
  3. package/lib/Platform/version.js.map +1 -1
  4. package/lib/Signer.d.ts +1 -1
  5. package/lib/Signer.js +59 -256
  6. package/lib/Signer.js.map +1 -1
  7. package/lib/clients/middleware/signing/signer/signatureV4/constants.d.ts +17 -0
  8. package/lib/clients/middleware/signing/signer/signatureV4/constants.js +26 -0
  9. package/lib/clients/middleware/signing/signer/signatureV4/constants.js.map +1 -0
  10. package/lib/clients/middleware/signing/signer/signatureV4/index.d.ts +3 -0
  11. package/lib/clients/middleware/signing/signer/signatureV4/index.js +12 -0
  12. package/lib/clients/middleware/signing/signer/signatureV4/index.js.map +1 -0
  13. package/lib/clients/middleware/signing/signer/signatureV4/index.native.d.ts +4 -0
  14. package/lib/clients/middleware/signing/signer/signatureV4/index.native.js +13 -0
  15. package/lib/clients/middleware/signing/signer/signatureV4/index.native.js.map +1 -0
  16. package/lib/clients/middleware/signing/signer/signatureV4/presignUrl.d.ts +9 -0
  17. package/lib/clients/middleware/signing/signer/signatureV4/presignUrl.js +40 -0
  18. package/lib/clients/middleware/signing/signer/signatureV4/presignUrl.js.map +1 -0
  19. package/lib/clients/middleware/signing/signer/signatureV4/signRequest.d.ts +10 -0
  20. package/lib/clients/middleware/signing/signer/signatureV4/signRequest.js +36 -0
  21. package/lib/clients/middleware/signing/signer/signatureV4/signRequest.js.map +1 -0
  22. package/lib/clients/middleware/signing/signer/signatureV4/types/index.d.ts +1 -0
  23. package/lib/clients/middleware/signing/signer/signatureV4/types/index.js +5 -0
  24. package/lib/clients/middleware/signing/signer/signatureV4/types/index.js.map +1 -0
  25. package/lib/clients/middleware/signing/signer/signatureV4/types/signer.d.ts +20 -0
  26. package/lib/clients/middleware/signing/signer/signatureV4/types/signer.js +5 -0
  27. package/lib/clients/middleware/signing/signer/signatureV4/types/signer.js.map +1 -0
  28. package/lib/clients/middleware/signing/signer/signatureV4/utils/dataHashHelpers.d.ts +18 -0
  29. package/lib/clients/middleware/signing/signer/signatureV4/utils/dataHashHelpers.js +35 -0
  30. package/lib/clients/middleware/signing/signer/signatureV4/utils/dataHashHelpers.js.map +1 -0
  31. package/lib/clients/middleware/signing/signer/signatureV4/utils/getCanonicalHeaders.d.ts +12 -0
  32. package/lib/clients/middleware/signing/signer/signatureV4/utils/getCanonicalHeaders.js +31 -0
  33. package/lib/clients/middleware/signing/signer/signatureV4/utils/getCanonicalHeaders.js.map +1 -0
  34. package/lib/clients/middleware/signing/signer/signatureV4/utils/getCanonicalQueryString.d.ts +12 -0
  35. package/lib/clients/middleware/signing/signer/signatureV4/utils/getCanonicalQueryString.js +39 -0
  36. package/lib/clients/middleware/signing/signer/signatureV4/utils/getCanonicalQueryString.js.map +1 -0
  37. package/lib/clients/middleware/signing/signer/signatureV4/utils/getCanonicalRequest.d.ts +16 -0
  38. package/lib/clients/middleware/signing/signer/signatureV4/utils/getCanonicalRequest.js +35 -0
  39. package/lib/clients/middleware/signing/signer/signatureV4/utils/getCanonicalRequest.js.map +1 -0
  40. package/lib/clients/middleware/signing/signer/signatureV4/utils/getCanonicalUri.d.ts +10 -0
  41. package/lib/clients/middleware/signing/signer/signatureV4/utils/getCanonicalUri.js +17 -0
  42. package/lib/clients/middleware/signing/signer/signatureV4/utils/getCanonicalUri.js.map +1 -0
  43. package/lib/clients/middleware/signing/signer/signatureV4/utils/getCredentialScope.d.ts +12 -0
  44. package/lib/clients/middleware/signing/signer/signatureV4/utils/getCredentialScope.js +18 -0
  45. package/lib/clients/middleware/signing/signer/signatureV4/utils/getCredentialScope.js.map +1 -0
  46. package/lib/clients/middleware/signing/signer/signatureV4/utils/getFormattedDates.d.ts +12 -0
  47. package/lib/clients/middleware/signing/signer/signatureV4/utils/getFormattedDates.js +22 -0
  48. package/lib/clients/middleware/signing/signer/signatureV4/utils/getFormattedDates.js.map +1 -0
  49. package/lib/clients/middleware/signing/signer/signatureV4/utils/getHashedPayload.d.ts +10 -0
  50. package/lib/clients/middleware/signing/signer/signatureV4/utils/getHashedPayload.js +35 -0
  51. package/lib/clients/middleware/signing/signer/signatureV4/utils/getHashedPayload.js.map +1 -0
  52. package/lib/clients/middleware/signing/signer/signatureV4/utils/getSignature.d.ts +13 -0
  53. package/lib/clients/middleware/signing/signer/signatureV4/utils/getSignature.js +31 -0
  54. package/lib/clients/middleware/signing/signer/signatureV4/utils/getSignature.js.map +1 -0
  55. package/lib/clients/middleware/signing/signer/signatureV4/utils/getSignedHeaders.d.ts +10 -0
  56. package/lib/clients/middleware/signing/signer/signatureV4/utils/getSignedHeaders.js +20 -0
  57. package/lib/clients/middleware/signing/signer/signatureV4/utils/getSignedHeaders.js.map +1 -0
  58. package/lib/clients/middleware/signing/signer/signatureV4/utils/getSigningKey.d.ts +13 -0
  59. package/lib/clients/middleware/signing/signer/signatureV4/utils/getSigningKey.js +27 -0
  60. package/lib/clients/middleware/signing/signer/signatureV4/utils/getSigningKey.js.map +1 -0
  61. package/lib/clients/middleware/signing/signer/signatureV4/utils/getSigningValues.d.ts +11 -0
  62. package/lib/clients/middleware/signing/signer/signatureV4/utils/getSigningValues.js +34 -0
  63. package/lib/clients/middleware/signing/signer/signatureV4/utils/getSigningValues.js.map +1 -0
  64. package/lib/clients/middleware/signing/signer/signatureV4/utils/getStringToSign.d.ts +16 -0
  65. package/lib/clients/middleware/signing/signer/signatureV4/utils/getStringToSign.js +24 -0
  66. package/lib/clients/middleware/signing/signer/signatureV4/utils/getStringToSign.js.map +1 -0
  67. package/lib/clients/types/aws.d.ts +2 -0
  68. package/lib/clients/types/aws.js +5 -0
  69. package/lib/clients/types/aws.js.map +1 -0
  70. package/lib/clients/types/core.d.ts +13 -0
  71. package/lib/clients/types/core.js +5 -0
  72. package/lib/clients/types/core.js.map +1 -0
  73. package/lib/clients/types/http.d.ts +26 -0
  74. package/lib/clients/types/http.js +5 -0
  75. package/lib/clients/types/http.js.map +1 -0
  76. package/lib/clients/types/index.d.ts +2 -0
  77. package/lib/clients/types/index.js +5 -0
  78. package/lib/clients/types/index.js.map +1 -0
  79. package/lib-esm/Platform/version.d.ts +1 -1
  80. package/lib-esm/Platform/version.js +1 -1
  81. package/lib-esm/Platform/version.js.map +1 -1
  82. package/lib-esm/Signer.d.ts +1 -1
  83. package/lib-esm/Signer.js +60 -257
  84. package/lib-esm/Signer.js.map +1 -1
  85. package/lib-esm/clients/middleware/signing/signer/signatureV4/constants.d.ts +17 -0
  86. package/lib-esm/clients/middleware/signing/signer/signatureV4/constants.js +24 -0
  87. package/lib-esm/clients/middleware/signing/signer/signatureV4/constants.js.map +1 -0
  88. package/lib-esm/clients/middleware/signing/signer/signatureV4/index.d.ts +3 -0
  89. package/lib-esm/clients/middleware/signing/signer/signatureV4/index.js +7 -0
  90. package/lib-esm/clients/middleware/signing/signer/signatureV4/index.js.map +1 -0
  91. package/lib-esm/clients/middleware/signing/signer/signatureV4/index.native.d.ts +4 -0
  92. package/lib-esm/clients/middleware/signing/signer/signatureV4/index.native.js +8 -0
  93. package/lib-esm/clients/middleware/signing/signer/signatureV4/index.native.js.map +1 -0
  94. package/lib-esm/clients/middleware/signing/signer/signatureV4/presignUrl.d.ts +9 -0
  95. package/lib-esm/clients/middleware/signing/signer/signatureV4/presignUrl.js +38 -0
  96. package/lib-esm/clients/middleware/signing/signer/signatureV4/presignUrl.js.map +1 -0
  97. package/lib-esm/clients/middleware/signing/signer/signatureV4/signRequest.d.ts +10 -0
  98. package/lib-esm/clients/middleware/signing/signer/signatureV4/signRequest.js +34 -0
  99. package/lib-esm/clients/middleware/signing/signer/signatureV4/signRequest.js.map +1 -0
  100. package/lib-esm/clients/middleware/signing/signer/signatureV4/types/index.d.ts +1 -0
  101. package/lib-esm/clients/middleware/signing/signer/signatureV4/types/index.js +3 -0
  102. package/lib-esm/clients/middleware/signing/signer/signatureV4/types/index.js.map +1 -0
  103. package/lib-esm/clients/middleware/signing/signer/signatureV4/types/signer.d.ts +20 -0
  104. package/lib-esm/clients/middleware/signing/signer/signatureV4/types/signer.js +3 -0
  105. package/lib-esm/clients/middleware/signing/signer/signatureV4/types/signer.js.map +1 -0
  106. package/lib-esm/clients/middleware/signing/signer/signatureV4/utils/dataHashHelpers.d.ts +18 -0
  107. package/lib-esm/clients/middleware/signing/signer/signatureV4/utils/dataHashHelpers.js +33 -0
  108. package/lib-esm/clients/middleware/signing/signer/signatureV4/utils/dataHashHelpers.js.map +1 -0
  109. package/lib-esm/clients/middleware/signing/signer/signatureV4/utils/getCanonicalHeaders.d.ts +12 -0
  110. package/lib-esm/clients/middleware/signing/signer/signatureV4/utils/getCanonicalHeaders.js +29 -0
  111. package/lib-esm/clients/middleware/signing/signer/signatureV4/utils/getCanonicalHeaders.js.map +1 -0
  112. package/lib-esm/clients/middleware/signing/signer/signatureV4/utils/getCanonicalQueryString.d.ts +12 -0
  113. package/lib-esm/clients/middleware/signing/signer/signatureV4/utils/getCanonicalQueryString.js +37 -0
  114. package/lib-esm/clients/middleware/signing/signer/signatureV4/utils/getCanonicalQueryString.js.map +1 -0
  115. package/lib-esm/clients/middleware/signing/signer/signatureV4/utils/getCanonicalRequest.d.ts +16 -0
  116. package/lib-esm/clients/middleware/signing/signer/signatureV4/utils/getCanonicalRequest.js +33 -0
  117. package/lib-esm/clients/middleware/signing/signer/signatureV4/utils/getCanonicalRequest.js.map +1 -0
  118. package/lib-esm/clients/middleware/signing/signer/signatureV4/utils/getCanonicalUri.d.ts +10 -0
  119. package/lib-esm/clients/middleware/signing/signer/signatureV4/utils/getCanonicalUri.js +15 -0
  120. package/lib-esm/clients/middleware/signing/signer/signatureV4/utils/getCanonicalUri.js.map +1 -0
  121. package/lib-esm/clients/middleware/signing/signer/signatureV4/utils/getCredentialScope.d.ts +12 -0
  122. package/lib-esm/clients/middleware/signing/signer/signatureV4/utils/getCredentialScope.js +16 -0
  123. package/lib-esm/clients/middleware/signing/signer/signatureV4/utils/getCredentialScope.js.map +1 -0
  124. package/lib-esm/clients/middleware/signing/signer/signatureV4/utils/getFormattedDates.d.ts +12 -0
  125. package/lib-esm/clients/middleware/signing/signer/signatureV4/utils/getFormattedDates.js +20 -0
  126. package/lib-esm/clients/middleware/signing/signer/signatureV4/utils/getFormattedDates.js.map +1 -0
  127. package/lib-esm/clients/middleware/signing/signer/signatureV4/utils/getHashedPayload.d.ts +10 -0
  128. package/lib-esm/clients/middleware/signing/signer/signatureV4/utils/getHashedPayload.js +33 -0
  129. package/lib-esm/clients/middleware/signing/signer/signatureV4/utils/getHashedPayload.js.map +1 -0
  130. package/lib-esm/clients/middleware/signing/signer/signatureV4/utils/getSignature.d.ts +13 -0
  131. package/lib-esm/clients/middleware/signing/signer/signatureV4/utils/getSignature.js +29 -0
  132. package/lib-esm/clients/middleware/signing/signer/signatureV4/utils/getSignature.js.map +1 -0
  133. package/lib-esm/clients/middleware/signing/signer/signatureV4/utils/getSignedHeaders.d.ts +10 -0
  134. package/lib-esm/clients/middleware/signing/signer/signatureV4/utils/getSignedHeaders.js +18 -0
  135. package/lib-esm/clients/middleware/signing/signer/signatureV4/utils/getSignedHeaders.js.map +1 -0
  136. package/lib-esm/clients/middleware/signing/signer/signatureV4/utils/getSigningKey.d.ts +13 -0
  137. package/lib-esm/clients/middleware/signing/signer/signatureV4/utils/getSigningKey.js +25 -0
  138. package/lib-esm/clients/middleware/signing/signer/signatureV4/utils/getSigningKey.js.map +1 -0
  139. package/lib-esm/clients/middleware/signing/signer/signatureV4/utils/getSigningValues.d.ts +11 -0
  140. package/lib-esm/clients/middleware/signing/signer/signatureV4/utils/getSigningValues.js +32 -0
  141. package/lib-esm/clients/middleware/signing/signer/signatureV4/utils/getSigningValues.js.map +1 -0
  142. package/lib-esm/clients/middleware/signing/signer/signatureV4/utils/getStringToSign.d.ts +16 -0
  143. package/lib-esm/clients/middleware/signing/signer/signatureV4/utils/getStringToSign.js +22 -0
  144. package/lib-esm/clients/middleware/signing/signer/signatureV4/utils/getStringToSign.js.map +1 -0
  145. package/lib-esm/clients/types/aws.d.ts +2 -0
  146. package/lib-esm/clients/types/aws.js +3 -0
  147. package/lib-esm/clients/types/aws.js.map +1 -0
  148. package/lib-esm/clients/types/core.d.ts +13 -0
  149. package/lib-esm/clients/types/core.js +3 -0
  150. package/lib-esm/clients/types/core.js.map +1 -0
  151. package/lib-esm/clients/types/http.d.ts +26 -0
  152. package/lib-esm/clients/types/http.js +3 -0
  153. package/lib-esm/clients/types/http.js.map +1 -0
  154. package/lib-esm/clients/types/index.d.ts +2 -0
  155. package/lib-esm/clients/types/index.js +3 -0
  156. package/lib-esm/clients/types/index.js.map +1 -0
  157. package/package.json +6 -2
  158. package/src/Platform/version.ts +1 -1
  159. package/src/Signer.ts +89 -344
  160. package/src/clients/middleware/signing/signer/signatureV4/constants.ts +28 -0
  161. package/src/clients/middleware/signing/signer/signatureV4/index.native.ts +9 -0
  162. package/src/clients/middleware/signing/signer/signatureV4/index.ts +7 -0
  163. package/src/clients/middleware/signing/signer/signatureV4/presignUrl.ts +59 -0
  164. package/src/clients/middleware/signing/signer/signatureV4/signRequest.ts +51 -0
  165. package/src/clients/middleware/signing/signer/signatureV4/types/index.ts +4 -0
  166. package/src/clients/middleware/signing/signer/signatureV4/types/signer.ts +31 -0
  167. package/src/clients/middleware/signing/signer/signatureV4/utils/dataHashHelpers.ts +42 -0
  168. package/src/clients/middleware/signing/signer/signatureV4/utils/getCanonicalHeaders.ts +25 -0
  169. package/src/clients/middleware/signing/signer/signatureV4/utils/getCanonicalQueryString.ts +32 -0
  170. package/src/clients/middleware/signing/signer/signatureV4/utils/getCanonicalRequest.ts +38 -0
  171. package/src/clients/middleware/signing/signer/signatureV4/utils/getCanonicalUri.ts +14 -0
  172. package/src/clients/middleware/signing/signer/signatureV4/utils/getCredentialScope.ts +21 -0
  173. package/src/clients/middleware/signing/signer/signatureV4/utils/getFormattedDates.ts +22 -0
  174. package/src/clients/middleware/signing/signer/signatureV4/utils/getHashedPayload.ts +38 -0
  175. package/src/clients/middleware/signing/signer/signatureV4/utils/getSignature.ts +52 -0
  176. package/src/clients/middleware/signing/signer/signatureV4/utils/getSignedHeaders.ts +19 -0
  177. package/src/clients/middleware/signing/signer/signatureV4/utils/getSigningKey.ts +31 -0
  178. package/src/clients/middleware/signing/signer/signatureV4/utils/getSigningValues.ts +43 -0
  179. package/src/clients/middleware/signing/signer/signatureV4/utils/getStringToSign.ts +28 -0
  180. package/src/clients/types/aws.ts +6 -0
  181. package/src/clients/types/core.ts +17 -0
  182. package/src/clients/types/http.ts +33 -0
  183. package/src/clients/types/index.ts +5 -0
@@ -0,0 +1,42 @@
1
+ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ // TODO: V6 update to different crypto dependency?
5
+ import { Sha256 } from '@aws-crypto/sha256-js';
6
+ import { SourceData } from '@aws-sdk/types';
7
+ import { toHex } from '@aws-sdk/util-hex-encoding';
8
+
9
+ /**
10
+ * Returns the hashed data a `Uint8Array`.
11
+ *
12
+ * @param key `SourceData` to be used as hashing key.
13
+ * @param data Hashable `SourceData`.
14
+ * @returns `Uint8Array` created from the data as input to a hash function.
15
+ */
16
+ export const getHashedData = (
17
+ key: SourceData | null,
18
+ data: SourceData
19
+ ): Uint8Array => {
20
+ const sha256 = new Sha256(key);
21
+ sha256.update(data);
22
+ // TODO: V6 flip to async digest
23
+ const hashedData = sha256.digestSync();
24
+ return hashedData;
25
+ };
26
+
27
+ /**
28
+ * Returns the hashed data as a hex string.
29
+ *
30
+ * @param key `SourceData` to be used as hashing key.
31
+ * @param data Hashable `SourceData`.
32
+ * @returns String using lowercase hexadecimal characters created from the data as input to a hash function.
33
+ *
34
+ * @internal
35
+ */
36
+ export const getHashedDataAsHex = (
37
+ key: SourceData | null,
38
+ data: SourceData
39
+ ): string => {
40
+ const hashedData = getHashedData(key, data);
41
+ return toHex(hashedData);
42
+ };
@@ -0,0 +1,25 @@
1
+ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ import { HttpRequest } from '../../../../../types';
5
+
6
+ /**
7
+ * Returns canonical headers.
8
+ *
9
+ * @param headers Headers from the request.
10
+ * @returns Request headers that will be signed, and their values, separated by newline characters. Header names must
11
+ * use lowercase characters, must appear in alphabetical order, and must be followed by a colon (:). For the values,
12
+ * trim any leading or trailing spaces, convert sequential spaces to a single space, and separate the values
13
+ * for a multi-value header using commas.
14
+ *
15
+ * @internal
16
+ */
17
+ export const getCanonicalHeaders = (headers: HttpRequest['headers']): string =>
18
+ Object.entries(headers)
19
+ .map(([key, value]) => ({
20
+ key: key.toLowerCase(),
21
+ value: value?.trim().replace(/\s+/g, ' ') ?? '',
22
+ }))
23
+ .sort((a, b) => (a.key < b.key ? -1 : 1))
24
+ .map(entry => `${entry.key}:${entry.value}\n`)
25
+ .join('');
@@ -0,0 +1,32 @@
1
+ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ /**
5
+ * Returns a canonical query string.
6
+ *
7
+ * @param searchParams `searchParams` from the request url.
8
+ * @returns URL-encoded query string parameters, separated by ampersands (&). Percent-encode reserved characters,
9
+ * including the space character. Encode names and values separately. If there are empty parameters, append the equals
10
+ * sign to the parameter name before encoding. After encoding, sort the parameters alphabetically by key name. If there
11
+ * is no query string, use an empty string ("").
12
+ *
13
+ * @internal
14
+ */
15
+ export const getCanonicalQueryString = (
16
+ searchParams: URLSearchParams
17
+ ): string =>
18
+ Array.from(searchParams)
19
+ .sort(([keyA, valA], [keyB, valB]) => {
20
+ if (keyA === keyB) {
21
+ return valA < valB ? -1 : 1;
22
+ }
23
+ return keyA < keyB ? -1 : 1;
24
+ })
25
+ .map(([key, val]) => `${escapeUri(key)}=${escapeUri(val)}`)
26
+ .join('&');
27
+
28
+ const escapeUri = (uri: string): string =>
29
+ encodeURIComponent(uri).replace(/[!'()*]/g, hexEncode);
30
+
31
+ const hexEncode = (c: string) =>
32
+ `%${c.charCodeAt(0).toString(16).toUpperCase()}`;
@@ -0,0 +1,38 @@
1
+ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ import { HttpRequest } from '../../../../../types';
5
+ import { getCanonicalHeaders } from './getCanonicalHeaders';
6
+ import { getCanonicalQueryString } from './getCanonicalQueryString';
7
+ import { getCanonicalUri } from './getCanonicalUri';
8
+ import { getHashedPayload } from './getHashedPayload';
9
+ import { getSignedHeaders } from './getSignedHeaders';
10
+
11
+ /**
12
+ * Returns a canonical request.
13
+ *
14
+ * @param request `HttpRequest` from which to create the canonical request from.
15
+ * @returns String created by by concatenating the following strings, separated by newline characters:
16
+ * - HTTPMethod
17
+ * - CanonicalUri
18
+ * - CanonicalQueryString
19
+ * - CanonicalHeaders
20
+ * - SignedHeaders
21
+ * - HashedPayload
22
+ *
23
+ * @internal
24
+ */
25
+ export const getCanonicalRequest = ({
26
+ body,
27
+ headers,
28
+ method,
29
+ url,
30
+ }: HttpRequest): string =>
31
+ [
32
+ method,
33
+ getCanonicalUri(url.pathname),
34
+ getCanonicalQueryString(url.searchParams),
35
+ getCanonicalHeaders(headers),
36
+ getSignedHeaders(headers),
37
+ getHashedPayload(body),
38
+ ].join('\n');
@@ -0,0 +1,14 @@
1
+ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ /**
5
+ * Returns a canonical uri.
6
+ *
7
+ * @param pathname `pathname` from request url.
8
+ * @returns URI-encoded version of the absolute path component URL (everything between the host and the question mark
9
+ * character (?) that starts the query string parameters). If the absolute path is empty, a forward slash character (/).
10
+ *
11
+ * @internal
12
+ */
13
+ export const getCanonicalUri = (pathname: string): string =>
14
+ pathname ? encodeURIComponent(pathname).replace(/%2F/g, '/') : '/';
@@ -0,0 +1,21 @@
1
+ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ import { KEY_TYPE_IDENTIFIER } from '../constants';
5
+
6
+ /**
7
+ * Returns the credential scope which restricts the resulting signature to the specified region and service.
8
+ *
9
+ * @param date Current date in the format 'YYYYMMDD'.
10
+ * @param region AWS region in which the service resides.
11
+ * @param service Service to which the signed request is being sent.
12
+ *
13
+ * @returns A string representing the credential scope with format 'YYYYMMDD/region/service/aws4_request'.
14
+ *
15
+ * @internal
16
+ */
17
+ export const getCredentialScope = (
18
+ date: string,
19
+ region: string,
20
+ service: string
21
+ ): string => `${date}/${region}/${service}/${KEY_TYPE_IDENTIFIER}`;
@@ -0,0 +1,22 @@
1
+ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ import { FormattedDates } from '../types/signer';
5
+
6
+ /**
7
+ * Returns expected date strings to be used in signing.
8
+ *
9
+ * @param date JavaScript `Date` object.
10
+ * @returns `FormattedDates` object containing the following:
11
+ * - longDate: A date string in 'YYYYMMDDThhmmssZ' format
12
+ * - shortDate: A date string in 'YYYYMMDD' format
13
+ *
14
+ * @internal
15
+ */
16
+ export const getFormattedDates = (date: Date): FormattedDates => {
17
+ const longDate = date.toISOString().replace(/[:\-]|\.\d{3}/g, '');
18
+ return {
19
+ longDate,
20
+ shortDate: longDate.slice(0, 8),
21
+ };
22
+ };
@@ -0,0 +1,38 @@
1
+ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ import { SourceData } from '@aws-sdk/types';
5
+ import { HttpRequest } from '../../../../../types';
6
+ import { EMPTY_HASH, UNSIGNED_PAYLOAD } from '../constants';
7
+ import { getHashedDataAsHex } from './dataHashHelpers';
8
+
9
+ /**
10
+ * Returns the hashed payload.
11
+ *
12
+ * @param body `body` (payload) from the request.
13
+ * @returns String created using the payload in the body of the HTTP request as input to a hash function. This string
14
+ * uses lowercase hexadecimal characters. If the payload is empty, return precalculated result of an empty hash.
15
+ *
16
+ * @internal
17
+ */
18
+ export const getHashedPayload = (body: HttpRequest['body']): string => {
19
+ // return precalculated empty hash if body is undefined or null
20
+ if (body == null) {
21
+ return EMPTY_HASH;
22
+ }
23
+
24
+ if (isSourceData(body)) {
25
+ const hashedData = getHashedDataAsHex(null, body);
26
+ return hashedData;
27
+ }
28
+
29
+ // Defined body is not signable. Return unsigned payload which may or may not be accepted by the service.
30
+ return UNSIGNED_PAYLOAD;
31
+ };
32
+
33
+ const isSourceData = (body: HttpRequest['body']): body is SourceData =>
34
+ typeof body === 'string' || ArrayBuffer.isView(body) || isArrayBuffer(body);
35
+
36
+ const isArrayBuffer = (arg: any): arg is ArrayBuffer =>
37
+ (typeof ArrayBuffer === 'function' && arg instanceof ArrayBuffer) ||
38
+ Object.prototype.toString.call(arg) === '[object ArrayBuffer]';
@@ -0,0 +1,52 @@
1
+ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ import { HttpRequest } from '../../../../../types';
5
+ import { SigningValues } from '../types/signer';
6
+ import { getHashedDataAsHex } from './dataHashHelpers';
7
+ import { getCanonicalRequest } from './getCanonicalRequest';
8
+ import { getSigningKey } from './getSigningKey';
9
+ import { getStringToSign } from './getStringToSign';
10
+
11
+ /**
12
+ * Calculates and returns an AWS API Signature.
13
+ * https://docs.aws.amazon.com/IAM/latest/UserGuide/create-signed-request.html
14
+ *
15
+ * @param request `HttpRequest` to be signed.
16
+ * @param signRequestOptions `SignRequestOptions` object containing values used to construct the signature.
17
+ * @returns AWS API Signature to sign a request or url with.
18
+ *
19
+ * @internal
20
+ */
21
+ export const getSignature = (
22
+ request: HttpRequest,
23
+ {
24
+ credentialScope,
25
+ longDate,
26
+ secretAccessKey,
27
+ shortDate,
28
+ signingRegion,
29
+ signingService,
30
+ }: SigningValues
31
+ ): string => {
32
+ // step 1: create a canonical request
33
+ const canonicalRequest = getCanonicalRequest(request);
34
+
35
+ // step 2: create a hash of the canonical request
36
+ const hashedRequest = getHashedDataAsHex(null, canonicalRequest);
37
+
38
+ // step 3: create a string to sign
39
+ const stringToSign = getStringToSign(
40
+ longDate,
41
+ credentialScope,
42
+ hashedRequest
43
+ );
44
+
45
+ // step 4: calculate the signature
46
+ const signature = getHashedDataAsHex(
47
+ getSigningKey(secretAccessKey, shortDate, signingRegion, signingService),
48
+ stringToSign
49
+ );
50
+
51
+ return signature;
52
+ };
@@ -0,0 +1,19 @@
1
+ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ import { HttpRequest } from '../../../../../types';
5
+
6
+ /**
7
+ * Returns signed headers.
8
+ *
9
+ * @param headers `headers` from the request.
10
+ * @returns List of headers included in canonical headers, separated by semicolons (;). This indicates which headers
11
+ * are part of the signing process. Header names must use lowercase characters and must appear in alphabetical order.
12
+ *
13
+ * @internal
14
+ */
15
+ export const getSignedHeaders = (headers: HttpRequest['headers']): string =>
16
+ Object.keys(headers)
17
+ .map(key => key.toLowerCase())
18
+ .sort()
19
+ .join(';');
@@ -0,0 +1,31 @@
1
+ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ import { KEY_TYPE_IDENTIFIER, SIGNATURE_IDENTIFIER } from '../constants';
5
+ import { getHashedData } from './dataHashHelpers';
6
+
7
+ /**
8
+ * Returns a signing key to be used for signing requests.
9
+ *
10
+ * @param secretAccessKey AWS secret access key from credentials.
11
+ * @param date Current date in the format 'YYYYMMDD'.
12
+ * @param region AWS region in which the service resides.
13
+ * @param service Service to which the signed request is being sent.
14
+ *
15
+ * @returns `Uint8Array` calculated from its composite parts.
16
+ *
17
+ * @internal
18
+ */
19
+ export const getSigningKey = (
20
+ secretAccessKey: string,
21
+ date: string,
22
+ region: string,
23
+ service: string
24
+ ): Uint8Array => {
25
+ const key = `${SIGNATURE_IDENTIFIER}${secretAccessKey}`;
26
+ const dateKey = getHashedData(key, date);
27
+ const regionKey = getHashedData(dateKey, region);
28
+ const serviceKey = getHashedData(regionKey, service);
29
+ const signingKey = getHashedData(serviceKey, KEY_TYPE_IDENTIFIER);
30
+ return signingKey;
31
+ };
@@ -0,0 +1,43 @@
1
+ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ import { SignRequestOptions } from '../types';
5
+ import { SigningValues } from '../types/signer';
6
+ import { getCredentialScope } from './getCredentialScope';
7
+ import { getFormattedDates } from './getFormattedDates';
8
+
9
+ /**
10
+ * Extracts common values used for signing both requests and urls.
11
+ *
12
+ * @param options `SignRequestOptions` object containing values used to construct the signature.
13
+ * @returns Common `SigningValues` used for signing.
14
+ *
15
+ * @internal
16
+ */
17
+ export const getSigningValues = ({
18
+ credentials,
19
+ signingDate = new Date(),
20
+ signingRegion,
21
+ signingService,
22
+ }: SignRequestOptions): SigningValues => {
23
+ // get properties from credentials
24
+ const { accessKeyId, secretAccessKey, sessionToken } = credentials;
25
+ // get formatted dates for signing
26
+ const { longDate, shortDate } = getFormattedDates(signingDate);
27
+ // copy header and set signing properties
28
+ const credentialScope = getCredentialScope(
29
+ shortDate,
30
+ signingRegion,
31
+ signingService
32
+ );
33
+ return {
34
+ accessKeyId,
35
+ credentialScope,
36
+ longDate,
37
+ secretAccessKey,
38
+ sessionToken,
39
+ shortDate,
40
+ signingRegion,
41
+ signingService,
42
+ };
43
+ };
@@ -0,0 +1,28 @@
1
+ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ import { SHA256_ALGORITHM_IDENTIFIER } from '../constants';
5
+
6
+ /**
7
+ * Returns a string to be signed.
8
+ *
9
+ * @param date Current date in the format 'YYYYMMDDThhmmssZ'.
10
+ * @param credentialScope String representing the credential scope with format 'YYYYMMDD/region/service/aws4_request'.
11
+ * @param hashedRequest Hashed canonical request.
12
+ *
13
+ * @returns A string created by by concatenating the following strings, separated by newline characters:
14
+ * - Algorithm
15
+ * - RequestDateTime
16
+ * - CredentialScope
17
+ * - HashedCanonicalRequest
18
+ *
19
+ * @internal
20
+ */
21
+ export const getStringToSign = (
22
+ date: string,
23
+ credentialScope: string,
24
+ hashedRequest: string
25
+ ): string =>
26
+ [SHA256_ALGORITHM_IDENTIFIER, date, credentialScope, hashedRequest].join(
27
+ '\n'
28
+ );
@@ -0,0 +1,6 @@
1
+ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ export type { Credentials } from '@aws-sdk/types';
5
+
6
+ export type SourceData = string | ArrayBuffer | ArrayBufferView;
@@ -0,0 +1,17 @@
1
+ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ /**
5
+ * General None HTTP-specific request interface
6
+ */
7
+ export interface Request {
8
+ url: URL;
9
+ body?: unknown;
10
+ }
11
+
12
+ export interface Response {
13
+ body: unknown;
14
+ }
15
+ export interface Endpoint {
16
+ url: URL;
17
+ }
@@ -0,0 +1,33 @@
1
+ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ import { Request, Response } from './core';
5
+
6
+ /**
7
+ * Use basic Record interface to workaround fetch Header class not available in Node.js
8
+ * The header names must be lowercased.
9
+ * TODO: use LowerCase<string> intrinsic when we can support typescript 4.0
10
+ */
11
+ export type Headers = Record<string, string>;
12
+
13
+ export interface HttpRequest extends Request {
14
+ method: string;
15
+ headers: Headers;
16
+ body?: BodyInit;
17
+ }
18
+
19
+ /**
20
+ * Reduce the API surface of Fetch API's Body mixin to only the methods we need.
21
+ * In React Native, body.arrayBuffer() is not supported.
22
+ * body.formData() is not supported for now.
23
+ */
24
+ export type ResponseBodyMixin = Pick<Body, 'blob' | 'json' | 'text'>;
25
+
26
+ export interface HttpResponse extends Response {
27
+ body: (ResponseBodyMixin & ReadableStream) | null;
28
+ statusCode: number;
29
+ /**
30
+ * @see {@link HttpRequest.headers}
31
+ */
32
+ headers: Headers;
33
+ }
@@ -0,0 +1,5 @@
1
+ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ export { Headers, HttpRequest, HttpResponse } from './http';
5
+ export { Credentials } from './aws';