Skip to content

Commit

Permalink
refactor: clarify resolve_username docstring rename local variables
Browse files Browse the repository at this point in the history
  • Loading branch information
consideRatio committed Sep 19, 2024
1 parent 310e5bc commit 392eee0
Showing 1 changed file with 14 additions and 12 deletions.
26 changes: 14 additions & 12 deletions ldapauthenticator/ldapauthenticator.py
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ def _validate_bind_dn_template(self, proposal):
default_value=None,
allow_none=True,
help="""
Attribute containing user's name needed for building DN string, if `lookup_dn` is set to True.
Attribute containing user's name needed for building DN string, if `lookup_dn` is set to True.
See `user_search_base` for info on how this attribute is used.
Expand Down Expand Up @@ -329,8 +329,9 @@ def _observe_escape_userdn(self, change):

def resolve_username(self, username_supplied_by_user):
"""
Resolves a username supplied by a user to the a user DN when lookup_dn
is True.
Resolves a username (that could be used to construct a DN through a
template), and a DN, based on a username supplied by a user via a login
prompt in JupyterHub.
"""
conn = self.get_connection(
userdn=self.lookup_dn_search_user,
Expand Down Expand Up @@ -378,22 +379,23 @@ def resolve_username(self, username_supplied_by_user):
)
return (None, None)

user_dn = response[0]["attributes"][self.lookup_dn_user_dn_attribute]
if isinstance(user_dn, list):
if len(user_dn) == 0:
userdn = response[0]["dn"]
username = response[0]["attributes"][self.lookup_dn_user_dn_attribute]
if isinstance(username, list):
if len(username) == 0:
return (None, None)
elif len(user_dn) == 1:
user_dn = user_dn[0]
elif len(username) == 1:
username = username[0]
else:
self.log.warning(
f"A lookup of the username '{username_supplied_by_user}' returned a list "
f"of entries for the attribute '{self.lookup_dn_user_dn_attribute}'. Only "
f"the first among these ('{user_dn[0]}') was used. The other entries "
f"({', '.join(user_dn[1:])}) were ignored."
f"the first among these ('{username[0]}') was used. The other entries "
f"({', '.join(username[1:])}) were ignored."
)
user_dn = user_dn[0]
username = username[0]

return (user_dn, response[0]["dn"])
return (username, userdn)

def get_connection(self, userdn, password):
"""
Expand Down

0 comments on commit 392eee0

Please sign in to comment.