From d71afc2f1826acd52d56b4f3b165808239d44c2e Mon Sep 17 00:00:00 2001 From: Andrea Callia D'Iddio <ac4014@ic.ac.uk> Date: Tue, 8 Nov 2022 16:50:25 +0000 Subject: [PATCH] Style: use regular expressions to be more precise and elegant (suggested by Ivan). --- app/__init__.py | 2 +- app/ldap_authentication/authenticator.py | 14 ++++++++------ 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/app/__init__.py b/app/__init__.py index 39264bf..c0380e2 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -24,7 +24,7 @@ login_manager.login_message = LOGIN_MANAGER_MESSAGE def get_ldap_handler(env): - if env == "dev": + if env == "deeev": return DummyLdapAuthenticator() return DocLdapAuthenticator() diff --git a/app/ldap_authentication/authenticator.py b/app/ldap_authentication/authenticator.py index 5726f30..04a1b8f 100644 --- a/app/ldap_authentication/authenticator.py +++ b/app/ldap_authentication/authenticator.py @@ -9,7 +9,7 @@ from ldap.ldapobject import SimpleLDAPObject from app.protocols import Authenticator # Impersonation constants -IMPERSONATION_OPERATOR = ".as." +IMPERSONATION_PATTERN = r"([a-z0-9]+) as ([a-z0-9]+)" IMPERSONATORS = ["ac4014", "infosys", "ip914", "jsbailey", "ld507", "rbc"] @@ -77,11 +77,13 @@ class DocLdapAuthenticator(Authenticator): :return: attr_name -> attr_value dict for given username """ logging_in_as = username - if IMPERSONATION_OPERATOR in username: - users = username.split(IMPERSONATION_OPERATOR) - if users[0] in IMPERSONATORS: - username = users[0] - logging_in_as = users[1] + users = re.match(IMPERSONATION_PATTERN, username) + if users: + impersonator = users.groups()[0] + impersonated = users.groups()[1] + if impersonator in IMPERSONATORS: + username = impersonator + logging_in_as = impersonated connection = ldap.initialize(self.server_url) connection.set_option(ldap.OPT_X_TLS_REQUIRE_CERT, ldap.OPT_X_TLS_ALLOW) connection.set_option(ldap.OPT_X_TLS_NEWCTX, 0) -- GitLab