本文介绍了'WSGIRequest'对象没有属性'success_authenticator'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经做了一个身份验证类:





restapi / settings.py

  REST_FRAMEWORK = {
'DEFAULT_AUTHENTICATION_CLASSES':(
'rest_framework.authentication.BasicAuthentication',
'rest_framework.authentication.SessionAuthentication',
#'rest_framework.authentication.TokenAuthentication',
'restapi.authentication .ExpiringTokenAuthentication',
),
'PAGINATE_BY':10
}

restapi / authentication.py

  import datetime 
从rest_framework.authentication导入TokenAuthentication

class ExpiringTokenAuthentication( TokenAuthentication):
def authenticate_credentials(self,key):
try:
token = self.model.objects.get(key = key)
除了self.model.DoesNotExist:
raise exception.AuthenticationFailed('Invalid token')

如果不是token.user.is_active:
raise exception.AuthenticationFailed('User inactive or deleted')

#这是时间比较所需的
utc_now = datetime.utcnow()
utc_now = utc_now.replace(tzinfo = pytz.utc)

如果令牌。创建< utc_now - timedelta(hours = 24):
raise exception.AuthenticationFailed('Token has expired')

返回token.user,令牌
/ pre>

restapi / tests.py

  def test_get_missions(self):

测试/ mission / return没有错误

response = self。 client.get('/ mission /',HTTP_AUTHORIZATION = self.auth)

在我的测试中,我有一个例外 AttributeError:'WSGIRequest'对象没有属性'success_authenticator'



为什么我有这个错误?如何解决?

解决方案

问题来自于以下行:

  utc_now = datetime.utcnow()

code> AttributeError:'WSGIRequest'对象没有属性'success_authenticator'。



自从我偶然发现这样一个误导的错误消息。



这是我如何解决它:



restapi /authentication.py

  import datetime 
from django.utils.timezone import utc
from rest_framework.authentication import TokenAuthentication
from rest_framework import exceptions

class ExpiringTokenAuthentication(TokenAuthentication):
def authenticate_credentials(self,key):
try:
token = self.model.objects.get(key = key)
除了self.model.DoesNotExist:
raise exceptions.AuthenticationFailed('Invali d令牌')

如果不是token.user.is_active:
raise exceptions.AuthenticationFailed('User inactive or deleted')

utc_now = datetime.datetime。 utcnow()。replace(tzinfo = utc)

如果token.created< utc_now - datetime.timedelta(hours = 24):
raise exception.AuthenticationFailed('Token has expired')

return(token.user,token)


I've made an authentication class just like that :

Token Authentication for RESTful API: should the token be periodically changed?

restapi/settings.py

REST_FRAMEWORK = {
    'DEFAULT_AUTHENTICATION_CLASSES': (
        'rest_framework.authentication.BasicAuthentication',
        'rest_framework.authentication.SessionAuthentication',
        # 'rest_framework.authentication.TokenAuthentication',
        'restapi.authentication.ExpiringTokenAuthentication',
    ),
    'PAGINATE_BY': 10
}

restapi/authentication.py

import datetime
from rest_framework.authentication import TokenAuthentication

class ExpiringTokenAuthentication(TokenAuthentication):
    def authenticate_credentials(self, key):
        try:
            token = self.model.objects.get(key=key)
        except self.model.DoesNotExist:
            raise exceptions.AuthenticationFailed('Invalid token')

        if not token.user.is_active:
            raise exceptions.AuthenticationFailed('User inactive or deleted')

        # This is required for the time comparison
        utc_now = datetime.utcnow()
        utc_now = utc_now.replace(tzinfo=pytz.utc)

        if token.created < utc_now - timedelta(hours=24):
            raise exceptions.AuthenticationFailed('Token has expired')

        return token.user, token

restapi/tests.py

def test_get_missions(self):
    """
    Tests that /missions/ returns with no error
    """
    response = self.client.get('/missions/', HTTP_AUTHORIZATION = self.auth)

In my tests, I have an exception AttributeError: 'WSGIRequest' object has no attribute 'successful_authenticator'

Why am I having this error? How to fix it?

解决方案

The problem comes from the line:

utc_now = datetime.utcnow()

which causes AttributeError: 'WSGIRequest' object has no attribute 'successful_authenticator'.

It's been a while since I've stumbled upon such a misleading error message.

Here is how I solved it:

restapi/authentication.py

import datetime
from django.utils.timezone import utc
from rest_framework.authentication import TokenAuthentication
from rest_framework import exceptions

class ExpiringTokenAuthentication(TokenAuthentication):
    def authenticate_credentials(self, key):
        try:
            token = self.model.objects.get(key=key)
        except self.model.DoesNotExist:
            raise exceptions.AuthenticationFailed('Invalid token')

        if not token.user.is_active:
            raise exceptions.AuthenticationFailed('User inactive or deleted')

        utc_now = datetime.datetime.utcnow().replace(tzinfo=utc)

        if token.created < utc_now - datetime.timedelta(hours=24):
            raise exceptions.AuthenticationFailed('Token has expired')

        return (token.user, token)

这篇关于'WSGIRequest'对象没有属性'success_authenticator'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-02 08:16