<commit_before>from __future__ import unicode_literals

from django.contrib.auth import get_user_model
from django.utils.translation import ugettext_lazy as _
from jose import jwt
from rest_framework.authentication import BaseAuthentication, get_authorization_header
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)
        token = self.get_token(header)
        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):
        return get_authorization_header(request)

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

        if not parts or parts[0] != AUTH_HEADER_TYPE_BYTES:
            return None

        if len(parts) == 1:
            raise AuthenticationFailed(_('No token provided.'))
        elif len(parts) > 2:
            raise AuthenticationFailed(_('Token should not contain spaces.'))

        return parts[1]

    def get_payload(self, token):
        try:
            return jwt.decode(token, SECRET_KEY, algorithms=['HS256'])
        except TypeError:
            raise AuthenticationFailed(_('Token was 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 inactive.'))

        return user<commit_msg>Update authentication.JWTAuthentication<commit_after>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
{'fraction_non_alphanumeric': 0.08579944645518416, 'fraction_numerical': 0.004045135192676176, 'mean_word_length': 2.892294946147473, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4218004', 'n_tokens_mistral': 1381, 'n_tokens_neox': 1320, 'n_words': 320}