From 9646b6e2ddfccfce45a207a381b53e40c2e98d1f Mon Sep 17 00:00:00 2001 From: Ozzie Isaacs Date: Sun, 26 Mar 2023 11:29:54 +0200 Subject: [PATCH] Enable debug output for ldap login --- cps/services/simpleldap.py | 37 +++++++++++++++++++++++++++++++++++-- optional-requirements.txt | 6 +++--- 2 files changed, 38 insertions(+), 5 deletions(-) diff --git a/cps/services/simpleldap.py b/cps/services/simpleldap.py index 872538d1..40c117b0 100644 --- a/cps/services/simpleldap.py +++ b/cps/services/simpleldap.py @@ -20,6 +20,7 @@ import base64 from flask_simpleldap import LDAP, LDAPException from flask_simpleldap import ldap as pyLDAP +from flask import current_app from .. import constants, logger try: @@ -28,9 +29,41 @@ except ImportError: pass log = logger.create() -_ldap = LDAP() +class mySimpleLDap(LDAP): + + @staticmethod + def init_app(app): + super(mySimpleLDap, mySimpleLDap).init_app(app) + app.config.setdefault('LDAP_LOGLEVEL', 0) + + + @property + def initialize(self): + """Initialize a connection to the LDAP server. + + :return: LDAP connection object. + """ + try: + log_level = 2 if current_app.config['LDAP_LOGLEVEL'] == logger.logging.DEBUG else 0 + conn = pyLDAP.initialize('{0}://{1}:{2}'.format( + current_app.config['LDAP_SCHEMA'], + current_app.config['LDAP_HOST'], + current_app.config['LDAP_PORT']), trace_level=log_level) + conn.set_option(pyLDAP.OPT_NETWORK_TIMEOUT, + current_app.config['LDAP_TIMEOUT']) + conn = self._set_custom_options(conn) + conn.protocol_version = pyLDAP.VERSION3 + if current_app.config['LDAP_USE_TLS']: + conn.start_tls_s() + return conn + except pyLDAP.LDAPError as e: + raise LDAPException(self.error(e.args)) + + +_ldap = mySimpleLDap() + def init_app(app, config): if config.config_login_type != constants.LOGIN_LDAP: return @@ -70,7 +103,7 @@ def init_app(app, config): app.config['LDAP_OPENLDAP'] = bool(config.config_ldap_openldap) app.config['LDAP_GROUP_OBJECT_FILTER'] = config.config_ldap_group_object_filter app.config['LDAP_GROUP_MEMBERS_FIELD'] = config.config_ldap_group_members_field - + app.config['LDAP_LOGLEVEL'] = config.config_log_level try: _ldap.init_app(app) except ValueError: diff --git a/optional-requirements.txt b/optional-requirements.txt index 9da682d9..87be3dbe 100644 --- a/optional-requirements.txt +++ b/optional-requirements.txt @@ -1,8 +1,8 @@ # GDrive Integration -google-api-python-client>=1.7.11,<2.78.0 +google-api-python-client>=1.7.11,<2.90.0 gevent>20.6.0,<23.0.0 greenlet>=0.4.17,<2.1.0 -httplib2>=0.9.2,<0.22.0 +httplib2>=0.9.2,<0.23.0 oauth2client>=4.0.0,<4.1.4 uritemplate>=3.0.0,<4.2.0 pyasn1-modules>=0.0.8,<0.3.0 @@ -13,7 +13,7 @@ rsa>=3.4.2,<4.10.0 # Gmail google-auth-oauthlib>=0.4.3,<0.9.0 -google-api-python-client>=1.7.11,<2.78.0 +google-api-python-client>=1.7.11,<2.90.0 # goodreads goodreads>=0.3.2,<0.4.0