oauth2-server/tests/Grant/ImplicitGrantTest.php

376 lines
16 KiB
PHP
Raw Normal View History

2016-03-10 20:40:08 +05:30
<?php
namespace LeagueTests\Grant;
use DateInterval;
2016-03-28 20:12:34 +05:30
use League\OAuth2\Server\CryptKey;
use League\OAuth2\Server\Exception\OAuthServerException;
use League\OAuth2\Server\Exception\UniqueTokenIdentifierConstraintViolationException;
2016-03-10 20:40:08 +05:30
use League\OAuth2\Server\Grant\ImplicitGrant;
use League\OAuth2\Server\Repositories\AccessTokenRepositoryInterface;
use League\OAuth2\Server\Repositories\ClientRepositoryInterface;
2016-07-08 18:59:21 +05:30
use League\OAuth2\Server\Repositories\RefreshTokenRepositoryInterface;
2016-09-19 14:36:00 +05:30
use League\OAuth2\Server\Repositories\ScopeRepositoryInterface;
2016-04-10 20:44:01 +05:30
use League\OAuth2\Server\RequestTypes\AuthorizationRequest;
use League\OAuth2\Server\ResponseTypes\RedirectResponse;
2016-04-09 20:16:40 +05:30
use LeagueTests\Stubs\AccessTokenEntity;
2016-03-15 05:40:47 +05:30
use LeagueTests\Stubs\ClientEntity;
use LeagueTests\Stubs\CryptTraitStub;
2016-09-19 14:36:00 +05:30
use LeagueTests\Stubs\ScopeEntity;
2016-03-10 20:40:08 +05:30
use LeagueTests\Stubs\StubResponseType;
use LeagueTests\Stubs\UserEntity;
use PHPUnit\Framework\TestCase;
2016-03-10 20:40:08 +05:30
use Zend\Diactoros\ServerRequest;
class ImplicitGrantTest extends TestCase
2016-03-10 20:40:08 +05:30
{
const DEFAULT_SCOPE = 'basic';
2016-03-18 01:48:28 +05:30
/**
* CryptTrait stub
*/
protected $cryptStub;
public function setUp(): void
2016-03-18 01:48:28 +05:30
{
$this->cryptStub = new CryptTraitStub();
2016-03-18 01:48:28 +05:30
}
2016-03-10 20:40:08 +05:30
public function testGetIdentifier()
{
$grant = new ImplicitGrant(new DateInterval('PT10M'));
2016-03-10 20:40:08 +05:30
$this->assertEquals('implicit', $grant->getIdentifier());
}
2016-04-10 20:44:01 +05:30
public function testCanRespondToAccessTokenRequest()
{
$grant = new ImplicitGrant(new DateInterval('PT10M'));
2016-04-10 20:44:01 +05:30
$this->assertFalse(
$grant->canRespondToAccessTokenRequest(new ServerRequest())
);
}
public function testRespondToAccessTokenRequest()
{
$grant = new ImplicitGrant(new DateInterval('PT10M'));
$this->expectException(\LogicException::class);
2016-04-10 20:44:01 +05:30
$grant->respondToAccessTokenRequest(
new ServerRequest(),
new StubResponseType(),
new DateInterval('PT10M')
2016-04-10 20:44:01 +05:30
);
}
public function testCanRespondToAuthorizationRequest()
2016-03-10 20:40:08 +05:30
{
$grant = new ImplicitGrant(new DateInterval('PT10M'));
2016-03-10 20:40:08 +05:30
$request = (new ServerRequest())->withQueryParams([
'response_type' => 'token',
'client_id' => 'foo',
]);
2016-03-10 20:40:08 +05:30
2016-04-10 20:44:01 +05:30
$this->assertTrue($grant->canRespondToAuthorizationRequest($request));
2016-03-10 20:40:08 +05:30
}
2016-04-10 20:44:01 +05:30
public function testValidateAuthorizationRequest()
2016-03-10 20:40:08 +05:30
{
$client = new ClientEntity();
$client->setRedirectUri('http://foo/bar');
$clientRepositoryMock = $this->getMockBuilder(ClientRepositoryInterface::class)->getMock();
$clientRepositoryMock->method('getClientEntity')->willReturn($client);
2016-09-19 14:36:00 +05:30
$scopeRepositoryMock = $this->getMockBuilder(ScopeRepositoryInterface::class)->getMock();
$scopeEntity = new ScopeEntity();
$scopeRepositoryMock->method('getScopeEntityByIdentifier')->willReturn($scopeEntity);
$grant = new ImplicitGrant(new DateInterval('PT10M'));
2016-03-10 20:40:08 +05:30
$grant->setClientRepository($clientRepositoryMock);
2016-09-19 14:36:00 +05:30
$grant->setScopeRepository($scopeRepositoryMock);
$grant->setDefaultScope(self::DEFAULT_SCOPE);
2016-03-10 20:40:08 +05:30
$request = (new ServerRequest())->withQueryParams([
'response_type' => 'code',
'client_id' => 'foo',
'redirect_uri' => 'http://foo/bar',
]);
2016-03-10 20:40:08 +05:30
2017-12-07 01:54:42 +05:30
$this->assertInstanceOf(AuthorizationRequest::class, $grant->validateAuthorizationRequest($request));
2016-03-10 20:40:08 +05:30
}
2016-04-10 20:44:01 +05:30
public function testValidateAuthorizationRequestRedirectUriArray()
2016-03-10 20:40:08 +05:30
{
2016-04-10 20:44:01 +05:30
$client = new ClientEntity();
$client->setRedirectUri(['http://foo/bar']);
$clientRepositoryMock = $this->getMockBuilder(ClientRepositoryInterface::class)->getMock();
$clientRepositoryMock->method('getClientEntity')->willReturn($client);
2016-09-19 14:36:00 +05:30
$scopeRepositoryMock = $this->getMockBuilder(ScopeRepositoryInterface::class)->getMock();
$scopeEntity = new ScopeEntity();
$scopeRepositoryMock->method('getScopeEntityByIdentifier')->willReturn($scopeEntity);
$grant = new ImplicitGrant(new DateInterval('PT10M'));
2016-04-10 20:44:01 +05:30
$grant->setClientRepository($clientRepositoryMock);
2016-09-19 14:36:00 +05:30
$grant->setScopeRepository($scopeRepositoryMock);
$grant->setDefaultScope(self::DEFAULT_SCOPE);
2016-03-10 20:40:08 +05:30
$request = (new ServerRequest())->withQueryParams([
'response_type' => 'code',
'client_id' => 'foo',
'redirect_uri' => 'http://foo/bar',
]);
2016-03-10 20:40:08 +05:30
2017-12-07 01:54:42 +05:30
$this->assertInstanceOf(AuthorizationRequest::class, $grant->validateAuthorizationRequest($request));
2016-03-10 20:40:08 +05:30
}
2016-04-10 20:44:01 +05:30
public function testValidateAuthorizationRequestMissingClientId()
2016-03-10 20:40:08 +05:30
{
$clientRepositoryMock = $this->getMockBuilder(ClientRepositoryInterface::class)->getMock();
$grant = new ImplicitGrant(new DateInterval('PT10M'));
2016-03-10 20:40:08 +05:30
$grant->setClientRepository($clientRepositoryMock);
$request = (new ServerRequest())->withQueryParams(['response_type' => 'code']);
$this->expectException(\League\OAuth2\Server\Exception\OAuthServerException::class);
$this->expectExceptionCode(3);
2016-03-10 20:40:08 +05:30
2016-04-10 20:44:01 +05:30
$grant->validateAuthorizationRequest($request);
2016-03-10 20:40:08 +05:30
}
2016-04-10 20:44:01 +05:30
public function testValidateAuthorizationRequestInvalidClientId()
2016-03-10 20:40:08 +05:30
{
$clientRepositoryMock = $this->getMockBuilder(ClientRepositoryInterface::class)->getMock();
2016-04-10 20:44:01 +05:30
$clientRepositoryMock->method('getClientEntity')->willReturn(null);
2016-03-10 20:40:08 +05:30
$grant = new ImplicitGrant(new DateInterval('PT10M'));
2016-03-10 20:40:08 +05:30
$grant->setClientRepository($clientRepositoryMock);
$request = (new ServerRequest())->withQueryParams([
'response_type' => 'code',
'client_id' => 'foo',
]);
$this->expectException(\League\OAuth2\Server\Exception\OAuthServerException::class);
$this->expectExceptionCode(4);
2016-03-10 20:40:08 +05:30
2016-04-10 20:44:01 +05:30
$grant->validateAuthorizationRequest($request);
2016-03-10 20:40:08 +05:30
}
2016-04-10 20:44:01 +05:30
public function testValidateAuthorizationRequestBadRedirectUriString()
2016-03-10 20:40:08 +05:30
{
$client = new ClientEntity();
$client->setRedirectUri('http://foo/bar');
$clientRepositoryMock = $this->getMockBuilder(ClientRepositoryInterface::class)->getMock();
$clientRepositoryMock->method('getClientEntity')->willReturn($client);
$grant = new ImplicitGrant(new DateInterval('PT10M'));
2016-03-10 20:40:08 +05:30
$grant->setClientRepository($clientRepositoryMock);
$request = (new ServerRequest())->withQueryParams([
'response_type' => 'code',
'client_id' => 'foo',
'redirect_uri' => 'http://bar',
]);
$this->expectException(\League\OAuth2\Server\Exception\OAuthServerException::class);
$this->expectExceptionCode(4);
2016-03-10 20:40:08 +05:30
2016-04-10 20:44:01 +05:30
$grant->validateAuthorizationRequest($request);
2016-03-10 20:40:08 +05:30
}
2016-04-10 20:44:01 +05:30
public function testValidateAuthorizationRequestBadRedirectUriArray()
2016-03-10 20:40:08 +05:30
{
$client = new ClientEntity();
2016-04-10 20:44:01 +05:30
$client->setRedirectUri(['http://foo/bar']);
2016-03-10 20:40:08 +05:30
$clientRepositoryMock = $this->getMockBuilder(ClientRepositoryInterface::class)->getMock();
$clientRepositoryMock->method('getClientEntity')->willReturn($client);
$grant = new ImplicitGrant(new DateInterval('PT10M'));
2016-03-10 20:40:08 +05:30
$grant->setClientRepository($clientRepositoryMock);
$request = (new ServerRequest())->withQueryParams([
'response_type' => 'code',
'client_id' => 'foo',
'redirect_uri' => 'http://bar',
]);
$this->expectException(\League\OAuth2\Server\Exception\OAuthServerException::class);
$this->expectExceptionCode(4);
2016-03-10 20:40:08 +05:30
2016-04-10 20:44:01 +05:30
$grant->validateAuthorizationRequest($request);
2016-03-10 20:40:08 +05:30
}
2016-04-10 20:44:01 +05:30
public function testCompleteAuthorizationRequest()
2016-03-10 20:40:08 +05:30
{
2018-06-24 18:08:55 +05:30
$client = new ClientEntity();
$client->setIdentifier('identifier');
2016-04-10 20:44:01 +05:30
$authRequest = new AuthorizationRequest();
$authRequest->setAuthorizationApproved(true);
2018-06-24 18:08:55 +05:30
$authRequest->setClient($client);
2016-04-10 20:44:01 +05:30
$authRequest->setGrantTypeId('authorization_code');
$authRequest->setUser(new UserEntity());
2016-03-10 20:40:08 +05:30
2018-06-24 18:08:55 +05:30
$accessToken = new AccessTokenEntity();
$accessToken->setClient($client);
$accessTokenRepositoryMock = $this->getMockBuilder(AccessTokenRepositoryInterface::class)->getMock();
2018-06-24 18:08:55 +05:30
$accessTokenRepositoryMock->method('getNewToken')->willReturn($accessToken);
$accessTokenRepositoryMock->method('persistNewAccessToken')->willReturnSelf();
2018-07-13 15:17:32 +05:30
$scopeRepositoryMock = $this->getMockBuilder(ScopeRepositoryInterface::class)->getMock();
$scopeRepositoryMock->method('finalizeScopes')->willReturnArgument(0);
2016-04-10 20:44:01 +05:30
$grant = new ImplicitGrant(new \DateInterval('PT10M'));
2016-03-28 20:12:34 +05:30
$grant->setPrivateKey(new CryptKey('file://' . __DIR__ . '/../Stubs/private.key'));
2016-04-10 20:44:01 +05:30
$grant->setAccessTokenRepository($accessTokenRepositoryMock);
2018-07-13 15:17:32 +05:30
$grant->setScopeRepository($scopeRepositoryMock);
2016-03-10 20:40:08 +05:30
2017-12-07 01:54:42 +05:30
$this->assertInstanceOf(RedirectResponse::class, $grant->completeAuthorizationRequest($authRequest));
2016-03-10 20:40:08 +05:30
}
2016-04-10 20:44:01 +05:30
public function testCompleteAuthorizationRequestDenied()
2016-03-10 20:40:08 +05:30
{
2016-04-10 20:44:01 +05:30
$authRequest = new AuthorizationRequest();
$authRequest->setAuthorizationApproved(false);
$authRequest->setClient(new ClientEntity());
$authRequest->setGrantTypeId('authorization_code');
$authRequest->setUser(new UserEntity());
2016-03-10 20:40:08 +05:30
2016-04-10 20:44:01 +05:30
$accessTokenRepositoryMock = $this->getMockBuilder(AccessTokenRepositoryInterface::class)->getMock();
$accessTokenRepositoryMock->method('getNewToken')->willReturn(new AccessTokenEntity());
$accessTokenRepositoryMock->method('persistNewAccessToken')->willReturnSelf();
2016-03-10 20:40:08 +05:30
2018-07-13 15:17:32 +05:30
$scopeRepositoryMock = $this->getMockBuilder(ScopeRepositoryInterface::class)->getMock();
$scopeRepositoryMock->method('finalizeScopes')->willReturnArgument(0);
2016-04-10 20:44:01 +05:30
$grant = new ImplicitGrant(new \DateInterval('PT10M'));
2016-03-28 20:12:34 +05:30
$grant->setPrivateKey(new CryptKey('file://' . __DIR__ . '/../Stubs/private.key'));
2016-04-10 20:44:01 +05:30
$grant->setAccessTokenRepository($accessTokenRepositoryMock);
2018-07-13 15:17:32 +05:30
$grant->setScopeRepository($scopeRepositoryMock);
2016-03-10 20:40:08 +05:30
$this->expectException(\League\OAuth2\Server\Exception\OAuthServerException::class);
$this->expectExceptionCode(9);
2016-04-10 20:44:01 +05:30
$grant->completeAuthorizationRequest($authRequest);
2016-03-10 20:40:08 +05:30
}
public function testAccessTokenRepositoryUniqueConstraintCheck()
{
2018-06-24 18:08:55 +05:30
$client = new ClientEntity();
$client->setIdentifier('identifier');
$authRequest = new AuthorizationRequest();
$authRequest->setAuthorizationApproved(true);
2018-06-24 18:08:55 +05:30
$authRequest->setClient($client);
$authRequest->setGrantTypeId('authorization_code');
$authRequest->setUser(new UserEntity());
2018-06-24 18:08:55 +05:30
$accessToken = new AccessTokenEntity();
$accessToken->setClient($client);
/** @var AccessTokenRepositoryInterface|\PHPUnit_Framework_MockObject_MockObject $accessTokenRepositoryMock */
$accessTokenRepositoryMock = $this->getMockBuilder(AccessTokenRepositoryInterface::class)->getMock();
2018-06-24 18:08:55 +05:30
$accessTokenRepositoryMock->method('getNewToken')->willReturn($accessToken);
$accessTokenRepositoryMock->expects($this->at(0))->method('persistNewAccessToken')->willThrowException(UniqueTokenIdentifierConstraintViolationException::create());
$accessTokenRepositoryMock->expects($this->at(1))->method('persistNewAccessToken')->willReturnSelf();
2018-07-13 15:17:32 +05:30
$scopeRepositoryMock = $this->getMockBuilder(ScopeRepositoryInterface::class)->getMock();
$scopeRepositoryMock->method('finalizeScopes')->willReturnArgument(0);
$grant = new ImplicitGrant(new \DateInterval('PT10M'));
$grant->setPrivateKey(new CryptKey('file://' . __DIR__ . '/../Stubs/private.key'));
$grant->setAccessTokenRepository($accessTokenRepositoryMock);
2018-07-13 15:17:32 +05:30
$grant->setScopeRepository($scopeRepositoryMock);
2017-12-07 01:54:42 +05:30
$this->assertInstanceOf(RedirectResponse::class, $grant->completeAuthorizationRequest($authRequest));
}
public function testAccessTokenRepositoryFailToPersist()
{
$authRequest = new AuthorizationRequest();
$authRequest->setAuthorizationApproved(true);
$authRequest->setClient(new ClientEntity());
$authRequest->setGrantTypeId('authorization_code');
$authRequest->setUser(new UserEntity());
/** @var AccessTokenRepositoryInterface|\PHPUnit_Framework_MockObject_MockObject $accessTokenRepositoryMock */
$accessTokenRepositoryMock = $this->getMockBuilder(AccessTokenRepositoryInterface::class)->getMock();
$accessTokenRepositoryMock->method('getNewToken')->willReturn(new AccessTokenEntity());
$accessTokenRepositoryMock->method('persistNewAccessToken')->willThrowException(OAuthServerException::serverError('something bad happened'));
2018-07-13 15:17:32 +05:30
$scopeRepositoryMock = $this->getMockBuilder(ScopeRepositoryInterface::class)->getMock();
$scopeRepositoryMock->method('finalizeScopes')->willReturnArgument(0);
$grant = new ImplicitGrant(new \DateInterval('PT10M'));
$grant->setPrivateKey(new CryptKey('file://' . __DIR__ . '/../Stubs/private.key'));
$grant->setAccessTokenRepository($accessTokenRepositoryMock);
2018-07-13 15:17:32 +05:30
$grant->setScopeRepository($scopeRepositoryMock);
$this->expectException(\League\OAuth2\Server\Exception\OAuthServerException::class);
$this->expectExceptionCode(7);
$grant->completeAuthorizationRequest($authRequest);
}
public function testAccessTokenRepositoryFailToPersistUniqueNoInfiniteLoop()
{
$authRequest = new AuthorizationRequest();
$authRequest->setAuthorizationApproved(true);
$authRequest->setClient(new ClientEntity());
$authRequest->setGrantTypeId('authorization_code');
$authRequest->setUser(new UserEntity());
/** @var AccessTokenRepositoryInterface|\PHPUnit_Framework_MockObject_MockObject $accessTokenRepositoryMock */
$accessTokenRepositoryMock = $this->getMockBuilder(AccessTokenRepositoryInterface::class)->getMock();
$accessTokenRepositoryMock->method('getNewToken')->willReturn(new AccessTokenEntity());
$accessTokenRepositoryMock->method('persistNewAccessToken')->willThrowException(UniqueTokenIdentifierConstraintViolationException::create());
2018-07-13 15:17:32 +05:30
$scopeRepositoryMock = $this->getMockBuilder(ScopeRepositoryInterface::class)->getMock();
$scopeRepositoryMock->method('finalizeScopes')->willReturnArgument(0);
$grant = new ImplicitGrant(new \DateInterval('PT10M'));
$grant->setPrivateKey(new CryptKey('file://' . __DIR__ . '/../Stubs/private.key'));
$grant->setAccessTokenRepository($accessTokenRepositoryMock);
2018-07-13 15:17:32 +05:30
$grant->setScopeRepository($scopeRepositoryMock);
$this->expectException(\League\OAuth2\Server\Exception\UniqueTokenIdentifierConstraintViolationException::class);
$this->expectExceptionCode(100);
$grant->completeAuthorizationRequest($authRequest);
}
public function testSetRefreshTokenTTL()
{
$grant = new ImplicitGrant(new DateInterval('PT10M'));
$this->expectException(\LogicException::class);
$grant->setRefreshTokenTTL(new DateInterval('PT10M'));
}
public function testSetRefreshTokenRepository()
{
$grant = new ImplicitGrant(new DateInterval('PT10M'));
$refreshTokenRepositoryMock = $this->getMockBuilder(RefreshTokenRepositoryInterface::class)->getMock();
$this->expectException(\LogicException::class);
$grant->setRefreshTokenRepository($refreshTokenRepositoryMock);
}
public function testCompleteAuthorizationRequestNoUser()
{
$grant = new ImplicitGrant(new DateInterval('PT10M'));
$this->expectException(\LogicException::class);
$grant->completeAuthorizationRequest(new AuthorizationRequest());
}
2016-03-10 20:40:08 +05:30
}