-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
105 lines (91 loc) · 2.7 KB
/
index.js
File metadata and controls
105 lines (91 loc) · 2.7 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
/**!
* ali-baseclient - index.js
*
* Copyright(c) ali-sdk and other contributors.
* MIT Licensed
*
* Authors:
* fengmk2 <m@fengmk2.com> (http://fengmk2.com)
*/
"use strict";
/**
* Module dependencies.
*/
var debug = require('debug')('ali-sdk:baseclient');
var urllib = require('urllib');
var crypto = require('crypto');
function BaseClient(type, options) {
this._type = type.toLowerCase();
this._TYPE = type.toUpperCase();
if (!options
|| !options.accessKeyId
|| !options.accessKeySecret) {
throw new TypeError('require accessKeyId, accessKeySecret');
}
this.options = options;
}
var proto = BaseClient.prototype;
/**
* set Authorization header
*
* @param {String} method
* @param {String} resource
* @param {Object} header
*/
proto.setAuthorization = function (method, canonicalizedResource, headers) {
// Authorization: $TYPE + ' ' + $AccessKeyId + ':' + $Signature
var auth = this._TYPE + ' ' + this.options.accessKeyId + ':' +
this.signature(method, canonicalizedResource, headers);
headers.Authorization = auth;
};
/**
* get Signature
*
* Signature = base64(hmac-sha1(Access Key Secret + "\n"
* + VERB + "\n"
* + CONTENT-MD5 + "\n"
* + CONTENT-TYPE + "\n"
* + DATE + "\n"
* + CanonicalizedOSSHeaders
* + CanonicalizedResource))
*
* @param {String} method
* @param {String} resource
* @param {Object} header
*/
proto.signature = function (method, canonicalizedResource, headers) {
// VERB + "\n"
// + CONTENT-MD5 + "\n"
// + CONTENT-TYPE + "\n"
// + DATE + "\n"
// + CanonicalizedHeaders + "\n"
// + CanonicalizedResource
var params = [
method.toUpperCase(),
headers['Content-Md5'] || headers['Content-MD5'] || '',
headers['Content-Type'] || '',
headers.Date || new Date().toGMTString()
];
var canonicalizedHeaders = {};
var canonicalizedPrefix = 'x-' + this._type + '-';
for (var key in headers) {
var lkey = key.toLowerCase().trim();
if (lkey.indexOf(canonicalizedPrefix) === 0) {
canonicalizedHeaders[lkey] = canonicalizedHeaders[lkey] || [];
canonicalizedHeaders[lkey].push(String(headers[key]).trim());
}
}
var canonicalizedHeadersList = [];
Object.keys(canonicalizedHeaders).sort().forEach(function (key) {
canonicalizedHeadersList.push(key + ':' + canonicalizedHeaders[key].join(','));
});
params = params.concat(canonicalizedHeadersList);
// TODO: support sub resource
params.push(canonicalizedResource);
var stringToSign = params.join('\n');
var signature = crypto.createHmac('sha1', this.options.accessKeySecret);
signature = signature.update(stringToSign).digest('base64');
debug('authorization stringToSign: %j, signature: %j',
stringToSign, signature);
return signature;
};