Skip to content

Commit

Permalink
Found a couple of spots that didn't have log.debug or that didn't com…
Browse files Browse the repository at this point in the history
…bine into a single line
  • Loading branch information
jkachel committed Mar 6, 2024
1 parent 68e87a2 commit 10ccc63
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 15 deletions.
3 changes: 1 addition & 2 deletions authentication/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,7 @@ def keycloak_get_user(user: User):
f"realms/{settings.KEYCLOAK_ADMIN_REALM}/users/"
)

log_str = f"Trying to get user info for {user.username}"
log.debug(log_str)
log.debug("Trying to get user info for %s", {user.username})

if user.keycloak_user_tokens.exists():
params = {"id": user.keycloak_user_tokens.first().keycloak_id}
Expand Down
23 changes: 10 additions & 13 deletions authentication/backends.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,50 +26,47 @@ class KeycloakRemoteUserBackend(RemoteUserBackend):
def authenticate(self, request, remote_user):
"""Authenticate the user, using Keycloak to grab their ID first."""

log_str = f"KeycloakRemoteUserBackend is running for {remote_user}"
log.warning(log_str)
log.debug("KeycloakRemoteUserBackend is running for %s", {remote_user})

userinfo_url = (
f"{settings.KEYCLOAK_ADMIN_URL}/auth/admin/"
f"realms/{settings.KEYCLOAK_ADMIN_REALM}/users/"
)

if not remote_user:
log.warning("No remote_user provided")
log.debug("No remote_user provided")
return None

userinfo = keycloak_session_init(
userinfo_url, verify=False, params={"email": remote_user}
)

if len(userinfo) == 0:
log.warning("Keycloak didn't return anything")
log.debug("Keycloak didn't return anything")
# User may have changed their email, so let's see if we can find an
# existing user

existing_user = User.objects.get(email=remote_user)

if existing_user is not None:
log_str = (
f"Found existing user {existing_user}, trying to get "
"Keycloak info for them"
log.debug(
"Found existing user %s, trying to get Keycloak info for them",
{existing_user},
)
log.warning(log_str)
userinfo = [
keycloak_session_init(
f"{userinfo_url}{existing_user.username}/", verify=False
)
]

if len(userinfo) == 0:
log_str = (
"Keycloak still returned nothing for ID "
f"{existing_user.username}, so giving up."
log.debug(
"Keycloak still returned nothing for ID %s, so giving up.",
{existing_user.username},
)
log.warning(log_str)
return None
else:
log.warning(
log.debug(
"Keycloak still returned nothing and we didn't find a user to"
" check, so giving up."
)
Expand Down

0 comments on commit 10ccc63

Please sign in to comment.