2016-03-10 17:22:48 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace LeagueTests\Middleware;
|
|
|
|
|
2018-11-08 10:41:01 +01:00
|
|
|
use DateInterval;
|
|
|
|
use DateTimeImmutable;
|
2016-03-28 16:42:34 +02:00
|
|
|
use League\OAuth2\Server\CryptKey;
|
2016-03-10 17:22:48 +00:00
|
|
|
use League\OAuth2\Server\Middleware\ResourceServerMiddleware;
|
|
|
|
use League\OAuth2\Server\Repositories\AccessTokenRepositoryInterface;
|
2016-04-17 12:54:39 +01:00
|
|
|
use League\OAuth2\Server\ResourceServer;
|
2016-04-09 10:46:40 -04:00
|
|
|
use LeagueTests\Stubs\AccessTokenEntity;
|
2016-03-18 00:25:32 +01:00
|
|
|
use LeagueTests\Stubs\ClientEntity;
|
2017-11-08 16:07:07 -02:00
|
|
|
use PHPUnit\Framework\TestCase;
|
2016-03-10 17:22:48 +00:00
|
|
|
use Zend\Diactoros\Response;
|
|
|
|
use Zend\Diactoros\ServerRequest;
|
|
|
|
|
2017-11-08 16:07:07 -02:00
|
|
|
class ResourceServerMiddlewareTest extends TestCase
|
2016-03-10 17:22:48 +00:00
|
|
|
{
|
|
|
|
public function testValidResponse()
|
|
|
|
{
|
2016-04-17 12:54:39 +01:00
|
|
|
$server = new ResourceServer(
|
2016-07-08 15:29:21 +02:00
|
|
|
$this->getMockBuilder(AccessTokenRepositoryInterface::class)->getMock(),
|
2016-04-17 12:54:39 +01:00
|
|
|
'file://' . __DIR__ . '/../Stubs/public.key'
|
2016-03-10 17:22:48 +00:00
|
|
|
);
|
|
|
|
|
2016-03-18 00:25:32 +01:00
|
|
|
$client = new ClientEntity();
|
|
|
|
$client->setIdentifier('clientName');
|
|
|
|
|
|
|
|
$accessToken = new AccessTokenEntity();
|
|
|
|
$accessToken->setIdentifier('test');
|
|
|
|
$accessToken->setUserIdentifier(123);
|
2018-11-08 10:41:01 +01:00
|
|
|
$accessToken->setExpiryDateTime((new DateTimeImmutable())->add(new DateInterval('PT1H')));
|
2016-03-18 00:25:32 +01:00
|
|
|
$accessToken->setClient($client);
|
2018-05-23 16:34:39 +01:00
|
|
|
$accessToken->setPrivateKey(new CryptKey('file://' . __DIR__ . '/../Stubs/private.key'));
|
2016-03-18 00:25:32 +01:00
|
|
|
|
2018-05-23 16:34:39 +01:00
|
|
|
$token = (string) $accessToken;
|
2016-03-18 00:25:32 +01:00
|
|
|
|
2016-03-10 17:22:48 +00:00
|
|
|
$request = new ServerRequest();
|
2016-03-18 00:25:32 +01:00
|
|
|
$request = $request->withHeader('authorization', sprintf('Bearer %s', $token));
|
2016-03-10 17:22:48 +00:00
|
|
|
|
|
|
|
$middleware = new ResourceServerMiddleware($server);
|
|
|
|
$response = $middleware->__invoke(
|
|
|
|
$request,
|
|
|
|
new Response(),
|
|
|
|
function () {
|
|
|
|
$this->assertEquals('test', func_get_args()[0]->getAttribute('oauth_access_token_id'));
|
2016-03-10 12:40:28 -05:00
|
|
|
|
2016-03-10 17:22:48 +00:00
|
|
|
return func_get_args()[1];
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertEquals(200, $response->getStatusCode());
|
|
|
|
}
|
|
|
|
|
2016-04-10 15:58:01 +01:00
|
|
|
public function testValidResponseExpiredToken()
|
|
|
|
{
|
2016-04-17 12:54:39 +01:00
|
|
|
$server = new ResourceServer(
|
2016-07-08 15:29:21 +02:00
|
|
|
$this->getMockBuilder(AccessTokenRepositoryInterface::class)->getMock(),
|
2016-04-17 12:54:39 +01:00
|
|
|
'file://' . __DIR__ . '/../Stubs/public.key'
|
2016-04-10 15:58:01 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
$client = new ClientEntity();
|
|
|
|
$client->setIdentifier('clientName');
|
|
|
|
|
|
|
|
$accessToken = new AccessTokenEntity();
|
|
|
|
$accessToken->setIdentifier('test');
|
|
|
|
$accessToken->setUserIdentifier(123);
|
2018-11-08 10:41:01 +01:00
|
|
|
$accessToken->setExpiryDateTime((new DateTimeImmutable())->sub(new DateInterval('PT1H')));
|
2016-04-10 15:58:01 +01:00
|
|
|
$accessToken->setClient($client);
|
2018-05-23 16:34:39 +01:00
|
|
|
$accessToken->setPrivateKey(new CryptKey('file://' . __DIR__ . '/../Stubs/private.key'));
|
2016-04-10 15:58:01 +01:00
|
|
|
|
2018-05-23 16:34:39 +01:00
|
|
|
$token = (string) $accessToken;
|
2016-04-10 15:58:01 +01:00
|
|
|
|
|
|
|
$request = new ServerRequest();
|
|
|
|
$request = $request->withHeader('authorization', sprintf('Bearer %s', $token));
|
|
|
|
|
|
|
|
$middleware = new ResourceServerMiddleware($server);
|
|
|
|
$response = $middleware->__invoke(
|
|
|
|
$request,
|
|
|
|
new Response(),
|
|
|
|
function () {
|
|
|
|
$this->assertEquals('test', func_get_args()[0]->getAttribute('oauth_access_token_id'));
|
|
|
|
|
|
|
|
return func_get_args()[1];
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertEquals(401, $response->getStatusCode());
|
|
|
|
}
|
|
|
|
|
2016-03-10 17:22:48 +00:00
|
|
|
public function testErrorResponse()
|
|
|
|
{
|
2016-04-17 12:54:39 +01:00
|
|
|
$server = new ResourceServer(
|
2016-07-08 15:29:21 +02:00
|
|
|
$this->getMockBuilder(AccessTokenRepositoryInterface::class)->getMock(),
|
2016-04-17 12:54:39 +01:00
|
|
|
'file://' . __DIR__ . '/../Stubs/public.key'
|
2016-03-10 17:22:48 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
$request = new ServerRequest();
|
|
|
|
$request = $request->withHeader('authorization', '');
|
|
|
|
|
|
|
|
$middleware = new ResourceServerMiddleware($server);
|
|
|
|
$response = $middleware->__invoke(
|
|
|
|
$request,
|
|
|
|
new Response(),
|
|
|
|
function () {
|
|
|
|
return func_get_args()[1];
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertEquals(401, $response->getStatusCode());
|
|
|
|
}
|
|
|
|
}
|