Skip to content

Commit

Permalink
Fix TypeError when logging into multivisor using authentication
Browse files Browse the repository at this point in the history
Fixes #72
  • Loading branch information
bbugyi200 authored and tiagocoutinho committed Oct 9, 2021
1 parent 9c6d2d0 commit 726bbec
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions multivisor/server/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ def constant_time_compare(val1, val2):
Taken from Django Source Code
"""
val1 = hashlib.sha1(val1).hexdigest()
val1 = hashlib.sha1(_safe_encode(val1)).hexdigest()
if val2.startswith("{SHA}"): # password can be specified as SHA-1 hash in config
val2 = val2.split("{SHA}")[1]
else:
val2 = hashlib.sha1(val2).hexdigest()
val2 = hashlib.sha1(_safe_encode(val2)).hexdigest()
if len(val1) != len(val2):
return False
result = 0
Expand All @@ -40,6 +40,15 @@ def constant_time_compare(val1, val2):
return result == 0


def _safe_encode(data):
"""Safely encode @data string to utf-8"""
try:
result = data.encode("utf-8")
except (UnicodeDecodeError, UnicodeEncodeError, AttributeError):
result = data
return result


def login_required(app):
"""
Decorator to mark view as requiring being logged in
Expand Down

0 comments on commit 726bbec

Please sign in to comment.