login-passport.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. module.exports = function(crowi, app) {
  2. 'use strict';
  3. var debug = require('debug')('crowi:routes:login-passport')
  4. , passport = require('passport')
  5. , config = crowi.getConfig()
  6. , Config = crowi.model('Config')
  7. , passportService = crowi.passportService
  8. ;
  9. /**
  10. * success handler
  11. * @param {*} req
  12. * @param {*} res
  13. */
  14. function loginSuccess(req, res) {
  15. debug('loginSuccess called');
  16. var jumpTo = req.session.jumpTo;
  17. if (jumpTo) {
  18. req.session.jumpTo = null;
  19. return res.redirect(jumpTo);
  20. } else {
  21. return res.redirect('/');
  22. }
  23. };
  24. /**
  25. * failure handler
  26. * @param {*} req
  27. * @param {*} res
  28. */
  29. const loginFailure = (req, res, next) => {
  30. req.flash('warningMessage', 'Sign in failure.');
  31. return res.redirect('/login');
  32. };
  33. /**
  34. * middleware that login with LdapStrategy
  35. * @param {*} req
  36. * @param {*} res
  37. * @param {*} next
  38. */
  39. const loginWithLdap = (req, res, next) => {
  40. if (!passportService.isLdapStrategySetup) {
  41. debug('LdapStrategy has not been set up');
  42. return next();
  43. }
  44. const loginForm = req.body.loginForm;
  45. if (!req.form.isValid) {
  46. debug("invalid form");
  47. return res.render('login', {
  48. });
  49. }
  50. passport.authenticate('ldapauth', (err, user, info) => {
  51. if (res.headersSent) { // dirty hack -- 2017.09.25
  52. return; // cz: somehow passport.authenticate called twice when ECONNREFUSED error occurred
  53. }
  54. debug('--- authenticate with LdapStrategy ---');
  55. debug('user', user);
  56. debug('info', info);
  57. if (err) { // DB Error
  58. console.log('LDAP Server Error: ', err);
  59. req.flash('warningMessage', 'LDAP Server Error occured.');
  60. return next(); // pass and the flash message is displayed when all of authentications are failed.
  61. }
  62. if (!user) { return next(); }
  63. req.logIn(user, (err) => {
  64. if (err) { return next(); }
  65. else {
  66. return loginSuccess(req, res);
  67. }
  68. });
  69. })(req, res, next);
  70. }
  71. /**
  72. * middleware that login with LocalStrategy
  73. * @param {*} req
  74. * @param {*} res
  75. * @param {*} next
  76. */
  77. const loginWithLocal = (req, res, next) => {
  78. const loginForm = req.body.loginForm;
  79. if (!req.form.isValid) {
  80. return res.render('login', {
  81. });
  82. }
  83. passport.authenticate('local', (err, user, info) => {
  84. debug('--- authenticate with LocalStrategy ---');
  85. debug('user', user);
  86. debug('info', info);
  87. if (err) { // DB Error
  88. console.log('Database Server Error: ', err);
  89. req.flash('warningMessage', 'Database Server Error occured.');
  90. return next(); // pass and the flash message is displayed when all of authentications are failed.
  91. }
  92. if (!user) { return next(); }
  93. req.logIn(user, (err) => {
  94. if (err) { return next(); }
  95. else {
  96. return loginSuccess(req, res);
  97. }
  98. });
  99. })(req, res, next);
  100. }
  101. return {
  102. loginFailure,
  103. loginWithLdap,
  104. loginWithLocal,
  105. };
  106. };