-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
142 lines (126 loc) · 3.76 KB
/
index.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
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
var crypto = require('crypto');
var _ = require('lodash');
var request = require('request');
var required = [
'Action',
'SignName',
'TemplateCode',
'RecNum',
'ParamString',
];
var SMS = function (config) {
config = config ? config : {};
this.AccessKeyId = config.AccessKeyId;
this.AccessKeySecret = config.AccessKeySecret;
if (!this.AccessKeyId) {
throw new Error('missing AccessKeyId');
}
if (!this.AccessKeySecret) {
throw new Error('missing AccessKeySecret');
}
this.Format = config.Format || 'JSON';
this.Version = config.Version || '2016-09-27';
this.SignatureMethod = config.SignatureMethod || 'HMAC-SHA1';
this.SignatureVersion = config.SignatureVersion || '1.0';
};
SMS.prototype.send = function(params, options, callback) {
if (typeof options === 'function') {
callback = options;
options = {};
}
if (!options) {
options = options || {};
}
var method = options.method || 'POST';
params = _.assign(this._getDefaults(), params);
for (var key in params) {
if(params[key] !== undefined && params[key] !== null){
params[key] = params[key].toString();
}
}
var missing = [];
required.forEach(function(key) {
var alters = key.split('|');
for (var i = alters.length - 1; i >= 0; i--) {
if (params[alters[i]]) {
return;
}
}
missing.push(key);
});
if (missing.length) {
return callback === 'function' && callback('missing params ' + missing.join(','));
}
params.Signature = this._getSignature(params, method);
var requestParams = {
method: method,
url: 'http://sms.aliyuncs.com/'
};
if (method === 'GET') {
requestParams.headers = {
'cache-control': 'no-cache'
};
requestParams.qs = params;
} else if (method === 'POST') {
requestParams.headers = {
'cache-control': 'no-cache',
'content-type': 'application/x-www-form-urlencoded'
};
requestParams.form = params;
} else {
return callback === 'function' && callback('request params error');
}
request(requestParams, function (error, response, body) {
if (params.Format === 'JSON') {
body = JSON.parse(body);
}
typeof callback === 'function' && callback(error, body);
});
};
SMS.prototype._getDefaults = function () {
var defaults = {
Format: this.Format,
Version: this.Version,
AccessKeyId: this.AccessKeyId,
SignatureMethod: this.SignatureMethod,
Timestamp: this._generateTimestamp(),
SignatureVersion: this.SignatureVersion,
SignatureNonce: this._generateSignatureNonce()
};
return defaults;
};
SMS.prototype._getSignature = function (pkg, method) {
pkg = _.clone(pkg);
delete pkg.Signature;
var string = encodeURIComponent(this._toQueryString(pkg));
string = method + '&' + encodeURIComponent('/') + '&' + string;
// only HMAC-SHA1
var stringToSign = crypto.createHmac('sha1', this.AccessKeySecret + '&').update(string).digest('base64');
return stringToSign;
};
SMS.prototype._toQueryString = function (object) {
return Object.keys(object).filter(function (key) {
return object[key] !== undefined && object[key] !== '';
}).sort().map(function (key) {
// TODO isUTF
if (~['ParamString', 'SignName', 'Timestamp'].indexOf(key)) {
return key + '=' + encodeURIComponent(object[key]);
} else {
return key + '=' + object[key];
}
}).join('&');
};
SMS.prototype._generateTimestamp = function () {
return new Date().toISOString();
};
SMS.prototype._generateSignatureNonce = function (length) {
var chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
var maxPos = chars.length;
var noceStr = '';
var i;
for (i = 0; i < (length || 32); i++) {
noceStr += chars.charAt(Math.floor(Math.random() * maxPos));
}
return noceStr;
};
module.exports = SMS;