-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtoken-generator.js
43 lines (38 loc) · 1.41 KB
/
token-generator.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
// module for creating JWTs
// https://www.npmjs.com/package/jsonwebtoken
const jwt = require('jsonwebtoken');
// Since we will be creating tokens for multiple people visitng the site,
// lets create a function which returns the JWT for each client request
module.exports = function createJWT(display_name){
// Generate random 8 digit number for 'sub' (whom the token refers to)
// this creates a new identity everytime a JWT is created
var genSub = "Moment-Guest-" + (Math.floor(Math.random()*7362592) + 10000000);
//var genSub = display_name;
//warning: static "subject" can send the guest to same space!
// consider using this for testing with the same space
//var genSub = "HC02132019";
console.log ("Generated subject ID - " + genSub);
console.log (`got display name as - ${display_name}`);
//create JWT paylod
var payload = {
"sub": genSub,
//"name": "Glitch Widget - "+genSub,
"name" : display_name,
"iss": process.env.ISS
};
//sign above payload
var token = jwt.sign(
payload,
Buffer.from(process.env.SECRET, 'base64'),
{ expiresIn: '1h' }
);
// sanity output to the console
//console.log ("Payload - "+ JSON.stringify(payload));
//console.log ("Token - "+ JSON.stringify(token));
var dataobj = {
token : token,
label : genSub
};
// send back the token to whoever called this function
return dataobj;
};