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

Scope param added to a bearer token response #800

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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: 6 additions & 0 deletions src/ResponseTypes/BearerTokenResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -30,6 +31,11 @@ public function generateHttpResponse(ResponseInterface $response)
'token_type' => 'Bearer',
'expires_in' => $expireDateTime - (new \DateTime())->getTimestamp(),
'access_token' => (string) $jwtAccessToken,
'scope' => implode(" ", array_map(
function (ScopeEntityInterface $scopeEntity) {
return $scopeEntity->getIdentifier();
}, $this->accessToken->getScopes()
))
];

if ($this->refreshToken instanceof RefreshTokenEntityInterface) {
Expand Down
24 changes: 18 additions & 6 deletions tests/ResponseTypes/BearerResponseTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,17 @@ public function testGenerateHttpResponse()
$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');
Expand All @@ -59,6 +62,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()
Expand All @@ -72,14 +78,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');
Expand All @@ -104,6 +113,9 @@ public function testGenerateHttpResponseWithExtraParams()
$this->assertTrue(isset($json->access_token));
$this->assertTrue(isset($json->refresh_token));

$this->assertTrue(isset($json->scope));
$this->assertEquals('basic1 basic2', $json->scope);

$this->assertTrue(isset($json->foo));
$this->assertEquals('bar', $json->foo);
}
Expand Down