0

I want a custom authentication which will authenticate the token in my model.I have a model called User_Auth where there is a field called 'secret_token'. I want the Django Rest Framework to Authenticate token through that model. I had followed the docs on django rest framework website but didnt work for me. Please help me to solve this issue.

models.py

class User_Auth(models.Model):
    id = models.AutoField(primary_key=True)
    user = models.ForeignKey(User_Master, on_delete=models.CASCADE)
    secret_token = models.CharField(max_length=2000, null=True, blank=True)

authentication.py

class UserAuthentication(authentication.TokenAuthentication):
    def authenticate(self, request):
        secret_token = request.META.get('Authorization')

        if not secret_token:
            return None
        
        try:
            ua = User_Auth.objects.get(secret_token=secret_token)
        except User_Auth.DoesNotExist:
            raise exceptions.AuthenticationFailed('Unauthorized')

        return (ua, None)

views.py

class User(viewsets.ModelViewSet):
    authentication_classes = (UserAuthentication,)
    permission_classes = (IsAuthenticated,)

I am passing the token through the Header.

Authorization: 'a7c14fc808b58533e4d1651cd2375c3b41a38ef5d120254a1cb4bbd90b3be1534215516b023818e4'

Its Returning this error

'User_Auth' object has no attribute 'is_authenticated'

Please help me.

5
  • ua = User_Auth.objects.get(secret_token=secret_token) should that line not be ua = User_Auth.objects.get(secret_token=key)? Commented Dec 30, 2020 at 10:04
  • Same error is returning after the changes Commented Dec 30, 2020 at 10:20
  • test it with a curl command. like this. curl -X GET 127.0.0.1:8000/api/example -H 'Authorization: Token a7c14fc808b58533e4d1651cd2375c3b41a38ef5d120254a1cb4bbd90b3be1534215516b023818e4' Commented Dec 30, 2020 at 10:46
  • Also it seems the line if not secret_token: return None is not working Commented Dec 30, 2020 at 10:47
  • if not secret_token: return None was a command in the docs of django rest framework Commented Dec 30, 2020 at 10:52

1 Answer 1

2

You are missing is_authenticated property in User_Auth model. This is a read only attribute and always return True for a valid user. Django's User class has this attribute and internally it has been used for authentication.

So to resolve the error add this attribute in User_Auth class like following.

class User_Auth(models.Model):
    id = models.AutoField(primary_key=True)
    user = models.ForeignKey(User_Master, on_delete=models.CASCADE)
    secret_token = models.CharField(max_length=2000, null=True, blank=True)

    @property
    def is_authenticated(self):
        """
        Always return True. This is a way to tell if the user has been
        authenticated in templates.
        """
        return True
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.