permission.AllowAny not working in knoxLoginView

466 views Asked by At

I tried using the knoxLoginView for login with token authentication,and it was stated on knox documentation that for the loginView to work and not throw a 401 unauthorized error i have to either add permission class "AllowAny" or override the authentication class with BasicAuthentication. I tried both neither worked, the login view still throws 401 unauthorized error.The surprising thing is i saw a youtube tutorial where a guy used the permission.AllowAny and it worked, don't know why its not working for me. here's the login view code:

class LoginAPI(KnoxLoginView):
    permission_classes = (permissions.AllowAny,)
    
    def post(self, request, format=None):
        serializer = AuthTokenSerializer(data=request.data)
        serializer.is_valid(raise_exception=True)
        user = serializer.validated_data['user']
        login(request, user)
        return super(LoginAPI, self).post(request, format=None)

settings.py:

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'rest_framework',
    'knox',
    'corsheaders',
    'users',
    
]

REST_FRAMEWORK = {
    'DEFAULT_PERMISSION_CLASSES': [
            'rest_framework.permissions.AllowAny',
        ],
    'DEFAULT_AUTHENTICATION_CLASSES': [
        #'rest_framework.authentication.BasicAuthentication' ,
        'knox.auth.TokenAuthentication',
        
    ]
    }
1

There are 1 answers

0
hakeem2000 On

For anyone also facing this problem and having the whole of stackoverflow not even attempting to help, incase you have this in your urls file:

path('path_url/', include('knox.urls')),

remove it because somehow it's denying permissions and insisting on giving authorization,that's why it will only work if you change your authentication to BasicAuthentication,probably a knox authentication bug.