Skip to content

Commit

Permalink
refactor(admin): move code to Admin.py
Browse files Browse the repository at this point in the history
  • Loading branch information
machikoyasuda committed Feb 6, 2024
1 parent 2e28ec7 commit a0c4b2e
Showing 1 changed file with 22 additions and 1 deletion.
23 changes: 22 additions & 1 deletion benefits/core/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
The core application: Admin interface configuration.
"""

from django.conf import settings
import requests

from django.conf import settings

if settings.ADMIN:
import logging
Expand All @@ -21,3 +22,23 @@
]:
logger.debug(f"Register {model.__name__}")
admin.site.register(model)

def pre_login_user(user, request):
logger.debug(f"Running pre-login callback for user: {user.username}")
token = request.session.get("google_sso_access_token")
if token:
headers = {
"Authorization": f"Bearer {token}",
}

# Request Google user info to get name and email
url = "https://www.googleapis.com/oauth2/v3/userinfo"
response = requests.get(url, headers=headers, timeout=settings.REQUESTS_TIMEOUT)
user_data = response.json()
logger.debug(f"Updating admin user data from Google for user with email: {user_data['email']}")

user.first_name = user_data["given_name"]
user.last_name = user_data["family_name"]
user.username = user_data["email"]
user.email = user_data["email"]
user.save()

0 comments on commit a0c4b2e

Please sign in to comment.