2017-04-11 21:41:14 +00:00
|
|
|
'use strict'
|
|
|
|
|
|
|
|
const Router = require('express').Router
|
|
|
|
const passport = require('passport')
|
|
|
|
const LDAPStrategy = require('passport-ldapauth')
|
|
|
|
const config = require('../../../config')
|
|
|
|
const models = require('../../../models')
|
|
|
|
const logger = require('../../../logger')
|
2019-05-30 22:27:56 +00:00
|
|
|
const { setReturnToFromReferer } = require('../utils')
|
|
|
|
const { urlencodedParser } = require('../../utils')
|
2017-04-11 21:41:14 +00:00
|
|
|
const response = require('../../../response')
|
|
|
|
|
|
|
|
let ldapAuth = module.exports = Router()
|
|
|
|
|
|
|
|
passport.use(new LDAPStrategy({
|
|
|
|
server: {
|
|
|
|
url: config.ldap.url || null,
|
2018-06-30 13:04:15 +00:00
|
|
|
bindDN: config.ldap.bindDn || null,
|
2017-04-11 21:41:14 +00:00
|
|
|
bindCredentials: config.ldap.bindCredentials || null,
|
|
|
|
searchBase: config.ldap.searchBase || null,
|
|
|
|
searchFilter: config.ldap.searchFilter || null,
|
|
|
|
searchAttributes: config.ldap.searchAttributes || null,
|
|
|
|
tlsOptions: config.ldap.tlsOptions || null
|
|
|
|
}
|
|
|
|
}, function (user, done) {
|
2018-03-17 20:56:52 +00:00
|
|
|
var uuid = user.uidNumber || user.uid || user.sAMAccountName || undefined
|
2018-03-01 22:51:47 +00:00
|
|
|
if (config.ldap.useridField && user[config.ldap.useridField]) {
|
|
|
|
uuid = user[config.ldap.useridField]
|
|
|
|
}
|
2017-12-09 10:17:06 +00:00
|
|
|
|
2018-03-17 20:56:52 +00:00
|
|
|
if (typeof uuid === 'undefined') {
|
|
|
|
throw new Error('Could not determine UUID for LDAP user. Check that ' +
|
|
|
|
'either uidNumber, uid or sAMAccountName is set in your LDAP directory ' +
|
|
|
|
'or use another unique attribute and configure it using the ' +
|
|
|
|
'"useridField" option in ldap settings.')
|
|
|
|
}
|
|
|
|
|
2018-03-01 22:51:47 +00:00
|
|
|
var username = uuid
|
2017-12-09 10:17:06 +00:00
|
|
|
if (config.ldap.usernameField && user[config.ldap.usernameField]) {
|
|
|
|
username = user[config.ldap.usernameField]
|
|
|
|
}
|
|
|
|
|
2017-04-11 21:41:14 +00:00
|
|
|
var profile = {
|
2017-10-06 23:49:43 +00:00
|
|
|
id: 'LDAP-' + uuid,
|
2017-12-09 10:17:06 +00:00
|
|
|
username: username,
|
2017-04-11 21:41:14 +00:00
|
|
|
displayName: user.displayName,
|
2018-03-09 13:37:03 +00:00
|
|
|
emails: user.mail ? Array.isArray(user.mail) ? user.mail : [user.mail] : [],
|
2017-04-11 21:41:14 +00:00
|
|
|
avatarUrl: null,
|
|
|
|
profileUrl: null,
|
|
|
|
provider: 'ldap'
|
|
|
|
}
|
|
|
|
var stringifiedProfile = JSON.stringify(profile)
|
|
|
|
models.User.findOrCreate({
|
|
|
|
where: {
|
|
|
|
profileid: profile.id.toString()
|
|
|
|
},
|
|
|
|
defaults: {
|
|
|
|
profile: stringifiedProfile
|
|
|
|
}
|
|
|
|
}).spread(function (user, created) {
|
|
|
|
if (user) {
|
|
|
|
var needSave = false
|
|
|
|
if (user.profile !== stringifiedProfile) {
|
|
|
|
user.profile = stringifiedProfile
|
|
|
|
needSave = true
|
|
|
|
}
|
|
|
|
if (needSave) {
|
|
|
|
user.save().then(function () {
|
|
|
|
if (config.debug) { logger.debug('user login: ' + user.id) }
|
|
|
|
return done(null, user)
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
if (config.debug) { logger.debug('user login: ' + user.id) }
|
|
|
|
return done(null, user)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}).catch(function (err) {
|
|
|
|
logger.error('ldap auth failed: ' + err)
|
|
|
|
return done(err, null)
|
|
|
|
})
|
|
|
|
}))
|
|
|
|
|
|
|
|
ldapAuth.post('/auth/ldap', urlencodedParser, function (req, res, next) {
|
|
|
|
if (!req.body.username || !req.body.password) return response.errorBadRequest(res)
|
|
|
|
setReturnToFromReferer(req)
|
|
|
|
passport.authenticate('ldapauth', {
|
2018-03-07 14:17:35 +00:00
|
|
|
successReturnToOrRedirect: config.serverURL + '/',
|
|
|
|
failureRedirect: config.serverURL + '/',
|
2017-04-11 21:41:14 +00:00
|
|
|
failureFlash: true
|
|
|
|
})(req, res, next)
|
|
|
|
})
|