Skip to content

Commit

Permalink
remove dependance between ldapmanager and config
Browse files Browse the repository at this point in the history
  • Loading branch information
vincentDcmps committed Mar 16, 2023
1 parent a276bfb commit 7f36d89
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 37 deletions.
2 changes: 1 addition & 1 deletion supysonic/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
71 changes: 36 additions & 35 deletions supysonic/managers/ldap.py
Original file line number Diff line number Diff line change
@@ -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
10 changes: 9 additions & 1 deletion supysonic/managers/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit 7f36d89

Please sign in to comment.