WSGIRequest object does not have attribute 'success_authenticator'

I made the authentication class this way:

Token authentication for RESTful APIs: should the token be changed periodically?

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 AttributeError: 'WSGIRequest' object has no attribute 'successful_authenticator' exception AttributeError: 'WSGIRequest' object has no attribute 'successful_authenticator'

Why do I have such an error? How to fix it?

+5
authentication django django-rest-framework token
May 24 '13 at 1:55
source share
1 answer

The problem comes from the line:

 utc_now = datetime.utcnow() 

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

Some time has passed since I stumbled upon such a misleading error message.

Here's 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) 
+6
May 24 '13 at 16:11
source share



All Articles