# -*- coding: utf-8 -*- from __future__ import unicode_literals from tastypie.authentication import Authentication from django.contrib.auth.models import User from django.conf import settings from .jwt_token import check_token, get_username import simplejson as json class JWTAuthentication(Authentication): ''' ''' def is_authenticated(self, request, **kwargs): # Check content type if request.content_type != 'application/json': return False # Check authorization header if settings.JWT_ACCEPT_HEADER not in request.META: return False authorization_header = request.META.get(settings.JWT_ACCEPT_HEADER) # Check authorization header prefix if not authorization_header.startswith(settings.JWT_PREFIX_HEADER): return False return check_token(authorization_header[4:]) ''' ''' def get_identifier(self, request): # Check content type if request.content_type != 'application/json': return False # Check authorization header if settings.JWT_ACCEPT_HEADER not in request.META: return False authorization_header = request.META.get(settings.JWT_ACCEPT_HEADER) # Check authorization header prefix if not authorization_header.startswith(settings.JWT_PREFIX_HEADER): return False return get_username(authorization_header[4:])