Repository navigation
Expand file tree
/
Copy pathindex.js
More file actions
executable file
·83 lines (66 loc) · 1.97 KB
/
Copy pathindex.js
File metadata and controls
executable file
·83 lines (66 loc) · 1.97 KB
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
const express = require('express')
const app = express()
const port = 3000
var bodyParser = require('body-parser')
const NodeRSA = require('node-rsa');
const key = new NodeRSA({ b: 512 });
app.use(express.static('public'));
app.use(bodyParser.json()); // to support JSON-encoded bodies
app.use(bodyParser.urlencoded({ // to support URL-encoded bodies
extended: true
}));
app.post('/login', (req, res) => {
var name = req.body.userName,
pass = req.body.password,
user = authenticate(name, pass);
if (!user) {
return res.send('no such user / password');
}
var authorization = authorize(user);
var licensing = getLicense(user);
if (!licensing) {
return res.send('no license available');
}
var header = Buffer.from(JSON.stringify(createJWTHeader())).toString("base64"),
body = Buffer.from(JSON.stringify(createJWTBody(user, authorization, licensing))).toString("base64"),
signature = Buffer.from(JSON.stringify(createSignature(header, body))).toString("base64");
res.send([header, body, signature].join("."));
});
authenticate = (username, pass) => {
return {
Username: username,
Name: "John Doe",
Id: "1"
}
}
authorize = (user) => {
return {
roles: ["admin"]
}
}
getLicense = (user) => {
return {
license: "pro"
}
}
createSignature = (header, body) => {
return key.encrypt(header + body, "base64");
}
createJWTBody = (user, authorization, licensing) => {
return {
iss: "GAMS",
exp: Date.now() + 1000 * 60 * 5,
aud: "for everybody",
sub: user.Id,
name: user.Name,
roles: authorization.roles,
license: licensing.license
}
}
createJWTHeader = () => {
return {
typ: "JWT",
alg: "RS256"
}
}
app.listen(port, () => console.log(`Example app listening on port ${port}!`))