-
Notifications
You must be signed in to change notification settings - Fork 154
Clean Up POST Parameters Code and OAuth2PKCEClient Code #470
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
b2b83ed to
5af280f
Compare
bocharsky-bw
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.
Great improvement, thank you!
I'm just not sure about that getSession(), before we had 2 different error messages but if we share it the way you did it - we will have only one error message, which will be confusing to see in the OAuth2PKCEClient because we don't work with state but with pkce_code_verifier there. So, I would better some duplications as it was before in favor of clarity, unless we have any other solutions to have those separate error messages for different cases
|
@bocharsky-bw I don't disagree and will admit it gave me initial pause, so you're not the only one thinking it. I definitely don't like the idea of the need to inject the RequestStack into Oauth2PKCEClient simply to have a different error message, though. While RequestStack is lighter than it used to be, the redundancy could be a minor performance hinderance. A try/catch block and return the same exception with a different message in the PKCE client? Or passing an optional boolean argument into getSession() that modifies the message? Let me think about that for a minute and I'll push up a change. |
|
@bocharsky-bw Force pushed the updates. Please review. I went with the optional boolean argument in |
|
@bocharsky-bw I just realized something as I was making the changes... I didn't technically need to make these changes:
/**
* @return SessionInterface
*
* @throws \LogicException When there is no current request
* @throws SessionNotFoundException When session is not set properly [thrown by Request::getSession()]
*/
protected function getSession()
{
$request = $this->requestStack->getCurrentRequest();
if (!$request) {
throw new \LogicException('There is no "current request", and it is needed to perform this action');
}
return $request->getSession();
}This /**
* @return Request
*/
private function getCurrentRequest()
{
$request = $this->requestStack->getCurrentRequest();
if (!$request) {
throw new \LogicException('There is no "current request", and it is needed to perform this action');
}
return $request;
}I think this is why my pause on the /**
* @return SessionInterface
*/
private function getSession()
{
if (!$this->getCurrentRequest()->hasSession()) {
throw new \LogicException('In order to use "state", you must have a session. Set the OAuth2Client to stateless to avoid state');
}
return $this->getCurrentRequest()->getSession();
}I think it may be advisable to leave the original Perhaps instead of TL;DR: The "different error messages" concern doesn't actually exist since Thoughts? |
|
As I think about it more, it would make sense to A) Check that a request exists and throw an exception if it doesn't in every situation I'll push something up shortly. |
243f4b1 to
ca11a4a
Compare
|
@bocharsky-bw This should handle everything. |
bocharsky-bw
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.
Nice code improvements, Brian!
And thank you for polishing it with several iterations :)
|
@bocharsky-bw My pleasure. Thanks for all of the incredible suggestions. |
From what I'm seeing, PR #463 cleans up a deprecation in the Request class (https://symfony.com/blog/new-in-symfony-7-4-request-class-improvements) by swapping
$this->getCurrentRequest()->get('state'|'code')with$request->query->get('state'|'code'). This, in turn, removed the ability for the state to be passed via POST. This became an issue for @benndt in #468 who patched it in #469.He was using the "fix" in #463 that defined
$requestat the top ofgetAccessToken()and passing the object as an argument ingetRequestParameter()method. We can just define$requestin that method.We can also use a ternary operator rather than an
ifblock to clean this up a bit.The changes in OAuth2PKCEClient are similar improvements. There is no reason to inject the RequestStack into both OAuth2Client and OAuth2PKCEClient to get the session when the session exists in the parent. Just needed to update the getSession in OAuth2Client to protected. It will handle the test to make sure that the request exists and that the session exists in the parent.