-
Notifications
You must be signed in to change notification settings - Fork 22
/
auth_jwt_sign.js
32 lines (27 loc) · 1.15 KB
/
auth_jwt_sign.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
var JWT = require('jsonwebtoken'); // used to sign our content
var aguid = require('aguid');
var ES = require('esta');
var dir = __dirname.split('/')[__dirname.split('/').length-1];
var file = dir + __filename.replace(__dirname, '') + " -> ";
module.exports = function sign(request, callback) {
// payload is the object that will be signed by JWT below
var payload = { jti:aguid() }; // v4 random UUID used as Session ID below
if (request.payload && request.payload.email) {
payload.iss = aguid(request.payload.email);
} // see: http://self-issued.info/docs/draft-ietf-oauth-json-web-token.html#issDef
else { // no email is set (means an anonymous person)
payload.iss = "anonymous";
} // this will need to be extended for other auth: http://git.io/pc1c
var token = JWT.sign(payload, process.env.JWT_SECRET); // http://git.io/xPBn
var session = { // set up session record for inserting into ES
index: "time",
type: "session",
id : payload.jti,
person: payload.iss,
ua: request.headers['user-agent'],
ct: new Date().toISOString()
}
ES.CREATE(session, function(esres) {
return callback(token, esres);
});
}