Skip to content

Commit beb30a8

Browse files
committed
refactor: reduce use of temporary variables like msg for logging
1 parent db5b738 commit beb30a8

File tree

1 file changed

+26
-53
lines changed

1 file changed

+26
-53
lines changed

ldapauthenticator/ldapauthenticator.py

Lines changed: 26 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -247,31 +247,24 @@ def resolve_username(self, username_supplied_by_user):
247247
if self.escape_userdn:
248248
search_dn = escape_filter_chars(search_dn)
249249
conn = self.get_connection(
250-
userdn=search_dn, password=self.lookup_dn_search_password
250+
userdn=search_dn,
251+
password=self.lookup_dn_search_password,
251252
)
252-
is_bound = conn.bind()
253-
if not is_bound:
254-
msg = "Failed to connect to LDAP server with search user '{search_dn}'"
255-
self.log.warning(msg.format(search_dn=search_dn))
253+
if not conn.bind():
254+
self.log.warning(
255+
f"Failed to connect to LDAP server with search user '{search_dn}'"
256+
)
256257
return (None, None)
257258

258259
search_filter = self.lookup_dn_search_filter.format(
259-
login_attr=self.user_attribute, login=username_supplied_by_user
260-
)
261-
msg = "\n".join(
262-
[
263-
"Looking up user with:",
264-
" search_base = '{search_base}'",
265-
" search_filter = '{search_filter}'",
266-
" attributes = '{attributes}'",
267-
]
260+
login_attr=self.user_attribute,
261+
login=username_supplied_by_user,
268262
)
269263
self.log.debug(
270-
msg.format(
271-
search_base=self.user_search_base,
272-
search_filter=search_filter,
273-
attributes=self.user_attribute,
274-
)
264+
"Looking up user with:\n",
265+
f" search_base = '{self.user_search_base}'\n",
266+
f" search_filter = '{search_filter}'\n",
267+
f" attributes = '{self.user_attribute}'",
275268
)
276269
conn.search(
277270
search_base=self.user_search_base,
@@ -281,14 +274,9 @@ def resolve_username(self, username_supplied_by_user):
281274
)
282275
response = conn.response
283276
if len(response) == 0 or "attributes" not in response[0].keys():
284-
msg = (
285-
"No entry found for user '{username}' "
286-
"when looking up attribute '{attribute}'"
287-
)
288277
self.log.warning(
289-
msg.format(
290-
username=username_supplied_by_user, attribute=self.user_attribute
291-
)
278+
f"No entry found for user '{username_supplied_by_user}' "
279+
f"when looking up attribute '{self.user_attribute}'"
292280
)
293281
return (None, None)
294282

@@ -299,19 +287,11 @@ def resolve_username(self, username_supplied_by_user):
299287
elif len(user_dn) == 1:
300288
user_dn = user_dn[0]
301289
else:
302-
msg = (
303-
"A lookup of the username '{username}' returned a list "
304-
"of entries for the attribute '{attribute}'. Only the "
305-
"first among these ('{first_entry}') was used. The other "
306-
"entries ({other_entries}) were ignored."
307-
)
308290
self.log.warn(
309-
msg.format(
310-
username=username_supplied_by_user,
311-
attribute=self.lookup_dn_user_dn_attribute,
312-
first_entry=user_dn[0],
313-
other_entries=", ".join(user_dn[1:]),
314-
)
291+
f"A lookup of the username '{username_supplied_by_user}' returned a list "
292+
f"of entries for the attribute '{self.lookup_dn_user_dn_attribute}'. Only "
293+
f"the first among these ('{user_dn[0]}') was used. The other entries "
294+
f"({', '.join(user_dn[1:])}) were ignored."
315295
)
316296
user_dn = user_dn[0]
317297

@@ -389,8 +369,7 @@ def authenticate(self, handler, data):
389369
userdn = dn.format(username=username)
390370
if self.escape_userdn:
391371
userdn = escape_filter_chars(userdn)
392-
msg = "Attempting to bind {username} with {userdn}"
393-
self.log.debug(msg.format(username=username, userdn=userdn))
372+
self.log.debug(f"Attempting to bind {username} with {userdn}")
394373
msg = "Status of user bind {username} with {userdn} : {is_bound}"
395374
try:
396375
conn = self.get_connection(userdn, password)
@@ -408,8 +387,7 @@ def authenticate(self, handler, data):
408387
break
409388

410389
if not is_bound:
411-
msg = "Invalid password for user '{username}'"
412-
self.log.warning(msg.format(username=username))
390+
self.log.warning(f"Invalid password for user '{username}'")
413391
return None
414392

415393
if self.search_filter:
@@ -424,20 +402,14 @@ def authenticate(self, handler, data):
424402
)
425403
n_users = len(conn.response)
426404
if n_users == 0:
427-
msg = "User with '{userattr}={username}' not found in directory"
428405
self.log.warning(
429-
msg.format(userattr=self.user_attribute, username=username)
406+
f"User with '{self.user_attribute}={username}' not found in directory"
430407
)
431408
return None
432409
if n_users > 1:
433-
msg = (
434-
"Duplicate users found! "
435-
"{n_users} users found with '{userattr}={username}'"
436-
)
437410
self.log.warning(
438-
msg.format(
439-
userattr=self.user_attribute, username=username, n_users=n_users
440-
)
411+
"Duplicate users found! {n_users} users found "
412+
f"with '{self.user_attribute}={username}'"
441413
)
442414
return None
443415

@@ -464,8 +436,9 @@ def authenticate(self, handler, data):
464436
break
465437
if not found:
466438
# If we reach here, then none of the groups matched
467-
msg = "username:{username} User not in any of the allowed groups"
468-
self.log.warning(msg.format(username=username))
439+
self.log.warning(
440+
f"username:{username} User not in any of the allowed groups"
441+
)
469442
return None
470443

471444
if not self.use_lookup_dn_username:

0 commit comments

Comments
 (0)