v4.js
6.57 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
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
var AWS = require('../core');
var v4Credentials = require('./v4_credentials');
var inherit = AWS.util.inherit;
/**
* @api private
*/
var expiresHeader = 'presigned-expires';
/**
* @api private
*/
AWS.Signers.V4 = inherit(AWS.Signers.RequestSigner, {
constructor: function V4(request, serviceName, options) {
AWS.Signers.RequestSigner.call(this, request);
this.serviceName = serviceName;
options = options || {};
this.signatureCache = typeof options.signatureCache === 'boolean' ? options.signatureCache : true;
this.operation = options.operation;
this.signatureVersion = options.signatureVersion;
},
algorithm: 'AWS4-HMAC-SHA256',
addAuthorization: function addAuthorization(credentials, date) {
var datetime = AWS.util.date.iso8601(date).replace(/[:\-]|\.\d{3}/g, '');
if (this.isPresigned()) {
this.updateForPresigned(credentials, datetime);
} else {
this.addHeaders(credentials, datetime);
}
this.request.headers['Authorization'] =
this.authorization(credentials, datetime);
},
addHeaders: function addHeaders(credentials, datetime) {
this.request.headers['X-Amz-Date'] = datetime;
if (credentials.sessionToken) {
this.request.headers['x-amz-security-token'] = credentials.sessionToken;
}
},
updateForPresigned: function updateForPresigned(credentials, datetime) {
var credString = this.credentialString(datetime);
var qs = {
'X-Amz-Date': datetime,
'X-Amz-Algorithm': this.algorithm,
'X-Amz-Credential': credentials.accessKeyId + '/' + credString,
'X-Amz-Expires': this.request.headers[expiresHeader],
'X-Amz-SignedHeaders': this.signedHeaders()
};
if (credentials.sessionToken) {
qs['X-Amz-Security-Token'] = credentials.sessionToken;
}
if (this.request.headers['Content-Type']) {
qs['Content-Type'] = this.request.headers['Content-Type'];
}
if (this.request.headers['Content-MD5']) {
qs['Content-MD5'] = this.request.headers['Content-MD5'];
}
if (this.request.headers['Cache-Control']) {
qs['Cache-Control'] = this.request.headers['Cache-Control'];
}
// need to pull in any other X-Amz-* headers
AWS.util.each.call(this, this.request.headers, function(key, value) {
if (key === expiresHeader) return;
if (this.isSignableHeader(key)) {
var lowerKey = key.toLowerCase();
// Metadata should be normalized
if (lowerKey.indexOf('x-amz-meta-') === 0) {
qs[lowerKey] = value;
} else if (lowerKey.indexOf('x-amz-') === 0) {
qs[key] = value;
}
}
});
var sep = this.request.path.indexOf('?') >= 0 ? '&' : '?';
this.request.path += sep + AWS.util.queryParamsToString(qs);
},
authorization: function authorization(credentials, datetime) {
var parts = [];
var credString = this.credentialString(datetime);
parts.push(this.algorithm + ' Credential=' +
credentials.accessKeyId + '/' + credString);
parts.push('SignedHeaders=' + this.signedHeaders());
parts.push('Signature=' + this.signature(credentials, datetime));
return parts.join(', ');
},
signature: function signature(credentials, datetime) {
var signingKey = v4Credentials.getSigningKey(
credentials,
datetime.substr(0, 8),
this.request.region,
this.serviceName,
this.signatureCache
);
return AWS.util.crypto.hmac(signingKey, this.stringToSign(datetime), 'hex');
},
stringToSign: function stringToSign(datetime) {
var parts = [];
parts.push('AWS4-HMAC-SHA256');
parts.push(datetime);
parts.push(this.credentialString(datetime));
parts.push(this.hexEncodedHash(this.canonicalString()));
return parts.join('\n');
},
canonicalString: function canonicalString() {
var parts = [], pathname = this.request.pathname();
if (this.serviceName !== 's3' && this.signatureVersion !== 's3v4') pathname = AWS.util.uriEscapePath(pathname);
parts.push(this.request.method);
parts.push(pathname);
parts.push(this.request.search());
parts.push(this.canonicalHeaders() + '\n');
parts.push(this.signedHeaders());
parts.push(this.hexEncodedBodyHash());
return parts.join('\n');
},
canonicalHeaders: function canonicalHeaders() {
var headers = [];
AWS.util.each.call(this, this.request.headers, function (key, item) {
headers.push([key, item]);
});
headers.sort(function (a, b) {
return a[0].toLowerCase() < b[0].toLowerCase() ? -1 : 1;
});
var parts = [];
AWS.util.arrayEach.call(this, headers, function (item) {
var key = item[0].toLowerCase();
if (this.isSignableHeader(key)) {
var value = item[1];
if (typeof value === 'undefined' || value === null || typeof value.toString !== 'function') {
throw AWS.util.error(new Error('Header ' + key + ' contains invalid value'), {
code: 'InvalidHeader'
});
}
parts.push(key + ':' +
this.canonicalHeaderValues(value.toString()));
}
});
return parts.join('\n');
},
canonicalHeaderValues: function canonicalHeaderValues(values) {
return values.replace(/\s+/g, ' ').replace(/^\s+|\s+$/g, '');
},
signedHeaders: function signedHeaders() {
var keys = [];
AWS.util.each.call(this, this.request.headers, function (key) {
key = key.toLowerCase();
if (this.isSignableHeader(key)) keys.push(key);
});
return keys.sort().join(';');
},
credentialString: function credentialString(datetime) {
return v4Credentials.createScope(
datetime.substr(0, 8),
this.request.region,
this.serviceName
);
},
hexEncodedHash: function hash(string) {
return AWS.util.crypto.sha256(string, 'hex');
},
hexEncodedBodyHash: function hexEncodedBodyHash() {
var request = this.request;
if (this.isPresigned() && this.serviceName === 's3' && !request.body) {
return 'UNSIGNED-PAYLOAD';
} else if (request.headers['X-Amz-Content-Sha256']) {
return request.headers['X-Amz-Content-Sha256'];
} else {
return this.hexEncodedHash(this.request.body || '');
}
},
unsignableHeaders: [
'authorization',
'content-type',
'content-length',
'user-agent',
expiresHeader,
'expect',
'x-amzn-trace-id'
],
isSignableHeader: function isSignableHeader(key) {
if (key.toLowerCase().indexOf('x-amz-') === 0) return true;
return this.unsignableHeaders.indexOf(key) < 0;
},
isPresigned: function isPresigned() {
return this.request.headers[expiresHeader] ? true : false;
}
});
/**
* @api private
*/
module.exports = AWS.Signers.V4;