| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121 |
- module.exports = function(crowi, app) {
- 'use strict';
- var debug = require('debug')('crowi:routes:login-passport')
- , passport = require('passport')
- , config = crowi.getConfig()
- , Config = crowi.model('Config')
- , passportService = crowi.passportService
- ;
- /**
- * success handler
- * @param {*} req
- * @param {*} res
- */
- function loginSuccess(req, res) {
- debug('loginSuccess called');
- var jumpTo = req.session.jumpTo;
- if (jumpTo) {
- req.session.jumpTo = null;
- return res.redirect(jumpTo);
- } else {
- return res.redirect('/');
- }
- };
- /**
- * failure handler
- * @param {*} req
- * @param {*} res
- */
- const loginFailure = (req, res, next) => {
- req.flash('warningMessage', 'Sign in failure.');
- return res.redirect('/login');
- };
- /**
- * middleware that login with LdapStrategy
- * @param {*} req
- * @param {*} res
- * @param {*} next
- */
- const loginWithLdap = (req, res, next) => {
- if (!passportService.isLdapStrategySetup) {
- debug('LdapStrategy has not been set up');
- return next();
- }
- const loginForm = req.body.loginForm;
- if (!req.form.isValid) {
- debug("invalid form");
- return res.render('login', {
- });
- }
- passport.authenticate('ldapauth', (err, user, info) => {
- if (res.headersSent) { // dirty hack -- 2017.09.25
- return; // cz: somehow passport.authenticate called twice when ECONNREFUSED error occurred
- }
- debug('--- authenticate with LdapStrategy ---');
- debug('user', user);
- debug('info', info);
- if (err) { // DB Error
- console.log('LDAP Server Error: ', err);
- req.flash('warningMessage', 'LDAP Server Error occured.');
- return next(); // pass and the flash message is displayed when all of authentications are failed.
- }
- if (!user) { return next(); }
- req.logIn(user, (err) => {
- if (err) { return next(); }
- else {
- return loginSuccess(req, res);
- }
- });
- })(req, res, next);
- }
- /**
- * middleware that login with LocalStrategy
- * @param {*} req
- * @param {*} res
- * @param {*} next
- */
- const loginWithLocal = (req, res, next) => {
- const loginForm = req.body.loginForm;
- if (!req.form.isValid) {
- return res.render('login', {
- });
- }
- passport.authenticate('local', (err, user, info) => {
- debug('--- authenticate with LocalStrategy ---');
- debug('user', user);
- debug('info', info);
- if (err) { // DB Error
- console.log('Database Server Error: ', err);
- req.flash('warningMessage', 'Database Server Error occured.');
- return next(); // pass and the flash message is displayed when all of authentications are failed.
- }
- if (!user) { return next(); }
- req.logIn(user, (err) => {
- if (err) { return next(); }
- else {
- return loginSuccess(req, res);
- }
- });
- })(req, res, next);
- }
- return {
- loginFailure,
- loginWithLdap,
- loginWithLocal,
- };
- };
|