-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
49 lines (45 loc) · 1.56 KB
/
Copy pathapp.js
File metadata and controls
49 lines (45 loc) · 1.56 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
require('dotenv').config();
const express = require('express');
const jwt = require('jsonwebtoken');
const cors = require('cors');
const fs = require('fs');
const app = express();
const path = '/api/v1';
app.use(cors());
app.options('*', cors());
app.get(`${path}/consumers/:consumerId/jwts`, (req, res) => {
const consumerId = req.params.consumerId;
const { roomName } = req.query;
if (consumerId) {
const header = {
keyid: consumerId,
algorithm: 'RS256'
}
const payLoad = {
sub: consumerId,
aud: 'clanmeeting',
iss: 'production',
room: roomName ?? '*',
// valid for 5 minutes
exp: Math.floor(Date.now() / 1000) + (60 * 120),
nbf: Math.floor(Date.now() / 1000),
context: {},
}
let signature;
try {
signature = fs.readFileSync(`./private-keys/cm-api-key-${consumerId}.pem`);
} catch (err) {
if (err.code !== 'ENOENT') return res.status(500).json({ success: false, error: { message: err } });
return res.status(500).json({ success: false, error: { message: 'Invalid consumerId' } });
}
const token = jwt.sign(payLoad, signature, header);
if (token) return res.status(200).json({ success: true, data: { token: token } });
return res.status(500).json({ success: false, error: { message: 'Error generating jwt' } });
}
return res.status(400).json({ success: false, error: { message: 'Please set a valid consumerId' } });
});
let port = process.env.PORT || 5000;
app.listen(port, () => {
console.log(`Server is up and running on ${port} ...`);
});
module.exports = app;