-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathidentity.js
38 lines (35 loc) · 1013 Bytes
/
identity.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
/**
* The identity middleware will take a token that is passed in and validate it in the system.
* @param kontx
* @param next
*/
module.exports = function identity(kontx, next){
'use strict';
var _ = require('lodash'),
db = require('../db')(),
Strings = require('../strings'),
strings = new Strings('en'),
err = require('../utils/error')(401),
crypto = require('../utils/crypto'),
config = require('../config');
if(_.isUndefined(kontx.token)){
next(err);
return;
}
db.tokens.getById(crypto.createHash(kontx.token, config.crypto.secret_passphrase))
.then(function(token){
db.users.getById(token.uid)
.then(function(identity){
kontx.user = identity;
next();
})
.fail(function(err){
next(err);
})
.done();
})
.fail(function(){
next(err);
})
.done();
};