Skip to content

Commit

Permalink
Another fixes for authlib
Browse files Browse the repository at this point in the history
GitHub should correctly retrieve e-mail of the user now. Adding few
debug messages for Fedora backend.

Signed-off-by: Michal Konecny <[email protected]>
  • Loading branch information
Zlopez committed Dec 5, 2024
1 parent 0ca6b15 commit 97e1a1e
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions anitya/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,23 @@ def auth(name): # pragma: no cover
if client is None:
flask.abort(404)
token = client.authorize_access_token()
# Fedora specifics
if name == "fedora":
user_info = client.userinfo(token=token)
user_info["username"] = user_info["preferred_username"]
user_info["email"] = user_info["username"] + "@fedoraproject.org"
# GitHub specifics
elif name == "github":
resp = client.get("user", token=token)
user_info = resp.json()
user_info["username"] = user_info["login"]
if not user_info["email"]:
resp = client.get("user/emails", token=token)
emails = resp.json()
for email in emails:
if email["primary"]:
user_info["email"] = email["email"]
# Google specifics
elif name == "google":
user_info = token.get("userinfo")
user_info["username"] = user_info["email"]
Expand All @@ -67,12 +76,15 @@ def auth(name): # pragma: no cover
_log.debug("Obtained user_info from %s: %s", name, user_info)

# Check if the user exists
_log.debug("Looking for the user %s in database...", user_info["email"])
user = User.query.filter(User.email == user_info["email"]).first()
if not user:
_log.debug("User not found. Creating new user...")
new_user = User(email=user_info["email"], username=user_info["username"])
Session.add(new_user)
Session.commit()
user = new_user
_log.debug("Logging as user %s", user.email)
flask_login.login_user(user)

if flask.session["next_url"]:
Expand Down

0 comments on commit 97e1a1e

Please sign in to comment.