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 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
15 changes: 15 additions & 0 deletions src/ResponseTypes/AbstractResponseType.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ abstract class AbstractResponseType implements ResponseTypeInterface
*/
protected $privateKey;

/**
* @var boolean
*/
protected $returnScopes = false;

/**
* {@inheritdoc}
*/
Expand All @@ -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;
}
}
9 changes: 9 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 @@ -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(
Expand Down
24 changes: 18 additions & 6 deletions tests/ResponseTypes/BearerResponseTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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()
Expand All @@ -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');
Expand All @@ -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);
}
Expand Down