from rest_framework_jwt.authentication import JSONWebTokenAuthentication from rest_framework_jwt.serializers import jwt_decode_handler from rest_framework import exceptions from django.utils.translation import ugettext as _ from crm.models import Consumer class ConsumerTokenAuthentication(JSONWebTokenAuthentication): def authenticate_credentials(self, payload): """ 返回登陆消费者 """ username = payload['username'] if not username: msg = _('签名有误.') raise exceptions.AuthenticationFailed(msg) try: consumer = Consumer.objects.get(username=username) except Consumer.DoesNotExist: msg = _('消费者不存在') raise exceptions.AuthenticationFailed(msg) return consumer