-
Notifications
You must be signed in to change notification settings - Fork 0
/
index-scope.js
84 lines (76 loc) · 2.39 KB
/
index-scope.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
const express = require("express");
const port = 8001;
const bodyParser = require("body-parser");
const router = express.Router();
var app = express();
// parse req body
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.use("/", router);
app.post("/token", function (req, res) {
expires_in = 60;
scope = req.body.scope;
access_token = Date.now() + expires_in * 1000;
if (req.body.grant_type === "client_credentials") {
console.log(
"/token",
"Acquired tokens, will expire on ",
new Date(access_token)
);
} else if (req.body.grant_type === "refresh_token") {
console.log(
"/token",
"Refreshed tokens, will expire on ",
new Date(access_token)
);
} else {
res.sendStatus(400); // bad input
}
access_token += "." + scope;
res_body = JSON.stringify({
access_token,
refresh_token: "refresh_token",
expires_in,
token_type: "Bearer",
});
console.log("res_body: ", res_body);
res.send(res_body);
});
app.get("/check", function (req, res) {
console.log("/check", "Received request");
console.log("Token: ", req.headers.authorization);
token_split = req.headers.authorization.replace("Bearer ", "").split(".");
tokenDate = new Date(parseInt(token_split[0]));
scope = token_split[1];
if (Date.now() > tokenDate) {
console.log("Token has expired, returning 401");
res.sendStatus(401);
} else {
// check scope
if (scope === "check") {
console.log("Token accepted");
res.send(JSON.stringify({ status: "ok" }));
} else {
console.log("Token does not has required scope");
res.sendStatus(403);
}
}
});
app.listen(port, function () {
console.log(`Example app listening on port ${port}!`);
var route,
routes = [];
app._router.stack.forEach(function (middleware) {
if (middleware.route) {
// routes registered directly on the app
routes.push(middleware.route);
} else if (middleware.name === "router") {
// router middleware
middleware.handle.stack.forEach(function (handler) {
route = handler.route;
route && routes.push(route);
});
}
});
console.log(routes);
});