This repository has been archived by the owner on Dec 20, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
auth.js
177 lines (151 loc) · 4.86 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/*
authentication mechanism
===
*/
const createError = require('http-errors')
const { isString, get } = require('lodash')
var settings = require('./settings'),
helpers = require('./helpers'),
passport = require('passport'),
LocalStrategy = require('passport-local').Strategy,
TwitterStrategy = require('passport-twitter').Strategy,
GoogleStrategy = require('passport-google-oauth').OAuth2Strategy,
queries = require('decypher')('./queries/user.cyp'),
User = require('./models/user'),
neo4j = require('seraph')(settings.neo4j.host);
// auth mechanism: Local
passport.use(new LocalStrategy(function (username, password, done) {
// get user having username or email = username and check if encription matches and check if
User.check({
username: username,
password: password
}, function(err, user) {
if(err)
done({reason: 'credentials not matching', error: err});
else if(user.props.status != 'enabled')
done({reason: 'user is NOT active, its status should be enabled', found: user.status});
else
done(null, user);
})
// neo4j.query('Match(user:user) WHERE user.email = {nickname} OR user.username = {nickname} RETURN user',{
// nickname: username
// }, function(err, res) {
// if(err)
// return done(err)
// if(!res.length)
// return done({reason: 'user not found'}) // the real reason, for loggin purposes. user not found
// var user = res[0];
// user.isValid = helpers.comparePassword(password, user.password, {
// from: 'localstrategy',
// secret: settings.secret.salt,
// salt: user.salt
// });
// if(!user.isValid)
// return done({reason: 'credentials not matching'});
// if(user.status != 'enabled')
// return done({reason: 'user is NOT active, its status should be enabled', found: user.status});
// return done(null, user)
// })
}));
/*
Auth mechanism for twitter
*/
passport.use(new TwitterStrategy({
consumerKey: settings.twitter.consumer_key,
consumerSecret: settings.twitter.consumer_secret,
callbackURL: "/auth/twitter/callback"
},
function(token, tokenSecret, profile, done) {
User.create({
email: '@' + profile.displayName,
username: '@' + profile.displayName,
firstname: '@' + profile.displayName,
salt: '',
password:'',
status: 'enabled',
strategy: 'twitter',
about: '' + profile.description,
picture: profile.photos? profile.photos.pop().value: '',
}, function(err, user) {
console.log('twitter:', err? 'error': 'ok');
if(err) {
console.log(err)
done(err);
} else
done(null, user);
});
}
));
/*
Auth mechanism for google
*/
passport.use(new GoogleStrategy({
clientID: settings.google.clientId,
clientSecret: settings.google.clientSecret,
callbackURL: "/auth/google/callback"
},
function(accessToken, refreshToken, profile, done) {
User.create({
email: 'g@' + profile.id,
username: profile.displayName + profile.id,
firstname: profile.displayName,
gender: profile.gender || '',
salt: '',
password:'',
status: 'enabled',
strategy: 'google',
about: '' + profile.description || '',
picture: profile.photos? profile.photos.pop().value: '',
}, function(err, user) {
console.log('google:', err? 'error': 'ok');
if(err) {
console.log(err);
done(err);
} else
done(null, user);
});
}
));
function asSerializedUser(user) {
return {
is_authentified: true,
firstname: user.props.firstname,
lastname: user.props.lastname,
email: user.email,
username: user.username,
id: user.id,
picture: user.props.picture,
apiKey: user.props.apiKey
}
}
passport.serializeUser((user, done) => {
done(null, asSerializedUser(user))
})
passport.deserializeUser((user, done) => {
done(null, user)
})
const AuthHeaderTokenRegex = /^Bearer\s+(.*)$/i
/**
* Middleware for authenticating user with an API Key.
* API Key can be provided as a bearer token or
* a query parameter (overrides token).
* @param {Request} req request
* @param {Response} res response
* @param {function} next callback
*/
function apiKeyAuthMiddleware(req, res, next) {
const { apiKey: apiKeyQueryParameterValue } = req.query
const apiKeyHeaderValue = get(get(req.headers, 'authorization', '').match(AuthHeaderTokenRegex), 1)
const apiKey = apiKeyQueryParameterValue || apiKeyHeaderValue
if (!isString(apiKey)) return next(createError(403, 'No API Key Provided ("apiKey")'))
return User.getByApiKey(apiKey, (err, user) => {
if (err) return next(err)
if (!user) return next(createError(403, 'Invalid API Key'))
req.user = asSerializedUser(user)
return next()
})
}
module.exports = {
passport,
apiKeyAuthMiddleware
}