-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
executable file
·159 lines (145 loc) · 6.45 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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#! /usr/bin/env node
const open = require('open');
const express = require('express');
const app = express();
const axios = require('axios');
const qs = require('qs');
const argv = require('yargs').argv;
const fs = require("fs");
const fp = require("find-free-port");
const jwt = require("jws");
const kcUrl = process.env.BRAUZIE_KC_URL || "https://google.com";
const realm = process.env.BRAUZIE_REALM || "";
const client_id = process.env.BRAUZIE_CLIENT_ID || "";
const client_secret = process.env.BRAUZIE_CLIENT_SECRET || "";
const username = process.env.BRAUZIE_USERNAME || "";
const password = process.env.BRAUZIE_PASSWORD || "";
const baseUrl = kcUrl + "/auth/realms/"+ realm + "/protocol/openid-connect";
const brauzieFolder = process.env.HOME + "/.brauzie";
const brauzieFile = brauzieFolder + "/jwt.json";
const brauzieTokenFile = brauzieFolder + "/id-token.json";
if (argv._[0] === "login") {
if (argv['direct-grant']) {
const req = {
client_id: client_id,
client_secret: client_secret,
username: username,
password: password,
grant_type: 'password',
scope: 'openid'
};
axios.post(baseUrl + "/token", qs.stringify(req))
.then(response => {
const jwt = saveTokenHandler(response.data);
process.exit(0);
})
.catch(error => {
console.error("Error while fetching token :(");
const errMsg = error.response.data;
if (errMsg) console.error(JSON.stringify(errMsg));
process.exit(1);
});
}
else {
fp(9000, (err, PORT) => {
const authEndpoint = "/brauzie";
const redirectUrl = "http://localhost:" + PORT + authEndpoint;
(async () => {
await open(baseUrl + '/auth?scope=openid&client_id=' + client_id + '&response_type=code&redirect_uri=' + redirectUrl);
})();
let server = app.listen(PORT);
setTimeout(() => {
server.close();
console.log("Timeout reached :(");
}, 2 * 1000 * 60);
app.get(authEndpoint, (req, res) => {
const code = req.query.code;
if (code) {
const req = {
code: code,
client_id: client_id,
grant_type: 'authorization_code',
redirect_uri: redirectUrl,
client_secret: client_secret
};
axios.post(baseUrl + "/token", qs.stringify(req))
.then(response => {
const token = response.data;
const jowt = saveTokenHandler(token);
if (argv.silent) {
res.send(
"<script>\n" +
" window.close();\n" +
"</script>"
);
} else {
res.send(
"<div style='max-width: 800px; margin: 0 auto;'>" +
"<div style='font-family: monospace;text-align: center;padding: 20px;font-size: 32px;background: #f2f2f2;font-weight: bold;'>" +
"Hi " + (jowt.given_name || jowt.preferred_username) + "! This is ~brauzie :)" +
"</div>" +
"<div style='font-family: monospace; padding: 20px 20px; background: #d9d9d9;'>" +
"You have been successfully authenticated. <br>" +
"Your JWT token was saved to ~/.brauzie/jwt.json. <br>" +
"Its json representation is pasted below. <br>" +
"</div>" +
"<div style='white-space: pre; font-family: monospace; padding: 20px; background: #f0f8ff;\n'>" +
JSON.stringify(jwt.decode(token["access_token"]).payload, null, 2) +
"</div>" +
"</div>"
);
}
server.close();
process.exit(0);
})
.catch(error => {
console.error("Error while exchanging code for JWT token :(");
const errMsg = error.response.data;
if (errMsg) console.error(JSON.stringify(errMsg));
res.send(
"<script>\n" +
" window.close();\n" +
"</script>"
);
server.close();
process.exit(1);
});
}
});
});
}
}
else if (argv._[0] === "logout") {
if (!fs.existsSync(brauzieFile)) {
console.log("Cannot logout, you must login first :(");
process.exit(1);
}
const refreshToken = JSON.parse(fs.readFileSync(brauzieFile)).refresh_token;
const req = {
client_id: client_id,
refresh_token: refreshToken,
client_secret: client_secret
};
axios.post(baseUrl + "/logout", qs.stringify(req))
.then(response => {
console.log("Successfully logged out :)");
fs.unlinkSync(brauzieFile);
fs.unlinkSync(brauzieTokenFile);
process.exit(0);
})
.catch(error => {
console.log("Could not logout :(");
const errMsg = error.response.data;
if (errMsg) console.error(JSON.stringify(errMsg));
process.exit(1);
});
}
function saveTokenHandler(token) {
if (!argv.quite) console.log(token.access_token);
const decodedJWT = jwt.decode(token.access_token).payload;
const decodedIdTokenJWT = jwt.decode(token.id_token).payload;
if (!fs.existsSync(brauzieFolder)) fs.mkdirSync(brauzieFolder);
fs.writeFileSync(brauzieFile, JSON.stringify(token, null, 2));
fs.writeFileSync(brauzieTokenFile, JSON.stringify(decodedIdTokenJWT, null, 2));
return decodedIdTokenJWT;
}