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.

118 lines
4.0 KiB

  1. 'use strict'
  2. /* global wiki */
  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 ExpressBruteRedisStore = require('express-brute-redis')
  9. const moment = require('moment')
  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('auth/login', {
  36. usr: res.locals.usr
  37. })
  38. })
  39. router.post('/login', bruteforce.prevent, function (req, res, next) {
  40. new Promise((resolve, reject) => {
  41. // [1] LOCAL AUTHENTICATION
  42. passport.authenticate('local', function (err, user, info) {
  43. if (err) { return reject(err) }
  44. if (!user) { return reject(new Error('INVALID_LOGIN')) }
  45. resolve(user)
  46. })(req, res, next)
  47. }).catch({ message: 'INVALID_LOGIN' }, err => {
  48. if (appconfig.auth.ldap && appconfig.auth.ldap.enabled) {
  49. // [2] LDAP AUTHENTICATION
  50. return new Promise((resolve, reject) => {
  51. passport.authenticate('ldapauth', function (err, user, info) {
  52. if (err) { return reject(err) }
  53. if (info && info.message) { return reject(new Error(info.message)) }
  54. if (!user) { return reject(new Error('INVALID_LOGIN')) }
  55. resolve(user)
  56. })(req, res, next)
  57. })
  58. } else {
  59. throw err
  60. }
  61. }).then((user) => {
  62. // LOGIN SUCCESS
  63. return req.logIn(user, function (err) {
  64. if (err) { return next(err) }
  65. req.brute.reset(function () {
  66. return res.redirect('/')
  67. })
  68. }) || true
  69. }).catch(err => {
  70. // LOGIN FAIL
  71. if (err.message === 'INVALID_LOGIN') {
  72. req.flash('alert', {
  73. title: wiki.lang.t('auth:errors.invalidlogin'),
  74. message: wiki.lang.t('auth:errors.invalidloginmsg')
  75. })
  76. return res.redirect('/login')
  77. } else {
  78. req.flash('alert', {
  79. title: wiki.lang.t('auth:errors.loginerror'),
  80. message: err.message
  81. })
  82. return res.redirect('/login')
  83. }
  84. })
  85. })
  86. /**
  87. * Social Login
  88. */
  89. router.get('/login/ms', passport.authenticate('windowslive', { scope: ['wl.signin', 'wl.basic', 'wl.emails'] }))
  90. router.get('/login/google', passport.authenticate('google', { scope: ['profile', 'email'] }))
  91. router.get('/login/facebook', passport.authenticate('facebook', { scope: ['public_profile', 'email'] }))
  92. router.get('/login/github', passport.authenticate('github', { scope: ['user:email'] }))
  93. router.get('/login/slack', passport.authenticate('slack', { scope: ['identity.basic', 'identity.email'] }))
  94. router.get('/login/azure', passport.authenticate('azure_ad_oauth2'))
  95. router.get('/login/ms/callback', passport.authenticate('windowslive', { failureRedirect: '/login', successRedirect: '/' }))
  96. router.get('/login/google/callback', passport.authenticate('google', { failureRedirect: '/login', successRedirect: '/' }))
  97. router.get('/login/facebook/callback', passport.authenticate('facebook', { failureRedirect: '/login', successRedirect: '/' }))
  98. router.get('/login/github/callback', passport.authenticate('github', { failureRedirect: '/login', successRedirect: '/' }))
  99. router.get('/login/slack/callback', passport.authenticate('slack', { failureRedirect: '/login', successRedirect: '/' }))
  100. router.get('/login/azure/callback', passport.authenticate('azure_ad_oauth2', { failureRedirect: '/login', successRedirect: '/' }))
  101. /**
  102. * Logout
  103. */
  104. router.get('/logout', function (req, res) {
  105. req.logout()
  106. res.redirect('/')
  107. })
  108. module.exports = router