Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork9.7k
[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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Conversation
bb06f61 to337d008Comparea52eb5d to6f9f981Compare
chalasr left a comment
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
ro0NL commentedAug 19, 2021 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
What about keeping isAuthenticated + a throwing getUser? (i tend to prefer a random UnauthenticatedTokenException, rather than a random null value) |
nicolas-grekas commentedAug 19, 2021 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
We should then make that I think I prefer making getUser nullable personally. |
chalasr commentedAug 19, 2021
Also we come from |
wouterj commentedAug 19, 2021 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
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 |
ro0NL commentedAug 19, 2021
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. |
ro0NL commentedAug 19, 2021 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
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. |
nicolas-grekas commentedAug 20, 2021
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 |
6f9f981 tod9cd41cComparero0NL commentedAug 20, 2021
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. |
ro0NL commentedAug 20, 2021
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. |
nicolas-grekas commentedAug 20, 2021 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
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? |
ro0NL commentedAug 20, 2021 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
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 in53215e2
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)? |
wouterj commentedAug 20, 2021
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.4Commits-------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.