# -*- 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 api.utils.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 prefix_length = len(settings.JWT_PREFIX_HEADER) (_, ok) = check_token(authorization_header[prefix_length + 1:]) return ok ''' ''' 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 prefix_length = len(settings.JWT_PREFIX_HEADER) return get_username(authorization_header[prefix_length + 1:])