Skip to content

Commit

Permalink
Merge pull request #230 from akondas/access-token
Browse files Browse the repository at this point in the history
Allow to pass options when fetching access token
  • Loading branch information
weaverryan authored Mar 19, 2020
2 parents fd892fe + 938fc9b commit e0d24f5
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 9 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
/vendor/*
/tests/app/cache
/.php_cs.cache
/.phpunit.result.cache
11 changes: 7 additions & 4 deletions src/Client/OAuth2Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,15 @@ public function redirect(array $scopes = [], array $options = [])
/**
* Call this after the user is redirected back to get the access token.
*
* @param array $options Additional options that should be passed to the getAccessToken() of the underlying provider
*
* @return AccessToken|\League\OAuth2\Client\Token\AccessTokenInterface
*
* @throws InvalidStateException
* @throws MissingAuthorizationCodeException
* @throws IdentityProviderException If token cannot be fetched
*/
public function getAccessToken()
public function getAccessToken(array $options = [])
{
if (!$this->isStateless) {
$expectedState = $this->getSession()->get(self::OAUTH2_SESSION_STATE_KEY);
Expand All @@ -102,9 +104,10 @@ public function getAccessToken()
throw new MissingAuthorizationCodeException('No "code" parameter was found (usually this is a query parameter)!');
}

return $this->provider->getAccessToken('authorization_code', [
'code' => $code,
]);
return $this->provider->getAccessToken(
'authorization_code',
array_merge(['code' => $code], $options)
);
}

/**
Expand Down
4 changes: 3 additions & 1 deletion src/Client/OAuth2ClientInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,15 @@ public function redirect(array $scopes, array $options);
/**
* Call this after the user is redirected back to get the access token.
*
* @param array $options Additional options that should be passed to the getAccessToken() of the underlying provider
*
* @return \League\OAuth2\Client\Token\AccessToken
*
* @throws \KnpU\OAuth2ClientBundle\Exception\InvalidStateException
* @throws \KnpU\OAuth2ClientBundle\Exception\MissingAuthorizationCodeException
* @throws \League\OAuth2\Client\Provider\Exception\IdentityProviderException If token cannot be fetched
*/
public function getAccessToken();
public function getAccessToken(array $options = []);

/**
* Returns the "User" information (called a resource owner).
Expand Down
4 changes: 2 additions & 2 deletions src/Security/Authenticator/SocialAuthenticator.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ abstract class SocialAuthenticator extends AbstractGuardAuthenticator
use PreviousUrlHelper;
use SaveAuthFailureMessage;

protected function fetchAccessToken(OAuth2ClientInterface $client)
protected function fetchAccessToken(OAuth2ClientInterface $client, array $options = [])
{
try {
return $client->getAccessToken();
return $client->getAccessToken($options);
} catch (MissingAuthorizationCodeException $e) {
throw new NoAuthCodeAuthenticationException();
} catch (IdentityProviderException $e) {
Expand Down
20 changes: 20 additions & 0 deletions tests/Client/OAuth2ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,26 @@ public function testGetAccessToken()
$this->assertSame($expectedToken->reveal(), $actualToken);
}

public function testGetAccessTokenWithOptions()
{
$this->request->query->set('state', 'THE_STATE');
$this->request->query->set('code', 'CODE_ABC');

$this->session->get(OAuth2Client::OAUTH2_SESSION_STATE_KEY)
->willReturn('THE_STATE');

$expectedToken = $this->prophesize('League\OAuth2\Client\Token\AccessToken');
$this->provider->getAccessToken('authorization_code', ['code' => 'CODE_ABC', 'redirect_uri' => 'https://some.url'])
->willReturn($expectedToken->reveal());

$client = new OAuth2Client(
$this->provider->reveal(),
$this->requestStack
);
$actualToken = $client->getAccessToken(['redirect_uri' => 'https://some.url']);
$this->assertSame($expectedToken->reveal(), $actualToken);
}

public function testGetAccessTokenFromPOST()
{
$this->request->request->set('code', 'CODE_ABC');
Expand Down
4 changes: 2 additions & 2 deletions tests/Security/Authenticator/SocialAuthenticatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public function testFetchAccessTokenSimplyReturns()
{
$authenticator = new StubSocialAuthenticator();
$client = $this->prophesize('KnpU\OAuth2ClientBundle\Client\OAuth2Client');
$client->getAccessToken()
$client->getAccessToken([])
->willReturn('expected_access_token');

$actualToken = $authenticator->doFetchAccessToken($client->reveal());
Expand All @@ -40,7 +40,7 @@ public function testFetchAccessTokenThrowsAuthenticationException()
$this->expectException(NoAuthCodeAuthenticationException::class);
$authenticator = new StubSocialAuthenticator();
$client = $this->prophesize('KnpU\OAuth2ClientBundle\Client\OAuth2Client');
$client->getAccessToken()
$client->getAccessToken([])
->willThrow(new MissingAuthorizationCodeException());

$authenticator->doFetchAccessToken($client->reveal());
Expand Down

0 comments on commit e0d24f5

Please sign in to comment.