Skip to content

Commit

Permalink
Merge pull request #145 from statisticsnorway/add-username-lookup
Browse files Browse the repository at this point in the history
Add method to look up e-mail from token
  • Loading branch information
mallport authored May 7, 2024
2 parents dec199f + dabb760 commit 350d932
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "dapla-toolbelt"
version = "2.0.12"
version = "2.0.13"
description = "Dapla Toolbelt"
authors = ["Dapla Developers <dapla-platform-developers@ssb.no>"]
license = "MIT"
Expand Down
17 changes: 17 additions & 0 deletions src/dapla/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from collections.abc import Sequence
from datetime import datetime
from datetime import timedelta
from functools import lru_cache
from typing import Any
from typing import Optional

Expand Down Expand Up @@ -148,6 +149,22 @@ def fetch_personal_token() -> str:
err._print_warning()
raise err

@staticmethod
@lru_cache(maxsize=1)
def fetch_email_from_credentials() -> Optional[str]:
"""Retrieves an e-mail based on current Google Credentials. Potentially makes a Google API call."""
if AuthClient.is_ready():
credentials = AuthClient.fetch_google_credentials()
response = requests.get(
url=f"https://oauth2.googleapis.com/tokeninfo?access_token={credentials.token}"
)

return response.json().get("email") if response.status_code == 200 else None

else:
user_info = AuthClient.fetch_local_user_from_jupyter()
return user_info.get("username")

@staticmethod
def fetch_google_token(
request: Optional[GoogleAuthRequest] = None,
Expand Down

0 comments on commit 350d932

Please sign in to comment.