forked from maxbeatty/aws-s3-browser-upload-example
-
Notifications
You must be signed in to change notification settings - Fork 0
/
aws.js
76 lines (69 loc) · 2.58 KB
/
aws.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
var crypto = require('crypto');
/*
* converts a basic ISO8601 string (YYYYMMDDTHHMMSSZ) to a dateString
* @param basicISOString - e.g. 19750101T123456Z
* @returns String - e.g. 1975-01-01T12:34:56.000Z
*/
function convertBasicISOStringToDateString(basicISOString) {
var matches = basicISOString.match(/^(\d{4})(\d{2})(\d{2})T(\d{2})(\d{2})(\d{2})Z$/);
return matches[1] + '-' + matches[2] + '-' + matches[3] + 'T' + matches[4] + ':' + matches[5] + ':' + matches[6] + '.000Z';
}
var utils = {
/*
* get hmac digest
* @param key
* @param data - what to encode
* @param algorithm [Optional] - dependent on OpenSSL. defaults to 'sha256'
* @param encoding [Optional] - encoding for digest. defaults to 'base64'
*/
hmac: function(key, data, algorithm, encoding) {
if (algorithm == null) {
algorithm = 'sha256';
}
if (encoding == null) {
encoding = 'base64';
}
return crypto.createHmac(algorithm, key).update(data).digest(encoding);
},
/*
* creates Base64 encoded signature to use in REST API requests
* @param secret - most likely process.env.AWS_SECRET_ACCESS_KEY
* @param date - YYYYMMDD
* @param region [Optional] - valid AWS Region. defaults to 'us-east-1'
* @param service [Optional] - valid AWS Service abbr. defaults to 's3'
*/
signature: function(secret, date, region, service) {
var dataToSign, dateKey, dateRegionKey, dateRegionServiceKey;
if (region == null) {
region = 'us-east-1';
}
if (service == null) {
service = 's3';
}
dateKey = this.hmac('AWS4' + secret, date);
dateRegionKey = this.hmac(dateKey, region);
dateRegionServiceKey = this.hmac(dateRegionKey, service);
return dataToSign = this.hmac(dateRegionServiceKey, 'aws4_request');
}
};
module.exports = {
signRequest: function(manifest) {
var d = new Date(convertBasicISOStringToDateString(manifest['x-amz-date']));
d.setHours(d.getHours() + 1); // allow upload for next hour
var YYYYMMDD = manifest['x-amz-date'].split('T')[0],
signingKey = utils.signature(process.env.AWS_SECRET_ACCESS_KEY, YYYYMMDD),
strToSign = new Buffer(JSON.stringify({
expiration: d.toISOString(),
conditions: [
{ acl: manifest.acl},
{ bucket: manifest.bucket},
['content-length-range', 1, 1024 * 1024 * 100], // 100 MB limit
{ key: manifest.key},
{'x-amz-algorithm': manifest['x-amz-algorithm']},
{'x-amz-credential': manifest['x-amz-credential']},
{'x-amz-date': manifest['x-amz-date']}
]
})).toString('base64');
return utils.hmac(signingKey, strToSign, null, 'hex');
}
}