Skip to content

Commit

Permalink
fix(oauth2): do not update profile when not null
Browse files Browse the repository at this point in the history
  • Loading branch information
joonas-yoon committed Sep 18, 2022
1 parent 078f2cb commit 0666530
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 14 deletions.
16 changes: 12 additions & 4 deletions fastapi/app/auth/github/libs.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,7 @@ async def login(self, strategy: Strategy, user: User, response: Response) -> Any
strategy_response = await super().login(strategy, user, response)
token = self.get_google_access_token(user)
profile = get_profile(token)
user.first_name = profile.get('first_name')
user.last_name = profile.get('last_name')
user.picture = profile.get('avatar_url')
user.last_login_at = datetime.now()
await update_profile(user, profile).save()
await user.save()
return strategy_response

Expand Down Expand Up @@ -49,6 +46,17 @@ def get_profile(access_token: str) -> dict:
return profile


def update_profile(user: User, profile: dict) -> User:
if user.first_name == None:
user.first_name = profile.get('first_name')
if user.last_name == None:
user.last_name = profile.get('last_name')
if user.picture == None:
user.picture = profile.get('avatar_url')
user.last_login_at = datetime.now()
return user


auth_backend_github = GithubAuthBackend(
name="jwt-github",
transport=bearer_transport,
Expand Down
2 changes: 1 addition & 1 deletion fastapi/app/auth/google/constants.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
GOOGLE_USERINFO_API = "https://www.googleapis.com/oauth2/v3/userinfo"
GOOGLE_USERINFO_URL = "https://www.googleapis.com/oauth2/v3/userinfo"
GOOGLE_SCOPE_PROFILE = "https://www.googleapis.com/auth/userinfo.profile"
GOOGLE_SCOPE_EMAIL = "https://www.googleapis.com/auth/userinfo.email"
25 changes: 16 additions & 9 deletions fastapi/app/auth/google/libs.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,15 @@
from ..exceptions import BadCredentialException
from ..libs import bearer_transport, get_jwt_strategy
from ..models import User
from .constants import GOOGLE_USERINFO_API
from .constants import GOOGLE_USERINFO_URL


class GoogleAuthBackend(AuthenticationBackend):
async def login(self, strategy: Strategy, user: User, response: Response) -> Any:
strategy_response = await super().login(strategy, user, response)
token = self.get_google_access_token(user)
userinfo = get_profile_from_google(token)
user.first_name = userinfo.get('given_name')
user.last_name = userinfo.get('family_name')
user.picture = userinfo.get('picture')
user.last_login_at = datetime.now()
await user.save()
profile = get_profile(token)
await update_profile(user, profile).save()
return strategy_response

def get_google_access_token(self, user: User) -> Optional[str]:
Expand All @@ -31,15 +27,26 @@ def get_google_access_token(self, user: User) -> Optional[str]:
return None


def get_profile_from_google(access_token: str) -> dict:
response = requests.get(url=GOOGLE_USERINFO_API,
def get_profile(access_token: str) -> dict:
response = requests.get(url=GOOGLE_USERINFO_URL,
params={'access_token': access_token})
if not response.ok:
raise BadCredentialException(
'Failed to get user information from Google.')
return response.json()


def update_profile(user: User, profile: dict) -> User:
if user.first_name == None:
user.first_name = profile.get('given_name')
if user.last_name == None:
user.last_name = profile.get('family_name')
if user.picture == None:
user.picture = profile.get('picture')
user.last_login_at = datetime.now()
return user


auth_backend_google = GoogleAuthBackend(
name="jwt-google",
transport=bearer_transport,
Expand Down

0 comments on commit 0666530

Please sign in to comment.