Skip to content

Commit

Permalink
- Fix bug
Browse files Browse the repository at this point in the history
  • Loading branch information
maycuatroi committed Feb 24, 2024
1 parent 00cd36a commit a186412
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 18 deletions.
33 changes: 16 additions & 17 deletions abstract_auth/abstract_auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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:
Expand All @@ -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)
Expand All @@ -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:
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

setup(
name="django-firebase-auth",
version="1.1.0",
version="1.1.1",
packages=find_packages(),
install_requires=["firebase-admin", "djangorestframework"],
url="https://github.com/maycuatroi/django-firebase-auth",
Expand Down

0 comments on commit a186412

Please sign in to comment.