-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
61 lines (55 loc) · 1.87 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
const moment = require('moment');
const uuid = require('node-uuid');
const aliyunUtils = require('aliyun-utils');
const http = require('https');
/**
* 临时授权访问类
*/
class AliyunSTS {
/**
* 构造函数
* @param accessKey 阿里云 AccessKey
* @param accessSecret 阿里云 AccessSecret
*/
constructor(accessKey, accessSecret) {
this.accessKey = accessKey;
this.accessSecret = accessSecret;
}
/**
* AssumeRole 接口调用
* @param roleArn
* @param roleSessionName
* @param durationSeconds
* @param callback 回调函数
*/
assumeRole(roleArn, roleSessionName, durationSeconds, callback) {
let url = 'https://sts.aliyuncs.com/?SignatureVersion=1.0';
url += '&Format=JSON';
url += `&Timestamp=${encodeURIComponent(moment.utc().format())}`;
url += `&RoleArn=${encodeURIComponent(roleArn)}`;
url += `&RoleSessionName=${roleSessionName}`;
url += `&AccessKeyId=${this.accessKey}`;
url += `&SignatureMethod=HMAC-SHA1`;
url += `&Version=2015-04-01`;
url += `&Action=AssumeRole`;
url += `&SignatureNonce=${uuid.v4()}`;
url += `&DurationSeconds=${durationSeconds}`;
let signedString = aliyunUtils.signature(url, this.accessSecret);
url += `&Signature=${encodeURIComponent(signedString)}`;
http.get(url, function (resp) {
if (resp.statusCode == 200) {
let entity = '';
resp.on('data', function (data) {
entity += data;
}).on('end', function () {
callback(resp.statusCode, JSON.parse(entity));
});
} else {
callback(status);
}
}).on('error', function (error) {
console.log(error.message);
});
}
}
module.exports = AliyunSTS;