2016-02-18 17:37:50 +05:30
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace LeagueTests;
|
|
|
|
|
2016-04-10 20:28:01 +05:30
|
|
|
use League\OAuth2\Server\CryptKey;
|
2016-03-10 22:04:32 +05:30
|
|
|
use League\OAuth2\Server\Exception\OAuthServerException;
|
|
|
|
use League\OAuth2\Server\Grant\AuthCodeGrant;
|
2016-02-18 17:37:50 +05:30
|
|
|
use League\OAuth2\Server\Grant\ClientCredentialsGrant;
|
|
|
|
use League\OAuth2\Server\Repositories\AccessTokenRepositoryInterface;
|
2016-03-10 22:04:32 +05:30
|
|
|
use League\OAuth2\Server\Repositories\AuthCodeRepositoryInterface;
|
2016-02-20 04:39:39 +05:30
|
|
|
use League\OAuth2\Server\Repositories\ClientRepositoryInterface;
|
2016-03-10 22:04:32 +05:30
|
|
|
use League\OAuth2\Server\Repositories\RefreshTokenRepositoryInterface;
|
2016-02-18 17:37:50 +05:30
|
|
|
use League\OAuth2\Server\Repositories\ScopeRepositoryInterface;
|
2016-04-10 20:28:01 +05:30
|
|
|
use League\OAuth2\Server\RequestTypes\AuthorizationRequest;
|
2016-03-10 22:04:32 +05:30
|
|
|
use League\OAuth2\Server\ResponseTypes\BearerTokenResponse;
|
2016-02-20 04:39:39 +05:30
|
|
|
use League\OAuth2\Server\Server;
|
2016-04-09 20:16:40 +05:30
|
|
|
use LeagueTests\Stubs\AccessTokenEntity;
|
|
|
|
use LeagueTests\Stubs\AuthCodeEntity;
|
2016-03-15 05:40:47 +05:30
|
|
|
use LeagueTests\Stubs\ClientEntity;
|
2016-02-20 04:39:39 +05:30
|
|
|
use LeagueTests\Stubs\StubResponseType;
|
2016-03-18 04:55:32 +05:30
|
|
|
use LeagueTests\Stubs\UserEntity;
|
2016-02-18 17:37:50 +05:30
|
|
|
use Psr\Http\Message\ResponseInterface;
|
2016-03-18 04:55:32 +05:30
|
|
|
use Zend\Diactoros\Response;
|
2016-04-10 20:28:01 +05:30
|
|
|
use Zend\Diactoros\ServerRequest;
|
2016-03-18 04:55:32 +05:30
|
|
|
use Zend\Diactoros\ServerRequestFactory;
|
2016-02-18 17:37:50 +05:30
|
|
|
|
|
|
|
class ServerTest extends \PHPUnit_Framework_TestCase
|
|
|
|
{
|
|
|
|
public function testRespondToRequestInvalidGrantType()
|
|
|
|
{
|
|
|
|
$server = new Server(
|
|
|
|
$this->getMock(ClientRepositoryInterface::class),
|
|
|
|
$this->getMock(AccessTokenRepositoryInterface::class),
|
|
|
|
$this->getMock(ScopeRepositoryInterface::class),
|
2016-03-28 20:40:41 +05:30
|
|
|
'file://' . __DIR__ . '/Stubs/private.key',
|
|
|
|
'file://' . __DIR__ . '/Stubs/public.key',
|
2016-02-18 17:37:50 +05:30
|
|
|
new StubResponseType()
|
|
|
|
);
|
|
|
|
|
|
|
|
$server->enableGrantType(new ClientCredentialsGrant(), new \DateInterval('PT1M'));
|
|
|
|
|
2016-03-11 04:53:29 +05:30
|
|
|
try {
|
2016-04-09 20:50:30 +05:30
|
|
|
$server->respondToAccessTokenRequest(ServerRequestFactory::fromGlobals(), new Response);
|
2016-03-11 04:53:29 +05:30
|
|
|
} catch (OAuthServerException $e) {
|
|
|
|
$this->assertEquals('unsupported_grant_type', $e->getErrorType());
|
|
|
|
$this->assertEquals(400, $e->getHttpStatusCode());
|
|
|
|
}
|
2016-02-18 17:37:50 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
public function testRespondToRequest()
|
|
|
|
{
|
|
|
|
$clientRepository = $this->getMock(ClientRepositoryInterface::class);
|
|
|
|
$clientRepository->method('getClientEntity')->willReturn(new ClientEntity());
|
|
|
|
|
2016-03-24 00:20:14 +05:30
|
|
|
$scopeRepositoryMock = $this->getMockBuilder(ScopeRepositoryInterface::class)->getMock();
|
|
|
|
$scopeRepositoryMock->method('finalizeScopes')->willReturnArgument(0);
|
|
|
|
|
2016-03-25 18:39:26 +05:30
|
|
|
$accessTokenRepositoryMock = $this->getMock(AccessTokenRepositoryInterface::class);
|
|
|
|
$accessTokenRepositoryMock->method('getNewToken')->willReturn(new AccessTokenEntity());
|
|
|
|
|
2016-02-18 17:37:50 +05:30
|
|
|
$server = new Server(
|
|
|
|
$clientRepository,
|
2016-03-25 18:39:26 +05:30
|
|
|
$accessTokenRepositoryMock,
|
2016-03-24 00:20:14 +05:30
|
|
|
$scopeRepositoryMock,
|
2016-03-28 20:40:41 +05:30
|
|
|
'file://' . __DIR__ . '/Stubs/private.key',
|
|
|
|
'file://' . __DIR__ . '/Stubs/public.key',
|
2016-02-18 17:37:50 +05:30
|
|
|
new StubResponseType()
|
|
|
|
);
|
|
|
|
|
|
|
|
$server->enableGrantType(new ClientCredentialsGrant(), new \DateInterval('PT1M'));
|
|
|
|
|
|
|
|
$_POST['grant_type'] = 'client_credentials';
|
|
|
|
$_POST['client_id'] = 'foo';
|
|
|
|
$_POST['client_secret'] = 'bar';
|
2016-04-09 20:50:30 +05:30
|
|
|
$response = $server->respondToAccessTokenRequest(ServerRequestFactory::fromGlobals(), new Response);
|
2016-02-18 17:37:50 +05:30
|
|
|
$this->assertEquals(200, $response->getStatusCode());
|
|
|
|
}
|
2016-03-10 22:04:32 +05:30
|
|
|
|
2016-04-10 20:28:01 +05:30
|
|
|
public function testGetResponseType()
|
2016-03-10 22:04:32 +05:30
|
|
|
{
|
|
|
|
$clientRepository = $this->getMock(ClientRepositoryInterface::class);
|
2016-03-24 00:20:14 +05:30
|
|
|
|
2016-03-10 22:04:32 +05:30
|
|
|
$server = new Server(
|
|
|
|
$clientRepository,
|
|
|
|
$this->getMock(AccessTokenRepositoryInterface::class),
|
2016-04-10 20:28:01 +05:30
|
|
|
$this->getMock(ScopeRepositoryInterface::class),
|
2016-03-18 04:55:32 +05:30
|
|
|
'file://' . __DIR__ . '/Stubs/private.key',
|
2016-04-10 20:28:01 +05:30
|
|
|
'file://' . __DIR__ . '/Stubs/public.key'
|
2016-03-10 22:04:32 +05:30
|
|
|
);
|
|
|
|
|
2016-04-10 20:28:01 +05:30
|
|
|
$abstractGrantReflection = new \ReflectionClass($server);
|
|
|
|
$method = $abstractGrantReflection->getMethod('getResponseType');
|
|
|
|
$method->setAccessible(true);
|
2016-03-18 04:55:32 +05:30
|
|
|
|
2016-04-10 20:28:01 +05:30
|
|
|
$this->assertTrue($method->invoke($server) instanceof BearerTokenResponse);
|
|
|
|
}
|
2016-03-25 18:39:26 +05:30
|
|
|
|
2016-04-10 20:28:01 +05:30
|
|
|
public function testValidateAuthenticatedRequest()
|
|
|
|
{
|
|
|
|
$clientRepository = $this->getMock(ClientRepositoryInterface::class);
|
|
|
|
|
|
|
|
$server = new Server(
|
|
|
|
$clientRepository,
|
|
|
|
$this->getMock(AccessTokenRepositoryInterface::class),
|
|
|
|
$this->getMock(ScopeRepositoryInterface::class),
|
|
|
|
'file://' . __DIR__ . '/Stubs/private.key',
|
|
|
|
'file://' . __DIR__ . '/Stubs/public.key'
|
2016-03-10 22:04:32 +05:30
|
|
|
);
|
|
|
|
|
2016-04-10 20:28:01 +05:30
|
|
|
try {
|
|
|
|
$server->validateAuthenticatedRequest(ServerRequestFactory::fromGlobals());
|
|
|
|
} catch (OAuthServerException $e) {
|
|
|
|
$this->assertEquals('Missing "Authorization" header', $e->getHint());
|
|
|
|
}
|
2016-03-10 22:04:32 +05:30
|
|
|
}
|
|
|
|
|
2016-04-10 20:28:01 +05:30
|
|
|
public function testCompleteAuthorizationRequest()
|
2016-03-10 22:04:32 +05:30
|
|
|
{
|
|
|
|
$clientRepository = $this->getMock(ClientRepositoryInterface::class);
|
|
|
|
|
|
|
|
$server = new Server(
|
|
|
|
$clientRepository,
|
|
|
|
$this->getMock(AccessTokenRepositoryInterface::class),
|
|
|
|
$this->getMock(ScopeRepositoryInterface::class),
|
2016-03-28 20:40:41 +05:30
|
|
|
'file://' . __DIR__ . '/Stubs/private.key',
|
|
|
|
'file://' . __DIR__ . '/Stubs/public.key'
|
2016-03-10 22:04:32 +05:30
|
|
|
);
|
|
|
|
|
2016-04-10 20:28:01 +05:30
|
|
|
$authCodeRepository = $this->getMockBuilder(AuthCodeRepositoryInterface::class)->getMock();
|
|
|
|
$authCodeRepository->method('getNewAuthCode')->willReturn(new AuthCodeEntity());
|
2016-03-10 22:04:32 +05:30
|
|
|
|
2016-04-10 20:28:01 +05:30
|
|
|
$grant = new AuthCodeGrant(
|
|
|
|
$authCodeRepository,
|
|
|
|
$this->getMock(RefreshTokenRepositoryInterface::class),
|
|
|
|
new \DateInterval('PT10M')
|
|
|
|
);
|
|
|
|
|
|
|
|
$grant->setPrivateKey(new CryptKey('file://' . __DIR__ . '/Stubs/private.key'));
|
|
|
|
$grant->setPublicKey(new CryptKey('file://' . __DIR__ . '/Stubs/public.key'));
|
|
|
|
|
|
|
|
$server->enableGrantType($grant);
|
|
|
|
|
|
|
|
$authRequest = new AuthorizationRequest();
|
|
|
|
$authRequest->setAuthorizationApproved(true);
|
|
|
|
$authRequest->setClient(new ClientEntity());
|
|
|
|
$authRequest->setGrantTypeId('authorization_code');
|
|
|
|
$authRequest->setUser(new UserEntity());
|
|
|
|
|
|
|
|
$this->assertTrue(
|
|
|
|
$server->completeAuthorizationRequest($authRequest, new Response) instanceof ResponseInterface
|
|
|
|
);
|
2016-03-10 22:04:32 +05:30
|
|
|
}
|
|
|
|
|
2016-04-10 20:28:01 +05:30
|
|
|
public function testValidateAuthorizationRequest()
|
2016-03-10 22:04:32 +05:30
|
|
|
{
|
2016-04-10 20:28:01 +05:30
|
|
|
$client = new ClientEntity();
|
|
|
|
$clientRepositoryMock = $this->getMockBuilder(ClientRepositoryInterface::class)->getMock();
|
|
|
|
$clientRepositoryMock->method('getClientEntity')->willReturn($client);
|
|
|
|
|
|
|
|
$grant = new AuthCodeGrant(
|
|
|
|
$this->getMock(AuthCodeRepositoryInterface::class),
|
|
|
|
$this->getMock(RefreshTokenRepositoryInterface::class),
|
|
|
|
new \DateInterval('PT10M')
|
|
|
|
);
|
|
|
|
$grant->setClientRepository($clientRepositoryMock);
|
2016-03-10 22:04:32 +05:30
|
|
|
|
|
|
|
$server = new Server(
|
2016-04-10 20:28:01 +05:30
|
|
|
$clientRepositoryMock,
|
2016-03-10 22:04:32 +05:30
|
|
|
$this->getMock(AccessTokenRepositoryInterface::class),
|
|
|
|
$this->getMock(ScopeRepositoryInterface::class),
|
2016-03-28 20:40:41 +05:30
|
|
|
'file://' . __DIR__ . '/Stubs/private.key',
|
|
|
|
'file://' . __DIR__ . '/Stubs/public.key'
|
2016-03-10 22:04:32 +05:30
|
|
|
);
|
2016-04-10 20:28:01 +05:30
|
|
|
$server->enableGrantType($grant);
|
|
|
|
|
|
|
|
$request = new ServerRequest(
|
|
|
|
[],
|
|
|
|
[],
|
|
|
|
null,
|
|
|
|
null,
|
|
|
|
'php://input',
|
|
|
|
$headers = [],
|
|
|
|
$cookies = [],
|
|
|
|
$queryParams = [
|
|
|
|
'response_type' => 'code',
|
|
|
|
'client_id' => 'foo',
|
|
|
|
]
|
|
|
|
);
|
2016-03-10 22:04:32 +05:30
|
|
|
|
2016-04-10 20:28:01 +05:30
|
|
|
$this->assertTrue($server->validateAuthorizationRequest($request) instanceof AuthorizationRequest);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException \League\OAuth2\Server\Exception\OAuthServerException
|
|
|
|
* @expectedExceptionCode 2
|
|
|
|
*/
|
|
|
|
public function testValidateAuthorizationRequestUnregistered()
|
|
|
|
{
|
|
|
|
$server = new Server(
|
|
|
|
$this->getMock(ClientRepositoryInterface::class),
|
|
|
|
$this->getMock(AccessTokenRepositoryInterface::class),
|
|
|
|
$this->getMock(ScopeRepositoryInterface::class),
|
|
|
|
'file://' . __DIR__ . '/Stubs/private.key',
|
|
|
|
'file://' . __DIR__ . '/Stubs/public.key'
|
|
|
|
);
|
|
|
|
|
|
|
|
$request = new ServerRequest(
|
|
|
|
[],
|
|
|
|
[],
|
|
|
|
null,
|
|
|
|
null,
|
|
|
|
'php://input',
|
|
|
|
$headers = [],
|
|
|
|
$cookies = [],
|
|
|
|
$queryParams = [
|
|
|
|
'response_type' => 'code',
|
|
|
|
'client_id' => 'foo',
|
|
|
|
]
|
|
|
|
);
|
|
|
|
|
|
|
|
$server->validateAuthorizationRequest($request);
|
2016-03-10 22:04:32 +05:30
|
|
|
}
|
2016-02-18 17:37:50 +05:30
|
|
|
}
|