wikijs-fork/server/controllers/auth.js

115 lines
4.1 KiB
JavaScript
Raw Normal View History

/* global wiki */
2017-03-12 04:38:47 +00:00
const Promise = require('bluebird')
2017-02-09 01:52:37 +00:00
const express = require('express')
const router = express.Router()
const ExpressBrute = require('express-brute')
const ExpressBruteRedisStore = require('express-brute-redis')
2017-02-09 01:52:37 +00:00
const moment = require('moment')
2017-09-12 01:52:10 +00:00
const _ = require('lodash')
2016-08-17 03:56:08 +00:00
/**
* Setup Express-Brute
*/
const EBstore = new ExpressBruteRedisStore({
client: wiki.redis
})
2017-02-09 01:52:37 +00:00
const bruteforce = new ExpressBrute(EBstore, {
freeRetries: 5,
minWait: 60 * 1000,
maxWait: 5 * 60 * 1000,
refreshTimeoutOnRequest: false,
failCallback (req, res, next, nextValidRequestDate) {
req.flash('alert', {
class: 'error',
title: wiki.lang.t('auth:errors.toomanyattempts'),
message: wiki.lang.t('auth:errors.toomanyattemptsmsg', { time: moment(nextValidRequestDate).fromNow() }),
2017-02-09 01:52:37 +00:00
iconClass: 'fa-times'
})
res.redirect('/login')
}
})
2016-08-17 03:56:08 +00:00
/**
* Login form
*/
2017-02-09 01:52:37 +00:00
router.get('/login', function (req, res, next) {
res.render('auth/login')
2017-02-09 01:52:37 +00:00
})
2016-08-17 03:56:08 +00:00
2017-02-09 01:52:37 +00:00
router.post('/login', bruteforce.prevent, function (req, res, next) {
2017-03-12 04:38:47 +00:00
new Promise((resolve, reject) => {
// [1] LOCAL AUTHENTICATION
2017-09-12 01:38:23 +00:00
wiki.auth.passport.authenticate('local', function (err, user, info) {
2017-03-12 04:38:47 +00:00
if (err) { return reject(err) }
if (!user) { return reject(new Error('INVALID_LOGIN')) }
resolve(user)
})(req, res, next)
}).catch({ message: 'INVALID_LOGIN' }, err => {
2017-09-12 01:38:23 +00:00
if (_.has(wiki.config.auth.strategy, 'ldap')) {
2017-03-12 04:38:47 +00:00
// [2] LDAP AUTHENTICATION
return new Promise((resolve, reject) => {
2017-09-12 01:38:23 +00:00
wiki.auth.passport.authenticate('ldapauth', function (err, user, info) {
2017-03-12 04:38:47 +00:00
if (err) { return reject(err) }
if (info && info.message) { return reject(new Error(info.message)) }
if (!user) { return reject(new Error('INVALID_LOGIN')) }
resolve(user)
})(req, res, next)
2017-02-09 01:52:37 +00:00
})
2017-03-12 04:38:47 +00:00
} else {
throw err
2017-02-09 01:52:37 +00:00
}
2017-03-12 04:38:47 +00:00
}).then((user) => {
// LOGIN SUCCESS
return req.logIn(user, function (err) {
2017-02-09 01:52:37 +00:00
if (err) { return next(err) }
2016-10-31 03:03:36 +00:00
req.brute.reset(function () {
2017-02-09 01:52:37 +00:00
return res.redirect('/')
})
}) || true
2017-03-12 04:38:47 +00:00
}).catch(err => {
// LOGIN FAIL
if (err.message === 'INVALID_LOGIN') {
req.flash('alert', {
title: wiki.lang.t('auth:errors.invalidlogin'),
message: wiki.lang.t('auth:errors.invalidloginmsg')
2017-03-12 04:38:47 +00:00
})
return res.redirect('/login')
} else {
req.flash('alert', {
title: wiki.lang.t('auth:errors.loginerror'),
2017-03-12 04:38:47 +00:00
message: err.message
})
return res.redirect('/login')
}
})
2017-02-09 01:52:37 +00:00
})
2016-08-17 03:56:08 +00:00
/**
* Social Login
*/
2017-09-12 01:38:23 +00:00
router.get('/login/ms', wiki.auth.passport.authenticate('windowslive', { scope: ['wl.signin', 'wl.basic', 'wl.emails'] }))
router.get('/login/google', wiki.auth.passport.authenticate('google', { scope: ['profile', 'email'] }))
router.get('/login/facebook', wiki.auth.passport.authenticate('facebook', { scope: ['public_profile', 'email'] }))
router.get('/login/github', wiki.auth.passport.authenticate('github', { scope: ['user:email'] }))
router.get('/login/slack', wiki.auth.passport.authenticate('slack', { scope: ['identity.basic', 'identity.email'] }))
router.get('/login/azure', wiki.auth.passport.authenticate('azure_ad_oauth2'))
2017-09-12 01:38:23 +00:00
router.get('/login/ms/callback', wiki.auth.passport.authenticate('windowslive', { failureRedirect: '/login', successRedirect: '/' }))
router.get('/login/google/callback', wiki.auth.passport.authenticate('google', { failureRedirect: '/login', successRedirect: '/' }))
router.get('/login/facebook/callback', wiki.auth.passport.authenticate('facebook', { failureRedirect: '/login', successRedirect: '/' }))
router.get('/login/github/callback', wiki.auth.passport.authenticate('github', { failureRedirect: '/login', successRedirect: '/' }))
router.get('/login/slack/callback', wiki.auth.passport.authenticate('slack', { failureRedirect: '/login', successRedirect: '/' }))
router.get('/login/azure/callback', wiki.auth.passport.authenticate('azure_ad_oauth2', { failureRedirect: '/login', successRedirect: '/' }))
2016-08-17 03:56:08 +00:00
/**
* Logout
*/
2017-02-09 01:52:37 +00:00
router.get('/logout', function (req, res) {
req.logout()
res.redirect('/')
})
2016-08-17 03:56:08 +00:00
2017-02-09 01:52:37 +00:00
module.exports = router