|
|
@ -1,5 +1,6 @@
|
|
|
|
'use strict'
|
|
|
|
'use strict'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const Promise = require('bluebird')
|
|
|
|
const express = require('express')
|
|
|
|
const express = require('express')
|
|
|
|
const router = express.Router()
|
|
|
|
const router = express.Router()
|
|
|
|
const passport = require('passport')
|
|
|
|
const passport = require('passport')
|
|
|
@ -37,24 +38,51 @@ router.get('/login', function (req, res, next) {
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
router.post('/login', bruteforce.prevent, function (req, res, next) {
|
|
|
|
router.post('/login', bruteforce.prevent, function (req, res, next) {
|
|
|
|
passport.authenticate('local', function (err, user, info) {
|
|
|
|
new Promise((resolve, reject) => {
|
|
|
|
if (err) { return next(err) }
|
|
|
|
// [1] LOCAL AUTHENTICATION
|
|
|
|
|
|
|
|
passport.authenticate('local', function (err, user, info) {
|
|
|
|
if (!user) {
|
|
|
|
if (err) { return reject(err) }
|
|
|
|
req.flash('alert', {
|
|
|
|
if (!user) { return reject(new Error('INVALID_LOGIN')) }
|
|
|
|
title: 'Invalid login',
|
|
|
|
resolve(user)
|
|
|
|
message: 'The email or password is invalid.'
|
|
|
|
})(req, res, next)
|
|
|
|
|
|
|
|
}).catch({ message: 'INVALID_LOGIN' }, err => {
|
|
|
|
|
|
|
|
if (appconfig.auth.ldap && appconfig.auth.ldap.enabled) {
|
|
|
|
|
|
|
|
// [2] LDAP AUTHENTICATION
|
|
|
|
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
|
|
|
|
passport.authenticate('ldapauth', function (err, user, info) {
|
|
|
|
|
|
|
|
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)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
return res.redirect('/login')
|
|
|
|
} else {
|
|
|
|
|
|
|
|
throw err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}).then((user) => {
|
|
|
|
req.logIn(user, function (err) {
|
|
|
|
// LOGIN SUCCESS
|
|
|
|
|
|
|
|
return req.logIn(user, function (err) {
|
|
|
|
if (err) { return next(err) }
|
|
|
|
if (err) { return next(err) }
|
|
|
|
req.brute.reset(function () {
|
|
|
|
req.brute.reset(function () {
|
|
|
|
return res.redirect('/')
|
|
|
|
return res.redirect('/')
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})(req, res, next)
|
|
|
|
}).catch(err => {
|
|
|
|
|
|
|
|
// LOGIN FAIL
|
|
|
|
|
|
|
|
if (err.message === 'INVALID_LOGIN') {
|
|
|
|
|
|
|
|
req.flash('alert', {
|
|
|
|
|
|
|
|
title: 'Invalid login',
|
|
|
|
|
|
|
|
message: 'The email or password is invalid.'
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
return res.redirect('/login')
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
req.flash('alert', {
|
|
|
|
|
|
|
|
title: 'Login error',
|
|
|
|
|
|
|
|
message: err.message
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
return res.redirect('/login')
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|