-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
65 lines (53 loc) · 2.01 KB
/
Copy pathindex.js
File metadata and controls
65 lines (53 loc) · 2.01 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
const express = require('express');
const app = express();
const jose = require('node-jose');
const fs = require('fs');
const ms = require('ms');
const cookieParser = require('cookie-parser');
const jwt = require('jsonwebtoken');
const crypto = require('crypto');
const health = require('@cloudnative/health-connect');
let healthcheck = new health.HealthChecker();
const shutdownPromise = () => new Promise(function (resolve, _reject) {
setTimeout(function () {
console.log('DONE!');
resolve();
}, 10);
});
let shutdownCheck = new health.ShutdownCheck("shutdownCheck", shutdownPromise);
healthcheck.registerShutdownCheck(shutdownCheck);
app.use('/live', health.LivenessEndpoint(healthcheck));
app.use('/ready', health.ReadinessEndpoint(healthcheck));
process.on('unhandledRejection', error => {
console.error('unhandledRejection', error);
process.exit(1);
});
(async () => {
const ks = JSON.parse(fs.readFileSync('secrets/keys.json'));
const config = JSON.parse(fs.readFileSync('config/config.json'));
const ttl = Math.floor(ms(config.ttl) / 1000);
const keyStore = await jose.JWK.asKeyStore(JSON.stringify(ks));
const [enc_key] = keyStore.all({ use: 'enc' });
app.use(cookieParser());
app.get('/tokens', async function (req, res) {
const now = Math.floor(Date.now() / 1000);
// creds if defined from the cookie
const creds = req.cookies.tokenId !== undefined ? jwt.decode(req.cookies.tokenId, {complete: true}).payload : {
sub: 'test',
user_id: 'test',
preferred_username: 'test'
};
const payload = JSON.stringify({
sub: creds.preferred_username,
exp: now + ttl,
iat: now,
key: req.query.key
});
const enc_token = await jose.JWE.createEncrypt({ format: 'compact', zip: true }, enc_key)
.update(payload)
.final();
res.set('Content-Type', 'text/plain');
res.send(enc_token);
});
app.listen(3000);
})();