Skip to content

Commit

Permalink
LDAP auth: flexibilize parsing of 'ldap_groups_attribute'
Browse files Browse the repository at this point in the history
Use helper methods from the LDAP modules to get individual elements
(like in our case the RDN value) out of attributes with DN syntax
in a standard compliant way instead fiddling around ourselves.

If these methods fail, fall back to using the whole attribute value,
which allows us to also use attributes with non-DN syntax for groups
and permissions.
  • Loading branch information
marschap committed Jan 3, 2025
1 parent 61145fd commit 2c297c7
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions radicale/auth/ldap.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,11 @@ def _login2(self, login: str, password: str) -> str:
tmp = []
for g in user_entry[1][self._ldap_groups_attr]:
"""Get group g's RDN's attribute value"""
g = g.decode('utf-8').split(',')[0]
tmp.append(g.partition('=')[2])
try:
rdns = self.ldap.dn.explode_dn(g, notypes=True)
tmp.append(rdns[0])
except Exception:
tmp.append(g.decode('utf8'))
self._ldap_groups = set(tmp)
logger.debug("_login2 LDAP groups of user: %s", ",".join(self._ldap_groups))
if self._ldap_user_attr:
Expand Down Expand Up @@ -230,8 +233,11 @@ def _login3(self, login: str, password: str) -> str:
tmp = []
for g in user_entry['attributes'][self._ldap_groups_attr]:
"""Get group g's RDN's attribute value"""
g = g.split(',')[0]
tmp.append(g.partition('=')[2])
try:
rdns = self.ldap3.utils.dn.parse_dn(g)
tmp.append(rdns[0][1])
except Exception:
tmp.append(g)
self._ldap_groups = set(tmp)
logger.debug("_login3 LDAP groups of user: %s", ",".join(self._ldap_groups))
if self._ldap_user_attr:
Expand Down

0 comments on commit 2c297c7

Please sign in to comment.