-
Notifications
You must be signed in to change notification settings - Fork 0
/
index-cert-scope.js
129 lines (120 loc) · 3.84 KB
/
index-cert-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
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
const express = require("express");
const port = 8003;
const bodyParser = require("body-parser");
const router = express.Router();
var app = express();
var fs = require('fs');
const https = require('https');
// 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;
let cert = req.socket.getPeerCertificate();
console.log(cert);
if (req.client.authorized) {
console.log(
`Certificate is VALID`
);
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);
}
// The Certificate is NOT VALID
else if (cert.subject) {
console.log(
`Certificates are NOT VALID`
);
res.sendStatus(403);
}
// A Certificate was NOT PROVIDED
else {
console.log(`No Certificate provided by the client`);
res.status(403).send(`Certificate Required`);
}
});
app.get("/check", function (req, res) {
console.log("/check", "Received request");
console.log("Token: ", req.headers.authorization);
let cert = req.socket.getPeerCertificate();
if (req.client.authorized) {
console.log(
`Certificate is VALID`
);
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);
}
}
} else if (cert.subject) {
// The Certificate is NOT VALID
console.log(
`Certificates are NOT VALID`
);
res.sendStatus(403);
} else {
// A Certificate was NOT PROVIDED
console.log(`No Certificate provided by the client`);
res.status(403).send(`Certificate Required`);
}
});
var options = {
key: fs.readFileSync('certs/server-key.pem'),
cert: fs.readFileSync('certs/server-crt.pem'),
ca: fs.readFileSync('certs/ca-crt.pem'),
requestCert: true,
rejectUnauthorized: true
};
https.createServer(options, 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);
});