-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjwthelper.js
59 lines (50 loc) · 1.47 KB
/
jwthelper.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
let jwt = require('jsonwebtoken');
var jwksClient = require('jwks-rsa');
var client = jwksClient({
jwksUri: "https://whydahdev.cantara.no/oauth2/.well-known/jwks.json"
});
function getKey(header, callback){
client.getSigningKey(header.kid, function(err, key) {
var signingKey = key.publicKey || key.rsaPublicKey;
callback(null, signingKey);
});
}
exports.isValidWithSecret = (token, secret) => {
let isValid = false;
try {
var decoded = jwt.verify(token, secret);
isValid = true;
} catch(err) {
// err
console.log("Failed to verify token: ", token , ". Reason: ", err);
}
return isValid;
}
exports.isValidWithJwks = (token, jwksUri) => {
let isValid = false;
const options = {
algorithm: "RS256"
}
try {
jwt.verify(token, getKey, options, function(err, decoded) {
console.log(decoded.aud) // bar
});
isValid = true;
} catch(err) {
// err
console.log("Failed to verify token: ", token , ". Reason: ", err);
}
return isValid;
}
exports.sum = (toAdd) => {
// Convert arguments object to an array
var args = Array.prototype.slice.call(toAdd);
// Throw error if arguments contain non-finite number values
if (!args.every(Number.isFinite)) {
throw new TypeError('sum() expects only numbers.')
}
// Return the sum of the arguments
return args.reduce(function (a, b) {
return a + b
}, 0);
}