-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
00cd36a
commit a186412
Showing
3 changed files
with
17 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -79,9 +79,11 @@ def djb2(seed): | |
|
||
|
||
class AbstractAuthentication(authentication.BaseAuthentication): | ||
token_post_index_name= "id_token" | ||
def authenticate(self, request): | ||
auth_header = request.META.get("HTTP_AUTHORIZATION") | ||
if not auth_header: | ||
auth_header = request.META.get("HTTP_AUTHORIZATION") or "" | ||
id_token = request.data.get(self.token_post_index_name) or auth_header.split(" ").pop() | ||
if not auth_header and not id_token: | ||
# return AnonymousUser, None | ||
return None | ||
host = request.get_host() | ||
|
@@ -91,7 +93,7 @@ def authenticate(self, request): | |
and DEBUG | ||
): | ||
return User.objects.get(username=auth_header), None | ||
id_token = auth_header.split(" ").pop() | ||
|
||
try: | ||
decoded_token = jwt.decode(id_token, verify=False) | ||
except ValueError as e: | ||
|
@@ -102,24 +104,21 @@ def authenticate(self, request): | |
) | ||
if is_expired: | ||
raise InvalidAuthToken("Authorization token is expired") | ||
if "supabase" in decoded_token.get("iss"): | ||
try: | ||
authenticated_user = self._verify_token(id_token) | ||
except ValueError as e: | ||
raise InvalidAuthToken() from e | ||
else: | ||
decoded_token = auth_with_application(id_token, decoded_token) | ||
try: | ||
authenticated_user = self._verify_token(id_token) | ||
except ValueError as e: | ||
raise InvalidAuthToken() from e | ||
|
||
if not id_token or not decoded_token: | ||
return None | ||
|
||
striped_user_name = decoded_token["email"].split("@")[0] | ||
striped_user_name = authenticated_user['email'].split("@")[0] | ||
# Let's add random chars after the stiped username | ||
# There may be the case where [email protected] and [email protected] users register | ||
# We will generate random string using the email as seed | ||
defaults = {"username": f"{striped_user_name}#{djb2(decoded_token['email'])}"} | ||
# There are some instances where the display_name may come as null from firebase | ||
display_name = decoded_token.get("name") | ||
display_name = authenticated_user.get("name") | ||
# If we have display_name, let's try and figure the first name and last name | ||
if display_name: | ||
first_name, last_name = self.convert_user_display_name(display_name) | ||
|
@@ -130,15 +129,15 @@ def authenticate(self, request): | |
email=decoded_token.get("email"), | ||
defaults=defaults, | ||
)[0] | ||
avatar_url = decoded_token["user_metadata"]["avatar_url"] | ||
uid = decoded_token["sub"] | ||
full_name = decoded_token["user_metadata"]["full_name"] | ||
avatar_url = authenticated_user.get("picture") | ||
uid =authenticated_user.get("uid") | ||
full_name = authenticated_user.get("name") | ||
first_name = full_name.split(" ")[0] | ||
last_name = ( | ||
" ".join(full_name.split(" ")[1:]) if len(full_name.split(" ")) > 1 else "" | ||
) | ||
profile: UserFirebaseProfile = self._get_or_create_profile( | ||
user, uid=uid, avatar=avatar_url | ||
profile = self._get_or_create_profile( | ||
user=user, uid=uid, avatar=avatar_url | ||
) | ||
|
||
if user.first_name != first_name or user.last_name != last_name: | ||
|
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters