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.

39 lines
1.0 KiB

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