-
-
Notifications
You must be signed in to change notification settings - Fork 6.7k
This issue was moved to a discussion.
You can continue the conversation there. Go to discussion →
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
Cookie based JWT tokens #480
Comments
So I actually got this to work with some simple changes, here is a small writeup for anyone that might be interested: First thing is to update the /token route:
Notice that I need to Now we need to create a similar class to
So this is the new class we need to replace it with:
This is the same exact code for Your code now becomes:
This works with /docs as well since the browser always sends back httonly cookies! |
FYI - Blog series using this approach with FastAPI by Nils de Bruin - https://medium.com/@nils_29588 |
In the future will this method be included in the docs? I think a httponly cookie is the best way to store the jwt token, and not sending in the body with json. Also thanks @joaodlf i will try your solution in my project in the next couple of days. Edit: Tried it today and it works nicely. |
Isn't this approach still vulnerable to CSRF attacks? |
@cbows yes it is still vulnerable to CSRF. But it's XSS safe (with httponly=true), which sending in the body (and storing on the client) isn't. SameSite is not fully supported in all browsers https://caniuse.com/#search=samesite yet. But i do think as well, that it does minimize the vulnerability a lot, if you don't have some cross-origin scenarios. Otherwise yes you have to use a token. in fastapi you could maybe implement it in your jwt claim and store it on the client. and with every request you send it in the header and compare it with the claim I think stuff like this would be awesome to include in the docs. fastapi and also the docs are really awesome. Thats why i think to have the best possible security in the docs is really nice and a big selling point :-) |
I totally agree with you. This definitely should go in the docs. With SameSite=Lax it might even be superior to the current approach in terms of default security. |
According to starlette docs, |
I have to look if it's beeing set in my cookie. But that would make it even more relevant to include this in the docs or even implement a class in fastapi |
@cbows I just checked my cookie and it does not set samesite. current version of fastapi (0.54.1) uses starlette (0.13.2). If you check starlette the current version is 0.13.4 and there it is set in the code. so fastapi first needs to update to 0.13.4 as a dependency. right now we have to set it manually |
I agree the docs should be updated to an example with HttpOnly cookies since I think that's best practice to protect session tokens from XSS |
Just as a heads up, you could follow the way that flask_jwt_extended does things where the function that sets the tokens as cookies also creates csrf tokens: |
FYI, @wshayes broken link (I think) should lead to this: https://archive.is/UsaXo
Perhaps @SelfhostedPro's suggestion which shows:
And leads to: https://archive.is/Bv1ub |
what is latest on this? |
this page is now 404 |
I think a better option is to save the token in a samtesite=Strict cookie
afaik this will prevent csrf and xss attacks |
Would it be an alternative to stick with a short-lived access-token in the body and add an additional long-lived refresh-token in a cookie? |
|
Hi Joao, Many thanks for this code. It is working perfectly with my API. However, I wonder about one important issue. Whenever you try to log out using the interface from swagger (screenshot below), the cookie still keeps in the browser letting to access the secured endpoints you have. Consider these two cases below. In the first case, you can see that the endpoint "/api/v1/users/me" should not allow access because the user is not yet authenticated (this can be checked with the lock open). However because the cookie is still in the browser, you can retrieve the data from the endpoint anyway In this second case, I delete the cookie manually from my browser. After doing that, the behaviour is the one expected (if you try to get data from the endpoint when you are not authenticated you should receive a 401 response, Unauthorized) Wonder how this can be fixed in terms of whenever you logout using the Login/Logout Form you can add/destroy the cookie. Any ideas? |
Hi @BigSamu, it's been quite a while since I've touched a project that uses fastapi, but your conclusion at the end makes sense to me - You'll need to hook into the logout endpoint (possibly via class extension as well?) and delete the cookie. |
This issue was moved to a discussion.
You can continue the conversation there. Go to discussion →
Description
First of all, I want to thank you for FastAPI - It's has been a while since I have been this excited about programming for the web. FastAPI is, so far, a really interesting project.
Looking through the documentation, I can see a very clear and concise practical guide to implement JWT tokens. I can see that the access token is returned as part of the response body:
This appears to be a requirement for the /docs to work as expected (where one can login and execute calls on the fly), this is really cool functionality, but it seems to be tied down to the response body.
I would like to be able to set a secure and httpOnly cookie to hold the access token, as I feel that exposing the access token as part of the response body is detrimental to the security of my application. At the same time, I would like the /docs to remain functional with a cookie based approach.
Would this be straightforward to accomplish? Is this at all supported out of the box by FastAPI?
The text was updated successfully, but these errors were encountered: