ecissue_20170611110149.js
1.3 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
var jwt = require('./');
var Service = function(options) {
this.options = options;
}
Service.prototype.authenticate = function() {
var self=this;
return new Promise((resolve, reject) => {
var cert = fs.readFileSync( self.options.cert ); // get public key
// sign asynchronously
var claims = { // token claims payload
//The issued at (iat) registered claim key, whose value indicates the time at which the token was generated,
//in terms of the number of seconds since Epoch, in UTC
iat: Math.floor( Date.now() / 1000 ),
//The expiration time (exp) registered claim key,
//whose value must not be greater than 15777000 (6 months in seconds) from the Current Unix Time on the server
exp: Math.floor(Date.now() / 1000) + ( 60 * 60 * 24 )
};
jwt.sign(claims,
cert,
{
keyid: "ABC123DEFG",
issuer: "DEF123GHIJ",
algorithm: "ES256"
},
function(error, token) {
if( error ) return reject(error);
else { // token created
var decoded = jwt.verify(token, cert);
console.log(decoded) // bar
self.token=token;
return resolve(token);
}
});
});
}//authenticate
module.exports = Service;