Skip to content

Commit 0fa8198

Browse files
authored
Revert BC break by only providing scopes in access token when set in options (#1053)
Partially reverts #1030 This will still allow to set a `scope` on the access token as array and format it properly, but it will not add the default scopes by default. Setting the scope in the access token request is optional according to https://www.rfc-editor.org/rfc/rfc6749#section-3.3
1 parent 56091ce commit 0fa8198

File tree

3 files changed

+68
-8
lines changed

3 files changed

+68
-8
lines changed

src/Provider/AbstractProvider.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -626,11 +626,7 @@ public function getAccessToken($grant, array $options = [])
626626
{
627627
$grant = $this->verifyGrant($grant);
628628

629-
if (empty($options['scope'])) {
630-
$options['scope'] = $this->getDefaultScopes();
631-
}
632-
633-
if (is_array($options['scope'])) {
629+
if (isset($options['scope']) && is_array($options['scope'])) {
634630
$separator = $this->getScopeSeparator();
635631
$options['scope'] = implode($separator, $options['scope']);
636632
}

test/src/Grant/PasswordTest.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,7 @@ protected function getParamExpectation()
2020
return !empty($body['grant_type'])
2121
&& $body['grant_type'] === 'password'
2222
&& !empty($body['username'])
23-
&& !empty($body['password'])
24-
&& !empty($body['scope']);
23+
&& !empty($body['password']);
2524
};
2625
}
2726

test/src/Provider/AbstractProviderTest.php

Lines changed: 66 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -632,7 +632,7 @@ public function testGetAccessToken($method)
632632
->once()
633633
->with(
634634
['client_id' => 'mock_client_id', 'client_secret' => 'mock_secret', 'redirect_uri' => 'none'],
635-
['code' => 'mock_authorization_code', 'scope' => 'test']
635+
['code' => 'mock_authorization_code']
636636
)
637637
->andReturn([]);
638638

@@ -675,6 +675,71 @@ public function testGetAccessToken($method)
675675
});
676676
}
677677

678+
/**
679+
* @dataProvider getAccessTokenMethodProvider
680+
*/
681+
#[DataProvider('getAccessTokenMethodProvider')]
682+
public function testGetAccessTokenWithScope($method)
683+
{
684+
$provider = new MockProvider([
685+
'clientId' => 'mock_client_id',
686+
'clientSecret' => 'mock_secret',
687+
'redirectUri' => 'none',
688+
]);
689+
690+
$provider->setAccessTokenMethod($method);
691+
692+
$raw_response = ['access_token' => 'okay', 'expires' => time() + 3600, 'resource_owner_id' => 3];
693+
694+
$grant = Mockery::mock(AbstractGrant::class);
695+
$grant
696+
->shouldReceive('prepareRequestParameters')
697+
->once()
698+
->with(
699+
['client_id' => 'mock_client_id', 'client_secret' => 'mock_secret', 'redirect_uri' => 'none'],
700+
['code' => 'mock_authorization_code', 'scope' => 'foo,bar']
701+
)
702+
->andReturn([]);
703+
704+
$stream = Mockery::mock(StreamInterface::class);
705+
$stream
706+
->shouldReceive('__toString')
707+
->once()
708+
->andReturn(json_encode($raw_response));
709+
710+
$response = Mockery::mock(ResponseInterface::class);
711+
$response
712+
->shouldReceive('getBody')
713+
->once()
714+
->andReturn($stream);
715+
$response
716+
->shouldReceive('getHeader')
717+
->once()
718+
->with('content-type')
719+
->andReturn(['application/json']);
720+
721+
$client = Mockery::spy(ClientInterface::class, [
722+
'send' => $response,
723+
]);
724+
725+
$provider->setHttpClient($client);
726+
$token = $provider->getAccessToken($grant, ['code' => 'mock_authorization_code', 'scope' => ['foo', 'bar']]);
727+
728+
$this->assertInstanceOf(AccessTokenInterface::class, $token);
729+
730+
$this->assertSame($raw_response['resource_owner_id'], $token->getResourceOwnerId());
731+
$this->assertSame($raw_response['access_token'], $token->getToken());
732+
$this->assertSame($raw_response['expires'], $token->getExpires());
733+
734+
$client
735+
->shouldHaveReceived('send')
736+
->once()
737+
->withArgs(function ($request) use ($provider) {
738+
return $request->getMethod() === $provider->getAccessTokenMethod()
739+
&& (string) $request->getUri() === $provider->getBaseAccessTokenUrl([]);
740+
});
741+
}
742+
678743
public function testGetAccessTokenWithNonJsonResponse()
679744
{
680745
$provider = $this->getMockProvider();

0 commit comments

Comments
 (0)