s3.js
4.42 KB
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
var AWS = require('../core');
var inherit = AWS.util.inherit;
/**
* @api private
*/
AWS.Signers.S3 = inherit(AWS.Signers.RequestSigner, {
/**
* When building the stringToSign, these sub resource params should be
* part of the canonical resource string with their NON-decoded values
*/
subResources: {
'acl': 1,
'accelerate': 1,
'analytics': 1,
'cors': 1,
'lifecycle': 1,
'delete': 1,
'inventory': 1,
'location': 1,
'logging': 1,
'metrics': 1,
'notification': 1,
'partNumber': 1,
'policy': 1,
'requestPayment': 1,
'replication': 1,
'restore': 1,
'tagging': 1,
'torrent': 1,
'uploadId': 1,
'uploads': 1,
'versionId': 1,
'versioning': 1,
'versions': 1,
'website': 1
},
// when building the stringToSign, these querystring params should be
// part of the canonical resource string with their NON-encoded values
responseHeaders: {
'response-content-type': 1,
'response-content-language': 1,
'response-expires': 1,
'response-cache-control': 1,
'response-content-disposition': 1,
'response-content-encoding': 1
},
addAuthorization: function addAuthorization(credentials, date) {
if (!this.request.headers['presigned-expires']) {
this.request.headers['X-Amz-Date'] = AWS.util.date.rfc822(date);
}
if (credentials.sessionToken) {
// presigned URLs require this header to be lowercased
this.request.headers['x-amz-security-token'] = credentials.sessionToken;
}
var signature = this.sign(credentials.secretAccessKey, this.stringToSign());
var auth = 'AWS ' + credentials.accessKeyId + ':' + signature;
this.request.headers['Authorization'] = auth;
},
stringToSign: function stringToSign() {
var r = this.request;
var parts = [];
parts.push(r.method);
parts.push(r.headers['Content-MD5'] || '');
parts.push(r.headers['Content-Type'] || '');
// This is the "Date" header, but we use X-Amz-Date.
// The S3 signing mechanism requires us to pass an empty
// string for this Date header regardless.
parts.push(r.headers['presigned-expires'] || '');
var headers = this.canonicalizedAmzHeaders();
if (headers) parts.push(headers);
parts.push(this.canonicalizedResource());
return parts.join('\n');
},
canonicalizedAmzHeaders: function canonicalizedAmzHeaders() {
var amzHeaders = [];
AWS.util.each(this.request.headers, function (name) {
if (name.match(/^x-amz-/i))
amzHeaders.push(name);
});
amzHeaders.sort(function (a, b) {
return a.toLowerCase() < b.toLowerCase() ? -1 : 1;
});
var parts = [];
AWS.util.arrayEach.call(this, amzHeaders, function (name) {
parts.push(name.toLowerCase() + ':' + String(this.request.headers[name]));
});
return parts.join('\n');
},
canonicalizedResource: function canonicalizedResource() {
var r = this.request;
var parts = r.path.split('?');
var path = parts[0];
var querystring = parts[1];
var resource = '';
if (r.virtualHostedBucket)
resource += '/' + r.virtualHostedBucket;
resource += path;
if (querystring) {
// collect a list of sub resources and query params that need to be signed
var resources = [];
AWS.util.arrayEach.call(this, querystring.split('&'), function (param) {
var name = param.split('=')[0];
var value = param.split('=')[1];
if (this.subResources[name] || this.responseHeaders[name]) {
var subresource = { name: name };
if (value !== undefined) {
if (this.subResources[name]) {
subresource.value = value;
} else {
subresource.value = decodeURIComponent(value);
}
}
resources.push(subresource);
}
});
resources.sort(function (a, b) { return a.name < b.name ? -1 : 1; });
if (resources.length) {
querystring = [];
AWS.util.arrayEach(resources, function (res) {
if (res.value === undefined) {
querystring.push(res.name);
} else {
querystring.push(res.name + '=' + res.value);
}
});
resource += '?' + querystring.join('&');
}
}
return resource;
},
sign: function sign(secret, string) {
return AWS.util.crypto.hmac(secret, string, 'base64', 'sha1');
}
});
/**
* @api private
*/
module.exports = AWS.Signers.S3;