from __future__ import unicode_literals

from django.contrib.auth import get_user_model
from django.utils.translation import ugettext_lazy as _
from django.utils.six import text_type
from jose import jwt
from rest_framework import HTTP_HEADER_ENCODING
from rest_framework.authentication import BaseAuthentication
from rest_framework.exceptions import AuthenticationFailed

AUTH_HEADER_TYPE = 'Bearer'
AUTH_HEADER_TYPE_BYTES = AUTH_HEADER_TYPE.encode('utf-8')

USER_ID_FIELD = 'pk'
PAYLOAD_ID_FIELD = 'user_pk'

SECRET_KEY = 'blah'

User = get_user_model()


class JWTAuthentication(BaseAuthentication):
    www_authenticate_realm = 'api'

    def authenticate(self, request):
        header = self.get_header(request)
        if header is None:
            return None

        token = self.get_token(header)
        if token is None:
            return None

        payload = self.get_payload(token)
        user_id = self.get_user_id(payload)

        return (self.get_user(user_id), None)

    def authenticate_header(self, request):
        return '{0} realm="{1}"'.format(
            AUTH_HEADER_TYPE,
            self.www_authenticate_realm,
        )

    def get_header(self, request):
        header = request.META.get('HTTP_AUTHORIZATION')

        if isinstance(header, text_type):
            # Work around django test client oddness
            header = header.encode(HTTP_HEADER_ENCODING)

        return header

    def get_token(self, header):
        parts = header.split()

        if parts[0] != AUTH_HEADER_TYPE_BYTES:
            return None

        if len(parts) != 2:
            raise AuthenticationFailed(_('Authorization header is invalid.'))

        return parts[1]

    def get_payload(self, token):
        try:
            return jwt.decode(token, SECRET_KEY, algorithms=['HS256'])
        except TypeError:
            raise AuthenticationFailed(_('Token is invalid.'))

    def get_user_id(self, payload):
        try:
            return payload[PAYLOAD_ID_FIELD]
        except KeyError:
            raise AuthenticationFailed(_('Token contained no user identification.'))

    def get_user(self, user_id):
        try:
            user = User.objects.get(**{USER_ID_FIELD: user_id})
        except User.DoesNotExist:
            raise AuthenticationFailed(_('User not found.'))

        if not user.is_active:
            raise AuthenticationFailed(_('User is inactive.'))

        return user

@@ -103,27 +103,19 @@
 ils.
-translation
+six
  import 
 uget
@@ -114,26 +114,17 @@
 ort 
-uget
 text_
-lazy as _
+type
 %0Afro
@@ -134,35 +134,43 @@
 jango.utils.
-six
+translation
  import 
 text_type%0Afr
@@ -153,33 +153,42 @@
 tion import 
+uget
 text_
-type
+lazy as _
 %0Afrom jose i
@@ -592,24 +592,156 @@
 ntication):%0A
+    %22%22%22%0A    An authentication plugin that authenticates requests through a JSON web%0A    token provided in a request header.%0A    %22%22%22%0A
     www_auth
@@ -1313,32 +1313,134 @@
 self, request):%0A
+        %22%22%22%0A        Extracts a header containing a JSON web token from the given request.%0A        %22%22%22%0A
         header =
@@ -1689,16 +1689,97 @@
 eader):%0A
+        %22%22%22%0A        Extracts a JSON web token from the given header.%0A        %22%22%22%0A
         
@@ -1849,16 +1849,82 @@
 _BYTES:%0A
+            # Assume the header does not contain a JSON web token%0A
         
@@ -2007,16 +2007,33 @@
 nFailed(
+%0A                
 _('Autho
@@ -2048,29 +2048,72 @@
  header 
-is invalid.')
+must contain two space-delimited values.'),%0A            
 )%0A%0A     
@@ -2162,24 +2162,111 @@
 lf, token):%0A
+        %22%22%22%0A        Extracts a data payload from the given JSON web token.%0A        %22%22%22%0A
         try:
@@ -2459,24 +2459,140 @@
 , payload):%0A
+        %22%22%22%0A        Extracts a recognizable user identifier from the given data payload%0A        object.%0A        %22%22%22%0A
         try:
@@ -2723,16 +2723,29 @@
 ined no 
+recognizable 
 user ide
@@ -2790,24 +2790,138 @@
 , user_id):%0A
+        %22%22%22%0A        Attempts to find and return a user record with the given user%0A        identifier.%0A        %22%22%22%0A
         try:

Update rest_framework_simplejwt.authentication