This repository has been archived by the owner on Sep 3, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 28
/
app.js
291 lines (264 loc) · 8.9 KB
/
app.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
"use strict";
const express = require("express");
const morgan = require("morgan");
const fs = require("fs");
const _ = require("underscore");
const session = require("express-session");
const randomstring = require("randomstring");
const bodyParser = require("body-parser");
const ui = _.template(fs.readFileSync("./input.html").toString());
const app = express();
app.use(morgan(":method :url :status Authorization: :req[authorization] Debug info: :res[x-debug] Redirect: :res[location]"));
app.use(bodyParser.urlencoded({ extended: false }));
const EXPECTED_CLIENT_ID = process.env.EXPECTED_CLIENT_ID || "dummy-client-id";
const EXPECTED_CLIENT_SECRET = process.env.EXPECTED_CLIENT_SECRET || "dummy-client-secret";
const AUTH_REQUEST_PATH = process.env.AUTH_REQUEST_PATH || "/o/oauth2/v2/auth";
const ACCESS_TOKEN_REQUEST_PATH = process.env.ACCESS_TOKEN_REQUEST_PATH || "/oauth2/v4/token";
const USERINFO_REQUEST_URL = process.env.TOKENINFO_REQUEST_URL || "/oauth2/v3/userinfo";
const TOKENINFO_REQUEST_URL = process.env.TOKENINFO_REQUEST_URL || "/oauth2/v3/tokeninfo";
const PERMITTED_REDIRECT_URLS = process.env.PERMITTED_REDIRECT_URLS ? process.env.PERMITTED_REDIRECT_URLS.split(",") : ["http://localhost:8181/auth/login"];
const code2token = {};
const refresh2personData = {};
const authHeader2personData = {};
const id_token2personData = {};
function now() {
return Math.round(new Date().valueOf() / 1000);
}
function errorMsg(descr, expected, actual) {
return "expected " + descr + ": " + expected + ", actual: " + actual;
}
function validateClientId(actualClientId, res) {
if (actualClientId === EXPECTED_CLIENT_ID) {
return true;
}
res.writeHead(400, {
"X-Debug": errorMsg("client_id", EXPECTED_CLIENT_ID, actualClientId)
});
res.end();
return false;
}
function permittedRedirectURLs() {
return _.reduce(PERMITTED_REDIRECT_URLS, (a, b) => a === "" ? b : a + ", " + b, "" );
}
function validateAuthRequest(req, res) {
const actualClientId = req.query.client_id;
if (validateClientId(actualClientId, res)) {
if (req.query.response_type !== "code") {
res.writeHead(401, {
"X-Debug": errorMsg("response_type", "code", req.query.response_type)
});
return false;
}
if (req.query.redirect_uri && ! _.contains(PERMITTED_REDIRECT_URLS, req.query.redirect_uri)) {
res.writeHead(401, {
"X-Debug" : errorMsg("redirect_uri", "one of " + permittedRedirectURLs(), req.query.redirect_uri)
});
return false;
}
return true;
}
return false;
}
function validateAuthorizationHeader(header, res) {
header = header.trim();
if (!header.startsWith("Basic ")) {
return false;
}
header = header.substring("Basic ".length).trim();
const decoded = new Buffer(header, "base64").toString("ascii");
if (decoded === "") {
return false;
}
const segments = decoded.split(":");
if (segments.length != 2) {
return false;
}
if (segments[0] !== EXPECTED_CLIENT_ID) {
return false;
}
if (segments[1] !== EXPECTED_CLIENT_SECRET) {
return false;
}
return true;
}
function validateAccessTokenRequest(req, res) {
let success = true, msg;
if (req.body.grant_type !== "authorization_code" && req.body.grant_type !== "refresh_token") {
success = false;
msg = errorMsg("grant_type", "authorization_code or refresh_token", req.body.grant_type);
}
if (req.body.grant_type === "refresh_token") {
let personData = refresh2personData[req.body.refresh_token];
if (personData === undefined) {
success = false;
msg = "invalid refresh token";
}
else if(!validateTokenExpiration(personData.date_of_creation, personData.refresh_token_expires_in)) {
success = false;
msg = "this token is already expired";
}
}
// if (!validateClientId(req.query.client_id, res)) {
// success = false;
// }
// if (!validateAuthorizationHeader(req.headers["authorization"])) {
// success = false;
// msg = errorMsg("Authorization header", req.headers["authorization"], "Basic ZHVtbXktY2xpZW50LWlkOmR1bW15LWNsaWVudC1zZWNyZXQ=");
// }
if (!validateClientId(req.body.client_id, res)) {
success = false;
}
if (req.body.client_secret !== EXPECTED_CLIENT_SECRET) {
success = false;
msg = errorMsg("client_secret", EXPECTED_CLIENT_SECRET, req.body.client_secret);
}
if (req.session.redirect_uri !== req.body.redirect_uri) {
success = false;
msg = errorMsg("redirect_uri", req.session.redirect_uri, req.body.redirect_uri);
}
if (!success) {
const params = {};
if (msg) {
params["X-Debug"] = msg;
}
res.writeHead(401, params);
}
return success;
}
function validateTokenExpiration(date_of_creation, expires_in) {
let success = true;
const expiration_date = Number(date_of_creation) + Number(expires_in);
const now = Date.now()/1000 | 0;
if(now > expiration_date) {
success = false;
}
return success;
}
function createToken(name, email, expires_in, refresh_token_expires_in, client_state) {
const code = "C-" + randomstring.generate(3);
const accesstoken = "ACCT-" + randomstring.generate(6);
const refreshtoken = "REFT-" + randomstring.generate(6);
const id_token = "IDT-" + randomstring.generate(6);
const date_of_creation = Date.now()/1000 | 0;
const token = {
access_token: accesstoken,
expires_in: expires_in,
refresh_token: refreshtoken,
id_token: id_token,
state: client_state,
date_of_creation: date_of_creation,
token_type: "Bearer"
};
id_token2personData[id_token] = authHeader2personData["Bearer " + accesstoken] = {
email: email,
email_verified: true,
name: name,
expires_in: expires_in,
date_of_creation: date_of_creation
};
code2token[code] = token;
refresh2personData[refreshtoken] = {
name: name,
email: email,
expires_in: expires_in,
refresh_token_expires_in: refresh_token_expires_in,
date_of_creation: date_of_creation
};
return code;
}
app.use(session({
secret: "keyboard cat",
resave: false,
saveUninitialized: true,
cookie: {secure: false}
}))
function authRequestHandler(req, res) {
if (validateAuthRequest(req, res)) {
req.session.redirect_uri = req.query.redirect_uri;
if (req.query.state) {
req.session.client_state = req.query.state;
}
res.send(ui({
query: req.query
}));
} else {
}
res.end();
}
app.get(AUTH_REQUEST_PATH, authRequestHandler);
app.get("/login-as", (req, res) => {
const code = createToken(req.query.name, req.query.email, req.query.expires_in, req.query.refresh_token_expires_in, req.session.client_state);
if (req.session.redirect_uri) {
let redirectUri = req.session.redirect_uri;
let location = `${redirectUri}${redirectUri.includes('?') ? '&' : '?'}code=${code}`;
if (req.session.client_state) {
location += "&state=" + req.session.client_state;
}
res.writeHead(307, {"Location": location});
res.end();
}
});
app.post(ACCESS_TOKEN_REQUEST_PATH, (req, res) => {
if (validateAccessTokenRequest(req, res)) {
let code = null;
if (req.body.grant_type === "refresh_token") {
const refresh = req.body.refresh_token;
const personData = refresh2personData[refresh];
code = createToken(personData.name, personData.email, personData.expires_in, personData.refresh_token_expires_in, null);
delete refresh2personData[refresh];
} else {
code = req.body.code;
}
const token = code2token[code];
if (token !== undefined) {
console.log("access token response body: ", token);
res.send(token);
}
}
res.end();
});
app.get(USERINFO_REQUEST_URL, (req, res) => {
const token_info = authHeader2personData[req.headers["authorization"]];
if (token_info !== undefined) {
console.log("userinfo response", token_info);
if(!validateTokenExpiration(token_info.date_of_creation, token_info.expires_in)) {
res.status(401);
res.send("this token is already expired");
}
else{
res.send(token_info);
}
} else {
res.status(404);
}
res.end();
});
app.get(TOKENINFO_REQUEST_URL, (req, res) => {
if (req.query.id_token == null) {
res.status(400)
res.send("missing id_token query parameter");
}
const token_info = id_token2personData[req.query.id_token];
if (token_info !== undefined) {
res.status(200);
res.send(token_info);
} else {
res.status(404);
res.send("token not found by id_token " + req.query.id_token);
}
res.end();
});
module.exports = {
app: app,
validateClientId: validateClientId,
validateAccessTokenRequest: validateAccessTokenRequest,
validateAuthorizationHeader: validateAuthorizationHeader,
validateAuthRequest: validateAuthRequest,
authRequestHandler: authRequestHandler,
EXPECTED_CLIENT_ID: EXPECTED_CLIENT_ID,
EXPECTED_CLIENT_SECRET: EXPECTED_CLIENT_SECRET,
AUTH_REQUEST_PATH : AUTH_REQUEST_PATH,
ACCESS_TOKEN_REQUEST_PATH : ACCESS_TOKEN_REQUEST_PATH,
PERMITTED_REDIRECT_URLS : PERMITTED_REDIRECT_URLS,
permittedRedirectURLs: permittedRedirectURLs
};