-
Notifications
You must be signed in to change notification settings - Fork 2.8k
/
authentication.js
173 lines (147 loc) · 5.16 KB
/
authentication.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
'use strict';
const async = require('async');
const passport = require('passport');
const passportLocal = require('passport-local').Strategy;
const BearerStrategy = require('passport-http-bearer').Strategy;
const winston = require('winston');
const meta = require('../meta');
const controllers = require('../controllers');
const helpers = require('../controllers/helpers');
const plugins = require('../plugins');
let loginStrategies = [];
const Auth = module.exports;
Auth.initialize = function (app, middleware) {
app.use(passport.initialize());
app.use(passport.session());
app.use((req, res, next) => {
Auth.setAuthVars(req, res);
next();
});
Auth.app = app;
Auth.middleware = middleware;
};
Auth.setAuthVars = function setAuthVars(req) {
const isSpider = req.isSpider();
req.loggedIn = !isSpider && !!req.user;
if (req.user) {
req.uid = parseInt(req.user.uid, 10);
} else if (isSpider) {
req.uid = -1;
} else {
req.uid = 0;
}
};
Auth.getLoginStrategies = function () {
return loginStrategies;
};
Auth.verifyToken = async function (token, done) {
const { tokens = [] } = await meta.settings.get('core.api');
const tokenObj = tokens.find(t => t.token === token);
const uid = tokenObj ? tokenObj.uid : undefined;
if (uid !== undefined) {
if (parseInt(uid, 10) > 0) {
done(null, {
uid: uid,
});
} else {
done(null, {
master: true,
});
}
} else {
done(false);
}
};
Auth.reloadRoutes = async function (params) {
loginStrategies.length = 0;
const { router } = params;
// Local Logins
if (plugins.hooks.hasListeners('action:auth.overrideLogin')) {
winston.warn('[authentication] Login override detected, skipping local login strategy.');
plugins.hooks.fire('action:auth.overrideLogin');
} else {
passport.use(new passportLocal({ passReqToCallback: true }, controllers.authentication.localLogin));
}
// HTTP bearer authentication
passport.use('core.api', new BearerStrategy({}, Auth.verifyToken));
// Additional logins via SSO plugins
try {
loginStrategies = await plugins.hooks.fire('filter:auth.init', loginStrategies);
} catch (err) {
winston.error(`[authentication] ${err.stack}`);
}
loginStrategies = loginStrategies || [];
loginStrategies.forEach((strategy) => {
if (strategy.url) {
router[strategy.urlMethod || 'get'](strategy.url, Auth.middleware.applyCSRF, async (req, res, next) => {
let opts = {
scope: strategy.scope,
prompt: strategy.prompt || undefined,
};
if (strategy.checkState !== false) {
req.session.ssoState = req.csrfToken && req.csrfToken();
opts.state = req.session.ssoState;
}
// Allow SSO plugins to override/append options (for use in passport prototype authorizationParams)
({ opts } = await plugins.hooks.fire('filter:auth.options', { req, res, opts }));
passport.authenticate(strategy.name, opts)(req, res, next);
});
}
router[strategy.callbackMethod || 'get'](strategy.callbackURL, (req, res, next) => {
// Ensure the passed-back state value is identical to the saved ssoState (unless explicitly skipped)
if (strategy.checkState === false) {
return next();
}
next(req.query.state !== req.session.ssoState ? new Error('[[error:csrf-invalid]]') : null);
}, (req, res, next) => {
// Trigger registration interstitial checks
req.session.registration = req.session.registration || {};
// save returnTo for later usage in /register/complete
// passport seems to remove `req.session.returnTo` after it redirects
req.session.registration.returnTo = req.session.returnTo;
passport.authenticate(strategy.name, (err, user) => {
if (err) {
if (req.session && req.session.registration) {
delete req.session.registration;
}
return next(err);
}
if (!user) {
if (req.session && req.session.registration) {
delete req.session.registration;
}
return helpers.redirect(res, strategy.failureUrl !== undefined ? strategy.failureUrl : '/login');
}
res.locals.user = user;
res.locals.strategy = strategy;
next();
})(req, res, next);
}, Auth.middleware.validateAuth, (req, res, next) => {
async.waterfall([
async.apply(req.login.bind(req), res.locals.user),
async.apply(controllers.authentication.onSuccessfulLogin, req, req.uid),
], (err) => {
if (err) {
return next(err);
}
helpers.redirect(res, strategy.successUrl !== undefined ? strategy.successUrl : '/');
});
});
});
const multipart = require('connect-multiparty');
const multipartMiddleware = multipart();
const middlewares = [multipartMiddleware, Auth.middleware.applyCSRF, Auth.middleware.applyBlacklist];
router.post('/register', middlewares, controllers.authentication.register);
router.post('/register/complete', middlewares, controllers.authentication.registerComplete);
router.post('/register/abort', controllers.authentication.registerAbort);
router.post('/login', Auth.middleware.applyCSRF, Auth.middleware.applyBlacklist, controllers.authentication.login);
router.post('/logout', Auth.middleware.applyCSRF, controllers.authentication.logout);
};
passport.serializeUser((user, done) => {
done(null, user.uid);
});
passport.deserializeUser((uid, done) => {
done(null, {
uid: uid,
});
});