oauth2-server/tests/Grant/ImplicitGrantTest.php

454 lines
17 KiB
PHP
Raw Normal View History

2016-03-10 20:40:08 +05:30
<?php
namespace LeagueTests\Grant;
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()
{
$this->cryptStub = new CryptTraitStub();
2016-03-18 01:48:28 +05:30
}
2016-03-10 20:40:08 +05:30
public function testGetIdentifier()
{
2016-04-10 20:44:01 +05:30
$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'));
$this->assertFalse(
$grant->canRespondToAccessTokenRequest(new ServerRequest())
);
}
/**
* @expectedException \LogicException
*/
public function testRespondToAccessTokenRequest()
{
$grant = new ImplicitGrant(new \DateInterval('PT10M'));
$grant->respondToAccessTokenRequest(
new ServerRequest(),
new StubResponseType(),
new \DateInterval('PT10M')
);
}
public function testCanRespondToAuthorizationRequest()
2016-03-10 20:40:08 +05:30
{
2016-04-10 20:44:01 +05:30
$grant = new ImplicitGrant(new \DateInterval('PT10M'));
2016-03-10 20:40:08 +05:30
$request = new ServerRequest(
[],
[],
null,
null,
'php://input',
2016-04-10 20:44:01 +05:30
$headers = [],
$cookies = [],
$queryParams = [
2016-03-10 23:10:28 +05:30
'response_type' => 'token',
2016-04-10 20:44:01 +05:30
'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);
$scopeRepositoryMock->method('finalizeScopes')->willReturnArgument(0);
2016-04-10 20:44:01 +05:30
$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(
2016-04-10 20:44:01 +05:30
[],
2016-03-10 20:40:08 +05:30
[],
null,
2016-04-10 20:44:01 +05:30
null,
2016-03-10 20:40:08 +05:30
'php://input',
2016-04-10 20:44:01 +05:30
$headers = [],
$cookies = [],
$queryParams = [
'response_type' => 'code',
2016-03-10 20:40:08 +05:30
'client_id' => 'foo',
2016-04-10 20:44:01 +05:30
'redirect_uri' => 'http://foo/bar',
2016-03-10 20:40:08 +05:30
]
);
2016-04-10 20:44:01 +05:30
$this->assertTrue($grant->validateAuthorizationRequest($request) instanceof AuthorizationRequest);
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);
$scopeRepositoryMock->method('finalizeScopes')->willReturnArgument(0);
2016-04-10 20:44:01 +05:30
$grant = new ImplicitGrant(new \DateInterval('PT10M'));
$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(
2016-04-10 20:44:01 +05:30
[],
2016-03-10 20:40:08 +05:30
[],
null,
2016-04-10 20:44:01 +05:30
null,
2016-03-10 20:40:08 +05:30
'php://input',
2016-04-10 20:44:01 +05:30
$headers = [],
$cookies = [],
$queryParams = [
'response_type' => 'code',
'client_id' => 'foo',
'redirect_uri' => 'http://foo/bar',
2016-03-10 20:40:08 +05:30
]
);
2016-04-10 20:44:01 +05:30
$this->assertTrue($grant->validateAuthorizationRequest($request) instanceof AuthorizationRequest);
2016-03-10 20:40:08 +05:30
}
2016-04-10 20:44:01 +05:30
/**
* @expectedException \League\OAuth2\Server\Exception\OAuthServerException
* @expectedExceptionCode 3
*/
public function testValidateAuthorizationRequestMissingClientId()
2016-03-10 20:40:08 +05:30
{
$clientRepositoryMock = $this->getMockBuilder(ClientRepositoryInterface::class)->getMock();
2016-04-10 20:44:01 +05:30
$grant = new ImplicitGrant(new \DateInterval('PT10M'));
2016-03-10 20:40:08 +05:30
$grant->setClientRepository($clientRepositoryMock);
$request = new ServerRequest(
[],
2016-04-10 20:44:01 +05:30
[],
null,
2016-03-10 20:40:08 +05:30
null,
'php://input',
2016-04-10 20:44:01 +05:30
$headers = [],
$cookies = [],
$queryParams = [
'response_type' => 'code',
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
/**
* @expectedException \League\OAuth2\Server\Exception\OAuthServerException
* @expectedExceptionCode 4
*/
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
2016-04-10 20:44:01 +05:30
$grant = new ImplicitGrant(new \DateInterval('PT10M'));
2016-03-10 20:40:08 +05:30
$grant->setClientRepository($clientRepositoryMock);
$request = new ServerRequest(
2016-04-10 20:44:01 +05:30
[],
2016-03-10 20:40:08 +05:30
[],
null,
2016-04-10 20:44:01 +05:30
null,
2016-03-10 20:40:08 +05:30
'php://input',
2016-04-10 20:44:01 +05:30
$headers = [],
$cookies = [],
$queryParams = [
'response_type' => 'code',
2016-03-10 20:40:08 +05:30
'client_id' => 'foo',
]
);
2016-04-10 20:44:01 +05:30
$grant->validateAuthorizationRequest($request);
2016-03-10 20:40:08 +05:30
}
/**
* @expectedException \League\OAuth2\Server\Exception\OAuthServerException
2016-04-10 20:44:01 +05:30
* @expectedExceptionCode 4
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);
2016-04-10 20:44:01 +05:30
$grant = new ImplicitGrant(new \DateInterval('PT10M'));
2016-03-10 20:40:08 +05:30
$grant->setClientRepository($clientRepositoryMock);
$request = new ServerRequest(
[],
2016-04-10 20:44:01 +05:30
[],
null,
2016-03-10 20:40:08 +05:30
null,
'php://input',
2016-04-10 20:44:01 +05:30
$headers = [],
$cookies = [],
$queryParams = [
'response_type' => 'code',
2016-03-10 20:40:08 +05:30
'client_id' => 'foo',
2016-04-10 20:44:01 +05:30
'redirect_uri' => 'http://bar',
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
/**
* @expectedException \League\OAuth2\Server\Exception\OAuthServerException
* @expectedExceptionCode 4
*/
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);
2016-04-10 20:44:01 +05:30
$grant = new ImplicitGrant(new \DateInterval('PT10M'));
2016-03-10 20:40:08 +05:30
$grant->setClientRepository($clientRepositoryMock);
$request = new ServerRequest(
[],
2016-04-10 20:44:01 +05:30
[],
null,
2016-03-10 20:40:08 +05:30
null,
'php://input',
2016-04-10 20:44:01 +05:30
$headers = [],
$cookies = [],
$queryParams = [
'response_type' => 'code',
2016-03-10 20:40:08 +05:30
'client_id' => 'foo',
2016-04-10 20:44:01 +05:30
'redirect_uri' => 'http://bar',
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
{
2016-04-10 20:44:01 +05:30
$authRequest = new AuthorizationRequest();
$authRequest->setAuthorizationApproved(true);
$authRequest->setClient(new ClientEntity());
$authRequest->setGrantTypeId('authorization_code');
$authRequest->setUser(new UserEntity());
2016-03-10 20:40:08 +05:30
$accessTokenRepositoryMock = $this->getMockBuilder(AccessTokenRepositoryInterface::class)->getMock();
2016-04-10 20:44:01 +05:30
$accessTokenRepositoryMock->method('getNewToken')->willReturn(new AccessTokenEntity());
$accessTokenRepositoryMock->method('persistNewAccessToken')->willReturnSelf();
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);
2016-03-10 20:40:08 +05:30
2016-04-10 20:44:01 +05:30
$this->assertTrue($grant->completeAuthorizationRequest($authRequest) instanceof RedirectResponse);
2016-03-10 20:40:08 +05:30
}
/**
* @expectedException \League\OAuth2\Server\Exception\OAuthServerException
* @expectedExceptionCode 9
*/
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
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);
2016-03-10 20:40:08 +05:30
2016-04-10 20:44:01 +05:30
$grant->completeAuthorizationRequest($authRequest);
2016-03-10 20:40:08 +05:30
}
public function testAccessTokenRepositoryUniqueConstraintCheck()
{
$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->expects($this->at(0))->method('persistNewAccessToken')->willThrowException(UniqueTokenIdentifierConstraintViolationException::create());
$accessTokenRepositoryMock->expects($this->at(1))->method('persistNewAccessToken')->willReturnSelf();
$grant = new ImplicitGrant(new \DateInterval('PT10M'));
$grant->setPrivateKey(new CryptKey('file://' . __DIR__ . '/../Stubs/private.key'));
$grant->setAccessTokenRepository($accessTokenRepositoryMock);
$this->assertTrue($grant->completeAuthorizationRequest($authRequest) instanceof RedirectResponse);
}
/**
* @expectedException \League\OAuth2\Server\Exception\OAuthServerException
* @expectedExceptionCode 7
*/
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'));
$grant = new ImplicitGrant(new \DateInterval('PT10M'));
$grant->setPrivateKey(new CryptKey('file://' . __DIR__ . '/../Stubs/private.key'));
$grant->setAccessTokenRepository($accessTokenRepositoryMock);
$grant->completeAuthorizationRequest($authRequest);
}
/**
* @expectedException \League\OAuth2\Server\Exception\UniqueTokenIdentifierConstraintViolationException
* @expectedExceptionCode 100
*/
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());
$grant = new ImplicitGrant(new \DateInterval('PT10M'));
$grant->setPrivateKey(new CryptKey('file://' . __DIR__ . '/../Stubs/private.key'));
$grant->setAccessTokenRepository($accessTokenRepositoryMock);
$grant->completeAuthorizationRequest($authRequest);
}
/**
* @expectedException \LogicException
*/
public function testSetRefreshTokenTTL()
{
$grant = new ImplicitGrant(new \DateInterval('PT10M'));
$grant->setRefreshTokenTTL(new \DateInterval('PT10M'));
}
/**
* @expectedException \LogicException
*/
public function testSetRefreshTokenRepository()
{
$grant = new ImplicitGrant(new \DateInterval('PT10M'));
$refreshTokenRepositoryMock = $this->getMockBuilder(RefreshTokenRepositoryInterface::class)->getMock();
$grant->setRefreshTokenRepository($refreshTokenRepositoryMock);
}
/**
* @expectedException \LogicException
*/
public function testCompleteAuthorizationRequestNoUser()
{
$grant = new ImplicitGrant(new \DateInterval('PT10M'));
$grant->completeAuthorizationRequest(new AuthorizationRequest());
}
/**
* @expectedException \League\OAuth2\Server\Exception\OAuthServerException
* @expectedExceptionCode 5
*/
public function testValidateAuthorizationRequestFailsWithoutScope()
{
$client = new ClientEntity();
$client->setRedirectUri('http://foo/bar');
$clientRepositoryMock = $this->getMockBuilder(ClientRepositoryInterface::class)->getMock();
$clientRepositoryMock->method('getClientEntity')->willReturn($client);
$scopeRepositoryMock = $this->getMockBuilder(ScopeRepositoryInterface::class)->getMock();
$scopeEntity = new ScopeEntity();
$scopeRepositoryMock->method('getScopeEntityByIdentifier')->willReturn($scopeEntity);
$scopeRepositoryMock->method('finalizeScopes')->willReturnArgument(0);
$grant = new ImplicitGrant(new \DateInterval('PT10M'));
$grant->setClientRepository($clientRepositoryMock);
$grant->setScopeRepository($scopeRepositoryMock);
$request = new ServerRequest(
[],
[],
null,
null,
'php://input',
$headers = [],
$cookies = [],
$queryParams = [
'response_type' => 'code',
'client_id' => 'foo',
'redirect_uri' => 'http://foo/bar',
]
);
$grant->validateAuthorizationRequest($request);
}
2016-03-10 20:40:08 +05:30
}