-
Notifications
You must be signed in to change notification settings - Fork 1
/
utility.js
72 lines (61 loc) · 1.64 KB
/
utility.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 jwt = require('node-webtokens');
const jsonToken = require('./jsonTokenAlgorithms');
const bcrypt = require('bcrypt');
function getKey() {
const saltRounds = 10;
const secret_key = 'IndiaIsABeautifulCountry';
const key1 = bcrypt.hashSync(secret_key, saltRounds);
const key2 = bcrypt.hashSync(key1, saltRounds);
const keys = key1 + key2;
const key = keys.substr(0, 100);
return key;
}
function getRandomNum(min, max) {
return Math.floor(Math.random() * (max - min + 1) + min);
}
function getToken(payload, key, type) {
let token = '';
switch (type) {
case 'jwe':
const jweAlgRand = getRandomNum(0, jsonToken.jweAlg.length - 1);
const jweAlg = jsonToken.jweAlg[jweAlgRand];
const jweEncRand = getRandomNum(0, jsonToken.jweEnc.length - 1);
const jweEnc = jsonToken.jweEnc[jweEncRand];
token = jwt.generate(jweAlg, jweEnc, payload, key);
break;
case 'jwt':
const jwtAlgRand = getRandomNum(0, jsonToken.jwtAlg.length - 1);
const jwtAlg = jsonToken.jwtAlg[jwtAlgRand];
token = jwt.generate(jwtAlg, payload, key);
break;
}
return token;
}
function isTokenValid(token, key, expirySeconds) {
return jwt
.parse(token)
.setTokenLifetime(expirySeconds)
.verify(key);
}
function getParsedToken(token, key) {
return jwt.parse(token).verify(key);
}
function getTokenDetails(payLoadInfo, key, type, token, parsedToken) {
const { header, payload, valid } = parsedToken;
return {
payLoadInfo,
key,
type,
token,
header,
payload,
valid
};
}
module.exports = {
getKey,
getToken,
isTokenValid,
getParsedToken,
getTokenDetails
};