Skip to content

Commit

Permalink
Improve password checks
Browse files Browse the repository at this point in the history
  • Loading branch information
tudoramariei committed Aug 9, 2024
1 parent 9ea48a9 commit b34341b
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions backend/users/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,10 @@ def refresh_token(self, commit=True):
return self.validation_token

def verify_token(self, token):
if not self.validation_token or not token:
validation_token: uuid.UUID = self.validation_token
if not validation_token or not token:
return False
if self.validation_token == token:
if hmac.compare_digest(validation_token.hex, token.hex):
return True
return False

Expand All @@ -117,7 +118,8 @@ def clear_token(self, commit=True):
if commit:
self.save()

def old_hash_password(self, password, method, salt=None, pepper=None):
@staticmethod
def old_hash_password(password, method, salt=None, pepper=None):
"""
Implement the old password hashing algorithm from webapp2
"""
Expand Down Expand Up @@ -149,9 +151,9 @@ def check_old_password(self, password: str = ""):
return False

pepper = settings.OLD_SESSION_KEY
hashval, method, salt = self.old_password.split("$", 2)
hash_val, method, salt = self.old_password.split("$", 2)

return self.old_hash_password(password, method, salt, pepper) == hashval
return hmac.compare_digest(self.old_hash_password(password, method, salt, pepper), hash_val)

@staticmethod
def create_admin_login_url(next_url=""):
Expand Down

0 comments on commit b34341b

Please sign in to comment.