Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

knox.auth.TokenAuthenticationd does not work in DEFAULT_AUTHENTICATION_CLASSES #371

Open
wilfredinni opened this issue Oct 23, 2024 · 1 comment

Comments

@wilfredinni
Copy link

I am having problems setting the default rest framework authentication class to knox.auth.TokenAuthentication. It seems that in the views I have to explicitly set authentication_classes = (TokenAuthentication,):
settings.y

REST_FRAMEWORK = {
    #  This is not having an effect at all.
    "DEFAULT_AUTHENTICATION_CLASSES": ("knox.auth.TokenAuthentication",)
}

views.py

from knox.auth import TokenAuthentication


class UserProfileView(generics.RetrieveUpdateAPIView):
    serializer_class = UserProfileSerializer
    authentication_classes = (TokenAuthentication,)  # It won't work without this line
    permission_classes = (permissions.IsAuthenticated,)

    def get_object(self):
        return self.request.user

And if I comment authentication_classes = (TokenAuthentication,) I get:

{
	"detail": "Authentication credentials were not provided."
}

I may be missing something, but I can't figure it out. Thank you!

@lubiah
Copy link

lubiah commented Dec 7, 2024

From the docs
"
You can activate TokenAuthentication on all your views by adding it to REST_FRAMEWORK["DEFAULT_AUTHENTICATION_CLASSES"].

If it is your only default authentication class, remember to overwrite knox's LoginView, otherwise it'll not work, since the login view will require a authentication token to generate a new token, rendering it unusable.

For instance, you can authenticate users using Basic Authentication by simply overwriting knox's LoginView and setting BasicAuthentication as one of the acceptable authentication classes, as follows:
"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants