Skip to content

Commit 5b89462

Browse files
authored
Add support for post parameters to OAuth2Client (#468)
1 parent cee9295 commit 5b89462

File tree

2 files changed

+34
-2
lines changed

2 files changed

+34
-2
lines changed

src/Client/OAuth2Client.php

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,13 +93,13 @@ public function getAccessToken(array $options = [])
9393

9494
if (!$this->isStateless()) {
9595
$expectedState = $this->getSession()->get(self::OAUTH2_SESSION_STATE_KEY);
96-
$actualState = $request->query->get('state');
96+
$actualState = $this->getRequestParameter('state');
9797
if (!$actualState || ($actualState !== $expectedState)) {
9898
throw new InvalidStateException('Invalid state');
9999
}
100100
}
101101

102-
$code = $request->query->get('code');
102+
$code = $this->getRequestParameter('code');
103103

104104
if (!$code) {
105105
throw new MissingAuthorizationCodeException('No "code" parameter was found (usually this is a query parameter)!');
@@ -194,4 +194,19 @@ private function getSession()
194194

195195
return $this->getCurrentRequest()->getSession();
196196
}
197+
198+
/**
199+
* @param RequestStack $request
200+
* @param string $key
201+
*
202+
* @return string|int|float|bool|null
203+
*/
204+
private function getRequestParameter(RequestStack $request, string $key)
205+
{
206+
if ($request->query->has($key)) {
207+
return $request->query->get($key);
208+
}
209+
210+
return $request->request->get($key);
211+
}
197212
}

tests/Client/OAuth2ClientTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,23 @@ public function testGetAccessTokenWithOptions()
160160
$this->assertSame($expectedToken, $actualToken);
161161
}
162162

163+
public function testGetAccessTokenFromPOST()
164+
{
165+
$this->request->request->set('code', 'CODE_ABC');
166+
167+
$expectedToken = new AccessToken(['access_token' => 'foo']);
168+
$this->provider->method('getAccessToken')
169+
->with('authorization_code', ['code' => 'CODE_ABC'])
170+
->willReturn($expectedToken);
171+
172+
$client = new OAuth2Client(
173+
$this->provider,
174+
$this->requestStack
175+
);
176+
$client->setAsStateless();
177+
$this->assertSame($expectedToken, $client->getAccessToken());
178+
}
179+
163180
public function testRefreshAccessToken()
164181
{
165182
$existingToken = new AccessToken([

0 commit comments

Comments
 (0)