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.

40 lines
1.0 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. profiles = Profile.objects.select_related("user")
  15. profile = profiles.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. class EmailBackend(BasicBackend):
  22. def authenticate(self, username=None, password=None):
  23. try:
  24. user = User.objects.get(email=username)
  25. except User.DoesNotExist:
  26. return None
  27. if user.check_password(password):
  28. return user