-
Notifications
You must be signed in to change notification settings - Fork 251
/
Copy pathprotected-resource.js
72 lines (61 loc) · 1.46 KB
/
protected-resource.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
const express = require("express")
const bodyParser = require("body-parser")
const fs = require("fs")
const { timeout } = require("./utils")
const jwt = require('jsonwebtoken');
const config = {
port: 9002,
publicKey: fs.readFileSync("assets/public_key.pem"),
}
const users = {
user1: {
username: "user1",
name: "User 1",
date_of_birth: "7th October 1990",
weight: 57,
},
john: {
username: "john",
name: "John Appleseed",
date_of_birth: "12th September 1998",
weight: 87,
},
}
const app = express()
app.use(timeout)
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({ extended: true }))
/*
Your code here
*/
app.get('/user-info', (req, res) => {
if (!req.headers.authorization) {
res.status(401).end();
return;
}
const prefix = 'bearer ';
const payload = req.headers.authorization.slice(prefix.length);
let decoded;
try {
decoded = jwt.verify(payload, config.publicKey, {algorithms: ['RS256']});
//console.log('decode: ' + JSON.stringify(x));
} catch { res.status(401).end(); return;}
let json = {};
let {userName, scope} = decoded;
let arr = scope.split(' ');
const prefix2 = 'permission:'
for (const i of arr) {
let perm = i.slice(prefix2.length);
json[perm] = users[userName][perm];
}
res.json(json);
});
const server = app.listen(config.port, "localhost", function () {
var host = server.address().address
var port = server.address().port
})
// for testing purposes
module.exports = {
app,
server,
}