This repository has been archived by the owner on Jun 26, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathindex.js
255 lines (232 loc) · 6.41 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
/* eslint-env node */
/* eslint no-use-before-define: [0, "nofunc"] */
"use strict";
// sources of inspiration:
// https://web-identity-federation-playground.s3.amazonaws.com/js/sigv4.js
// http://docs.aws.amazon.com/AmazonS3/latest/API/sigv4-query-string-auth.html
var crypto = require("crypto");
var querystring = require("querystring");
var path = require("path");
exports.createCanonicalRequest = function(
method,
pathname,
query,
headers,
payload,
doubleEscape
) {
return [
method.toUpperCase(),
createCanonicalURI(
doubleEscape
? pathname
.split(/\//g)
.map(v => encodeURIComponent(v))
.join("/")
: pathname
),
exports.createCanonicalQueryString(query),
exports.createCanonicalHeaders(headers),
exports.createSignedHeaders(headers),
createCanonicalPayload(payload)
].join("\n");
};
function createCanonicalURI(uri) {
var url = path.resolve(uri);
if (uri[uri.length - 1] == "/" && url[url.length - 1] != "/") {
url += "/";
}
return url;
}
function createCanonicalPayload(payload) {
if (payload == "UNSIGNED-PAYLOAD") {
return payload;
}
return hash(payload || "", "hex");
}
exports.createCanonicalQueryString = function(params) {
if (!params) {
return "";
}
if (typeof params == "string") {
params = querystring.parse(params);
}
return Object.keys(params)
.sort()
.map(function(key) {
var values = Array.isArray(params[key]) ? params[key] : [params[key]];
return values
.sort()
.map(function(val) {
return encodeURIComponent(key) + "=" + encodeURIComponent(val);
})
.join("&");
})
.join("&");
};
exports.createCanonicalHeaders = function(headers) {
return Object.keys(headers)
.sort()
.map(function(name) {
var values = Array.isArray(headers[name])
? headers[name]
: [headers[name]];
return (
name.toLowerCase().trim() +
":" +
values
.map(function(v) {
return v.replace(/\s+/g, " ").replace(/^\s+|\s+$/g, "");
})
.join(",") +
"\n"
);
})
.join("");
};
exports.createSignedHeaders = function(headers) {
return Object.keys(headers)
.sort()
.map(function(name) {
return name.toLowerCase().trim();
})
.join(";");
};
exports.createCredentialScope = function(time, region, service) {
return [toDate(time), region, service, "aws4_request"].join("/");
};
exports.createStringToSign = function(time, region, service, request) {
return [
"AWS4-HMAC-SHA256",
toTime(time),
exports.createCredentialScope(time, region, service),
hash(request, "hex")
].join("\n");
};
exports.createAuthorizationHeader = function(
key,
scope,
signedHeaders,
signature
) {
return [
"AWS4-HMAC-SHA256 Credential=" + key + "/" + scope,
"SignedHeaders=" + signedHeaders,
"Signature=" + signature
].join(", ");
};
exports.createSignature = function(
secret,
time,
region,
service,
stringToSign
) {
var h1 = hmac("AWS4" + secret, toDate(time)); // date-key
var h2 = hmac(h1, region); // region-key
var h3 = hmac(h2, service); // service-key
var h4 = hmac(h3, "aws4_request"); // signing-key
return hmac(h4, stringToSign, "hex");
};
exports.createPresignedS3URL = function(name, options) {
options = options || {};
options.method = options.method || "GET";
options.bucket = options.bucket || process.env.AWS_S3_BUCKET;
options.signSessionToken = true;
options.doubleEscape = false;
return exports.createPresignedURL(
options.method,
options.bucket + ".s3.amazonaws.com",
"/" + name,
"s3",
"UNSIGNED-PAYLOAD",
options
);
};
exports.createPresignedURL = function(
method,
host,
path,
service,
payload,
options
) {
options = options || {};
options.key = options.key || process.env.AWS_ACCESS_KEY_ID;
options.secret = options.secret || process.env.AWS_SECRET_ACCESS_KEY;
options.sessionToken = options.sessionToken || process.env.AWS_SESSION_TOKEN;
options.protocol = options.protocol || "https";
options.timestamp = options.timestamp || Date.now();
options.region = options.region || process.env.AWS_REGION || "us-east-1";
options.expires = options.expires || 86400; // 24 hours
options.headers = options.headers || {};
options.signSessionToken = options.signSessionToken || false;
options.doubleEscape =
options.doubleEscape !== undefined ? options.doubleEscape : true;
// host is required
options.headers.Host = host;
var query = options.query ? querystring.parse(options.query) : {};
query["X-Amz-Algorithm"] = "AWS4-HMAC-SHA256";
query["X-Amz-Credential"] =
options.key +
"/" +
exports.createCredentialScope(options.timestamp, options.region, service);
query["X-Amz-Date"] = toTime(options.timestamp);
query["X-Amz-Expires"] = options.expires;
query["X-Amz-SignedHeaders"] = exports.createSignedHeaders(options.headers);
// when a session token must be "signed" into the canonical request
// (needed for some services, such as s3)
if (options.sessionToken && options.signSessionToken) {
query["X-Amz-Security-Token"] = options.sessionToken;
}
var canonicalRequest = exports.createCanonicalRequest(
method,
path,
query,
options.headers,
payload,
options.doubleEscape
);
var stringToSign = exports.createStringToSign(
options.timestamp,
options.region,
service,
canonicalRequest
);
var signature = exports.createSignature(
options.secret,
options.timestamp,
options.region,
service,
stringToSign
);
query["X-Amz-Signature"] = signature;
// when a session token must NOT be "signed" into the canonical request
// (needed for some services, such as IoT)
if (options.sessionToken && !options.signSessionToken) {
query["X-Amz-Security-Token"] = options.sessionToken;
} else {
delete query["X-Amz-Security-Token"];
}
return (
options.protocol + "://" + host + path + "?" + querystring.stringify(query)
);
};
function toTime(time) {
return new Date(time).toISOString().replace(/[:\-]|\.\d{3}/g, "");
}
function toDate(time) {
return toTime(time).substring(0, 8);
}
function hmac(key, string, encoding) {
return crypto
.createHmac("sha256", key)
.update(string, "utf8")
.digest(encoding);
}
function hash(string, encoding) {
return crypto
.createHash("sha256")
.update(string, "utf8")
.digest(encoding);
}