Skip to content

Instantly share code, notes, and snippets.

@Beomi
Created February 18, 2017 09:43
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Beomi/54754a09fd95195d6cd9e14b75126d92 to your computer and use it in GitHub Desktop.
Save Beomi/54754a09fd95195d6cd9e14b75126d92 to your computer and use it in GitHub Desktop.
class MyUserAuthBackend(object):
def check_legacy_password(self, db_password, supplied_password):
return constant_time_compare('*'+ hashlib.sha1(hashlib.sha1(supplied_password.encode('utf-8')).digest()).hexdigest().upper(), db_password)
def authenticate(self, username=None, password=None):
""" Authenticate a user based on mb_id as the user name. """
try:
user = User.objects.get(mb_id=username)
if '$' not in user.password:
if self.check_legacy_password(user.password, password):
return user
else:
return None
else:
if user.check_password(password):
return user
except User.DoesNotExist:
return None
def get_user(self, user_id):
""" Get a User object from the user_id. """
try:
return User.objects.get(pk=user_id)
except User.DoesNotExist:
return None
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment