2016-02-21 20:02:27 +05:30
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace LeagueTests\Grant;
|
|
|
|
|
2016-03-28 20:12:34 +05:30
|
|
|
use League\OAuth2\Server\CryptKey;
|
2016-04-09 19:55:45 +05:30
|
|
|
use League\OAuth2\Server\Entities\AccessTokenEntityInterface;
|
|
|
|
use League\OAuth2\Server\Entities\RefreshTokenEntityInterface;
|
2016-02-21 23:43:39 +05:30
|
|
|
use League\OAuth2\Server\Exception\OAuthServerException;
|
2016-06-20 13:48:53 +05:30
|
|
|
use League\OAuth2\Server\Exception\UniqueTokenIdentifierConstraintViolationException;
|
2016-02-21 20:02:27 +05:30
|
|
|
use League\OAuth2\Server\Grant\AuthCodeGrant;
|
2016-02-22 13:30:50 +05:30
|
|
|
use League\OAuth2\Server\Repositories\AccessTokenRepositoryInterface;
|
2016-02-21 20:02:27 +05:30
|
|
|
use League\OAuth2\Server\Repositories\AuthCodeRepositoryInterface;
|
|
|
|
use League\OAuth2\Server\Repositories\ClientRepositoryInterface;
|
|
|
|
use League\OAuth2\Server\Repositories\RefreshTokenRepositoryInterface;
|
2016-03-15 05:40:47 +05:30
|
|
|
use League\OAuth2\Server\Repositories\ScopeRepositoryInterface;
|
2016-04-10 19:45:29 +05:30
|
|
|
use League\OAuth2\Server\RequestTypes\AuthorizationRequest;
|
2016-03-18 04:55:32 +05:30
|
|
|
use League\OAuth2\Server\ResponseTypes\RedirectResponse;
|
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-03-18 01:48:28 +05:30
|
|
|
use LeagueTests\Stubs\CryptTraitStub;
|
2016-04-09 20:16:40 +05:30
|
|
|
use LeagueTests\Stubs\RefreshTokenEntity;
|
2016-03-15 05:40:47 +05:30
|
|
|
use LeagueTests\Stubs\ScopeEntity;
|
2016-02-21 20:02:27 +05:30
|
|
|
use LeagueTests\Stubs\StubResponseType;
|
|
|
|
use LeagueTests\Stubs\UserEntity;
|
|
|
|
use Zend\Diactoros\ServerRequest;
|
|
|
|
|
|
|
|
class AuthCodeGrantTest extends \PHPUnit_Framework_TestCase
|
|
|
|
{
|
2016-03-18 01:48:28 +05:30
|
|
|
/**
|
2016-05-06 19:53:16 +05:30
|
|
|
* @var CryptTraitStub
|
2016-03-18 01:48:28 +05:30
|
|
|
*/
|
|
|
|
protected $cryptStub;
|
|
|
|
|
|
|
|
public function setUp()
|
|
|
|
{
|
|
|
|
$this->cryptStub = new CryptTraitStub;
|
|
|
|
}
|
|
|
|
|
2016-02-21 20:02:27 +05:30
|
|
|
public function testGetIdentifier()
|
|
|
|
{
|
|
|
|
$grant = new AuthCodeGrant(
|
|
|
|
$this->getMock(AuthCodeRepositoryInterface::class),
|
|
|
|
$this->getMock(RefreshTokenRepositoryInterface::class),
|
2016-03-16 17:02:21 +05:30
|
|
|
new \DateInterval('PT10M')
|
2016-02-21 20:02:27 +05:30
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertEquals('authorization_code', $grant->getIdentifier());
|
|
|
|
}
|
|
|
|
|
2016-04-10 19:45:29 +05:30
|
|
|
public function testCanRespondToAuthorizationRequest()
|
2016-02-21 20:02:27 +05:30
|
|
|
{
|
|
|
|
$grant = new AuthCodeGrant(
|
|
|
|
$this->getMock(AuthCodeRepositoryInterface::class),
|
|
|
|
$this->getMock(RefreshTokenRepositoryInterface::class),
|
|
|
|
new \DateInterval('PT10M')
|
|
|
|
);
|
|
|
|
|
|
|
|
$request = new ServerRequest(
|
|
|
|
[],
|
2016-02-22 13:30:50 +05:30
|
|
|
[],
|
2016-02-21 20:02:27 +05:30
|
|
|
null,
|
|
|
|
null,
|
|
|
|
'php://input',
|
|
|
|
$headers = [],
|
|
|
|
$cookies = [],
|
|
|
|
$queryParams = [
|
|
|
|
'response_type' => 'code',
|
2016-02-21 22:38:57 +05:30
|
|
|
'client_id' => 'foo',
|
2016-02-21 20:02:27 +05:30
|
|
|
]
|
|
|
|
);
|
|
|
|
|
2016-04-10 19:45:29 +05:30
|
|
|
$this->assertTrue($grant->canRespondToAuthorizationRequest($request));
|
2016-02-21 20:02:27 +05:30
|
|
|
}
|
|
|
|
|
2016-04-10 19:45:29 +05:30
|
|
|
public function testValidateAuthorizationRequest()
|
2016-02-21 20:02:27 +05:30
|
|
|
{
|
|
|
|
$client = new ClientEntity();
|
|
|
|
$client->setRedirectUri('http://foo/bar');
|
|
|
|
$clientRepositoryMock = $this->getMockBuilder(ClientRepositoryInterface::class)->getMock();
|
|
|
|
$clientRepositoryMock->method('getClientEntity')->willReturn($client);
|
|
|
|
|
|
|
|
$grant = new AuthCodeGrant(
|
2016-04-10 19:45:29 +05:30
|
|
|
$this->getMock(AuthCodeRepositoryInterface::class),
|
2016-02-21 20:02:27 +05:30
|
|
|
$this->getMock(RefreshTokenRepositoryInterface::class),
|
2016-03-16 17:02:21 +05:30
|
|
|
new \DateInterval('PT10M')
|
2016-02-21 20:02:27 +05:30
|
|
|
);
|
|
|
|
$grant->setClientRepository($clientRepositoryMock);
|
2016-02-21 22:38:57 +05:30
|
|
|
|
|
|
|
$request = new ServerRequest(
|
2016-04-10 19:45:29 +05:30
|
|
|
[],
|
2016-02-22 13:30:50 +05:30
|
|
|
[],
|
2016-02-21 22:38:57 +05:30
|
|
|
null,
|
2016-04-10 19:45:29 +05:30
|
|
|
null,
|
2016-02-21 22:38:57 +05:30
|
|
|
'php://input',
|
2016-04-10 19:45:29 +05:30
|
|
|
$headers = [],
|
|
|
|
$cookies = [],
|
|
|
|
$queryParams = [
|
2016-02-21 22:38:57 +05:30
|
|
|
'response_type' => 'code',
|
|
|
|
'client_id' => 'foo',
|
2016-04-10 19:45:29 +05:30
|
|
|
'redirect_uri' => 'http://foo/bar',
|
2016-02-21 22:38:57 +05:30
|
|
|
]
|
|
|
|
);
|
|
|
|
|
2016-04-10 19:45:29 +05:30
|
|
|
$this->assertTrue($grant->validateAuthorizationRequest($request) instanceof AuthorizationRequest);
|
2016-02-21 22:38:57 +05:30
|
|
|
}
|
|
|
|
|
2016-04-10 19:45:29 +05:30
|
|
|
public function testValidateAuthorizationRequestRedirectUriArray()
|
2016-02-21 22:38:57 +05:30
|
|
|
{
|
|
|
|
$client = new ClientEntity();
|
2016-04-10 19:45:29 +05:30
|
|
|
$client->setRedirectUri(['http://foo/bar']);
|
2016-02-21 22:38:57 +05:30
|
|
|
$clientRepositoryMock = $this->getMockBuilder(ClientRepositoryInterface::class)->getMock();
|
|
|
|
$clientRepositoryMock->method('getClientEntity')->willReturn($client);
|
|
|
|
|
|
|
|
$grant = new AuthCodeGrant(
|
|
|
|
$this->getMock(AuthCodeRepositoryInterface::class),
|
|
|
|
$this->getMock(RefreshTokenRepositoryInterface::class),
|
2016-03-16 17:02:21 +05:30
|
|
|
new \DateInterval('PT10M')
|
2016-02-21 22:38:57 +05:30
|
|
|
);
|
|
|
|
$grant->setClientRepository($clientRepositoryMock);
|
|
|
|
|
|
|
|
$request = new ServerRequest(
|
2016-04-10 19:45:29 +05:30
|
|
|
[],
|
2016-02-22 13:30:50 +05:30
|
|
|
[],
|
2016-02-21 22:38:57 +05:30
|
|
|
null,
|
2016-04-10 19:45:29 +05:30
|
|
|
null,
|
2016-02-21 22:38:57 +05:30
|
|
|
'php://input',
|
2016-04-10 19:45:29 +05:30
|
|
|
$headers = [],
|
|
|
|
$cookies = [],
|
|
|
|
$queryParams = [
|
2016-02-21 22:38:57 +05:30
|
|
|
'response_type' => 'code',
|
|
|
|
'client_id' => 'foo',
|
2016-04-10 19:45:29 +05:30
|
|
|
'redirect_uri' => 'http://foo/bar',
|
2016-02-21 22:38:57 +05:30
|
|
|
]
|
|
|
|
);
|
|
|
|
|
2016-04-10 19:45:29 +05:30
|
|
|
$this->assertTrue($grant->validateAuthorizationRequest($request) instanceof AuthorizationRequest);
|
2016-02-21 22:38:57 +05:30
|
|
|
}
|
|
|
|
|
2016-05-06 19:53:16 +05:30
|
|
|
|
|
|
|
public function testValidateAuthorizationRequestCodeChallenge()
|
|
|
|
{
|
|
|
|
$client = new ClientEntity();
|
|
|
|
$client->setRedirectUri('http://foo/bar');
|
|
|
|
$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->enableCodeExchangeProof();
|
|
|
|
$grant->setClientRepository($clientRepositoryMock);
|
|
|
|
|
|
|
|
$request = new ServerRequest(
|
|
|
|
[],
|
|
|
|
[],
|
|
|
|
null,
|
|
|
|
null,
|
|
|
|
'php://input',
|
|
|
|
$headers = [],
|
|
|
|
$cookies = [],
|
|
|
|
$queryParams = [
|
|
|
|
'response_type' => 'code',
|
|
|
|
'client_id' => 'foo',
|
|
|
|
'redirect_uri' => 'http://foo/bar',
|
|
|
|
'code_challenge' => 'FOOBAR',
|
|
|
|
]
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertTrue($grant->validateAuthorizationRequest($request) instanceof AuthorizationRequest);
|
|
|
|
}
|
|
|
|
|
2016-02-21 22:38:57 +05:30
|
|
|
/**
|
|
|
|
* @expectedException \League\OAuth2\Server\Exception\OAuthServerException
|
|
|
|
* @expectedExceptionCode 3
|
|
|
|
*/
|
2016-04-10 19:45:29 +05:30
|
|
|
public function testValidateAuthorizationRequestMissingClientId()
|
2016-02-21 22:38:57 +05:30
|
|
|
{
|
|
|
|
$clientRepositoryMock = $this->getMockBuilder(ClientRepositoryInterface::class)->getMock();
|
|
|
|
|
|
|
|
$grant = new AuthCodeGrant(
|
|
|
|
$this->getMock(AuthCodeRepositoryInterface::class),
|
|
|
|
$this->getMock(RefreshTokenRepositoryInterface::class),
|
2016-03-16 17:02:21 +05:30
|
|
|
new \DateInterval('PT10M')
|
2016-02-21 22:38:57 +05:30
|
|
|
);
|
|
|
|
$grant->setClientRepository($clientRepositoryMock);
|
|
|
|
|
|
|
|
$request = new ServerRequest(
|
2016-02-22 13:30:50 +05:30
|
|
|
[],
|
|
|
|
[],
|
2016-02-21 22:38:57 +05:30
|
|
|
null,
|
2016-04-10 19:45:29 +05:30
|
|
|
null,
|
2016-02-21 22:38:57 +05:30
|
|
|
'php://input',
|
2016-04-10 19:45:29 +05:30
|
|
|
$headers = [],
|
|
|
|
$cookies = [],
|
|
|
|
$queryParams = [
|
2016-02-21 22:38:57 +05:30
|
|
|
'response_type' => 'code',
|
|
|
|
]
|
|
|
|
);
|
|
|
|
|
2016-04-10 19:45:29 +05:30
|
|
|
$grant->validateAuthorizationRequest($request);
|
2016-02-21 23:43:39 +05:30
|
|
|
}
|
2016-02-21 22:38:57 +05:30
|
|
|
|
2016-04-10 19:45:29 +05:30
|
|
|
/**
|
|
|
|
* @expectedException \League\OAuth2\Server\Exception\OAuthServerException
|
|
|
|
* @expectedExceptionCode 4
|
|
|
|
*/
|
|
|
|
public function testValidateAuthorizationRequestInvalidClientId()
|
2016-02-21 23:43:39 +05:30
|
|
|
{
|
|
|
|
$clientRepositoryMock = $this->getMockBuilder(ClientRepositoryInterface::class)->getMock();
|
2016-04-10 19:45:29 +05:30
|
|
|
$clientRepositoryMock->method('getClientEntity')->willReturn(null);
|
2016-02-21 23:43:39 +05:30
|
|
|
|
|
|
|
$grant = new AuthCodeGrant(
|
|
|
|
$this->getMock(AuthCodeRepositoryInterface::class),
|
|
|
|
$this->getMock(RefreshTokenRepositoryInterface::class),
|
|
|
|
new \DateInterval('PT10M')
|
|
|
|
);
|
|
|
|
$grant->setClientRepository($clientRepositoryMock);
|
|
|
|
|
|
|
|
$request = new ServerRequest(
|
2016-04-10 19:45:29 +05:30
|
|
|
[],
|
2016-02-22 13:30:50 +05:30
|
|
|
[],
|
2016-02-21 23:43:39 +05:30
|
|
|
null,
|
2016-04-10 19:45:29 +05:30
|
|
|
null,
|
2016-02-21 23:43:39 +05:30
|
|
|
'php://input',
|
2016-04-10 19:45:29 +05:30
|
|
|
$headers = [],
|
|
|
|
$cookies = [],
|
|
|
|
$queryParams = [
|
2016-02-21 23:43:39 +05:30
|
|
|
'response_type' => 'code',
|
|
|
|
'client_id' => 'foo',
|
|
|
|
]
|
|
|
|
);
|
|
|
|
|
2016-04-10 19:45:29 +05:30
|
|
|
$grant->validateAuthorizationRequest($request);
|
2016-02-21 22:38:57 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException \League\OAuth2\Server\Exception\OAuthServerException
|
2016-04-10 19:45:29 +05:30
|
|
|
* @expectedExceptionCode 4
|
2016-02-21 22:38:57 +05:30
|
|
|
*/
|
2016-04-10 19:45:29 +05:30
|
|
|
public function testValidateAuthorizationRequestBadRedirectUriString()
|
2016-02-21 22:38:57 +05:30
|
|
|
{
|
|
|
|
$client = new ClientEntity();
|
|
|
|
$client->setRedirectUri('http://foo/bar');
|
|
|
|
$clientRepositoryMock = $this->getMockBuilder(ClientRepositoryInterface::class)->getMock();
|
|
|
|
$clientRepositoryMock->method('getClientEntity')->willReturn($client);
|
|
|
|
|
|
|
|
$grant = new AuthCodeGrant(
|
|
|
|
$this->getMock(AuthCodeRepositoryInterface::class),
|
|
|
|
$this->getMock(RefreshTokenRepositoryInterface::class),
|
2016-03-16 17:02:21 +05:30
|
|
|
new \DateInterval('PT10M')
|
2016-02-21 22:38:57 +05:30
|
|
|
);
|
|
|
|
$grant->setClientRepository($clientRepositoryMock);
|
|
|
|
|
|
|
|
$request = new ServerRequest(
|
2016-04-10 19:45:29 +05:30
|
|
|
[],
|
2016-02-22 13:30:50 +05:30
|
|
|
[],
|
2016-02-21 22:38:57 +05:30
|
|
|
null,
|
2016-04-10 19:45:29 +05:30
|
|
|
null,
|
2016-02-21 22:38:57 +05:30
|
|
|
'php://input',
|
2016-04-10 19:45:29 +05:30
|
|
|
$headers = [],
|
|
|
|
$cookies = [],
|
|
|
|
$queryParams = [
|
2016-02-21 22:38:57 +05:30
|
|
|
'response_type' => 'code',
|
|
|
|
'client_id' => 'foo',
|
2016-04-10 19:45:29 +05:30
|
|
|
'redirect_uri' => 'http://bar',
|
2016-02-21 22:38:57 +05:30
|
|
|
]
|
|
|
|
);
|
|
|
|
|
2016-04-10 19:45:29 +05:30
|
|
|
$grant->validateAuthorizationRequest($request);
|
2016-02-21 22:38:57 +05:30
|
|
|
}
|
|
|
|
|
2016-04-10 19:45:29 +05:30
|
|
|
/**
|
|
|
|
* @expectedException \League\OAuth2\Server\Exception\OAuthServerException
|
|
|
|
* @expectedExceptionCode 4
|
|
|
|
*/
|
|
|
|
public function testValidateAuthorizationRequestBadRedirectUriArray()
|
2016-02-21 22:38:57 +05:30
|
|
|
{
|
|
|
|
$client = new ClientEntity();
|
2016-04-10 19:45:29 +05:30
|
|
|
$client->setRedirectUri(['http://foo/bar']);
|
2016-02-21 22:38:57 +05:30
|
|
|
$clientRepositoryMock = $this->getMockBuilder(ClientRepositoryInterface::class)->getMock();
|
|
|
|
$clientRepositoryMock->method('getClientEntity')->willReturn($client);
|
|
|
|
|
|
|
|
$grant = new AuthCodeGrant(
|
2016-04-10 19:45:29 +05:30
|
|
|
$this->getMock(AuthCodeRepositoryInterface::class),
|
2016-02-21 22:38:57 +05:30
|
|
|
$this->getMock(RefreshTokenRepositoryInterface::class),
|
2016-03-16 17:02:21 +05:30
|
|
|
new \DateInterval('PT10M')
|
2016-02-21 22:38:57 +05:30
|
|
|
);
|
|
|
|
$grant->setClientRepository($clientRepositoryMock);
|
|
|
|
|
|
|
|
$request = new ServerRequest(
|
2016-04-10 19:45:29 +05:30
|
|
|
[],
|
2016-02-22 13:30:50 +05:30
|
|
|
[],
|
2016-02-21 22:38:57 +05:30
|
|
|
null,
|
2016-04-10 19:45:29 +05:30
|
|
|
null,
|
2016-02-21 22:38:57 +05:30
|
|
|
'php://input',
|
2016-04-10 19:45:29 +05:30
|
|
|
$headers = [],
|
|
|
|
$cookies = [],
|
|
|
|
$queryParams = [
|
2016-02-21 22:38:57 +05:30
|
|
|
'response_type' => 'code',
|
|
|
|
'client_id' => 'foo',
|
2016-04-10 19:45:29 +05:30
|
|
|
'redirect_uri' => 'http://bar',
|
2016-02-21 22:38:57 +05:30
|
|
|
]
|
|
|
|
);
|
|
|
|
|
2016-04-10 19:45:29 +05:30
|
|
|
$grant->validateAuthorizationRequest($request);
|
2016-02-21 22:38:57 +05:30
|
|
|
}
|
|
|
|
|
2016-05-06 19:53:16 +05:30
|
|
|
/**
|
|
|
|
* @expectedException \League\OAuth2\Server\Exception\OAuthServerException
|
|
|
|
* @expectedExceptionCode 3
|
|
|
|
*/
|
|
|
|
public function testValidateAuthorizationRequestMissingCodeChallenge()
|
|
|
|
{
|
|
|
|
$client = new ClientEntity();
|
|
|
|
$client->setRedirectUri('http://foo/bar');
|
|
|
|
$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->enableCodeExchangeProof();
|
|
|
|
$grant->setClientRepository($clientRepositoryMock);
|
|
|
|
|
|
|
|
$request = new ServerRequest(
|
|
|
|
[],
|
|
|
|
[],
|
|
|
|
null,
|
|
|
|
null,
|
|
|
|
'php://input',
|
|
|
|
$headers = [],
|
|
|
|
$cookies = [],
|
|
|
|
$queryParams = [
|
|
|
|
'response_type' => 'code',
|
|
|
|
'client_id' => 'foo',
|
|
|
|
'redirect_uri' => 'http://foo/bar',
|
|
|
|
]
|
|
|
|
);
|
|
|
|
|
|
|
|
$grant->validateAuthorizationRequest($request);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException \League\OAuth2\Server\Exception\OAuthServerException
|
|
|
|
* @expectedExceptionCode 3
|
|
|
|
*/
|
|
|
|
public function testValidateAuthorizationRequestInvalidCodeChallengeMethod()
|
|
|
|
{
|
|
|
|
$client = new ClientEntity();
|
|
|
|
$client->setRedirectUri('http://foo/bar');
|
|
|
|
$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->enableCodeExchangeProof();
|
|
|
|
$grant->setClientRepository($clientRepositoryMock);
|
|
|
|
|
|
|
|
$request = new ServerRequest(
|
|
|
|
[],
|
|
|
|
[],
|
|
|
|
null,
|
|
|
|
null,
|
|
|
|
'php://input',
|
|
|
|
$headers = [],
|
|
|
|
$cookies = [],
|
|
|
|
$queryParams = [
|
|
|
|
'response_type' => 'code',
|
|
|
|
'client_id' => 'foo',
|
|
|
|
'redirect_uri' => 'http://foo/bar',
|
|
|
|
'code_challenge' => 'foobar',
|
|
|
|
'code_challenge_method' => 'foo',
|
|
|
|
]
|
|
|
|
);
|
|
|
|
|
|
|
|
$grant->validateAuthorizationRequest($request);
|
|
|
|
}
|
|
|
|
|
2016-04-10 19:45:29 +05:30
|
|
|
public function testCompleteAuthorizationRequest()
|
2016-02-21 22:38:57 +05:30
|
|
|
{
|
2016-04-10 19:45:29 +05:30
|
|
|
$authRequest = new AuthorizationRequest();
|
|
|
|
$authRequest->setAuthorizationApproved(true);
|
|
|
|
$authRequest->setClient(new ClientEntity());
|
|
|
|
$authRequest->setGrantTypeId('authorization_code');
|
|
|
|
$authRequest->setUser(new UserEntity());
|
2016-02-21 22:38:57 +05:30
|
|
|
|
2016-04-10 19:45:29 +05:30
|
|
|
$authCodeRepository = $this->getMockBuilder(AuthCodeRepositoryInterface::class)->getMock();
|
|
|
|
$authCodeRepository->method('getNewAuthCode')->willReturn(new AuthCodeEntity());
|
2016-03-18 04:55:32 +05:30
|
|
|
|
2016-02-21 22:38:57 +05:30
|
|
|
$grant = new AuthCodeGrant(
|
2016-04-10 19:45:29 +05:30
|
|
|
$authCodeRepository,
|
2016-02-21 22:38:57 +05:30
|
|
|
$this->getMock(RefreshTokenRepositoryInterface::class),
|
|
|
|
new \DateInterval('PT10M')
|
|
|
|
);
|
|
|
|
|
2016-04-10 19:45:29 +05:30
|
|
|
$grant->setPrivateKey(new CryptKey('file://' . __DIR__ . '/../Stubs/private.key'));
|
|
|
|
$grant->setPublicKey(new CryptKey('file://' . __DIR__ . '/../Stubs/public.key'));
|
2016-03-18 04:55:32 +05:30
|
|
|
|
2016-04-10 19:45:29 +05:30
|
|
|
$this->assertTrue($grant->completeAuthorizationRequest($authRequest) instanceof RedirectResponse);
|
2016-02-21 22:38:57 +05:30
|
|
|
}
|
|
|
|
|
2016-04-10 19:45:29 +05:30
|
|
|
/**
|
|
|
|
* @expectedException \League\OAuth2\Server\Exception\OAuthServerException
|
|
|
|
* @expectedExceptionCode 9
|
|
|
|
*/
|
|
|
|
public function testCompleteAuthorizationRequestDenied()
|
2016-02-21 22:38:57 +05:30
|
|
|
{
|
2016-04-10 19:45:29 +05:30
|
|
|
$authRequest = new AuthorizationRequest();
|
|
|
|
$authRequest->setAuthorizationApproved(false);
|
|
|
|
$authRequest->setClient(new ClientEntity());
|
|
|
|
$authRequest->setGrantTypeId('authorization_code');
|
|
|
|
$authRequest->setUser(new UserEntity());
|
2016-02-21 22:38:57 +05:30
|
|
|
|
2016-04-10 19:45:29 +05:30
|
|
|
$authCodeRepository = $this->getMockBuilder(AuthCodeRepositoryInterface::class)->getMock();
|
|
|
|
$authCodeRepository->method('getNewAuthCode')->willReturn(new AuthCodeEntity());
|
2016-03-18 04:55:32 +05:30
|
|
|
|
2016-02-21 22:38:57 +05:30
|
|
|
$grant = new AuthCodeGrant(
|
2016-04-10 19:45:29 +05:30
|
|
|
$authCodeRepository,
|
2016-02-21 22:38:57 +05:30
|
|
|
$this->getMock(RefreshTokenRepositoryInterface::class),
|
|
|
|
new \DateInterval('PT10M')
|
|
|
|
);
|
2016-02-21 20:02:27 +05:30
|
|
|
|
2016-04-10 19:45:29 +05:30
|
|
|
$grant->setPrivateKey(new CryptKey('file://' . __DIR__ . '/../Stubs/private.key'));
|
|
|
|
$grant->setPublicKey(new CryptKey('file://' . __DIR__ . '/../Stubs/public.key'));
|
2016-02-21 20:02:27 +05:30
|
|
|
|
2016-04-10 19:45:29 +05:30
|
|
|
$grant->completeAuthorizationRequest($authRequest);
|
2016-02-21 20:02:27 +05:30
|
|
|
}
|
2016-02-21 23:43:39 +05:30
|
|
|
|
|
|
|
public function testRespondToAccessTokenRequest()
|
|
|
|
{
|
|
|
|
$client = new ClientEntity();
|
|
|
|
$client->setIdentifier('foo');
|
|
|
|
$client->setRedirectUri('http://foo/bar');
|
|
|
|
$clientRepositoryMock = $this->getMockBuilder(ClientRepositoryInterface::class)->getMock();
|
|
|
|
$clientRepositoryMock->method('getClientEntity')->willReturn($client);
|
|
|
|
|
2016-03-15 05:40:47 +05:30
|
|
|
$scopeRepositoryMock = $this->getMockBuilder(ScopeRepositoryInterface::class)->getMock();
|
|
|
|
$scopeEntity = new ScopeEntity();
|
|
|
|
$scopeRepositoryMock->method('getScopeEntityByIdentifier')->willReturn($scopeEntity);
|
2016-04-10 21:45:48 +05:30
|
|
|
$scopeRepositoryMock->method('finalizeScopes')->willReturnArgument(0);
|
2016-03-15 05:40:47 +05:30
|
|
|
|
2016-02-21 23:43:39 +05:30
|
|
|
$accessTokenRepositoryMock = $this->getMockBuilder(AccessTokenRepositoryInterface::class)->getMock();
|
2016-03-25 18:39:26 +05:30
|
|
|
$accessTokenRepositoryMock->method('getNewToken')->willReturn(new AccessTokenEntity());
|
2016-02-21 23:43:39 +05:30
|
|
|
$accessTokenRepositoryMock->method('persistNewAccessToken')->willReturnSelf();
|
|
|
|
|
|
|
|
$refreshTokenRepositoryMock = $this->getMockBuilder(RefreshTokenRepositoryInterface::class)->getMock();
|
|
|
|
$refreshTokenRepositoryMock->method('persistNewRefreshToken')->willReturnSelf();
|
2016-03-25 18:39:26 +05:30
|
|
|
$refreshTokenRepositoryMock->method('getNewRefreshToken')->willReturn(new RefreshTokenEntity());
|
2016-02-21 23:43:39 +05:30
|
|
|
|
|
|
|
$grant = new AuthCodeGrant(
|
|
|
|
$this->getMock(AuthCodeRepositoryInterface::class),
|
|
|
|
$this->getMock(RefreshTokenRepositoryInterface::class),
|
|
|
|
new \DateInterval('PT10M')
|
|
|
|
);
|
|
|
|
$grant->setClientRepository($clientRepositoryMock);
|
2016-03-15 05:40:47 +05:30
|
|
|
$grant->setScopeRepository($scopeRepositoryMock);
|
2016-02-21 23:43:39 +05:30
|
|
|
$grant->setAccessTokenRepository($accessTokenRepositoryMock);
|
|
|
|
$grant->setRefreshTokenRepository($refreshTokenRepositoryMock);
|
2016-03-28 20:12:34 +05:30
|
|
|
$grant->setPublicKey(new CryptKey('file://' . __DIR__ . '/../Stubs/public.key'));
|
|
|
|
$grant->setPrivateKey(new CryptKey('file://' . __DIR__ . '/../Stubs/private.key'));
|
2016-02-21 23:43:39 +05:30
|
|
|
|
|
|
|
$request = new ServerRequest(
|
|
|
|
[],
|
2016-02-22 13:30:50 +05:30
|
|
|
[],
|
2016-02-21 23:43:39 +05:30
|
|
|
null,
|
|
|
|
'POST',
|
|
|
|
'php://input',
|
|
|
|
[],
|
|
|
|
[],
|
|
|
|
[],
|
|
|
|
[
|
|
|
|
'grant_type' => 'authorization_code',
|
|
|
|
'client_id' => 'foo',
|
|
|
|
'redirect_uri' => 'http://foo/bar',
|
2016-03-18 01:48:28 +05:30
|
|
|
'code' => $this->cryptStub->doEncrypt(
|
2016-02-21 23:43:39 +05:30
|
|
|
json_encode(
|
|
|
|
[
|
|
|
|
'auth_code_id' => uniqid(),
|
|
|
|
'expire_time' => time() + 3600,
|
|
|
|
'client_id' => 'foo',
|
|
|
|
'user_id' => 123,
|
|
|
|
'scopes' => ['foo'],
|
|
|
|
'redirect_uri' => 'http://foo/bar',
|
|
|
|
]
|
2016-03-18 04:55:32 +05:30
|
|
|
)
|
2016-02-21 23:43:39 +05:30
|
|
|
),
|
|
|
|
]
|
|
|
|
);
|
|
|
|
|
|
|
|
/** @var StubResponseType $response */
|
2016-04-10 19:45:29 +05:30
|
|
|
$response = $grant->respondToAccessTokenRequest($request, new StubResponseType(), new \DateInterval('PT10M'));
|
2016-02-21 23:43:39 +05:30
|
|
|
|
|
|
|
$this->assertTrue($response->getAccessToken() instanceof AccessTokenEntityInterface);
|
|
|
|
$this->assertTrue($response->getRefreshToken() instanceof RefreshTokenEntityInterface);
|
|
|
|
}
|
|
|
|
|
2016-05-06 19:53:16 +05:30
|
|
|
public function testRespondToAccessTokenRequestCodeChallengePlain()
|
|
|
|
{
|
|
|
|
$client = new ClientEntity();
|
|
|
|
$client->setIdentifier('foo');
|
|
|
|
$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);
|
|
|
|
|
|
|
|
$accessTokenRepositoryMock = $this->getMockBuilder(AccessTokenRepositoryInterface::class)->getMock();
|
|
|
|
$accessTokenRepositoryMock->method('getNewToken')->willReturn(new AccessTokenEntity());
|
|
|
|
$accessTokenRepositoryMock->method('persistNewAccessToken')->willReturnSelf();
|
|
|
|
|
|
|
|
$refreshTokenRepositoryMock = $this->getMockBuilder(RefreshTokenRepositoryInterface::class)->getMock();
|
|
|
|
$refreshTokenRepositoryMock->method('persistNewRefreshToken')->willReturnSelf();
|
|
|
|
$refreshTokenRepositoryMock->method('getNewRefreshToken')->willReturn(new RefreshTokenEntity());
|
|
|
|
|
|
|
|
$grant = new AuthCodeGrant(
|
|
|
|
$this->getMock(AuthCodeRepositoryInterface::class),
|
|
|
|
$this->getMock(RefreshTokenRepositoryInterface::class),
|
|
|
|
new \DateInterval('PT10M')
|
|
|
|
);
|
|
|
|
$grant->enableCodeExchangeProof();
|
|
|
|
$grant->setClientRepository($clientRepositoryMock);
|
|
|
|
$grant->setScopeRepository($scopeRepositoryMock);
|
|
|
|
$grant->setAccessTokenRepository($accessTokenRepositoryMock);
|
|
|
|
$grant->setRefreshTokenRepository($refreshTokenRepositoryMock);
|
|
|
|
$grant->setPublicKey(new CryptKey('file://' . __DIR__ . '/../Stubs/public.key'));
|
|
|
|
$grant->setPrivateKey(new CryptKey('file://' . __DIR__ . '/../Stubs/private.key'));
|
|
|
|
|
|
|
|
$request = new ServerRequest(
|
|
|
|
[],
|
|
|
|
[],
|
|
|
|
null,
|
|
|
|
'POST',
|
|
|
|
'php://input',
|
|
|
|
[],
|
|
|
|
[],
|
|
|
|
[],
|
|
|
|
[
|
|
|
|
'grant_type' => 'authorization_code',
|
|
|
|
'client_id' => 'foo',
|
|
|
|
'redirect_uri' => 'http://foo/bar',
|
|
|
|
'code_verifier' => 'foobar',
|
|
|
|
'code' => $this->cryptStub->doEncrypt(
|
|
|
|
json_encode(
|
|
|
|
[
|
|
|
|
'auth_code_id' => uniqid(),
|
|
|
|
'expire_time' => time() + 3600,
|
|
|
|
'client_id' => 'foo',
|
|
|
|
'user_id' => 123,
|
|
|
|
'scopes' => ['foo'],
|
|
|
|
'redirect_uri' => 'http://foo/bar',
|
|
|
|
'code_challenge' => 'foobar',
|
|
|
|
'code_challenge_method' => 'plain',
|
|
|
|
]
|
|
|
|
)
|
|
|
|
),
|
|
|
|
]
|
|
|
|
);
|
|
|
|
|
|
|
|
/** @var StubResponseType $response */
|
|
|
|
$response = $grant->respondToAccessTokenRequest($request, new StubResponseType(), new \DateInterval('PT10M'));
|
|
|
|
|
|
|
|
$this->assertTrue($response->getAccessToken() instanceof AccessTokenEntityInterface);
|
|
|
|
$this->assertTrue($response->getRefreshToken() instanceof RefreshTokenEntityInterface);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testRespondToAccessTokenRequestCodeChallengeS256()
|
|
|
|
{
|
|
|
|
$client = new ClientEntity();
|
|
|
|
$client->setIdentifier('foo');
|
|
|
|
$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);
|
|
|
|
|
|
|
|
$accessTokenRepositoryMock = $this->getMockBuilder(AccessTokenRepositoryInterface::class)->getMock();
|
|
|
|
$accessTokenRepositoryMock->method('getNewToken')->willReturn(new AccessTokenEntity());
|
|
|
|
$accessTokenRepositoryMock->method('persistNewAccessToken')->willReturnSelf();
|
|
|
|
|
|
|
|
$refreshTokenRepositoryMock = $this->getMockBuilder(RefreshTokenRepositoryInterface::class)->getMock();
|
|
|
|
$refreshTokenRepositoryMock->method('persistNewRefreshToken')->willReturnSelf();
|
|
|
|
$refreshTokenRepositoryMock->method('getNewRefreshToken')->willReturn(new RefreshTokenEntity());
|
|
|
|
|
|
|
|
$grant = new AuthCodeGrant(
|
|
|
|
$this->getMock(AuthCodeRepositoryInterface::class),
|
|
|
|
$this->getMock(RefreshTokenRepositoryInterface::class),
|
|
|
|
new \DateInterval('PT10M')
|
|
|
|
);
|
|
|
|
$grant->enableCodeExchangeProof();
|
|
|
|
$grant->setClientRepository($clientRepositoryMock);
|
|
|
|
$grant->setScopeRepository($scopeRepositoryMock);
|
|
|
|
$grant->setAccessTokenRepository($accessTokenRepositoryMock);
|
|
|
|
$grant->setRefreshTokenRepository($refreshTokenRepositoryMock);
|
|
|
|
$grant->setPublicKey(new CryptKey('file://' . __DIR__ . '/../Stubs/public.key'));
|
|
|
|
$grant->setPrivateKey(new CryptKey('file://' . __DIR__ . '/../Stubs/private.key'));
|
|
|
|
|
|
|
|
$request = new ServerRequest(
|
|
|
|
[],
|
|
|
|
[],
|
|
|
|
null,
|
|
|
|
'POST',
|
|
|
|
'php://input',
|
|
|
|
[],
|
|
|
|
[],
|
|
|
|
[],
|
|
|
|
[
|
|
|
|
'grant_type' => 'authorization_code',
|
|
|
|
'client_id' => 'foo',
|
|
|
|
'redirect_uri' => 'http://foo/bar',
|
|
|
|
'code_verifier' => 'foobar',
|
|
|
|
'code' => $this->cryptStub->doEncrypt(
|
|
|
|
json_encode(
|
|
|
|
[
|
|
|
|
'auth_code_id' => uniqid(),
|
|
|
|
'expire_time' => time() + 3600,
|
|
|
|
'client_id' => 'foo',
|
|
|
|
'user_id' => 123,
|
|
|
|
'scopes' => ['foo'],
|
|
|
|
'redirect_uri' => 'http://foo/bar',
|
|
|
|
'code_challenge' => urlencode(base64_encode(hash('sha256', 'foobar'))),
|
|
|
|
'code_challenge_method' => 'S256',
|
|
|
|
]
|
|
|
|
)
|
|
|
|
),
|
|
|
|
]
|
|
|
|
);
|
|
|
|
|
|
|
|
/** @var StubResponseType $response */
|
|
|
|
$response = $grant->respondToAccessTokenRequest($request, new StubResponseType(), new \DateInterval('PT10M'));
|
|
|
|
|
|
|
|
$this->assertTrue($response->getAccessToken() instanceof AccessTokenEntityInterface);
|
|
|
|
$this->assertTrue($response->getRefreshToken() instanceof RefreshTokenEntityInterface);
|
|
|
|
}
|
|
|
|
|
2016-02-21 23:43:39 +05:30
|
|
|
/**
|
|
|
|
* @expectedException \League\OAuth2\Server\Exception\OAuthServerException
|
|
|
|
* @expectedExceptionCode 3
|
|
|
|
*/
|
|
|
|
public function testRespondToAccessTokenRequestMissingRedirectUri()
|
|
|
|
{
|
|
|
|
$clientRepositoryMock = $this->getMockBuilder(ClientRepositoryInterface::class)->getMock();
|
|
|
|
$accessTokenRepositoryMock = $this->getMockBuilder(AccessTokenRepositoryInterface::class)->getMock();
|
|
|
|
$refreshTokenRepositoryMock = $this->getMockBuilder(RefreshTokenRepositoryInterface::class)->getMock();
|
|
|
|
|
|
|
|
$grant = new AuthCodeGrant(
|
|
|
|
$this->getMock(AuthCodeRepositoryInterface::class),
|
|
|
|
$this->getMock(RefreshTokenRepositoryInterface::class),
|
|
|
|
new \DateInterval('PT10M')
|
|
|
|
);
|
|
|
|
$grant->setClientRepository($clientRepositoryMock);
|
|
|
|
$grant->setAccessTokenRepository($accessTokenRepositoryMock);
|
|
|
|
$grant->setRefreshTokenRepository($refreshTokenRepositoryMock);
|
2016-03-28 20:12:34 +05:30
|
|
|
$grant->setPublicKey(new CryptKey('file://' . __DIR__ . '/../Stubs/public.key'));
|
|
|
|
$grant->setPrivateKey(new CryptKey('file://' . __DIR__ . '/../Stubs/private.key'));
|
2016-02-21 23:43:39 +05:30
|
|
|
|
|
|
|
$request = new ServerRequest(
|
|
|
|
[],
|
2016-02-22 13:30:50 +05:30
|
|
|
[],
|
2016-02-21 23:43:39 +05:30
|
|
|
null,
|
|
|
|
'POST',
|
|
|
|
'php://input',
|
|
|
|
[],
|
|
|
|
[],
|
|
|
|
[],
|
|
|
|
[
|
|
|
|
'grant_type' => 'authorization_code',
|
|
|
|
]
|
|
|
|
);
|
|
|
|
|
2016-02-22 13:30:50 +05:30
|
|
|
/* @var StubResponseType $response */
|
2016-04-10 19:45:29 +05:30
|
|
|
$grant->respondToAccessTokenRequest($request, new StubResponseType(), new \DateInterval('PT10M'));
|
2016-02-21 23:43:39 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException \League\OAuth2\Server\Exception\OAuthServerException
|
|
|
|
* @expectedExceptionCode 3
|
|
|
|
*/
|
|
|
|
public function testRespondToAccessTokenRequestMissingCode()
|
|
|
|
{
|
|
|
|
$client = new ClientEntity();
|
|
|
|
$client->setRedirectUri('http://foo/bar');
|
|
|
|
$clientRepositoryMock = $this->getMockBuilder(ClientRepositoryInterface::class)->getMock();
|
|
|
|
$clientRepositoryMock->method('getClientEntity')->willReturn($client);
|
|
|
|
|
|
|
|
$accessTokenRepositoryMock = $this->getMockBuilder(AccessTokenRepositoryInterface::class)->getMock();
|
|
|
|
$refreshTokenRepositoryMock = $this->getMockBuilder(RefreshTokenRepositoryInterface::class)->getMock();
|
|
|
|
|
|
|
|
$grant = new AuthCodeGrant(
|
|
|
|
$this->getMock(AuthCodeRepositoryInterface::class),
|
|
|
|
$this->getMock(RefreshTokenRepositoryInterface::class),
|
|
|
|
new \DateInterval('PT10M')
|
|
|
|
);
|
|
|
|
$grant->setClientRepository($clientRepositoryMock);
|
|
|
|
$grant->setAccessTokenRepository($accessTokenRepositoryMock);
|
|
|
|
$grant->setRefreshTokenRepository($refreshTokenRepositoryMock);
|
2016-03-28 20:12:34 +05:30
|
|
|
$grant->setPublicKey(new CryptKey('file://' . __DIR__ . '/../Stubs/public.key'));
|
|
|
|
$grant->setPrivateKey(new CryptKey('file://' . __DIR__ . '/../Stubs/private.key'));
|
2016-02-21 23:43:39 +05:30
|
|
|
|
|
|
|
$request = new ServerRequest(
|
|
|
|
[],
|
2016-02-22 13:30:50 +05:30
|
|
|
[],
|
2016-02-21 23:43:39 +05:30
|
|
|
null,
|
|
|
|
'POST',
|
|
|
|
'php://input',
|
|
|
|
[],
|
|
|
|
[],
|
|
|
|
[],
|
|
|
|
[
|
|
|
|
'grant_type' => 'authorization_code',
|
|
|
|
'client_id' => 'foo',
|
|
|
|
'client_secret' => 'bar',
|
|
|
|
'redirect_uri' => 'http://foo/bar',
|
|
|
|
]
|
|
|
|
);
|
|
|
|
|
2016-02-22 13:30:50 +05:30
|
|
|
/* @var StubResponseType $response */
|
2016-04-10 19:45:29 +05:30
|
|
|
$grant->respondToAccessTokenRequest($request, new StubResponseType(), new \DateInterval('PT10M'));
|
2016-02-21 23:43:39 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
public function testRespondToAccessTokenRequestExpiredCode()
|
|
|
|
{
|
|
|
|
$client = new ClientEntity();
|
|
|
|
$client->setIdentifier('foo');
|
|
|
|
$client->setRedirectUri('http://foo/bar');
|
|
|
|
$clientRepositoryMock = $this->getMockBuilder(ClientRepositoryInterface::class)->getMock();
|
|
|
|
$clientRepositoryMock->method('getClientEntity')->willReturn($client);
|
|
|
|
|
|
|
|
$accessTokenRepositoryMock = $this->getMockBuilder(AccessTokenRepositoryInterface::class)->getMock();
|
|
|
|
$accessTokenRepositoryMock->method('persistNewAccessToken')->willReturnSelf();
|
|
|
|
|
|
|
|
$refreshTokenRepositoryMock = $this->getMockBuilder(RefreshTokenRepositoryInterface::class)->getMock();
|
|
|
|
$refreshTokenRepositoryMock->method('persistNewRefreshToken')->willReturnSelf();
|
|
|
|
|
|
|
|
$grant = new AuthCodeGrant(
|
|
|
|
$this->getMock(AuthCodeRepositoryInterface::class),
|
|
|
|
$this->getMock(RefreshTokenRepositoryInterface::class),
|
|
|
|
new \DateInterval('PT10M')
|
|
|
|
);
|
|
|
|
$grant->setClientRepository($clientRepositoryMock);
|
|
|
|
$grant->setAccessTokenRepository($accessTokenRepositoryMock);
|
|
|
|
$grant->setRefreshTokenRepository($refreshTokenRepositoryMock);
|
2016-03-28 20:12:34 +05:30
|
|
|
$grant->setPublicKey(new CryptKey('file://' . __DIR__ . '/../Stubs/public.key'));
|
|
|
|
$grant->setPrivateKey(new CryptKey('file://' . __DIR__ . '/../Stubs/private.key'));
|
2016-02-21 23:43:39 +05:30
|
|
|
|
|
|
|
$request = new ServerRequest(
|
|
|
|
[],
|
2016-02-22 13:30:50 +05:30
|
|
|
[],
|
2016-02-21 23:43:39 +05:30
|
|
|
null,
|
|
|
|
'POST',
|
|
|
|
'php://input',
|
|
|
|
[],
|
|
|
|
[],
|
|
|
|
[],
|
|
|
|
[
|
|
|
|
'grant_type' => 'authorization_code',
|
|
|
|
'client_id' => 'foo',
|
|
|
|
'redirect_uri' => 'http://foo/bar',
|
2016-03-18 01:48:28 +05:30
|
|
|
'code' => $this->cryptStub->doEncrypt(
|
2016-02-21 23:43:39 +05:30
|
|
|
json_encode(
|
|
|
|
[
|
|
|
|
'auth_code_id' => uniqid(),
|
|
|
|
'expire_time' => time() - 3600,
|
|
|
|
'client_id' => 'foo',
|
|
|
|
'user_id' => 123,
|
|
|
|
'scopes' => ['foo'],
|
|
|
|
'redirect_uri' => 'http://foo/bar',
|
|
|
|
]
|
2016-03-18 04:55:32 +05:30
|
|
|
)
|
2016-02-21 23:43:39 +05:30
|
|
|
),
|
|
|
|
]
|
|
|
|
);
|
|
|
|
|
|
|
|
try {
|
2016-02-22 13:30:50 +05:30
|
|
|
/* @var StubResponseType $response */
|
2016-04-10 19:45:29 +05:30
|
|
|
$grant->respondToAccessTokenRequest($request, new StubResponseType(), new \DateInterval('PT10M'));
|
2016-02-21 23:43:39 +05:30
|
|
|
} catch (OAuthServerException $e) {
|
|
|
|
$this->assertEquals($e->getHint(), 'Authorization code has expired');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testRespondToAccessTokenRequestRevokedCode()
|
|
|
|
{
|
|
|
|
$client = new ClientEntity();
|
|
|
|
$client->setIdentifier('foo');
|
|
|
|
$client->setRedirectUri('http://foo/bar');
|
|
|
|
$clientRepositoryMock = $this->getMockBuilder(ClientRepositoryInterface::class)->getMock();
|
|
|
|
$clientRepositoryMock->method('getClientEntity')->willReturn($client);
|
|
|
|
|
|
|
|
$accessTokenRepositoryMock = $this->getMockBuilder(AccessTokenRepositoryInterface::class)->getMock();
|
|
|
|
$accessTokenRepositoryMock->method('persistNewAccessToken')->willReturnSelf();
|
|
|
|
|
|
|
|
$refreshTokenRepositoryMock = $this->getMockBuilder(RefreshTokenRepositoryInterface::class)->getMock();
|
|
|
|
$refreshTokenRepositoryMock->method('persistNewRefreshToken')->willReturnSelf();
|
|
|
|
|
|
|
|
$authCodeRepositoryMock = $this->getMockBuilder(AuthCodeRepositoryInterface::class)->getMock();
|
|
|
|
$authCodeRepositoryMock->method('isAuthCodeRevoked')->willReturn(true);
|
|
|
|
|
|
|
|
$grant = new AuthCodeGrant(
|
|
|
|
$authCodeRepositoryMock,
|
|
|
|
$this->getMock(RefreshTokenRepositoryInterface::class),
|
|
|
|
new \DateInterval('PT10M')
|
|
|
|
);
|
|
|
|
$grant->setClientRepository($clientRepositoryMock);
|
|
|
|
$grant->setAccessTokenRepository($accessTokenRepositoryMock);
|
|
|
|
$grant->setRefreshTokenRepository($refreshTokenRepositoryMock);
|
2016-03-28 20:12:34 +05:30
|
|
|
$grant->setPublicKey(new CryptKey('file://' . __DIR__ . '/../Stubs/public.key'));
|
|
|
|
$grant->setPrivateKey(new CryptKey('file://' . __DIR__ . '/../Stubs/private.key'));
|
2016-02-21 23:43:39 +05:30
|
|
|
|
|
|
|
$request = new ServerRequest(
|
|
|
|
[],
|
2016-02-22 13:30:50 +05:30
|
|
|
[],
|
2016-02-21 23:43:39 +05:30
|
|
|
null,
|
|
|
|
'POST',
|
|
|
|
'php://input',
|
|
|
|
[],
|
|
|
|
[],
|
|
|
|
[],
|
|
|
|
[
|
|
|
|
'grant_type' => 'authorization_code',
|
|
|
|
'client_id' => 'foo',
|
|
|
|
'redirect_uri' => 'http://foo/bar',
|
2016-03-18 01:48:28 +05:30
|
|
|
'code' => $this->cryptStub->doEncrypt(
|
2016-02-21 23:43:39 +05:30
|
|
|
json_encode(
|
|
|
|
[
|
|
|
|
'auth_code_id' => uniqid(),
|
|
|
|
'expire_time' => time() + 3600,
|
|
|
|
'client_id' => 'foo',
|
|
|
|
'user_id' => 123,
|
|
|
|
'scopes' => ['foo'],
|
|
|
|
'redirect_uri' => 'http://foo/bar',
|
|
|
|
]
|
2016-03-18 04:55:32 +05:30
|
|
|
)
|
2016-02-21 23:43:39 +05:30
|
|
|
),
|
|
|
|
]
|
|
|
|
);
|
|
|
|
|
|
|
|
try {
|
2016-02-22 13:30:50 +05:30
|
|
|
/* @var StubResponseType $response */
|
2016-04-10 19:45:29 +05:30
|
|
|
$grant->respondToAccessTokenRequest($request, new StubResponseType(), new \DateInterval('PT10M'));
|
2016-02-21 23:43:39 +05:30
|
|
|
} catch (OAuthServerException $e) {
|
|
|
|
$this->assertEquals($e->getHint(), 'Authorization code has been revoked');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testRespondToAccessTokenRequestClientMismatch()
|
|
|
|
{
|
|
|
|
$client = new ClientEntity();
|
|
|
|
$client->setIdentifier('foo');
|
|
|
|
$client->setRedirectUri('http://foo/bar');
|
|
|
|
$clientRepositoryMock = $this->getMockBuilder(ClientRepositoryInterface::class)->getMock();
|
|
|
|
$clientRepositoryMock->method('getClientEntity')->willReturn($client);
|
|
|
|
|
|
|
|
$accessTokenRepositoryMock = $this->getMockBuilder(AccessTokenRepositoryInterface::class)->getMock();
|
|
|
|
$accessTokenRepositoryMock->method('persistNewAccessToken')->willReturnSelf();
|
|
|
|
|
|
|
|
$refreshTokenRepositoryMock = $this->getMockBuilder(RefreshTokenRepositoryInterface::class)->getMock();
|
|
|
|
$refreshTokenRepositoryMock->method('persistNewRefreshToken')->willReturnSelf();
|
|
|
|
|
|
|
|
$grant = new AuthCodeGrant(
|
|
|
|
$this->getMock(AuthCodeRepositoryInterface::class),
|
|
|
|
$this->getMock(RefreshTokenRepositoryInterface::class),
|
|
|
|
new \DateInterval('PT10M')
|
|
|
|
);
|
|
|
|
$grant->setClientRepository($clientRepositoryMock);
|
|
|
|
$grant->setAccessTokenRepository($accessTokenRepositoryMock);
|
|
|
|
$grant->setRefreshTokenRepository($refreshTokenRepositoryMock);
|
2016-03-28 20:12:34 +05:30
|
|
|
$grant->setPublicKey(new CryptKey('file://' . __DIR__ . '/../Stubs/public.key'));
|
|
|
|
$grant->setPrivateKey(new CryptKey('file://' . __DIR__ . '/../Stubs/private.key'));
|
2016-02-21 23:43:39 +05:30
|
|
|
|
|
|
|
$request = new ServerRequest(
|
|
|
|
[],
|
2016-02-22 13:30:50 +05:30
|
|
|
[],
|
2016-02-21 23:43:39 +05:30
|
|
|
null,
|
|
|
|
'POST',
|
|
|
|
'php://input',
|
|
|
|
[],
|
|
|
|
[],
|
|
|
|
[],
|
|
|
|
[
|
|
|
|
'grant_type' => 'authorization_code',
|
|
|
|
'client_id' => 'foo',
|
|
|
|
'redirect_uri' => 'http://foo/bar',
|
2016-03-18 01:48:28 +05:30
|
|
|
'code' => $this->cryptStub->doEncrypt(
|
2016-02-21 23:43:39 +05:30
|
|
|
json_encode(
|
|
|
|
[
|
|
|
|
'auth_code_id' => uniqid(),
|
|
|
|
'expire_time' => time() + 3600,
|
|
|
|
'client_id' => 'bar',
|
|
|
|
'user_id' => 123,
|
|
|
|
'scopes' => ['foo'],
|
|
|
|
'redirect_uri' => 'http://foo/bar',
|
|
|
|
]
|
2016-03-18 04:55:32 +05:30
|
|
|
)
|
2016-02-21 23:43:39 +05:30
|
|
|
),
|
|
|
|
]
|
|
|
|
);
|
|
|
|
|
|
|
|
try {
|
2016-02-22 13:30:50 +05:30
|
|
|
/* @var StubResponseType $response */
|
2016-04-10 19:45:29 +05:30
|
|
|
$grant->respondToAccessTokenRequest($request, new StubResponseType(), new \DateInterval('PT10M'));
|
2016-02-21 23:43:39 +05:30
|
|
|
} catch (OAuthServerException $e) {
|
|
|
|
$this->assertEquals($e->getHint(), 'Authorization code was not issued to this client');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testRespondToAccessTokenRequestBadCodeEncryption()
|
|
|
|
{
|
|
|
|
$client = new ClientEntity();
|
|
|
|
$client->setIdentifier('foo');
|
|
|
|
$client->setRedirectUri('http://foo/bar');
|
|
|
|
$clientRepositoryMock = $this->getMockBuilder(ClientRepositoryInterface::class)->getMock();
|
|
|
|
$clientRepositoryMock->method('getClientEntity')->willReturn($client);
|
|
|
|
|
|
|
|
$accessTokenRepositoryMock = $this->getMockBuilder(AccessTokenRepositoryInterface::class)->getMock();
|
|
|
|
$accessTokenRepositoryMock->method('persistNewAccessToken')->willReturnSelf();
|
|
|
|
|
|
|
|
$refreshTokenRepositoryMock = $this->getMockBuilder(RefreshTokenRepositoryInterface::class)->getMock();
|
|
|
|
$refreshTokenRepositoryMock->method('persistNewRefreshToken')->willReturnSelf();
|
|
|
|
|
|
|
|
$grant = new AuthCodeGrant(
|
|
|
|
$this->getMock(AuthCodeRepositoryInterface::class),
|
|
|
|
$this->getMock(RefreshTokenRepositoryInterface::class),
|
|
|
|
new \DateInterval('PT10M')
|
|
|
|
);
|
|
|
|
$grant->setClientRepository($clientRepositoryMock);
|
|
|
|
$grant->setAccessTokenRepository($accessTokenRepositoryMock);
|
|
|
|
$grant->setRefreshTokenRepository($refreshTokenRepositoryMock);
|
2016-03-28 20:12:34 +05:30
|
|
|
$grant->setPublicKey(new CryptKey('file://' . __DIR__ . '/../Stubs/public.key'));
|
|
|
|
$grant->setPrivateKey(new CryptKey('file://' . __DIR__ . '/../Stubs/private.key'));
|
2016-02-21 23:43:39 +05:30
|
|
|
|
|
|
|
$request = new ServerRequest(
|
|
|
|
[],
|
2016-02-22 13:30:50 +05:30
|
|
|
[],
|
2016-02-21 23:43:39 +05:30
|
|
|
null,
|
|
|
|
'POST',
|
|
|
|
'php://input',
|
|
|
|
[],
|
|
|
|
[],
|
|
|
|
[],
|
|
|
|
[
|
|
|
|
'grant_type' => 'authorization_code',
|
|
|
|
'client_id' => 'foo',
|
|
|
|
'redirect_uri' => 'http://foo/bar',
|
|
|
|
'code' => 'sdfsfsd',
|
|
|
|
]
|
|
|
|
);
|
|
|
|
|
|
|
|
try {
|
2016-02-22 13:30:50 +05:30
|
|
|
/* @var StubResponseType $response */
|
2016-04-10 19:45:29 +05:30
|
|
|
$grant->respondToAccessTokenRequest($request, new StubResponseType(), new \DateInterval('PT10M'));
|
2016-02-21 23:43:39 +05:30
|
|
|
} catch (OAuthServerException $e) {
|
|
|
|
$this->assertEquals($e->getHint(), 'Cannot decrypt the authorization code');
|
|
|
|
}
|
|
|
|
}
|
2016-05-06 19:53:16 +05:30
|
|
|
|
|
|
|
public function testRespondToAccessTokenRequestBadCodeVerifierPlain()
|
|
|
|
{
|
|
|
|
$client = new ClientEntity();
|
|
|
|
$client->setIdentifier('foo');
|
|
|
|
$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);
|
|
|
|
|
|
|
|
$accessTokenRepositoryMock = $this->getMockBuilder(AccessTokenRepositoryInterface::class)->getMock();
|
|
|
|
$accessTokenRepositoryMock->method('getNewToken')->willReturn(new AccessTokenEntity());
|
|
|
|
$accessTokenRepositoryMock->method('persistNewAccessToken')->willReturnSelf();
|
|
|
|
|
|
|
|
$refreshTokenRepositoryMock = $this->getMockBuilder(RefreshTokenRepositoryInterface::class)->getMock();
|
|
|
|
$refreshTokenRepositoryMock->method('persistNewRefreshToken')->willReturnSelf();
|
|
|
|
$refreshTokenRepositoryMock->method('getNewRefreshToken')->willReturn(new RefreshTokenEntity());
|
|
|
|
|
|
|
|
$grant = new AuthCodeGrant(
|
|
|
|
$this->getMock(AuthCodeRepositoryInterface::class),
|
|
|
|
$this->getMock(RefreshTokenRepositoryInterface::class),
|
|
|
|
new \DateInterval('PT10M')
|
|
|
|
);
|
|
|
|
$grant->enableCodeExchangeProof();
|
|
|
|
$grant->setClientRepository($clientRepositoryMock);
|
|
|
|
$grant->setAccessTokenRepository($accessTokenRepositoryMock);
|
|
|
|
$grant->setRefreshTokenRepository($refreshTokenRepositoryMock);
|
|
|
|
$grant->setScopeRepository($scopeRepositoryMock);
|
|
|
|
$grant->setPublicKey(new CryptKey('file://' . __DIR__ . '/../Stubs/public.key'));
|
|
|
|
$grant->setPrivateKey(new CryptKey('file://' . __DIR__ . '/../Stubs/private.key'));
|
|
|
|
|
|
|
|
$request = new ServerRequest(
|
|
|
|
[],
|
|
|
|
[],
|
|
|
|
null,
|
|
|
|
'POST',
|
|
|
|
'php://input',
|
|
|
|
[],
|
|
|
|
[],
|
|
|
|
[],
|
|
|
|
[
|
|
|
|
'grant_type' => 'authorization_code',
|
|
|
|
'client_id' => 'foo',
|
|
|
|
'redirect_uri' => 'http://foo/bar',
|
|
|
|
'code_verifier' => 'nope',
|
|
|
|
'code' => $this->cryptStub->doEncrypt(
|
|
|
|
json_encode(
|
|
|
|
[
|
|
|
|
'auth_code_id' => uniqid(),
|
|
|
|
'expire_time' => time() + 3600,
|
|
|
|
'client_id' => 'foo',
|
|
|
|
'user_id' => 123,
|
|
|
|
'scopes' => ['foo'],
|
|
|
|
'redirect_uri' => 'http://foo/bar',
|
|
|
|
'code_challenge' => 'foobar',
|
|
|
|
'code_challenge_method' => 'plain',
|
|
|
|
]
|
|
|
|
)
|
|
|
|
),
|
|
|
|
]
|
|
|
|
);
|
|
|
|
|
|
|
|
try {
|
|
|
|
/* @var StubResponseType $response */
|
|
|
|
$grant->respondToAccessTokenRequest($request, new StubResponseType(), new \DateInterval('PT10M'));
|
|
|
|
} catch (OAuthServerException $e) {
|
|
|
|
$this->assertEquals($e->getHint(), 'Failed to verify `code_verifier`.');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testRespondToAccessTokenRequestBadCodeVerifierS256()
|
|
|
|
{
|
|
|
|
$client = new ClientEntity();
|
|
|
|
$client->setIdentifier('foo');
|
|
|
|
$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);
|
|
|
|
|
|
|
|
$accessTokenRepositoryMock = $this->getMockBuilder(AccessTokenRepositoryInterface::class)->getMock();
|
|
|
|
$accessTokenRepositoryMock->method('getNewToken')->willReturn(new AccessTokenEntity());
|
|
|
|
$accessTokenRepositoryMock->method('persistNewAccessToken')->willReturnSelf();
|
|
|
|
|
|
|
|
$refreshTokenRepositoryMock = $this->getMockBuilder(RefreshTokenRepositoryInterface::class)->getMock();
|
|
|
|
$refreshTokenRepositoryMock->method('persistNewRefreshToken')->willReturnSelf();
|
|
|
|
$refreshTokenRepositoryMock->method('getNewRefreshToken')->willReturn(new RefreshTokenEntity());
|
|
|
|
|
|
|
|
$grant = new AuthCodeGrant(
|
|
|
|
$this->getMock(AuthCodeRepositoryInterface::class),
|
|
|
|
$this->getMock(RefreshTokenRepositoryInterface::class),
|
|
|
|
new \DateInterval('PT10M')
|
|
|
|
);
|
|
|
|
$grant->enableCodeExchangeProof();
|
|
|
|
$grant->setClientRepository($clientRepositoryMock);
|
|
|
|
$grant->setAccessTokenRepository($accessTokenRepositoryMock);
|
|
|
|
$grant->setRefreshTokenRepository($refreshTokenRepositoryMock);
|
|
|
|
$grant->setScopeRepository($scopeRepositoryMock);
|
|
|
|
$grant->setPublicKey(new CryptKey('file://' . __DIR__ . '/../Stubs/public.key'));
|
|
|
|
$grant->setPrivateKey(new CryptKey('file://' . __DIR__ . '/../Stubs/private.key'));
|
|
|
|
|
|
|
|
$request = new ServerRequest(
|
|
|
|
[],
|
|
|
|
[],
|
|
|
|
null,
|
|
|
|
'POST',
|
|
|
|
'php://input',
|
|
|
|
[],
|
|
|
|
[],
|
|
|
|
[],
|
|
|
|
[
|
|
|
|
'grant_type' => 'authorization_code',
|
|
|
|
'client_id' => 'foo',
|
|
|
|
'redirect_uri' => 'http://foo/bar',
|
|
|
|
'code_verifier' => 'nope',
|
|
|
|
'code' => $this->cryptStub->doEncrypt(
|
|
|
|
json_encode(
|
|
|
|
[
|
|
|
|
'auth_code_id' => uniqid(),
|
|
|
|
'expire_time' => time() + 3600,
|
|
|
|
'client_id' => 'foo',
|
|
|
|
'user_id' => 123,
|
|
|
|
'scopes' => ['foo'],
|
|
|
|
'redirect_uri' => 'http://foo/bar',
|
|
|
|
'code_challenge' => 'foobar',
|
|
|
|
'code_challenge_method' => 'S256',
|
|
|
|
]
|
|
|
|
)
|
|
|
|
),
|
|
|
|
]
|
|
|
|
);
|
|
|
|
|
|
|
|
try {
|
|
|
|
/* @var StubResponseType $response */
|
|
|
|
$grant->respondToAccessTokenRequest($request, new StubResponseType(), new \DateInterval('PT10M'));
|
|
|
|
} catch (OAuthServerException $e) {
|
|
|
|
$this->assertEquals($e->getHint(), 'Failed to verify `code_verifier`.');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testRespondToAccessTokenRequestMissingCodeVerifier()
|
|
|
|
{
|
|
|
|
$client = new ClientEntity();
|
|
|
|
$client->setIdentifier('foo');
|
|
|
|
$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);
|
|
|
|
|
|
|
|
$accessTokenRepositoryMock = $this->getMockBuilder(AccessTokenRepositoryInterface::class)->getMock();
|
|
|
|
$accessTokenRepositoryMock->method('getNewToken')->willReturn(new AccessTokenEntity());
|
|
|
|
$accessTokenRepositoryMock->method('persistNewAccessToken')->willReturnSelf();
|
|
|
|
|
|
|
|
$refreshTokenRepositoryMock = $this->getMockBuilder(RefreshTokenRepositoryInterface::class)->getMock();
|
|
|
|
$refreshTokenRepositoryMock->method('persistNewRefreshToken')->willReturnSelf();
|
|
|
|
$refreshTokenRepositoryMock->method('getNewRefreshToken')->willReturn(new RefreshTokenEntity());
|
|
|
|
|
|
|
|
$grant = new AuthCodeGrant(
|
|
|
|
$this->getMock(AuthCodeRepositoryInterface::class),
|
|
|
|
$this->getMock(RefreshTokenRepositoryInterface::class),
|
|
|
|
new \DateInterval('PT10M')
|
|
|
|
);
|
|
|
|
$grant->enableCodeExchangeProof();
|
|
|
|
$grant->setClientRepository($clientRepositoryMock);
|
|
|
|
$grant->setAccessTokenRepository($accessTokenRepositoryMock);
|
|
|
|
$grant->setRefreshTokenRepository($refreshTokenRepositoryMock);
|
|
|
|
$grant->setScopeRepository($scopeRepositoryMock);
|
|
|
|
$grant->setPublicKey(new CryptKey('file://' . __DIR__ . '/../Stubs/public.key'));
|
|
|
|
$grant->setPrivateKey(new CryptKey('file://' . __DIR__ . '/../Stubs/private.key'));
|
|
|
|
|
|
|
|
$request = new ServerRequest(
|
|
|
|
[],
|
|
|
|
[],
|
|
|
|
null,
|
|
|
|
'POST',
|
|
|
|
'php://input',
|
|
|
|
[],
|
|
|
|
[],
|
|
|
|
[],
|
|
|
|
[
|
|
|
|
'grant_type' => 'authorization_code',
|
|
|
|
'client_id' => 'foo',
|
|
|
|
'redirect_uri' => 'http://foo/bar',
|
|
|
|
'code' => $this->cryptStub->doEncrypt(
|
|
|
|
json_encode(
|
|
|
|
[
|
|
|
|
'auth_code_id' => uniqid(),
|
|
|
|
'expire_time' => time() + 3600,
|
|
|
|
'client_id' => 'foo',
|
|
|
|
'user_id' => 123,
|
|
|
|
'scopes' => ['foo'],
|
|
|
|
'redirect_uri' => 'http://foo/bar',
|
|
|
|
'code_challenge' => 'foobar',
|
|
|
|
'code_challenge_method' => 'plain',
|
|
|
|
]
|
|
|
|
)
|
|
|
|
),
|
|
|
|
]
|
|
|
|
);
|
|
|
|
|
|
|
|
try {
|
|
|
|
/* @var StubResponseType $response */
|
|
|
|
$grant->respondToAccessTokenRequest($request, new StubResponseType(), new \DateInterval('PT10M'));
|
|
|
|
} catch (OAuthServerException $e) {
|
|
|
|
$this->assertEquals($e->getHint(), 'Check the `code_verifier` parameter');
|
|
|
|
}
|
|
|
|
}
|
2016-06-20 13:48:53 +05:30
|
|
|
|
|
|
|
public function testAuthCodeRepositoryUniqueConstraintCheck()
|
|
|
|
{
|
|
|
|
$authRequest = new AuthorizationRequest();
|
|
|
|
$authRequest->setAuthorizationApproved(true);
|
|
|
|
$authRequest->setClient(new ClientEntity());
|
|
|
|
$authRequest->setGrantTypeId('authorization_code');
|
|
|
|
$authRequest->setUser(new UserEntity());
|
|
|
|
|
|
|
|
$authCodeRepository = $this->getMockBuilder(AuthCodeRepositoryInterface::class)->getMock();
|
|
|
|
$authCodeRepository->method('getNewAuthCode')->willReturn(new AuthCodeEntity());
|
|
|
|
|
|
|
|
$authCodeRepository->expects($this->at(0))->method('persistNewAuthCode')->willThrowException(UniqueTokenIdentifierConstraintViolationException::create());
|
|
|
|
$authCodeRepository->expects($this->at(1))->method('persistNewAuthCode');
|
|
|
|
|
|
|
|
$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'));
|
|
|
|
|
|
|
|
$this->assertTrue($grant->completeAuthorizationRequest($authRequest) instanceof RedirectResponse);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException \League\OAuth2\Server\Exception\OAuthServerException
|
|
|
|
* @expectedExceptionCode 7
|
|
|
|
*/
|
|
|
|
public function testAuthCodeRepositoryFailToPersist()
|
|
|
|
{
|
|
|
|
$authRequest = new AuthorizationRequest();
|
|
|
|
$authRequest->setAuthorizationApproved(true);
|
|
|
|
$authRequest->setClient(new ClientEntity());
|
|
|
|
$authRequest->setGrantTypeId('authorization_code');
|
|
|
|
$authRequest->setUser(new UserEntity());
|
|
|
|
|
|
|
|
$authCodeRepository = $this->getMockBuilder(AuthCodeRepositoryInterface::class)->getMock();
|
|
|
|
$authCodeRepository->method('getNewAuthCode')->willReturn(new AuthCodeEntity());
|
|
|
|
$authCodeRepository->method('persistNewAuthCode')->willThrowException(OAuthServerException::serverError('something bad happened'));
|
|
|
|
|
|
|
|
$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'));
|
|
|
|
|
|
|
|
$this->assertTrue($grant->completeAuthorizationRequest($authRequest) instanceof RedirectResponse);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException \League\OAuth2\Server\Exception\UniqueTokenIdentifierConstraintViolationException
|
|
|
|
* @expectedExceptionCode 100
|
|
|
|
*/
|
|
|
|
public function testAuthCodeRepositoryFailToPersistUniqueNoInfiniteLoop()
|
|
|
|
{
|
|
|
|
$authRequest = new AuthorizationRequest();
|
|
|
|
$authRequest->setAuthorizationApproved(true);
|
|
|
|
$authRequest->setClient(new ClientEntity());
|
|
|
|
$authRequest->setGrantTypeId('authorization_code');
|
|
|
|
$authRequest->setUser(new UserEntity());
|
|
|
|
|
|
|
|
$authCodeRepository = $this->getMockBuilder(AuthCodeRepositoryInterface::class)->getMock();
|
|
|
|
$authCodeRepository->method('getNewAuthCode')->willReturn(new AuthCodeEntity());
|
|
|
|
$authCodeRepository->method('persistNewAuthCode')->willThrowException(UniqueTokenIdentifierConstraintViolationException::create());
|
|
|
|
|
|
|
|
$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'));
|
|
|
|
|
|
|
|
$this->assertTrue($grant->completeAuthorizationRequest($authRequest) instanceof RedirectResponse);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testRefreshTokenRepositoryUniqueConstraintCheck()
|
|
|
|
{
|
|
|
|
$client = new ClientEntity();
|
|
|
|
$client->setIdentifier('foo');
|
|
|
|
$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);
|
|
|
|
|
|
|
|
$accessTokenRepositoryMock = $this->getMockBuilder(AccessTokenRepositoryInterface::class)->getMock();
|
|
|
|
$accessTokenRepositoryMock->method('getNewToken')->willReturn(new AccessTokenEntity());
|
|
|
|
$accessTokenRepositoryMock->method('persistNewAccessToken')->willReturnSelf();
|
|
|
|
|
|
|
|
$refreshTokenRepositoryMock = $this->getMockBuilder(RefreshTokenRepositoryInterface::class)->getMock();
|
|
|
|
$refreshTokenRepositoryMock->method('getNewRefreshToken')->willReturn(new RefreshTokenEntity());
|
|
|
|
$refreshTokenRepositoryMock->expects($this->at(0))->method('persistNewRefreshToken')->willThrowException(UniqueTokenIdentifierConstraintViolationException::create());
|
|
|
|
$refreshTokenRepositoryMock->expects($this->at(1))->method('persistNewRefreshToken');
|
|
|
|
|
|
|
|
$grant = new AuthCodeGrant(
|
|
|
|
$this->getMock(AuthCodeRepositoryInterface::class),
|
|
|
|
$this->getMock(RefreshTokenRepositoryInterface::class),
|
|
|
|
new \DateInterval('PT10M')
|
|
|
|
);
|
|
|
|
$grant->setClientRepository($clientRepositoryMock);
|
|
|
|
$grant->setScopeRepository($scopeRepositoryMock);
|
|
|
|
$grant->setAccessTokenRepository($accessTokenRepositoryMock);
|
|
|
|
$grant->setRefreshTokenRepository($refreshTokenRepositoryMock);
|
|
|
|
$grant->setPublicKey(new CryptKey('file://' . __DIR__ . '/../Stubs/public.key'));
|
|
|
|
$grant->setPrivateKey(new CryptKey('file://' . __DIR__ . '/../Stubs/private.key'));
|
|
|
|
|
|
|
|
$request = new ServerRequest(
|
|
|
|
[],
|
|
|
|
[],
|
|
|
|
null,
|
|
|
|
'POST',
|
|
|
|
'php://input',
|
|
|
|
[],
|
|
|
|
[],
|
|
|
|
[],
|
|
|
|
[
|
|
|
|
'grant_type' => 'authorization_code',
|
|
|
|
'client_id' => 'foo',
|
|
|
|
'redirect_uri' => 'http://foo/bar',
|
|
|
|
'code' => $this->cryptStub->doEncrypt(
|
|
|
|
json_encode(
|
|
|
|
[
|
|
|
|
'auth_code_id' => uniqid(),
|
|
|
|
'expire_time' => time() + 3600,
|
|
|
|
'client_id' => 'foo',
|
|
|
|
'user_id' => 123,
|
|
|
|
'scopes' => ['foo'],
|
|
|
|
'redirect_uri' => 'http://foo/bar',
|
|
|
|
]
|
|
|
|
)
|
|
|
|
),
|
|
|
|
]
|
|
|
|
);
|
|
|
|
|
|
|
|
/** @var StubResponseType $response */
|
|
|
|
$response = $grant->respondToAccessTokenRequest($request, new StubResponseType(), new \DateInterval('PT10M'));
|
|
|
|
|
|
|
|
$this->assertTrue($response->getAccessToken() instanceof AccessTokenEntityInterface);
|
|
|
|
$this->assertTrue($response->getRefreshToken() instanceof RefreshTokenEntityInterface);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException \League\OAuth2\Server\Exception\OAuthServerException
|
|
|
|
* @expectedExceptionCode 7
|
|
|
|
*/
|
|
|
|
public function testRefreshTokenRepositoryFailToPersist()
|
|
|
|
{
|
|
|
|
$client = new ClientEntity();
|
|
|
|
$client->setIdentifier('foo');
|
|
|
|
$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);
|
|
|
|
|
|
|
|
$accessTokenRepositoryMock = $this->getMockBuilder(AccessTokenRepositoryInterface::class)->getMock();
|
|
|
|
$accessTokenRepositoryMock->method('getNewToken')->willReturn(new AccessTokenEntity());
|
|
|
|
$accessTokenRepositoryMock->method('persistNewAccessToken')->willReturnSelf();
|
|
|
|
|
|
|
|
$refreshTokenRepositoryMock = $this->getMockBuilder(RefreshTokenRepositoryInterface::class)->getMock();
|
|
|
|
$refreshTokenRepositoryMock->method('getNewRefreshToken')->willReturn(new RefreshTokenEntity());
|
|
|
|
$refreshTokenRepositoryMock->method('persistNewRefreshToken')->willThrowException(OAuthServerException::serverError('something bad happened'));
|
|
|
|
|
|
|
|
$grant = new AuthCodeGrant(
|
|
|
|
$this->getMock(AuthCodeRepositoryInterface::class),
|
|
|
|
$this->getMock(RefreshTokenRepositoryInterface::class),
|
|
|
|
new \DateInterval('PT10M')
|
|
|
|
);
|
|
|
|
$grant->setClientRepository($clientRepositoryMock);
|
|
|
|
$grant->setScopeRepository($scopeRepositoryMock);
|
|
|
|
$grant->setAccessTokenRepository($accessTokenRepositoryMock);
|
|
|
|
$grant->setRefreshTokenRepository($refreshTokenRepositoryMock);
|
|
|
|
$grant->setPublicKey(new CryptKey('file://' . __DIR__ . '/../Stubs/public.key'));
|
|
|
|
$grant->setPrivateKey(new CryptKey('file://' . __DIR__ . '/../Stubs/private.key'));
|
|
|
|
|
|
|
|
$request = new ServerRequest(
|
|
|
|
[],
|
|
|
|
[],
|
|
|
|
null,
|
|
|
|
'POST',
|
|
|
|
'php://input',
|
|
|
|
[],
|
|
|
|
[],
|
|
|
|
[],
|
|
|
|
[
|
|
|
|
'grant_type' => 'authorization_code',
|
|
|
|
'client_id' => 'foo',
|
|
|
|
'redirect_uri' => 'http://foo/bar',
|
|
|
|
'code' => $this->cryptStub->doEncrypt(
|
|
|
|
json_encode(
|
|
|
|
[
|
|
|
|
'auth_code_id' => uniqid(),
|
|
|
|
'expire_time' => time() + 3600,
|
|
|
|
'client_id' => 'foo',
|
|
|
|
'user_id' => 123,
|
|
|
|
'scopes' => ['foo'],
|
|
|
|
'redirect_uri' => 'http://foo/bar',
|
|
|
|
]
|
|
|
|
)
|
|
|
|
),
|
|
|
|
]
|
|
|
|
);
|
|
|
|
|
|
|
|
/** @var StubResponseType $response */
|
|
|
|
$response = $grant->respondToAccessTokenRequest($request, new StubResponseType(), new \DateInterval('PT10M'));
|
|
|
|
|
|
|
|
$this->assertTrue($response->getAccessToken() instanceof AccessTokenEntityInterface);
|
|
|
|
$this->assertTrue($response->getRefreshToken() instanceof RefreshTokenEntityInterface);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException \League\OAuth2\Server\Exception\UniqueTokenIdentifierConstraintViolationException
|
|
|
|
* @expectedExceptionCode 100
|
|
|
|
*/
|
|
|
|
public function testRefreshTokenRepositoryFailToPersistUniqueNoInfiniteLoop()
|
|
|
|
{
|
|
|
|
$client = new ClientEntity();
|
|
|
|
$client->setIdentifier('foo');
|
|
|
|
$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);
|
|
|
|
|
|
|
|
$accessTokenRepositoryMock = $this->getMockBuilder(AccessTokenRepositoryInterface::class)->getMock();
|
|
|
|
$accessTokenRepositoryMock->method('getNewToken')->willReturn(new AccessTokenEntity());
|
|
|
|
$accessTokenRepositoryMock->method('persistNewAccessToken')->willReturnSelf();
|
|
|
|
|
|
|
|
$refreshTokenRepositoryMock = $this->getMockBuilder(RefreshTokenRepositoryInterface::class)->getMock();
|
|
|
|
$refreshTokenRepositoryMock->method('getNewRefreshToken')->willReturn(new RefreshTokenEntity());
|
|
|
|
$refreshTokenRepositoryMock->method('persistNewRefreshToken')->willThrowException(UniqueTokenIdentifierConstraintViolationException::create());
|
|
|
|
|
|
|
|
$grant = new AuthCodeGrant(
|
|
|
|
$this->getMock(AuthCodeRepositoryInterface::class),
|
|
|
|
$this->getMock(RefreshTokenRepositoryInterface::class),
|
|
|
|
new \DateInterval('PT10M')
|
|
|
|
);
|
|
|
|
$grant->setClientRepository($clientRepositoryMock);
|
|
|
|
$grant->setScopeRepository($scopeRepositoryMock);
|
|
|
|
$grant->setAccessTokenRepository($accessTokenRepositoryMock);
|
|
|
|
$grant->setRefreshTokenRepository($refreshTokenRepositoryMock);
|
|
|
|
$grant->setPublicKey(new CryptKey('file://' . __DIR__ . '/../Stubs/public.key'));
|
|
|
|
$grant->setPrivateKey(new CryptKey('file://' . __DIR__ . '/../Stubs/private.key'));
|
|
|
|
|
|
|
|
$request = new ServerRequest(
|
|
|
|
[],
|
|
|
|
[],
|
|
|
|
null,
|
|
|
|
'POST',
|
|
|
|
'php://input',
|
|
|
|
[],
|
|
|
|
[],
|
|
|
|
[],
|
|
|
|
[
|
|
|
|
'grant_type' => 'authorization_code',
|
|
|
|
'client_id' => 'foo',
|
|
|
|
'redirect_uri' => 'http://foo/bar',
|
|
|
|
'code' => $this->cryptStub->doEncrypt(
|
|
|
|
json_encode(
|
|
|
|
[
|
|
|
|
'auth_code_id' => uniqid(),
|
|
|
|
'expire_time' => time() + 3600,
|
|
|
|
'client_id' => 'foo',
|
|
|
|
'user_id' => 123,
|
|
|
|
'scopes' => ['foo'],
|
|
|
|
'redirect_uri' => 'http://foo/bar',
|
|
|
|
]
|
|
|
|
)
|
|
|
|
),
|
|
|
|
]
|
|
|
|
);
|
|
|
|
|
|
|
|
/** @var StubResponseType $response */
|
|
|
|
$response = $grant->respondToAccessTokenRequest($request, new StubResponseType(), new \DateInterval('PT10M'));
|
|
|
|
|
|
|
|
$this->assertTrue($response->getAccessToken() instanceof AccessTokenEntityInterface);
|
|
|
|
$this->assertTrue($response->getRefreshToken() instanceof RefreshTokenEntityInterface);
|
|
|
|
}
|
|
|
|
}
|