jwt_authentication.py 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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 django.conf import settings
  6. from .jwt_token import check_token, get_username
  7. import simplejson as json
  8. class JWTAuthentication(Authentication):
  9. '''
  10. '''
  11. def is_authenticated(self, request, **kwargs):
  12. # Check content type
  13. if request.content_type != 'application/json':
  14. return False
  15. # Check authorization header
  16. if settings.JWT_ACCEPT_HEADER not in request.META:
  17. return False
  18. authorization_header = request.META.get(settings.JWT_ACCEPT_HEADER)
  19. # Check authorization header prefix
  20. if not authorization_header.startswith(settings.JWT_PREFIX_HEADER):
  21. return False
  22. return check_token(authorization_header[4:])
  23. '''
  24. '''
  25. def get_identifier(self, request):
  26. # Check content type
  27. if request.content_type != 'application/json':
  28. return False
  29. # Check authorization header
  30. if settings.JWT_ACCEPT_HEADER not in request.META:
  31. return False
  32. authorization_header = request.META.get(settings.JWT_ACCEPT_HEADER)
  33. # Check authorization header prefix
  34. if not authorization_header.startswith(settings.JWT_PREFIX_HEADER):
  35. return False
  36. return get_username(authorization_header[4:])