Skip to content
This repository has been archived by the owner on May 26, 2020. It is now read-only.

using JSONWebTokenAuthentication with DRF IsAuthenticatedOrReadOnly permission throws 401 on GET #254

Closed
lucibit opened this issue Aug 17, 2016 · 3 comments

Comments

@lucibit
Copy link

lucibit commented Aug 17, 2016

If I'm trying to build rest endpoints (single view) that allow both safe (GET) and unsafe methods, whilst relying only on JSONWebTokenAuthentication as authentication class, I get a 401 on GET instead of the request succeeding.

Reason for this is that JSONWebTokenAuthentication throws an exception if the token is invalid instead of not setting request.user.

https://github.com/GetBlimp/django-rest-framework-jwt/blob/master/rest_framework_jwt/authentication.py#L36

The default behavior for SessionAuthentication in django is to simply not set user on request, leaving the request unauthenticated and letting the permissions do their thing after.

There are several ways around this, as far as I can tell:

  1. Do not send Authenticate header from the client for the GET requests.
  2. Build a separate view for the GET endpoints i want to expose and set authentication_class explicitly to not try to authenticate.

None of these are however ideal for several reasons:

  1. What if I want custom behavior on the GET endpoint depending on wether I'm authenticated or not.
  2. It's nice and efficient(and RESTFUL) to use a single view with post/get/put/delete for a resource.
  3. It doesn't tie in nicely with DRF's or django's permissions.

I'm not sure wether this was built on purpose because of the JWT standard but It would be nice to be able to control this behavior from setting perhaps? OR maybe some other elegant solution.

@dopeboy
Copy link

dopeboy commented Sep 21, 2016

@lucibit - I don't have anything to offer to this discussion. I was stuck on this and found your post via google. Ended up with going with workaround (1) on my client. Thanks for posting that.

@oliverbienert
Copy link

Thank you for posting this! Workaround (1) also works for me.

@angvp
Copy link
Contributor

angvp commented Jun 12, 2017

👍

@angvp angvp closed this as completed Jun 12, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants