Skip to content

Commit b5d8fa3

Browse files
authored
Clean Up POST Parameters Code (#470)
1 parent d59e4dc commit b5d8fa3

File tree

2 files changed

+20
-36
lines changed

2 files changed

+20
-36
lines changed

src/Client/OAuth2Client.php

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -90,17 +90,16 @@ public function redirect(array $scopes = [], array $options = [])
9090
*/
9191
public function getAccessToken(array $options = [])
9292
{
93-
$request = $this->getCurrentRequest();
94-
9593
if (!$this->isStateless()) {
9694
$expectedState = $this->getSession()->get(self::OAUTH2_SESSION_STATE_KEY);
97-
$actualState = $this->getRequestParameter($request, 'state');
95+
$actualState = $this->getRequestParameter('state');
96+
9897
if (!$actualState || ($actualState !== $expectedState)) {
9998
throw new InvalidStateException('Invalid state');
10099
}
101100
}
102101

103-
$code = $this->getRequestParameter($request, 'code');
102+
$code = $this->getRequestParameter('code');
104103

105104
if (!$code) {
106105
throw new MissingAuthorizationCodeException('No "code" parameter was found (usually this is a query parameter)!');
@@ -187,10 +186,14 @@ private function getCurrentRequest()
187186
/**
188187
* @return SessionInterface
189188
*/
190-
private function getSession()
189+
protected function getSession(bool $isPKCE = false)
191190
{
192191
if (!$this->getCurrentRequest()->hasSession()) {
193-
throw new \LogicException('In order to use "state", you must have a session. Set the OAuth2Client to stateless to avoid state');
192+
$errorMessage = $isPKCE ?
193+
'You must have a session to utilize the PKCE OAuth2 Client flow. Ensure you are not utilizing PKCE in a stateless environment.' :
194+
'In order to use "state", you must have a session. Set the OAuth2Client to stateless to avoid state';
195+
196+
throw new \LogicException($errorMessage);
194197
}
195198

196199
return $this->getCurrentRequest()->getSession();
@@ -199,12 +202,10 @@ private function getSession()
199202
/**
200203
* @return string|int|float|bool|null
201204
*/
202-
private function getRequestParameter(Request $request, string $key)
205+
private function getRequestParameter(string $key)
203206
{
204-
if ($request->query->has($key)) {
205-
return $request->query->get($key);
206-
}
207+
$request = $this->getCurrentRequest();
207208

208-
return $request->request->get($key);
209+
return $request->query->has($key) ? $request->query->get($key) : $request->request->get($key);
209210
}
210211
}

src/Client/OAuth2PKCEClient.php

Lines changed: 8 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,9 @@
1010

1111
namespace KnpU\OAuth2ClientBundle\Client;
1212

13-
use League\OAuth2\Client\Provider\AbstractProvider;
1413
use League\OAuth2\Client\Token\AccessToken;
1514
use League\OAuth2\Client\Token\AccessTokenInterface;
16-
use Symfony\Component\HttpFoundation\Exception\SessionNotFoundException;
1715
use Symfony\Component\HttpFoundation\RedirectResponse;
18-
use Symfony\Component\HttpFoundation\RequestStack;
1916
use Symfony\Component\HttpFoundation\Session\SessionInterface;
2017

2118
/**
@@ -27,14 +24,6 @@ class OAuth2PKCEClient extends OAuth2Client
2724
{
2825
public const VERIFIER_KEY = 'pkce_code_verifier';
2926

30-
private RequestStack $requestStack;
31-
32-
public function __construct(AbstractProvider $provider, RequestStack $requestStack)
33-
{
34-
parent::__construct($provider, $requestStack);
35-
$this->requestStack = $requestStack;
36-
}
37-
3827
/**
3928
* Enhance the RedirectResponse prepared by OAuth2Client::redirect() with
4029
* PKCE code challenge and code challenge method parameters.
@@ -66,29 +55,23 @@ public function redirect(array $scopes = [], array $options = [])
6655
*/
6756
public function getAccessToken(array $options = [])
6857
{
69-
if (!$this->getSession()->has(static::VERIFIER_KEY)) {
58+
$session = $this->getSession();
59+
60+
if (!$session->has(static::VERIFIER_KEY)) {
7061
throw new \LogicException('Unable to fetch token from OAuth2 server because there is no PKCE code verifier stored in the session');
7162
}
72-
$pkce = ['code_verifier' => $this->getSession()->get(static::VERIFIER_KEY)];
73-
$this->getSession()->remove(static::VERIFIER_KEY);
63+
64+
$pkce = ['code_verifier' => $session->get(static::VERIFIER_KEY)];
65+
$session->remove(static::VERIFIER_KEY);
7466

7567
return parent::getAccessToken($options + $pkce);
7668
}
7769

7870
/**
7971
* @return SessionInterface
80-
*
81-
* @throws \LogicException When there is no current request
82-
* @throws SessionNotFoundException When session is not set properly [thrown by Request::getSession()]
8372
*/
84-
protected function getSession()
73+
protected function getSession(bool $isPKCE = true)
8574
{
86-
$request = $this->requestStack->getCurrentRequest();
87-
88-
if (!$request) {
89-
throw new \LogicException('There is no "current request", and it is needed to perform this action');
90-
}
91-
92-
return $request->getSession();
75+
return parent::getSession($isPKCE);
9376
}
9477
}

0 commit comments

Comments
 (0)