examtest/test_server/crm/authentication.py

25 lines
767 B
Python

from rest_framework_jwt.authentication import JSONWebTokenAuthentication
from rest_framework_jwt.serializers import jwt_decode_handler
from rest_framework import exceptions
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