sigv4-lite 1.0.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.
- package/LICENSE +21 -0
- package/README.md +88 -0
- package/package.json +25 -0
- package/src/index.js +147 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 ProfessionalFlare
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
# sigv4-lite
|
|
2
|
+
|
|
3
|
+
AWS SigV4 signing for runtimes without Node's `crypto` module — Cloudflare Workers, Deno, Bun, browsers. Works with S3, R2, B2, Wasabi, and any other SigV4-compatible endpoint.
|
|
4
|
+
|
|
5
|
+
Zero dependencies. Uses Web Crypto (`crypto.subtle`) only.
|
|
6
|
+
|
|
7
|
+
By ProfessionalFlare.
|
|
8
|
+
|
|
9
|
+
## Install
|
|
10
|
+
|
|
11
|
+
```
|
|
12
|
+
npm install sigv4-lite
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
## Usage
|
|
16
|
+
|
|
17
|
+
### Sign a request
|
|
18
|
+
|
|
19
|
+
```js
|
|
20
|
+
import { signRequest } from "sigv4-lite";
|
|
21
|
+
|
|
22
|
+
const headers = await signRequest({
|
|
23
|
+
method: "PUT",
|
|
24
|
+
url: "https://s3.us-west-002.backblazeb2.com/my-bucket/my-file.txt",
|
|
25
|
+
body: "hello world",
|
|
26
|
+
accessKeyId: env.B2_KEY_ID,
|
|
27
|
+
secretAccessKey: env.B2_APP_KEY,
|
|
28
|
+
region: "us-west-002",
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
const response = await fetch("https://s3.us-west-002.backblazeb2.com/my-bucket/my-file.txt", {
|
|
32
|
+
method: "PUT",
|
|
33
|
+
headers,
|
|
34
|
+
body: "hello world",
|
|
35
|
+
});
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
### Presign a URL
|
|
39
|
+
|
|
40
|
+
```js
|
|
41
|
+
import { presignUrl } from "sigv4-lite";
|
|
42
|
+
|
|
43
|
+
const url = await presignUrl({
|
|
44
|
+
method: "GET",
|
|
45
|
+
url: "https://s3.us-west-002.backblazeb2.com/my-bucket/my-file.txt",
|
|
46
|
+
accessKeyId: env.B2_KEY_ID,
|
|
47
|
+
secretAccessKey: env.B2_APP_KEY,
|
|
48
|
+
region: "us-west-002",
|
|
49
|
+
expiresIn: 3600,
|
|
50
|
+
});
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
## API
|
|
54
|
+
|
|
55
|
+
### `signRequest(options)`
|
|
56
|
+
|
|
57
|
+
Returns a headers object with `authorization`, `x-amz-date`, and `x-amz-content-sha256` set.
|
|
58
|
+
|
|
59
|
+
| option | type | default | required |
|
|
60
|
+
|---|---|---|---|
|
|
61
|
+
| `method` | string | `"GET"` | no |
|
|
62
|
+
| `url` | string | — | yes |
|
|
63
|
+
| `headers` | object | `{}` | no |
|
|
64
|
+
| `body` | string | `""` | no |
|
|
65
|
+
| `accessKeyId` | string | — | yes |
|
|
66
|
+
| `secretAccessKey` | string | — | yes |
|
|
67
|
+
| `region` | string | — | yes |
|
|
68
|
+
| `service` | string | `"s3"` | no |
|
|
69
|
+
| `date` | Date | `new Date()` | no |
|
|
70
|
+
|
|
71
|
+
### `presignUrl(options)`
|
|
72
|
+
|
|
73
|
+
Returns a signed URL string valid for `expiresIn` seconds.
|
|
74
|
+
|
|
75
|
+
| option | type | default | required |
|
|
76
|
+
|---|---|---|---|
|
|
77
|
+
| `method` | string | `"GET"` | no |
|
|
78
|
+
| `url` | string | — | yes |
|
|
79
|
+
| `accessKeyId` | string | — | yes |
|
|
80
|
+
| `secretAccessKey` | string | — | yes |
|
|
81
|
+
| `region` | string | — | yes |
|
|
82
|
+
| `service` | string | `"s3"` | no |
|
|
83
|
+
| `expiresIn` | number | `3600` | no |
|
|
84
|
+
| `date` | Date | `new Date()` | no |
|
|
85
|
+
|
|
86
|
+
## License
|
|
87
|
+
|
|
88
|
+
MIT
|
package/package.json
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "sigv4-lite",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Minimal AWS SigV4 signer for runtimes without Node's crypto module",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "src/index.js",
|
|
7
|
+
"exports": {
|
|
8
|
+
".": "./src/index.js"
|
|
9
|
+
},
|
|
10
|
+
"files": [
|
|
11
|
+
"src"
|
|
12
|
+
],
|
|
13
|
+
"keywords": [
|
|
14
|
+
"sigv4",
|
|
15
|
+
"aws4",
|
|
16
|
+
"s3",
|
|
17
|
+
"cloudflare-workers",
|
|
18
|
+
"deno",
|
|
19
|
+
"bun",
|
|
20
|
+
"b2",
|
|
21
|
+
"r2"
|
|
22
|
+
],
|
|
23
|
+
"author": "ProfessionalFlare",
|
|
24
|
+
"license": "MIT"
|
|
25
|
+
}
|
package/src/index.js
ADDED
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
const encoder = new TextEncoder();
|
|
2
|
+
|
|
3
|
+
function toHex(buffer) {
|
|
4
|
+
return [...new Uint8Array(buffer)].map((b) => b.toString(16).padStart(2, "0")).join("");
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
async function sha256(data) {
|
|
8
|
+
const buf = typeof data === "string" ? encoder.encode(data) : data;
|
|
9
|
+
const hash = await crypto.subtle.digest("SHA-256", buf);
|
|
10
|
+
return toHex(hash);
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
async function hmac(key, data) {
|
|
14
|
+
const cryptoKey = await crypto.subtle.importKey(
|
|
15
|
+
"raw",
|
|
16
|
+
typeof key === "string" ? encoder.encode(key) : key,
|
|
17
|
+
{ name: "HMAC", hash: "SHA-256" },
|
|
18
|
+
false,
|
|
19
|
+
["sign"]
|
|
20
|
+
);
|
|
21
|
+
return crypto.subtle.sign("HMAC", cryptoKey, encoder.encode(data));
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
function toAmzDate(date) {
|
|
25
|
+
return date.toISOString().replace(/[:-]|\.\d{3}/g, "");
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
function encodeRfc3986(str) {
|
|
29
|
+
return encodeURIComponent(str).replace(/[!'()*]/g, (c) => "%" + c.charCodeAt(0).toString(16).toUpperCase());
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
function canonicalQueryString(url) {
|
|
33
|
+
const params = [...url.searchParams.entries()].sort(([a], [b]) => (a < b ? -1 : a > b ? 1 : 0));
|
|
34
|
+
return params.map(([k, v]) => `${encodeRfc3986(k)}=${encodeRfc3986(v)}`).join("&");
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export async function signRequest({
|
|
38
|
+
method = "GET",
|
|
39
|
+
url,
|
|
40
|
+
headers = {},
|
|
41
|
+
body = "",
|
|
42
|
+
accessKeyId,
|
|
43
|
+
secretAccessKey,
|
|
44
|
+
region,
|
|
45
|
+
service = "s3",
|
|
46
|
+
date = new Date(),
|
|
47
|
+
}) {
|
|
48
|
+
const parsedUrl = new URL(url);
|
|
49
|
+
const amzDate = toAmzDate(date);
|
|
50
|
+
const dateStamp = amzDate.slice(0, 8);
|
|
51
|
+
const payloadHash = await sha256(body);
|
|
52
|
+
|
|
53
|
+
const canonicalHeaders = { ...headers };
|
|
54
|
+
canonicalHeaders["host"] = parsedUrl.host;
|
|
55
|
+
canonicalHeaders["x-amz-date"] = amzDate;
|
|
56
|
+
canonicalHeaders["x-amz-content-sha256"] = payloadHash;
|
|
57
|
+
|
|
58
|
+
const sortedHeaderKeys = Object.keys(canonicalHeaders)
|
|
59
|
+
.map((k) => k.toLowerCase())
|
|
60
|
+
.sort();
|
|
61
|
+
|
|
62
|
+
const canonicalHeadersStr =
|
|
63
|
+
sortedHeaderKeys.map((k) => `${k}:${String(canonicalHeaders[k]).trim()}\n`).join("");
|
|
64
|
+
|
|
65
|
+
const signedHeaders = sortedHeaderKeys.join(";");
|
|
66
|
+
|
|
67
|
+
const canonicalRequest = [
|
|
68
|
+
method.toUpperCase(),
|
|
69
|
+
parsedUrl.pathname || "/",
|
|
70
|
+
canonicalQueryString(parsedUrl),
|
|
71
|
+
canonicalHeadersStr,
|
|
72
|
+
signedHeaders,
|
|
73
|
+
payloadHash,
|
|
74
|
+
].join("\n");
|
|
75
|
+
|
|
76
|
+
const credentialScope = `${dateStamp}/${region}/${service}/aws4_request`;
|
|
77
|
+
const stringToSign = [
|
|
78
|
+
"AWS4-HMAC-SHA256",
|
|
79
|
+
amzDate,
|
|
80
|
+
credentialScope,
|
|
81
|
+
await sha256(canonicalRequest),
|
|
82
|
+
].join("\n");
|
|
83
|
+
|
|
84
|
+
const kDate = await hmac(`AWS4${secretAccessKey}`, dateStamp);
|
|
85
|
+
const kRegion = await hmac(kDate, region);
|
|
86
|
+
const kService = await hmac(kRegion, service);
|
|
87
|
+
const kSigning = await hmac(kService, "aws4_request");
|
|
88
|
+
const signature = toHex(await hmac(kSigning, stringToSign));
|
|
89
|
+
|
|
90
|
+
const authorizationHeader = `AWS4-HMAC-SHA256 Credential=${accessKeyId}/${credentialScope}, SignedHeaders=${signedHeaders}, Signature=${signature}`;
|
|
91
|
+
|
|
92
|
+
return {
|
|
93
|
+
...headers,
|
|
94
|
+
host: parsedUrl.host,
|
|
95
|
+
"x-amz-date": amzDate,
|
|
96
|
+
"x-amz-content-sha256": payloadHash,
|
|
97
|
+
authorization: authorizationHeader,
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
export async function presignUrl({
|
|
102
|
+
url,
|
|
103
|
+
method = "GET",
|
|
104
|
+
accessKeyId,
|
|
105
|
+
secretAccessKey,
|
|
106
|
+
region,
|
|
107
|
+
service = "s3",
|
|
108
|
+
expiresIn = 3600,
|
|
109
|
+
date = new Date(),
|
|
110
|
+
}) {
|
|
111
|
+
const parsedUrl = new URL(url);
|
|
112
|
+
const amzDate = toAmzDate(date);
|
|
113
|
+
const dateStamp = amzDate.slice(0, 8);
|
|
114
|
+
const credentialScope = `${dateStamp}/${region}/${service}/aws4_request`;
|
|
115
|
+
|
|
116
|
+
parsedUrl.searchParams.set("X-Amz-Algorithm", "AWS4-HMAC-SHA256");
|
|
117
|
+
parsedUrl.searchParams.set("X-Amz-Credential", `${accessKeyId}/${credentialScope}`);
|
|
118
|
+
parsedUrl.searchParams.set("X-Amz-Date", amzDate);
|
|
119
|
+
parsedUrl.searchParams.set("X-Amz-Expires", String(expiresIn));
|
|
120
|
+
parsedUrl.searchParams.set("X-Amz-SignedHeaders", "host");
|
|
121
|
+
|
|
122
|
+
const canonicalRequest = [
|
|
123
|
+
method.toUpperCase(),
|
|
124
|
+
parsedUrl.pathname || "/",
|
|
125
|
+
canonicalQueryString(parsedUrl),
|
|
126
|
+
`host:${parsedUrl.host}\n`,
|
|
127
|
+
"host",
|
|
128
|
+
"UNSIGNED-PAYLOAD",
|
|
129
|
+
].join("\n");
|
|
130
|
+
|
|
131
|
+
const stringToSign = [
|
|
132
|
+
"AWS4-HMAC-SHA256",
|
|
133
|
+
amzDate,
|
|
134
|
+
credentialScope,
|
|
135
|
+
await sha256(canonicalRequest),
|
|
136
|
+
].join("\n");
|
|
137
|
+
|
|
138
|
+
const kDate = await hmac(`AWS4${secretAccessKey}`, dateStamp);
|
|
139
|
+
const kRegion = await hmac(kDate, region);
|
|
140
|
+
const kService = await hmac(kRegion, service);
|
|
141
|
+
const kSigning = await hmac(kService, "aws4_request");
|
|
142
|
+
const signature = toHex(await hmac(kSigning, stringToSign));
|
|
143
|
+
|
|
144
|
+
parsedUrl.searchParams.set("X-Amz-Signature", signature);
|
|
145
|
+
|
|
146
|
+
return parsedUrl.toString();
|
|
147
|
+
}
|