v3.js
2.05 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
var AWS = require('../core');
var inherit = AWS.util.inherit;
/**
* @api private
*/
AWS.Signers.V3 = inherit(AWS.Signers.RequestSigner, {
addAuthorization: function addAuthorization(credentials, date) {
var datetime = AWS.util.date.rfc822(date);
this.request.headers['X-Amz-Date'] = datetime;
if (credentials.sessionToken) {
this.request.headers['x-amz-security-token'] = credentials.sessionToken;
}
this.request.headers['X-Amzn-Authorization'] =
this.authorization(credentials, datetime);
},
authorization: function authorization(credentials) {
return 'AWS3 ' +
'AWSAccessKeyId=' + credentials.accessKeyId + ',' +
'Algorithm=HmacSHA256,' +
'SignedHeaders=' + this.signedHeaders() + ',' +
'Signature=' + this.signature(credentials);
},
signedHeaders: function signedHeaders() {
var headers = [];
AWS.util.arrayEach(this.headersToSign(), function iterator(h) {
headers.push(h.toLowerCase());
});
return headers.sort().join(';');
},
canonicalHeaders: function canonicalHeaders() {
var headers = this.request.headers;
var parts = [];
AWS.util.arrayEach(this.headersToSign(), function iterator(h) {
parts.push(h.toLowerCase().trim() + ':' + String(headers[h]).trim());
});
return parts.sort().join('\n') + '\n';
},
headersToSign: function headersToSign() {
var headers = [];
AWS.util.each(this.request.headers, function iterator(k) {
if (k === 'Host' || k === 'Content-Encoding' || k.match(/^X-Amz/i)) {
headers.push(k);
}
});
return headers;
},
signature: function signature(credentials) {
return AWS.util.crypto.hmac(credentials.secretAccessKey, this.stringToSign(), 'base64');
},
stringToSign: function stringToSign() {
var parts = [];
parts.push(this.request.method);
parts.push('/');
parts.push('');
parts.push(this.canonicalHeaders());
parts.push(this.request.body);
return AWS.util.crypto.sha256(parts.join('\n'));
}
});
/**
* @api private
*/
module.exports = AWS.Signers.V3;