-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth.js
71 lines (61 loc) · 1.98 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
/**
* Module dependencies.
*/
var passport = require('passport')
, LocalStrategy = require('passport-local').Strategy
, BasicStrategy = require('passport-http').BasicStrategy
, ClientPasswordStrategy = require('passport-oauth2-client-password').Strategy
, BearerStrategy = require('passport-http-bearer').Strategy
, db = require('./db')
passport.use(new LocalStrategy(
function(username, password, done) {
db.users.findByUsername(username, function(err, user) {
if (err) { return done(err); }
if (!user) { return done(null, false); }
if (user.password != password) { return done(null, false); }
return done(null, user);
});
}
));
passport.serializeUser(function(user, done) {
done(null, user.id);
});
passport.deserializeUser(function(id, done) {
db.users.find(id, function (err, user) {
done(err, user);
});
});
passport.use(new BasicStrategy(
function(username, password, done) {
db.clients.findByClientId(username, function(err, client) {
if (err) { return done(err); }
if (!client) { return done(null, false); }
if (client.clientSecret != password) { return done(null, false); }
return done(null, client);
});
}
));
passport.use(new ClientPasswordStrategy(
function(clientId, clientSecret, done) {
db.clients.findByClientId(clientId, function(err, client) {
if (err) { return done(err); }
if (!client) { return done(null, false); }
if (client.clientSecret != clientSecret) { return done(null, false); }
return done(null, client);
});
}
));
passport.use(new BearerStrategy(
function(accessToken, done) {
db.accessTokens.find(accessToken, function(err, token) {
if (err) { return done(err); }
if (!token) { return done(null, false); }
db.users.find(token.userID, function(err, user) {
if (err) { return done(err); }
if (!user) { return done(null, false); }
var info = { scope: '*' }
done(null, user, info);
});
});
}
));