line endings? #3343

v1.18.x
Julian Lam 10 years ago
parent d095e5925c
commit c781e55ea9

@ -1,86 +1,86 @@
(function(Auth) { (function(Auth) {
"use strict"; "use strict";
var passport = require('passport'), var passport = require('passport'),
passportLocal = require('passport-local').Strategy, passportLocal = require('passport-local').Strategy,
nconf = require('nconf'), nconf = require('nconf'),
winston = require('winston'), winston = require('winston'),
express = require('express'), express = require('express'),
controllers = require('../controllers'), controllers = require('../controllers'),
plugins = require('../plugins'), plugins = require('../plugins'),
hotswap = require('../hotswap'), hotswap = require('../hotswap'),
loginStrategies = []; loginStrategies = [];
Auth.initialize = function(app, middleware) { Auth.initialize = function(app, middleware) {
app.use(passport.initialize()); app.use(passport.initialize());
app.use(passport.session()); app.use(passport.session());
app.use(function(req, res, next) { app.use(function(req, res, next) {
req.uid = req.user ? parseInt(req.user.uid, 10) : 0; req.uid = req.user ? parseInt(req.user.uid, 10) : 0;
next(); next();
}); });
Auth.app = app; Auth.app = app;
Auth.middleware = middleware; Auth.middleware = middleware;
}; };
Auth.getLoginStrategies = function() { Auth.getLoginStrategies = function() {
return loginStrategies; return loginStrategies;
}; };
Auth.reloadRoutes = function(callback) { Auth.reloadRoutes = function(callback) {
var router = express.Router(); var router = express.Router();
router.hotswapId = 'auth'; router.hotswapId = 'auth';
loginStrategies.length = 0; loginStrategies.length = 0;
if (plugins.hasListeners('action:auth.overrideLogin')) { if (plugins.hasListeners('action:auth.overrideLogin')) {
winston.warn('[authentication] Login override detected, skipping local login strategy.'); winston.warn('[authentication] Login override detected, skipping local login strategy.');
plugins.fireHook('action:auth.overrideLogin'); plugins.fireHook('action:auth.overrideLogin');
} else { } else {
passport.use(new passportLocal({passReqToCallback: true}, controllers.authentication.localLogin)); passport.use(new passportLocal({passReqToCallback: true}, controllers.authentication.localLogin));
} }
plugins.fireHook('filter:auth.init', loginStrategies, function(err) { plugins.fireHook('filter:auth.init', loginStrategies, function(err) {
if (err) { if (err) {
winston.error('filter:auth.init - plugin failure'); winston.error('filter:auth.init - plugin failure');
return callback(err); return callback(err);
} }
loginStrategies.forEach(function(strategy) { loginStrategies.forEach(function(strategy) {
if (strategy.url) { if (strategy.url) {
router.get(strategy.url, passport.authenticate(strategy.name, { router.get(strategy.url, passport.authenticate(strategy.name, {
scope: strategy.scope scope: strategy.scope
})); }));
} }
router.get(strategy.callbackURL, passport.authenticate(strategy.name, { router.get(strategy.callbackURL, passport.authenticate(strategy.name, {
successReturnToOrRedirect: nconf.get('relative_path') + (strategy.successUrl !== undefined ? strategy.successUrl : '/'), successReturnToOrRedirect: nconf.get('relative_path') + (strategy.successUrl !== undefined ? strategy.successUrl : '/'),
failureRedirect: nconf.get('relative_path') + (strategy.failureUrl !== undefined ? strategy.failureUrl : '/login') failureRedirect: nconf.get('relative_path') + (strategy.failureUrl !== undefined ? strategy.failureUrl : '/login')
})); }));
}); });
router.post('/register', Auth.middleware.applyCSRF, controllers.authentication.register); router.post('/register', Auth.middleware.applyCSRF, controllers.authentication.register);
router.post('/login', Auth.middleware.applyCSRF, controllers.authentication.login); router.post('/login', Auth.middleware.applyCSRF, controllers.authentication.login);
router.post('/logout', Auth.middleware.applyCSRF, controllers.authentication.logout); router.post('/logout', Auth.middleware.applyCSRF, controllers.authentication.logout);
hotswap.replace('auth', router); hotswap.replace('auth', router);
if (typeof callback === 'function') { if (typeof callback === 'function') {
callback(); callback();
} }
}); });
}; };
passport.serializeUser(function(user, done) { passport.serializeUser(function(user, done) {
done(null, user.uid); done(null, user.uid);
}); });
passport.deserializeUser(function(uid, done) { passport.deserializeUser(function(uid, done) {
done(null, { done(null, {
uid: uid uid: uid
}); });
}); });
}(exports)); }(exports));

Loading…
Cancel
Save