This repository has been archived by the owner on Sep 29, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth.js
79 lines (70 loc) · 2.54 KB
/
auth.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
73
74
75
76
77
78
79
var LocalStrategy = require('passport-local').Strategy,
Promise = require('bluebird'),
scrypt = require('scrypt'),
crypto = require('crypto'),
scryptParameters = scrypt.params(0.1),
config = require('./config');
// promisify
crypto.pbkdf2 = Promise.promisify(crypto.pbkdf2);
module.exports = function(passport, db) {
var User = db.User;
passport.serializeUser(function(user, done) {
done(null, user.id);
});
passport.deserializeUser(function(id, done) {
User.get(id).run().then(function(user) {
user = user[0];
done(null, user);
}, function(err) {
done(err);
});
});
passport.use(new LocalStrategy({
usernameField: 'username',
passwordField: 'clientHash'
}, function(username, clientHash, done) {
var user;
User.getAll(username, {
index: 'username'
}).pluck('passHash', 'passSalt', 'passIter', 'passHashSize', 'displayUsername', 'id')
.execute().then(function(cursor) {
return cursor.next();
}).then(function(data) {
user = data;
return crypto.pbkdf2(clientHash, user.passSalt, user.passIter, user.passHashSize);
}, function(err) {
done(null, false, {
// Invalid username
// TODO: Change to more ambiguous text
message: 'No user found'
});
}).then(function(hash) {
// Can't find a way to convert scrypt.verifyHash into a promise
scrypt.verifyHash(user.passHash, hash.toString('base64'), function(err, isValid) {
// err_code 4 means invalid password
if (err && err.err_code !== 4) {
done(err, false);
} else if (isValid) {
done(null, user);
} else {
done(null, false, {
// Incorrect password
// TODO: Change to more ambiguous text
message: 'Invalid password'
});
}
});
}, function(err) {
done(err);
});
}));
return {
ensureAuthenticated: function(req, res, next) {
if (req.isAuthenticated()) {
return next();
}
// Not signed in, send 401 (Unauthorized)
res.send(401);
}
};
};