From 070d2978bfdd123c3c871667aaa65e36e530da93 Mon Sep 17 00:00:00 2001 From: Alvaro Chevez Date: Thu, 19 Jan 2023 13:10:43 -0600 Subject: [PATCH 1/3] feat: add randomized string to the username --- django_firebase_auth/firebase_auth.py | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/django_firebase_auth/firebase_auth.py b/django_firebase_auth/firebase_auth.py index 23721c2..25fd593 100644 --- a/django_firebase_auth/firebase_auth.py +++ b/django_firebase_auth/firebase_auth.py @@ -69,6 +69,24 @@ 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") @@ -99,10 +117,13 @@ def authenticate(self, request): if not id_token or not decoded_token: return None - + striped_user_name = decoded_token["email"].split("@")[0] + # Let's add random chars after the stiped username + # There may be the case where some@email1.com and some@email2.com users register + # We will generate random string using the email as seed defaults = { - "username": striped_user_name + "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") From 3f6ad4bc697b9db7772092ea6465215ad39f7125 Mon Sep 17 00:00:00 2001 From: Alvaro Chevez Date: Thu, 19 Jan 2023 13:14:12 -0600 Subject: [PATCH 2/3] lint: formatted with black default --- django_firebase_auth/firebase_auth.py | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/django_firebase_auth/firebase_auth.py b/django_firebase_auth/firebase_auth.py index 25fd593..9c96fa8 100644 --- a/django_firebase_auth/firebase_auth.py +++ b/django_firebase_auth/firebase_auth.py @@ -71,13 +71,13 @@ def auth_with_application(id_token, 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. + 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. + 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 @@ -85,7 +85,8 @@ def djb2(seed): for c in seed: hash = ((hash << 5) + hash) + ord(c) - return hex(hash & 0xffffffff)[2:] + return hex(hash & 0xFFFFFFFF)[2:] + class FirebaseAuthentication(authentication.BaseAuthentication): def authenticate(self, request): @@ -117,14 +118,12 @@ def authenticate(self, request): if not id_token or not decoded_token: return None - + striped_user_name = decoded_token["email"].split("@")[0] # Let's add random chars after the stiped username # There may be the case where some@email1.com and some@email2.com users register # We will generate random string using the email as seed - defaults = { - "username": f"{striped_user_name}#{djb2(decoded_token['email'])}" - } + 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 @@ -132,7 +131,7 @@ def authenticate(self, request): 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, From 60098bad4a4a256873a050bfeeb36cc2f2e13da7 Mon Sep 17 00:00:00 2001 From: Alvaro Chevez Date: Thu, 19 Jan 2023 13:15:02 -0600 Subject: [PATCH 3/3] bump: 1.0.9 --- setup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup.py b/setup.py index aad1455..9d1facf 100644 --- a/setup.py +++ b/setup.py @@ -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",