jwt_authentication.py 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. # -*- coding: utf-8 -*-
  2. from __future__ import unicode_literals
  3. from tastypie.authentication import Authentication
  4. from django.contrib.auth.models import User
  5. from .jwt_token import check_token, get_username
  6. import simplejson as json
  7. class JWTAuthentication(Authentication):
  8. '''
  9. '''
  10. def is_authenticated(self, request, **kwargs):
  11. # Check content type
  12. if request.content_type != 'application/json':
  13. return False
  14. # Check body
  15. if not request.body:
  16. return False
  17. body = json.loads(request.body)
  18. # Check required parameters
  19. if 'token' not in body:
  20. return False
  21. return check_token(body['token'])
  22. '''
  23. '''
  24. def get_identifier(self, request):
  25. # Check content type
  26. if request.content_type != 'application/json':
  27. return None
  28. # Check body
  29. if not request.body:
  30. return None
  31. body = json.loads(request.body)
  32. # Check required parameters
  33. if 'token' not in body:
  34. return None
  35. return get_username(body['token'])