diff --git a/src/ResponseTypes/AbstractResponseType.php b/src/ResponseTypes/AbstractResponseType.php index d013bab02..586e2dc02 100644 --- a/src/ResponseTypes/AbstractResponseType.php +++ b/src/ResponseTypes/AbstractResponseType.php @@ -35,6 +35,11 @@ abstract class AbstractResponseType implements ResponseTypeInterface */ protected $privateKey; + /** + * @var boolean + */ + protected $returnScopes = false; + /** * {@inheritdoc} */ @@ -60,4 +65,14 @@ public function setPrivateKey(CryptKey $key) { $this->privateKey = $key; } + + /** + * Whether to include scopes to response params. Defaults to `false`. + * + * @param boolean $returnScopes + */ + public function setReturnScopes($returnScopes) + { + $this->returnScopes = $returnScopes; + } } diff --git a/src/ResponseTypes/BearerTokenResponse.php b/src/ResponseTypes/BearerTokenResponse.php index a57573a05..ef2c3d1c1 100644 --- a/src/ResponseTypes/BearerTokenResponse.php +++ b/src/ResponseTypes/BearerTokenResponse.php @@ -13,6 +13,7 @@ use League\OAuth2\Server\Entities\AccessTokenEntityInterface; use League\OAuth2\Server\Entities\RefreshTokenEntityInterface; +use League\OAuth2\Server\Entities\ScopeEntityInterface; use Psr\Http\Message\ResponseInterface; class BearerTokenResponse extends AbstractResponseType @@ -32,6 +33,14 @@ public function generateHttpResponse(ResponseInterface $response) 'access_token' => (string) $jwtAccessToken, ]; + if ($this->returnScopes === true) { + $responseParams['scope'] = implode(" ", array_map( + function (ScopeEntityInterface $scopeEntity) { + return $scopeEntity->getIdentifier(); + }, $this->accessToken->getScopes() + )); + } + if ($this->refreshToken instanceof RefreshTokenEntityInterface) { $refreshToken = $this->encrypt( json_encode( diff --git a/tests/ResponseTypes/BearerResponseTypeTest.php b/tests/ResponseTypes/BearerResponseTypeTest.php index 7f710d927..a4be3e214 100644 --- a/tests/ResponseTypes/BearerResponseTypeTest.php +++ b/tests/ResponseTypes/BearerResponseTypeTest.php @@ -24,18 +24,22 @@ public function testGenerateHttpResponse() $responseType = new BearerTokenResponse($accessTokenRepositoryMock); $responseType->setPrivateKey(new CryptKey('file://' . __DIR__ . '/../Stubs/private.key')); $responseType->setEncryptionKey(base64_encode(random_bytes(36))); + $responseType->setReturnScopes(true); $client = new ClientEntity(); $client->setIdentifier('clientName'); - $scope = new ScopeEntity(); - $scope->setIdentifier('basic'); + $scope1 = new ScopeEntity(); + $scope1->setIdentifier('basic1'); + $scope2 = new ScopeEntity(); + $scope2->setIdentifier('basic2'); $accessToken = new AccessTokenEntity(); $accessToken->setIdentifier('abcdef'); $accessToken->setExpiryDateTime((new \DateTime())->add(new \DateInterval('PT1H'))); $accessToken->setClient($client); - $accessToken->addScope($scope); + $accessToken->addScope($scope1); + $accessToken->addScope($scope2); $refreshToken = new RefreshTokenEntity(); $refreshToken->setIdentifier('abcdef'); @@ -59,6 +63,9 @@ public function testGenerateHttpResponse() $this->assertTrue(isset($json->expires_in)); $this->assertTrue(isset($json->access_token)); $this->assertTrue(isset($json->refresh_token)); + + $this->assertTrue(isset($json->scope)); + $this->assertEquals('basic1 basic2', $json->scope); } public function testGenerateHttpResponseWithExtraParams() @@ -72,14 +79,17 @@ public function testGenerateHttpResponseWithExtraParams() $client = new ClientEntity(); $client->setIdentifier('clientName'); - $scope = new ScopeEntity(); - $scope->setIdentifier('basic'); + $scope1 = new ScopeEntity(); + $scope1->setIdentifier('basic1'); + $scope2 = new ScopeEntity(); + $scope2->setIdentifier('basic2'); $accessToken = new AccessTokenEntity(); $accessToken->setIdentifier('abcdef'); $accessToken->setExpiryDateTime((new \DateTime())->add(new \DateInterval('PT1H'))); $accessToken->setClient($client); - $accessToken->addScope($scope); + $accessToken->addScope($scope1); + $accessToken->addScope($scope2); $refreshToken = new RefreshTokenEntity(); $refreshToken->setIdentifier('abcdef'); @@ -104,6 +114,8 @@ public function testGenerateHttpResponseWithExtraParams() $this->assertTrue(isset($json->access_token)); $this->assertTrue(isset($json->refresh_token)); + $this->assertFalse(isset($json->scope)); + $this->assertTrue(isset($json->foo)); $this->assertEquals('bar', $json->foo); }