You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

116 lines
4.0 KiB

  1. 'use strict'
  2. /* global db, lang */
  3. const Promise = require('bluebird')
  4. const express = require('express')
  5. const router = express.Router()
  6. const passport = require('passport')
  7. const ExpressBrute = require('express-brute')
  8. const ExpressBruteMongooseStore = require('express-brute-mongoose')
  9. const moment = require('moment')
  10. /**
  11. * Setup Express-Brute
  12. */
  13. const EBstore = new ExpressBruteMongooseStore(db.Bruteforce)
  14. const bruteforce = new ExpressBrute(EBstore, {
  15. freeRetries: 5,
  16. minWait: 60 * 1000,
  17. maxWait: 5 * 60 * 1000,
  18. refreshTimeoutOnRequest: false,
  19. failCallback (req, res, next, nextValidRequestDate) {
  20. req.flash('alert', {
  21. class: 'error',
  22. title: lang.t('auth:errors.toomanyattempts'),
  23. message: lang.t('auth:errors.toomanyattemptsmsg', { time: moment(nextValidRequestDate).fromNow() }),
  24. iconClass: 'fa-times'
  25. })
  26. res.redirect('/login')
  27. }
  28. })
  29. /**
  30. * Login form
  31. */
  32. router.get('/login', function (req, res, next) {
  33. res.render('auth/login', {
  34. usr: res.locals.usr
  35. })
  36. })
  37. router.post('/login', bruteforce.prevent, function (req, res, next) {
  38. new Promise((resolve, reject) => {
  39. // [1] LOCAL AUTHENTICATION
  40. passport.authenticate('local', function (err, user, info) {
  41. if (err) { return reject(err) }
  42. if (!user) { return reject(new Error('INVALID_LOGIN')) }
  43. resolve(user)
  44. })(req, res, next)
  45. }).catch({ message: 'INVALID_LOGIN' }, err => {
  46. if (appconfig.auth.ldap && appconfig.auth.ldap.enabled) {
  47. // [2] LDAP AUTHENTICATION
  48. return new Promise((resolve, reject) => {
  49. passport.authenticate('ldapauth', function (err, user, info) {
  50. if (err) { return reject(err) }
  51. if (info && info.message) { return reject(new Error(info.message)) }
  52. if (!user) { return reject(new Error('INVALID_LOGIN')) }
  53. resolve(user)
  54. })(req, res, next)
  55. })
  56. } else {
  57. throw err
  58. }
  59. }).then((user) => {
  60. // LOGIN SUCCESS
  61. return req.logIn(user, function (err) {
  62. if (err) { return next(err) }
  63. req.brute.reset(function () {
  64. return res.redirect('/')
  65. })
  66. }) || true
  67. }).catch(err => {
  68. // LOGIN FAIL
  69. if (err.message === 'INVALID_LOGIN') {
  70. req.flash('alert', {
  71. title: lang.t('auth:errors.invalidlogin'),
  72. message: lang.t('auth:errors.invalidloginmsg')
  73. })
  74. return res.redirect('/login')
  75. } else {
  76. req.flash('alert', {
  77. title: lang.t('auth:errors.loginerror'),
  78. message: err.message
  79. })
  80. return res.redirect('/login')
  81. }
  82. })
  83. })
  84. /**
  85. * Social Login
  86. */
  87. router.get('/login/ms', passport.authenticate('windowslive', { scope: ['wl.signin', 'wl.basic', 'wl.emails'] }))
  88. router.get('/login/google', passport.authenticate('google', { scope: ['profile', 'email'] }))
  89. router.get('/login/facebook', passport.authenticate('facebook', { scope: ['public_profile', 'email'] }))
  90. router.get('/login/github', passport.authenticate('github', { scope: ['user:email'] }))
  91. router.get('/login/slack', passport.authenticate('slack', { scope: ['identity.basic', 'identity.email'] }))
  92. router.get('/login/azure', passport.authenticate('azure_ad_oauth2'))
  93. router.get('/login/ms/callback', passport.authenticate('windowslive', { failureRedirect: '/login', successRedirect: '/' }))
  94. router.get('/login/google/callback', passport.authenticate('google', { failureRedirect: '/login', successRedirect: '/' }))
  95. router.get('/login/facebook/callback', passport.authenticate('facebook', { failureRedirect: '/login', successRedirect: '/' }))
  96. router.get('/login/github/callback', passport.authenticate('github', { failureRedirect: '/login', successRedirect: '/' }))
  97. router.get('/login/slack/callback', passport.authenticate('slack', { failureRedirect: '/login', successRedirect: '/' }))
  98. router.get('/login/azure/callback', passport.authenticate('azure_ad_oauth2', { failureRedirect: '/login', successRedirect: '/' }))
  99. /**
  100. * Logout
  101. */
  102. router.get('/logout', function (req, res) {
  103. req.logout()
  104. res.redirect('/')
  105. })
  106. module.exports = router