Advertisement
Guest User

Untitled

a guest
Oct 21st, 2017
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.19 KB | None | 0 0
  1. from django.contrib.auth.models import AnonymousUser
  2. from rest_framework.authentication import BasicAuthentication
  3. from rest_framework.exceptions import PermissionDenied
  4.  
  5. from .models import VKUser
  6. from app.settings import DEFAULT_AUTHENTICATION_CREDENTIAL
  7.  
  8.  
  9. class APIUser(AnonymousUser):
  10. def is_staff(self):
  11. return True
  12.  
  13.  
  14. class APIVKUser(AnonymousUser):
  15. def __init__(self, vk_user):
  16. super(APIVKUser, self).__init__()
  17. self.vk_user = vk_user
  18.  
  19. def is_vkuser(self):
  20. return True
  21.  
  22. def is_staff(self):
  23. return False
  24.  
  25.  
  26. class DefaultBasicAuthentication(BasicAuthentication):
  27. def authenticate_credentials(self, userid, password, request=None):
  28. default_login = DEFAULT_AUTHENTICATION_CREDENTIAL.get("login")
  29. default_password = DEFAULT_AUTHENTICATION_CREDENTIAL.get("password")
  30.  
  31. try:
  32. vk_user = VKUser.objects.get(vk_id=userid, auth_token=password)
  33. return (APIVKUser(vk_user), None)
  34.  
  35. except VKUser.DoesNotExist:
  36. if not default_login or (userid, password) != (default_login, default_password):
  37. raise PermissionDenied
  38.  
  39. return (APIUser(), None)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement