Skip to content
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

Only provide scopes when set in options #1053

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 1 addition & 5 deletions src/Provider/AbstractProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -624,11 +624,7 @@ public function getAccessToken($grant, array $options = [])
{
$grant = $this->verifyGrant($grant);

if (empty($options['scope'])) {
$options['scope'] = $this->getDefaultScopes();
}

if (is_array($options['scope'])) {
if (isset($options['scope']) && is_array($options['scope'])) {
$separator = $this->getScopeSeparator();
$options['scope'] = implode($separator, $options['scope']);
}
Expand Down
3 changes: 1 addition & 2 deletions test/src/Grant/PasswordTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ protected function getParamExpectation()
return !empty($body['grant_type'])
&& $body['grant_type'] === 'password'
&& !empty($body['username'])
&& !empty($body['password'])
&& !empty($body['scope']);
&& !empty($body['password']);
};
}

Expand Down
67 changes: 66 additions & 1 deletion test/src/Provider/AbstractProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -632,7 +632,7 @@ public function testGetAccessToken($method)
->once()
->with(
['client_id' => 'mock_client_id', 'client_secret' => 'mock_secret', 'redirect_uri' => 'none'],
['code' => 'mock_authorization_code', 'scope' => 'test']
['code' => 'mock_authorization_code']
)
->andReturn([]);

Expand Down Expand Up @@ -675,6 +675,71 @@ public function testGetAccessToken($method)
});
}

/**
* @dataProvider getAccessTokenMethodProvider
*/
#[DataProvider('getAccessTokenMethodProvider')]
public function testGetAccessTokenWithScope($method)
{
$provider = new MockProvider([
'clientId' => 'mock_client_id',
'clientSecret' => 'mock_secret',
'redirectUri' => 'none',
]);

$provider->setAccessTokenMethod($method);

$raw_response = ['access_token' => 'okay', 'expires' => time() + 3600, 'resource_owner_id' => 3];

$grant = Mockery::mock(AbstractGrant::class);
$grant
->shouldReceive('prepareRequestParameters')
->once()
->with(
['client_id' => 'mock_client_id', 'client_secret' => 'mock_secret', 'redirect_uri' => 'none'],
['code' => 'mock_authorization_code', 'scope' => 'foo,bar']
)
->andReturn([]);

$stream = Mockery::mock(StreamInterface::class);
$stream
->shouldReceive('__toString')
->once()
->andReturn(json_encode($raw_response));

$response = Mockery::mock(ResponseInterface::class);
$response
->shouldReceive('getBody')
->once()
->andReturn($stream);
$response
->shouldReceive('getHeader')
->once()
->with('content-type')
->andReturn(['application/json']);

$client = Mockery::spy(ClientInterface::class, [
'send' => $response,
]);

$provider->setHttpClient($client);
$token = $provider->getAccessToken($grant, ['code' => 'mock_authorization_code', 'scope' => ['foo', 'bar']]);

$this->assertInstanceOf(AccessTokenInterface::class, $token);

$this->assertSame($raw_response['resource_owner_id'], $token->getResourceOwnerId());
$this->assertSame($raw_response['access_token'], $token->getToken());
$this->assertSame($raw_response['expires'], $token->getExpires());

$client
->shouldHaveReceived('send')
->once()
->withArgs(function ($request) use ($provider) {
return $request->getMethod() === $provider->getAccessTokenMethod()
&& (string) $request->getUri() === $provider->getBaseAccessTokenUrl([]);
});
}

public function testGetAccessTokenWithNonJsonResponse()
{
$provider = $this->getMockProvider();
Expand Down
Loading