def authenticate_credentials(self, payload): """ Returns an active user that matches the payload's user id and email. """ User = get_user_model() username = jwt_get_username_from_payload(payload) if not username: msg = _('Invalid payload.') raise exceptions.AuthenticationFailed(msg) try: user = User.objects.get_by_natural_key(username) except User.DoesNotExist: msg = _('Invalid signature.') raise exceptions.AuthenticationFailed(msg) if not user.is_active: msg = _('User account is disabled.') raise exceptions.AuthenticationFailed(msg) return user
import jwt from calendar import timegm from datetime import datetime, timedelta from django.contrib.auth import authenticate from django.utils.translation import ugettext as _ from rest_framework_3 import serializers from .compat import Serializer from rest_framework_jwt_courb.settings import api_settings from rest_framework_jwt_courb.compat import (get_user_model, get_username_field, PasswordField) User = get_user_model() jwt_payload_handler = api_settings.JWT_PAYLOAD_HANDLER jwt_encode_handler = api_settings.JWT_ENCODE_HANDLER jwt_decode_handler = api_settings.JWT_DECODE_HANDLER jwt_get_username_from_payload = api_settings.JWT_PAYLOAD_GET_USERNAME_HANDLER class JSONWebTokenSerializer(Serializer): """ Serializer class used to validate a username and password. 'username' is identified by the custom UserModel.USERNAME_FIELD. Returns a JSON Web Token that can be used to authenticate later calls. """ def __init__(self, *args, **kwargs): """
from distutils.version import StrictVersion import rest_framework from django.test import TestCase from django.utils import unittest from rest_framework_jwt_courb.compat import get_user_model from rest_framework_jwt_courb.serializers import JSONWebTokenSerializer from rest_framework_jwt import utils User = get_user_model() drf2 = rest_framework.VERSION < StrictVersion('3.0.0') drf3 = rest_framework.VERSION >= StrictVersion('3.0.0') class JSONWebTokenSerializerTests(TestCase): def setUp(self): self.email = '*****@*****.**' self.username = '******' self.password = '******' self.user = User.objects.create_user( self.username, self.email, self.password) self.data = { 'username': self.username, 'password': self.password } @unittest.skipUnless(drf2, 'not supported in this version') def test_empty_drf2(self):