Skip to content

Commit

Permalink
Merge pull request #73 from citysciencelab/remove-gravatar
Browse files Browse the repository at this point in the history
feat: remove gravatar url
  • Loading branch information
hwbllmnn authored Nov 5, 2024
2 parents 4272b4b + fe26b0d commit fd16733
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 16 deletions.
14 changes: 3 additions & 11 deletions src/ump/api/keycloak_utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
"""Keycloak helper functions"""

import hashlib
from os import environ as env

from keycloak import KeycloakAdmin, KeycloakOpenIDConnection
Expand Down Expand Up @@ -28,14 +27,7 @@ def find_user_id_by_email(email):
return None


def get_user_name(user_id):
"""Retrieve the username by user id"""
def get_user_details(user_id):
"""Retrieve the user details by user id"""
user = keycloak_admin.get_user(user_id)
return user["username"] if user else None


def get_gravatar_url(user_id):
"""Retrieve the gravatar url by user id"""
email = keycloak_admin.get_user(user_id)["email"].strip().lower()
hash_email = hashlib.md5(email.encode("utf-8")).hexdigest()
return f"https://www.gravatar.com/avatar/{hash_email}"
return user
11 changes: 6 additions & 5 deletions src/ump/api/routes/users.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from apiflask import APIBlueprint
from flask import Response, g

from ump.api.keycloak_utils import get_gravatar_url, get_user_name
from ump.api.keycloak_utils import get_user_details

users = APIBlueprint("users", __name__)

Expand All @@ -17,13 +17,14 @@ def index(user_id=None):
if auth is None:
return Response(mimetype="application/json", status=401)

user_name = get_user_name(user_id)
gravatar_url = get_gravatar_url(user_id)
details = get_user_details(user_id)

response_data = {
"user_id": user_id,
"username": user_name,
"gravatar_url": gravatar_url,
"username": details['username'],
"firstName": details['firstName'],
"lastName": details['lastName'],
"email": details['email'],
}

return Response(json.dumps(response_data), mimetype="application/json")

0 comments on commit fd16733

Please sign in to comment.