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

Jwt cookie bis #321

Open
wants to merge 20 commits into
base: master
Choose a base branch
from
Open

Jwt cookie bis #321

wants to merge 20 commits into from

Conversation

loicgasser
Copy link

@loicgasser loicgasser commented Oct 19, 2020

This is the work I have done based on #157
I addressed all the comments in the pull requests, added some tests, rebased it to master, fixed the built, refactored the code and changed the approach to a "more secure" approach.

In my view, leaving the access and refresh token in the response body defies the purpose of putting the token in a httpOnly cookie.
The only way local storage can be compromised is if your application is already compromised with XSS, in which case it would be easy to call the server and retrieve both the access and refresh token.
Now, if an attacker was able to inject a script in your site, he would also be able to send a request to one of his server and could extract the access and refresh token. This is why if you don't use SameSite with values Strict or Lax, this approach has no real impact on security, since it would only raise the difficulty for attackers to steal the credentials.

Note that the SameSite setting is not available in older browsers like Internet Explorer which still account for around 6% of the global market.

So I would argue that storing the tokens in a httpOnly cookie is better, but only marginally better. In my case I decided to not support IE.

Here is a demo that uses this branch with Angular:

SimpleJWT/drf-SimpleJWT-Angular#1

This pull request still needs doc. But has it a chance to be merged?

NiyazNz and others added 20 commits October 18, 2020 18:52
… work at the same time when AUTH_COOKIE is enabled
…esh view name if required

Set `token_refresh_view_name` argument in `as_view` method of TokenObtainPairView and TokenCookieDeleteView in urlpatterns to change refresh token view name

Example:
```python
urlpatterns = [
    path('api/token/', TokenObtainPairView.as_view(token_refresh_view_name='jwt_refresh'), name='token_obtain_pair'),
    path('api/token/delete/', TokenCookieDeleteView.as_view(token_refresh_view_name='jwt_refresh'), name='token_delete'),
]
```
# where TokenRefreshView url name is `jwt_refresh`
@loicgasser loicgasser changed the title [WIP] Jwt cookie bis Jwt cookie bis Oct 19, 2020
@Andrew-Chen-Wang
Copy link
Member

Andrew-Chen-Wang commented Nov 23, 2020

@loicgasser Sorry I didn't look at this earlier. Didn't notice it was changed back from [WIP]. I'll take a look at this and also ask David to also look. Not sure if it'll be settled to not use httpOnly cookies based on the referenced PR/issue.

Also updating master branch might be helpful (plus CI checks look fine besides the workflow one).

if raw_token is None:
return None

validated_token = self.get_validated_token(raw_token)

return self.get_user(validated_token), validated_token
user = self.get_user(validated_token)
if not user or not user.is_active:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is now handled by the new auth rules (we definitely need to update our changelog and documentation)...

@loicgasser
Copy link
Author

loicgasser commented Nov 24, 2020

Yeah, I did rebase it when I first opened it. I can rebase again. It seems that the workflow is broken in all pull requests.

@Andrew-Chen-Wang
Copy link
Member

Andrew-Chen-Wang commented Nov 24, 2020

There still seems to be some merge conflicts @loicgasser And yeah, we fixed the initial problem, but when I was trying to fix it, I managed to duck something up so we're back at square one. But they should be working fine soon. Edit: you may just need to rebase again since we fixed the problem after your last commit.

I wouldn't take too much time to continuously rebase this until we review it again (after not reviewing this thing for awhile). I think we might as well merge this PR considering it's been too long. If anything, if a CVE comes up, we'll resolve it. We can label this feature as experimental as well, but it might as well be published at this point.

@ddehart
Copy link

ddehart commented Dec 21, 2020

This is not a suggestion veiled as a question; I'm genuinely curious.

Is there a good reason not to make authentication via cookie its own authentication class? So, just as I can choose one or more authentication schemes in DRF through authentication_classes in a view, it could be that I want to choose whether to allow JWT cookie authentication or header authentication on a per-view basis.

@Andrew-Chen-Wang
Copy link
Member

Good question. To me, it's just to avoid duplicate code, so it's easier to maintain (especially with a couple of new features that I haven't had time to document).

allow JWT cookie authentication or header authentication on a per-view basis.

I think the code is taking away the need for you to check, so you just don't need to worry about context switching (between mobile Auth header and browser cookies).

Specifically: the if header: return None statement (can't remember the line) was when the Authentication header was not used. Now that there are cookies, if you're not using the Authentication header (it's mostly used for mobile) and instead use cookies (used for your JS frameworks), the if header does not immediately return None but instead checks your cookies.

@AfrazHussain
Copy link

AfrazHussain commented Feb 4, 2022

Any reason why this still isn't merged? Happy to help and contribute as necessary, but I think this is a critical functionality to be merged knowing the fact that most DRF APIs are used with SPAs like React, Vue, and storing tokens in cookies is considered to be one of the safest methods.

@Andrew-Chen-Wang
Copy link
Member

@AfrazHussain This is my preferred PR for JWT cookies. The issue at the moment is 1) resolving merge conflicts and 2) it's just huge. I've never taken the time to look thoroughly through everything. I'm skeptical of tests (I'm always skeptical of tests, so it has nothing specific to do with this PR). And finally, this just needs to be marked as super experimental.

Even though people have been doing cookie stuff with SimpleJWT for awhile, I don't want to encourage people to use cookies with JWTs for their SPAs. But I digress. I just want a very very safe PR for the users, not devs, of Django SPA projects.

@dorneanu-cl
Copy link

Hi! Is there any way how we could support you in speeding up the reviewing process? I took me some while to find out what is the current state of the "Store JWT tokens in Cookies" discussion and finally I've landed here :)

@Andrew-Chen-Wang
Copy link
Member

hi this needs to be updated to master and pass tests with updates docs. another swipe through and we can mark it as experimental with some dangers in its use. then it can be merged

@mij
Copy link

mij commented May 25, 2023

@Andrew-Chen-Wang SimpleJWT is currently implemented in a way which reflects the original concept of JWTs and goes against the best practice consistently recommended around the world. It pushes people to manage their tokens in the JS, which in turn requires them to store the tokens in long-term local storage.

I understand your skepticism, but there's a consideration of Opportunity Cost at play here, too. You have 100+ reactions collectively in tickets and PRs about this specific functional gap.

If you can point out specific tasks to complete in order for you to merge this PR, I am available to contribute some or all of them.
I currently use a middleware to work around this functional gap in SimpleJWT, and I'm sure that many others are using this or other forms of ugly wrappings to get this job done, to the detriment of maintainability. This patch is surely going to improve on that.

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

Successfully merging this pull request may close these issues.

8 participants