From 7f36d89768470f884d4db48c1753960b8c7fe0ff Mon Sep 17 00:00:00 2001 From: vincent Date: Thu, 16 Mar 2023 21:34:12 +0100 Subject: [PATCH] remove dependance between ldapmanager and config --- supysonic/config.py | 2 +- supysonic/managers/ldap.py | 71 +++++++++++++++++++------------------- supysonic/managers/user.py | 10 +++++- 3 files changed, 46 insertions(+), 37 deletions(-) diff --git a/supysonic/config.py b/supysonic/config.py index fae47dc..71cfcac 100644 --- a/supysonic/config.py +++ b/supysonic/config.py @@ -54,7 +54,7 @@ class DefaultConfig: "base_dn": None, "user_filter": "(&(objectClass=inetOrgPerson))", "admin_filter": None, - "bind_user": None, + "bind_dn": None, "bind_password": None, "username_attr": "uid", "email_attr": "mail", diff --git a/supysonic/managers/ldap.py b/supysonic/managers/ldap.py index 9137776..0f9e16a 100644 --- a/supysonic/managers/ldap.py +++ b/supysonic/managers/ldap.py @@ -1,67 +1,68 @@ import logging -try: - import ldap3 -except ModuleNotFoundError: - ldap3 = None -from ..config import get_current_config +import ldap3 + logger = logging.getLogger(__name__) class LdapManager: - @staticmethod - def try_auth(user, password): - config = get_current_config().LDAP - entrie = LdapManager.search_user(user, config["admin_filter"]) - if entrie: - logger.debug("{0} is admin".format(user)) - admin = True - else: - entrie = LdapManager.search_user(user, config["user_filter"]) + + def __init__(self, ldap_server, base_dn, user_filter, admin_filter, bind_dn, bind_password, username_attr, email_attr): + self.ldap_server=ldap_server + self.base_dn=base_dn + self.user_filter=user_filter + self.admin_filter=admin_filter + self.bind_dn=bind_dn + self.bind_password=bind_password + self.username_attr=username_attr + self.email_attr=email_attr + if not self.ldap_server: + raise ValueError("No LDAP configured") + self.server = ldap3.Server(self.ldap_server, get_info="ALL") + + def try_auth(self,user, password): + admin= False + if self.admin_filter: + entrie = self.search_user(user, self.admin_filter) if entrie: - admin = False - else: + logger.debug("{0} is admin".format(user)) + admin = True + if not admin: + entrie = self.search_user(user, self.user_filter) + if not entrie: return False - server = ldap3.Server(config["ldap_server"], get_info="ALL") try: with ldap3.Connection( - server, entrie.entry_dn, password, read_only=True + self.server, entrie.entry_dn, password, read_only=True ) as conn: return { - "uid": entrie[config["username_attr"]], - "mail": entrie[config["email_attr"]], + "uid": entrie[self.username_attr], + "mail": entrie[self.email_attr], "admin": admin, } except ldap3.core.exceptions.LDAPBindError: logger.warning("wrong password for user {0}".format(user)) return False - @staticmethod - def search_user(user, filter): - if not ldap3: - logger.warning("module 'ldap2' is not installed") - return False - config = get_current_config().LDAP - if not config["ldap_server"]: - logger.info("No LDAP configured") - return False - server = ldap3.Server(config["ldap_server"], get_info="ALL") + def search_user(self,user, filter): + try: with ldap3.Connection( - server, config["bind_dn"], config["bind_password"], read_only=True + self.server, self.bind_dn, self.bind_password, read_only=True ) as conn: conn.search( - config["base_dn"], + self.base_dn, filter, - attributes=[config["email_attr"], config["username_attr"]], + attributes=[self.email_attr, self.username_attr], ) entries = conn.entries except ldap3.core.exceptions.LDAPBindError: - logger.warning("wrong can't bind LDAP with {-1}".format(config["bind_dn"])) + logger.warning( + "wrong can't bind LDAP with {0}".format(self.bind_dn)) for entrie in entries: - if entrie[config["username_attr"]] == user: + if entrie[self.username_attr] == user: return entrie return False diff --git a/supysonic/managers/user.py b/supysonic/managers/user.py index ad4cd6e..4a54437 100644 --- a/supysonic/managers/user.py +++ b/supysonic/managers/user.py @@ -13,7 +13,12 @@ from ..db import User from .ldap import LdapManager +from ..config import get_current_config +try: + ldap=LdapManager(**get_current_config().LDAP) +except: + ldap=None class UserManager: @staticmethod @@ -47,7 +52,10 @@ def delete_by_name(name): @staticmethod def try_auth(name, password): - ldap_user = LdapManager.try_auth(name, password) + if ldap: + ldap_user = ldap.try_auth(name, password) + else: + ldap_user= False user = User.get_or_none(name=name) if ldap_user: if user is None: