do i need to have CSRF when i use JWT stateless auth?
My understanding of it is, that of i have a JWT (which is generated/encrypted with a secret key), this JWT is added as a cookie and every request/navigation in the frontend the JWT is checked, if its missing or manipulated it will make a logout.
So a csrf isnt necassary because we are stateless or am i wrong?
CSRF is needed whenever cookies are used, regardless of whether the cookie contains a session id or a JWT.
CSRF protects against the fact that an attacker on a separate domain can make the browser send a request that includes the user’s cookie even though the browser’s cross-site restrictions prevent the attacker from reading the response.
Okay thanks, but how to handle it with vaadin? I have tried adding it and i see a XSRF but it makes no sense, if i remove or change the XSRF everything behave the same. So it doesnt matter if the XSRF token is there or not or do i await to much of it :S How can i confirm that it does what it should?
Flow handles CSRF for you so no need to configure that separately with Spring Security for requests that are handled by Flow.
Could also be noted that there’s not really any point in using JWT for regular authentication with Flow since everything else with Flow is anyways stateful. If you get a JWT from an external system such as an OAuth authentication flow, then it’s usually easiest to verify that JWT once and then rely on the regular session for the rest of the session.
Yes, that csrfToken field in the shown JSON payload is the token that is automatically generated and verified by Vaadin.
Sounds like you’re using JWT as a more fancy “remember me” cookie. In that sense, the authentication in the session is the primary source and the JWT is just a fallback. To do things 100% properly with JWT, you still also need a separate mechanism for explicitly invalidating a JWT before it expires. That’s necessary to force logout for a user in case they change their password because their computer was compromised or things like that. And when you implement those things, then most of the benefits of JWT disappear and you could just as well use a simpler random id and store info about that id in the database.
Thanks, what do you mean with a simpler random id? Is there a example anywhere? The most important is, that the user must stay authenticated for x days. If not used he must be logged out. And it should survive a reboot, so the user still is logged in. All that JWT stateless is already working out of the box.
If i change the JWT i get logged out, excactly what i need. Sounds hart to build this by my own
It doesn’t work in the opposite direction without additional infrastructure. If a user’s computer is compromised, then you need to prevent logging in with the token or cookie stored on that computer. This means that you need some state related to the functionality in the database.