You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

43 lines
1.1 KiB

  1. from django.contrib.auth.hashers import check_password
  2. from django.contrib.auth.models import User
  3. from hc.accounts.models import Profile
  4. class BasicBackend(object):
  5. def get_user(self, user_id):
  6. try:
  7. return User.objects.get(pk=user_id)
  8. except User.DoesNotExist:
  9. return None
  10. # Authenticate against the token in user's profile.
  11. class ProfileBackend(BasicBackend):
  12. def authenticate(self, username=None, token=None):
  13. try:
  14. profile = (Profile.objects
  15. .select_related("user").get(user__username=username))
  16. except Profile.DoesNotExist:
  17. return None
  18. if not check_password(token, profile.token):
  19. return None
  20. return profile.user
  21. def get_user(self, user_id):
  22. return User.objects.filter(pk=user_id).first()
  23. class EmailBackend(BasicBackend):
  24. def authenticate(self, username=None, password=None):
  25. try:
  26. user = User.objects.get(email=username)
  27. except User.DoesNotExist:
  28. return None
  29. if user.check_password(password):
  30. return user