2020-04-18 00:25:33 +00:00
|
|
|
const LDAPMiddleware = require('./LDAPMiddleware')
|
|
|
|
const LDAP = require('ldapjs')
|
|
|
|
|
|
|
|
class BindUserMiddleware extends LDAPMiddleware {
|
|
|
|
static get services() {
|
|
|
|
return [...super.services, 'canon', 'output', 'ldap_server']
|
|
|
|
}
|
|
|
|
|
|
|
|
async test(req, res, next) {
|
|
|
|
const bind_dn = req.connection.ldap.bindDN
|
|
|
|
|
|
|
|
if ( bind_dn.equals(this.ldap_server.anonymous()) ) {
|
|
|
|
this.output.warn(`Blocked anonymous LDAP request on user-protected route.`)
|
|
|
|
return next(new LDAP.InsufficientAccessRightsError())
|
|
|
|
}
|
|
|
|
|
2020-04-21 03:46:19 +00:00
|
|
|
const user = await this.user_controller().get_resource_from_dn(bind_dn)
|
2020-04-18 00:25:33 +00:00
|
|
|
if ( !user || !user.can('ldap:bind') ) {
|
|
|
|
return next(new LDAP.InvalidCredentialsError())
|
|
|
|
}
|
|
|
|
|
|
|
|
req.user = user
|
|
|
|
req.bindDN = bind_dn
|
|
|
|
|
|
|
|
return next()
|
|
|
|
}
|
|
|
|
|
|
|
|
user_controller() {
|
|
|
|
return this.canon.get('ldap_controller::Users')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = exports = BindUserMiddleware
|