Fix for opds with LDAP authentication

pull/976/head
Ozzieisaacs 5 years ago
parent e734bb120a
commit 405a3909b0

@ -19,14 +19,13 @@
from __future__ import division, print_function, unicode_literals from __future__ import division, print_function, unicode_literals
import base64 import base64
from flask_simpleldap import LDAP from flask_simpleldap import LDAP, LDAPException
from ldap import SERVER_DOWN, INVALID_CREDENTIALS
from .. import constants, logger from .. import constants, logger
log = logger.create() log = logger.create()
_ldap = None _ldap = LDAP()
def init_app(app, config): def init_app(app, config):
@ -53,7 +52,8 @@ def init_app(app, config):
# app.config['LDAP_BASE_DN'] = 'ou=users,dc=yunohost,dc=org' # app.config['LDAP_BASE_DN'] = 'ou=users,dc=yunohost,dc=org'
# app.config['LDAP_USER_OBJECT_FILTER'] = '(uid=%s)' # app.config['LDAP_USER_OBJECT_FILTER'] = '(uid=%s)'
_ldap = LDAP(app) _ldap.init_app(app)
def basic_auth_required(func): def basic_auth_required(func):
@ -61,6 +61,7 @@ def basic_auth_required(func):
def bind_user(username, password): def bind_user(username, password):
# ulf= _ldap.get_object_details('admin')
'''Attempts a LDAP login. '''Attempts a LDAP login.
:returns: True if login succeeded, False if login failed, None if server unavailable. :returns: True if login succeeded, False if login failed, None if server unavailable.
@ -69,9 +70,13 @@ def bind_user(username, password):
result = _ldap.bind_user(username, password) result = _ldap.bind_user(username, password)
log.debug("LDAP login '%s': %r", username, result) log.debug("LDAP login '%s': %r", username, result)
return result is not None return result is not None
except SERVER_DOWN as ex: except LDAPException as ex:
log.warning('LDAP Server down: %s', ex) if ex.message == 'Invalid credentials':
return None log.info("LDAP login '%s' failed: %s", username, ex)
except INVALID_CREDENTIALS as ex: return False
log.info("LDAP login '%s' failed: %s", username, ex) if ex.message == "Can't contact LDAP server":
return False log.warning('LDAP Server down: %s', ex)
return None
else:
log.warning('LDAP Server error: %s', ex.message)
return None

Loading…
Cancel
Save