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.

115 lines
4.1 KiB

  1. /* global WIKI */
  2. const Promise = require('bluebird')
  3. const express = require('express')
  4. const router = express.Router()
  5. const ExpressBrute = require('express-brute')
  6. const ExpressBruteRedisStore = require('express-brute-redis')
  7. const jwt = require('jsonwebtoken')
  8. const moment = require('moment')
  9. const _ = require('lodash')
  10. /**
  11. * Setup Express-Brute
  12. */
  13. const EBstore = new ExpressBruteRedisStore({
  14. client: WIKI.redis
  15. })
  16. const bruteforce = new ExpressBrute(EBstore, {
  17. freeRetries: 5,
  18. minWait: 60 * 1000,
  19. maxWait: 5 * 60 * 1000,
  20. refreshTimeoutOnRequest: false,
  21. failCallback (req, res, next, nextValidRequestDate) {
  22. req.flash('alert', {
  23. class: 'error',
  24. title: WIKI.lang.t('auth:errors.toomanyattempts'),
  25. message: WIKI.lang.t('auth:errors.toomanyattemptsmsg', { time: moment(nextValidRequestDate).fromNow() }),
  26. iconClass: 'fa-times'
  27. })
  28. res.redirect('/login')
  29. }
  30. })
  31. /**
  32. * Login form
  33. */
  34. router.get('/login', function (req, res, next) {
  35. res.render('login')
  36. })
  37. router.post('/login', bruteforce.prevent, function (req, res, next) {
  38. new Promise((resolve, reject) => {
  39. // [1] LOCAL AUTHENTICATION
  40. WIKI.auth.passport.authenticate('local', { session: false }, 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 (_.has(WIKI.config.auth.strategy, 'ldap')) {
  47. // [2] LDAP AUTHENTICATION
  48. return new Promise((resolve, reject) => {
  49. WIKI.auth.passport.authenticate('ldapauth', { session: false }, 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, { session: false }, function (err) {
  62. if (err) { return next(err) }
  63. req.brute.reset(function () {
  64. return res.redirect('/')
  65. })
  66. })
  67. }).catch(err => {
  68. // LOGIN FAIL
  69. if (err.message === 'INVALID_LOGIN') {
  70. req.flash('alert', {
  71. title: WIKI.lang.t('auth:errors.invalidlogin'),
  72. message: WIKI.lang.t('auth:errors.invalidloginmsg')
  73. })
  74. return res.redirect('/login')
  75. } else {
  76. req.flash('alert', {
  77. title: WIKI.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', WIKI.auth.passport.authenticate('windowslive', { scope: ['wl.signin', 'wl.basic', 'wl.emails'] }))
  88. router.get('/login/google', WIKI.auth.passport.authenticate('google', { scope: ['profile', 'email'] }))
  89. router.get('/login/facebook', WIKI.auth.passport.authenticate('facebook', { scope: ['public_profile', 'email'] }))
  90. router.get('/login/github', WIKI.auth.passport.authenticate('github', { scope: ['user:email'] }))
  91. router.get('/login/slack', WIKI.auth.passport.authenticate('slack', { scope: ['identity.basic', 'identity.email'] }))
  92. router.get('/login/azure', WIKI.auth.passport.authenticate('azure_ad_oauth2'))
  93. router.get('/login/ms/callback', WIKI.auth.passport.authenticate('windowslive', { failureRedirect: '/login', successRedirect: '/' }))
  94. router.get('/login/google/callback', WIKI.auth.passport.authenticate('google', { failureRedirect: '/login', successRedirect: '/' }))
  95. router.get('/login/facebook/callback', WIKI.auth.passport.authenticate('facebook', { failureRedirect: '/login', successRedirect: '/' }))
  96. router.get('/login/github/callback', WIKI.auth.passport.authenticate('github', { failureRedirect: '/login', successRedirect: '/' }))
  97. router.get('/login/slack/callback', WIKI.auth.passport.authenticate('slack', { failureRedirect: '/login', successRedirect: '/' }))
  98. router.get('/login/azure/callback', WIKI.auth.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