1
0
Fork 0
mirror of https://github.com/Kozea/Radicale.git synced 2025-08-01 18:18:31 +00:00

Merge pull request #1570 from petervarkoly/master

Fix issue #197
This commit is contained in:
Peter Bieringer 2024-09-17 20:28:40 +02:00 committed by GitHub
commit 7fbc0e70e9
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -59,6 +59,7 @@ class Auth(auth.BaseAuth):
def _login2(self, login: str, password: str) -> str: def _login2(self, login: str, password: str) -> str:
try: try:
"""Bind as reader dn""" """Bind as reader dn"""
logger.debug(f"_login2 {self._ldap_uri}, {self._ldap_reader_dn}")
conn = self.ldap.initialize(self._ldap_uri) conn = self.ldap.initialize(self._ldap_uri)
conn.protocol_version = 3 conn.protocol_version = 3
conn.set_option(self.ldap.OPT_REFERRALS, 0) conn.set_option(self.ldap.OPT_REFERRALS, 0)
@ -72,8 +73,8 @@ class Auth(auth.BaseAuth):
logger.debug("LDAP Auth user: %s", user_dn) logger.debug("LDAP Auth user: %s", user_dn)
"""Close ldap connection""" """Close ldap connection"""
conn.unbind() conn.unbind()
except Exception: except Exception as e:
raise RuntimeError("Invalide ldap configuration") raise RuntimeError(f"Invalid ldap configuration:{e}")
try: try:
"""Bind as user to authenticate""" """Bind as user to authenticate"""
@ -96,43 +97,52 @@ class Auth(auth.BaseAuth):
def _login3(self, login: str, password: str) -> str: def _login3(self, login: str, password: str) -> str:
"""Connect the server""" """Connect the server"""
try: try:
logger.debug(f"_login3 {self._ldap_uri}, {self._ldap_reader_dn}")
server = self.ldap3.Server(self._ldap_uri) server = self.ldap3.Server(self._ldap_uri)
conn = self.ldap3.Connection(server, self._ldap_reader_dn, password=self._ldap_secret) conn = self.ldap3.Connection(server, self._ldap_reader_dn, password=self._ldap_secret)
except self.ldap3.core.exceptions.LDAPSocketOpenError: except self.ldap3.core.exceptions.LDAPSocketOpenError:
raise RuntimeError("Unable to reach ldap server") raise RuntimeError("Unable to reach ldap server")
except Exception: except Exception as e:
logger.debug(f"_login3 error 1 {e}")
pass pass
if not conn.bind(): if not conn.bind():
logger.debug("_login3 can not bind")
raise RuntimeError("Unable to read from ldap server") raise RuntimeError("Unable to read from ldap server")
logger.debug(f"_login3 bind as {self._ldap_reader_dn}")
"""Search the user dn""" """Search the user dn"""
conn.search( conn.search(
search_base=self._ldap_base, search_base=self._ldap_base,
search_filter=self._ldap_filter.format(login), search_filter=self._ldap_filter.format(login),
search_scope='SUBTREE', search_scope=self.ldap3.SUBTREE,
attributes=['memberOf'] attributes=['memberOf']
) )
if len(conn.entries) == 0: if len(conn.entries) == 0:
logger.debug(f"_login3 user '{login}' can not be find")
"""User could not be find""" """User could not be find"""
return "" return ""
user_entry = conn.entries[0].entry_to_json() user_entry = conn.response[0]
conn.unbind() conn.unbind()
user_dn = user_entry['dn'] user_dn = user_entry['dn']
logger.debug(f"_login3 found user_dn {user_dn}")
try: try:
"""Try to bind as the user itself""" """Try to bind as the user itself"""
conn = self.ldap3.Connection(server, user_dn, password=password) conn = self.ldap3.Connection(server, user_dn, password=password)
if not conn.bind(): if not conn.bind():
logger.debug(f"_login3 user '{login}' can not be find")
return "" return ""
if self._ldap_load_groups: if self._ldap_load_groups:
tmp = [] tmp = []
for g in user_entry['attributes']['memberOf']: for g in user_entry['attributes']['memberOf']:
tmp.append(g) tmp.append(g.split(',')[0][3:])
self._ldap_groups = set(tmp) self._ldap_groups = set(tmp)
conn.unbind() conn.unbind()
logger.debug(f"_login3 {login} successfully authorized")
return login return login
except Exception: except Exception as e:
logger.debug(f"_login3 error 2 {e}")
pass pass
return "" return ""