Skip to content

Commit

Permalink
Only provide scopes when set in options
Browse files Browse the repository at this point in the history
  • Loading branch information
barryvdh committed Dec 21, 2024
1 parent 7a4e44d commit a325926
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 8 deletions.
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' => 'test']
)
->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' => 'test']);

$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

0 comments on commit a325926

Please sign in to comment.