-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
78 lines (68 loc) · 2.01 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
const http = require("http");
const request = require("request");
const AWS = require("aws-sdk");
const STS = new AWS.STS();
const CONSOLE = "https://ap-northeast-1.console.aws.amazon.com/console";
const LOGIN = "https://signin.aws.amazon.com/federation";
const ISS = "awsmc";
const ASSUME_ROLE_REQ = require("./json/AWSLogin.json");
const hostname = '127.0.0.1';
const port = 5201;
const server = http.createServer(async (req, res) => {
const assumeRole = await getAssumeRole();
const token = await getToken(assumeRole.Credentials);
const url = await getRedirectUrl(token);
res.statusCode = 301;
res.setHeader('Location', url);
res.setHeader('Cache-Control', "no-cache, no-store");
res.setHeader('Pragma', "no-cache");
res.end();
});
server.listen(port, hostname, () => {
console.log("Server running at http:\/\/localhost:5201");
});
function getAssumeRole() {
return new Promise(function(resolve, reject){
STS.assumeRole(ASSUME_ROLE_REQ, function(err, data) {
if (err) {
reject(new Error(err));
} else {
resolve(data);
}
});
});
}
function getToken(assumeRole) {
return new Promise(function(resolve, reject){
var session = {
sessionId: assumeRole.AccessKeyId,
sessionKey: assumeRole.SecretAccessKey,
sessionToken: assumeRole.SessionToken
}
requestUrl = LOGIN
+ "?Action=getSigninToken"
+ "&SessionType=json"
+ "&Session=" + encodeURIComponent(JSON.stringify(session));
var options = {
url: requestUrl,
json: true
};
request.get(options, function (err, res, body) {
if (err) {
reject(new Error(err));
} else {
resolve(body.SigninToken);
}
})
});
}
function getRedirectUrl(token) {
return new Promise(function(resolve, reject){
var redirectUrl = LOGIN
+ "?Action=login"
+ "&SigninToken=" + encodeURIComponent(token)
+ "&Issuer=" + encodeURIComponent(ISS)
+ "&Destination=" + encodeURIComponent(CONSOLE)
resolve(redirectUrl);
});
}