-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Security] make TokenInterface::getUser() nullable to tell about unauthenticated tokens #42650
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
Conversation
bb06f61
to
337d008
Compare
a52eb5d
to
6f9f981
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So it seems we have no other choice than to keep the concept of unauthenticated tokens in order to workaround the API incompatibility we have between the authentication layer and the authorization one.
I think we can work with that. Thank you
What about keeping isAuthenticated + a throwing getUser? (i tend to prefer a random UnauthenticatedTokenException, rather than a random null value) |
We should then make that I think I prefer making getUser nullable personally. |
Also we come from |
Thanks! I surely prefer a nullable user over reintroducing Nullable return types are not optimal. However, in this case 99% of the apps I see already do something like Btw, I'm personally not sure if adding a new |
it be a contract violation. I proposed it because of less deprecation hassle actually 😅 but yes, it's a try/catch shortcut sure. going from a non-nulllable to nullable is a bigger gap IMHO.
hence my proposal. |
actually, if isAuthenticated() is public API specific tokens can do a more sophisticated check. Where isAuthenticated is leveraged in getUser(). (this can be achieved in both styles btw, but i still tend to prefer explicit isAuthenticated/UnauthenticatedTokenException somewhat :/) even without isAuthenticated, a voter doing the try/catch is still 100% explicit. |
A try/catch is not a nice API to me. nullability works just great. I tend to prefer APIs that don't open traps for implementation bugs with to-enforce correlations between their methods. isAuthenticated should not be used in getUser because they are different views of the same state - and not just correlated states.
that would require knowing about both interfaces to cover the domain, and that would require another interface for |
…thenticated tokens
6f9f981
to
d9cd41c
Compare
sorry, im not convinced (yet). Still i believe as a consumer i'd prefer
But we'll be null checking in 99% of authentcated cases 👍 https://symfony.com/doc/current/security.html#retrieving-the-user-object null check is missing here btw. I think if Security/Controller::getUser can convey authenticated yes/no thru nullability, im not sure the tokenstorage needs to. Nevertheless a nullable Security/Controller::getUser is also pesky :) From the tokenstorage layer i think TokenNotFoundException + new UnauthenticatedTokenException fits yes. |
basically what im saying is, if you want a boolean value there's a access decision manager for it. But 99% the check is encapsulated IMHO. |
not needed because of the call to denyAccessUnlessGranted
Can you be more explicit about that please? Do you mean to always return a NullToken instead of throwing?
yep, we already have this nullable API elsewhere
That's technically possible, I'm just not convinced it's better. Another opinion or argument to help decide? |
it's about runtime. Do we want to halt, or let a null value bubble unexpectedly. btw im 100% aligned with
Here we put such wisdom on real tiles :) (https://en.wikipedia.org/wiki/Tegelspreuken) symfony/src/Symfony/Component/Security/Core/Authentication/Token/Storage/TokenStorageInterface.php Lines 23 to 28 in 53215e2
could we infer unauthenticated state here already? (edit: right, this will trigger our voter + NullToken issue) what if we go back to an UnauthenticatedToken for internal usage (mostly)? |
Thank you Nicolas! |
…) (chalasr) This PR was merged into the 5.4 branch. Discussion ---------- [Security] Fix AuthenticationTrustResolver::isAnonymous() | Q | A | ------------- | --- | Branch? | 5.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | #42726 | License | MIT | Doc PR | - This method wasn't checking if a token is null nor `$token->isAuthenticated()` until #42650. Reverting that behavior change fixes tests on both 5.3 and 5.4 Commits ------- 83da786 [Security] Fix AuthenticationTrustResolver::isAnonymous()
…lasr) This PR was merged into the 5.4 branch. Discussion ---------- [Security] Fix return value of `NullToken::getUser()` | Q | A | ------------- | --- | Branch? | 5.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | #44909 | License | MIT | Doc PR | - We went back & forth on this one but according to the history, we've just forgot to do it in #42650. Note: it's already `null` on 6.0+ Commits ------- d892a51 Fix return value of `NullToken::getUser()`
As discussed in #42644
I think this might work well.