-
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
070d297
commit 3f6ad4b
Showing
1 changed file
with
11 additions
and
12 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 |
---|---|---|
|
@@ -71,21 +71,22 @@ 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 | ||
|
||
hash = 5381 | ||
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,22 +118,20 @@ 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 [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'])}" | ||
} | ||
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, | ||
|