Skip to content

Commit

Permalink
Merge pull request #5 from ProfileStash/main
Browse files Browse the repository at this point in the history
feat: add randomized string to the username
  • Loading branch information
maycuatroi authored Jan 20, 2023
2 parents cff701f + 60098ba commit 971607f
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 5 deletions.
28 changes: 24 additions & 4 deletions django_firebase_auth/firebase_auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,25 @@ def auth_with_application(id_token, decoded_token):
return decoded_token


def djb2(seed):
"""
djb2 is a hash function that was created by Dan Bernstein
and presented in the article "Notes on hashing" in the April 1997
issue of comp.lang.c.
The hash function is designed to be very fast,
and produces a hash value that is almost identical for all strings,
even those that are very long.
"""
# http://www.cse.yorku.ca/~oz/hash.html

hash = 5381
for c in seed:
hash = ((hash << 5) + hash) + ord(c)

return hex(hash & 0xFFFFFFFF)[2:]


class FirebaseAuthentication(authentication.BaseAuthentication):
def authenticate(self, request):
auth_header = request.META.get("HTTP_AUTHORIZATION")
Expand Down Expand Up @@ -101,17 +120,18 @@ def authenticate(self, request):
return None

striped_user_name = decoded_token["email"].split("@")[0]
defaults = {
"username": striped_user_name
}
# 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")
# 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)
defaults["first_name"] = first_name
if last_name:
defaults["last_name"] = last_name
defaults["last_name"] = last_name
user: User = User.objects.get_or_create(
email=decoded_token.get("email"),
defaults=defaults,
Expand Down
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.0.8",
version="1.0.9",
packages=find_packages(),
install_requires=["firebase-admin", "djangorestframework"],
url="https://github.com/maycuatroi/django-firebase-auth",
Expand Down

0 comments on commit 971607f

Please sign in to comment.