2016-07-31 00:26:49 +03:00
|
|
|
<?php
|
2017-10-12 11:13:20 +03:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
2016-07-31 00:26:49 +03:00
|
|
|
namespace ShlinkioTest\Shlink\Rest\Middleware;
|
|
|
|
|
2017-03-24 22:34:18 +03:00
|
|
|
use PHPUnit\Framework\TestCase;
|
2017-03-25 11:37:13 +03:00
|
|
|
use Prophecy\Prophecy\MethodProphecy;
|
2016-07-31 00:26:49 +03:00
|
|
|
use Prophecy\Prophecy\ObjectProphecy;
|
2018-03-26 20:02:41 +03:00
|
|
|
use Psr\Http\Server\RequestHandlerInterface;
|
2017-07-07 14:12:45 +03:00
|
|
|
use Shlinkio\Shlink\Rest\Action\AuthenticateAction;
|
2016-08-07 20:53:14 +03:00
|
|
|
use Shlinkio\Shlink\Rest\Authentication\JWTService;
|
2016-07-31 00:26:49 +03:00
|
|
|
use Shlinkio\Shlink\Rest\Middleware\CheckAuthenticationMiddleware;
|
2017-03-25 11:37:13 +03:00
|
|
|
use ShlinkioTest\Shlink\Common\Util\TestUtils;
|
2016-07-31 00:26:49 +03:00
|
|
|
use Zend\Diactoros\Response;
|
|
|
|
use Zend\Diactoros\ServerRequestFactory;
|
2017-03-24 23:38:43 +03:00
|
|
|
use Zend\Expressive\Router\Route;
|
2016-07-31 00:26:49 +03:00
|
|
|
use Zend\Expressive\Router\RouteResult;
|
|
|
|
use Zend\I18n\Translator\Translator;
|
2018-03-21 03:05:55 +03:00
|
|
|
use function Zend\Stratigility\middleware;
|
|
|
|
|
2016-07-31 00:26:49 +03:00
|
|
|
class CheckAuthenticationMiddlewareTest extends TestCase
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var CheckAuthenticationMiddleware
|
|
|
|
*/
|
|
|
|
protected $middleware;
|
|
|
|
/**
|
|
|
|
* @var ObjectProphecy
|
|
|
|
*/
|
2016-08-07 20:53:14 +03:00
|
|
|
protected $jwtService;
|
2016-07-31 00:26:49 +03:00
|
|
|
|
2018-03-21 13:13:03 +03:00
|
|
|
/**
|
|
|
|
* @var callable
|
|
|
|
*/
|
|
|
|
protected $dummyMiddleware;
|
|
|
|
|
2016-07-31 00:26:49 +03:00
|
|
|
public function setUp()
|
|
|
|
{
|
2016-08-07 20:53:14 +03:00
|
|
|
$this->jwtService = $this->prophesize(JWTService::class);
|
|
|
|
$this->middleware = new CheckAuthenticationMiddleware($this->jwtService->reveal(), Translator::factory([]));
|
2018-03-21 03:05:55 +03:00
|
|
|
$this->dummyMiddleware = middleware(function ($request, $handler) {
|
|
|
|
return new Response\EmptyResponse;
|
|
|
|
});
|
2016-07-31 00:26:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
*/
|
2016-08-07 20:53:14 +03:00
|
|
|
public function someWhiteListedSituationsFallbackToNextMiddleware()
|
2016-07-31 00:26:49 +03:00
|
|
|
{
|
|
|
|
$request = ServerRequestFactory::fromGlobals();
|
2018-03-26 20:02:41 +03:00
|
|
|
$delegate = $this->prophesize(RequestHandlerInterface::class);
|
2017-03-25 11:37:13 +03:00
|
|
|
/** @var MethodProphecy $process */
|
2018-03-26 20:02:41 +03:00
|
|
|
$process = $delegate->handle($request)->willReturn(new Response());
|
2017-03-25 11:37:13 +03:00
|
|
|
|
|
|
|
$this->middleware->process($request, $delegate->reveal());
|
|
|
|
$process->shouldHaveBeenCalledTimes(1);
|
2016-07-31 00:26:49 +03:00
|
|
|
|
|
|
|
$request = ServerRequestFactory::fromGlobals()->withAttribute(
|
|
|
|
RouteResult::class,
|
|
|
|
RouteResult::fromRouteFailure(['GET'])
|
|
|
|
);
|
2018-03-26 20:02:41 +03:00
|
|
|
$delegate = $this->prophesize(RequestHandlerInterface::class);
|
2017-03-25 11:37:13 +03:00
|
|
|
/** @var MethodProphecy $process */
|
2018-03-26 20:02:41 +03:00
|
|
|
$process = $delegate->handle($request)->willReturn(new Response());
|
2017-03-25 11:37:13 +03:00
|
|
|
$this->middleware->process($request, $delegate->reveal());
|
|
|
|
$process->shouldHaveBeenCalledTimes(1);
|
2016-07-31 00:26:49 +03:00
|
|
|
|
|
|
|
$request = ServerRequestFactory::fromGlobals()->withAttribute(
|
|
|
|
RouteResult::class,
|
2018-03-21 03:05:55 +03:00
|
|
|
RouteResult::fromRoute(new Route(
|
|
|
|
'foo',
|
|
|
|
$this->dummyMiddleware,
|
|
|
|
Route::HTTP_METHOD_ANY,
|
|
|
|
AuthenticateAction::class
|
|
|
|
))
|
2016-07-31 00:26:49 +03:00
|
|
|
);
|
2018-03-26 20:02:41 +03:00
|
|
|
$delegate = $this->prophesize(RequestHandlerInterface::class);
|
2017-03-25 11:37:13 +03:00
|
|
|
/** @var MethodProphecy $process */
|
2018-03-26 20:02:41 +03:00
|
|
|
$process = $delegate->handle($request)->willReturn(new Response());
|
2017-03-25 11:37:13 +03:00
|
|
|
$this->middleware->process($request, $delegate->reveal());
|
|
|
|
$process->shouldHaveBeenCalledTimes(1);
|
2016-07-31 00:26:49 +03:00
|
|
|
|
|
|
|
$request = ServerRequestFactory::fromGlobals()->withAttribute(
|
|
|
|
RouteResult::class,
|
2018-03-21 03:05:55 +03:00
|
|
|
RouteResult::fromRoute(new Route('bar', $this->dummyMiddleware), [])
|
2016-07-31 00:26:49 +03:00
|
|
|
)->withMethod('OPTIONS');
|
2018-03-26 20:02:41 +03:00
|
|
|
$delegate = $this->prophesize(RequestHandlerInterface::class);
|
2017-03-25 11:37:13 +03:00
|
|
|
/** @var MethodProphecy $process */
|
2018-03-26 20:02:41 +03:00
|
|
|
$process = $delegate->handle($request)->willReturn(new Response());
|
2017-03-25 11:37:13 +03:00
|
|
|
$this->middleware->process($request, $delegate->reveal());
|
|
|
|
$process->shouldHaveBeenCalledTimes(1);
|
2016-07-31 00:26:49 +03:00
|
|
|
}
|
2016-07-31 14:01:08 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
*/
|
|
|
|
public function noHeaderReturnsError()
|
|
|
|
{
|
|
|
|
$request = ServerRequestFactory::fromGlobals()->withAttribute(
|
|
|
|
RouteResult::class,
|
2018-03-21 03:05:55 +03:00
|
|
|
RouteResult::fromRoute(new Route('bar', $this->dummyMiddleware), [])
|
2016-07-31 14:01:08 +03:00
|
|
|
);
|
2018-03-26 20:02:41 +03:00
|
|
|
$response = $this->middleware->process($request, TestUtils::createReqHandlerMock()->reveal());
|
2016-07-31 14:01:08 +03:00
|
|
|
$this->assertEquals(401, $response->getStatusCode());
|
|
|
|
}
|
|
|
|
|
2016-08-07 20:53:14 +03:00
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
*/
|
|
|
|
public function provideAnAuthorizationWithoutTypeReturnsError()
|
|
|
|
{
|
|
|
|
$authToken = 'ABC-abc';
|
|
|
|
$request = ServerRequestFactory::fromGlobals()->withAttribute(
|
|
|
|
RouteResult::class,
|
2018-03-21 03:05:55 +03:00
|
|
|
RouteResult::fromRoute(new Route('bar', $this->dummyMiddleware), [])
|
2016-08-07 20:53:14 +03:00
|
|
|
)->withHeader(CheckAuthenticationMiddleware::AUTHORIZATION_HEADER, $authToken);
|
|
|
|
|
2018-03-26 20:02:41 +03:00
|
|
|
$response = $this->middleware->process($request, TestUtils::createReqHandlerMock()->reveal());
|
2017-03-25 11:37:13 +03:00
|
|
|
|
2016-08-07 20:53:14 +03:00
|
|
|
$this->assertEquals(401, $response->getStatusCode());
|
|
|
|
$this->assertTrue(strpos($response->getBody()->getContents(), 'You need to provide the Bearer type') > 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
*/
|
|
|
|
public function provideAnAuthorizationWithWrongTypeReturnsError()
|
|
|
|
{
|
|
|
|
$authToken = 'ABC-abc';
|
|
|
|
$request = ServerRequestFactory::fromGlobals()->withAttribute(
|
|
|
|
RouteResult::class,
|
2018-03-21 03:05:55 +03:00
|
|
|
RouteResult::fromRoute(new Route('bar', $this->dummyMiddleware), [])
|
2016-08-07 20:53:14 +03:00
|
|
|
)->withHeader(CheckAuthenticationMiddleware::AUTHORIZATION_HEADER, 'Basic ' . $authToken);
|
|
|
|
|
2018-03-26 20:02:41 +03:00
|
|
|
$response = $this->middleware->process($request, TestUtils::createReqHandlerMock()->reveal());
|
2017-03-25 11:37:13 +03:00
|
|
|
|
2016-08-07 20:53:14 +03:00
|
|
|
$this->assertEquals(401, $response->getStatusCode());
|
|
|
|
$this->assertTrue(
|
|
|
|
strpos($response->getBody()->getContents(), 'Provided authorization type Basic is not supported') > 0
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2016-07-31 14:01:08 +03:00
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
*/
|
|
|
|
public function provideAnExpiredTokenReturnsError()
|
|
|
|
{
|
|
|
|
$authToken = 'ABC-abc';
|
|
|
|
$request = ServerRequestFactory::fromGlobals()->withAttribute(
|
|
|
|
RouteResult::class,
|
2018-03-21 03:05:55 +03:00
|
|
|
RouteResult::fromRoute(new Route('bar', $this->dummyMiddleware), [])
|
2016-08-07 20:53:14 +03:00
|
|
|
)->withHeader(CheckAuthenticationMiddleware::AUTHORIZATION_HEADER, 'Bearer ' . $authToken);
|
|
|
|
$this->jwtService->verify($authToken)->willReturn(false)->shouldBeCalledTimes(1);
|
2016-07-31 14:01:08 +03:00
|
|
|
|
2018-03-26 20:02:41 +03:00
|
|
|
$response = $this->middleware->process($request, TestUtils::createReqHandlerMock()->reveal());
|
2016-07-31 14:01:08 +03:00
|
|
|
$this->assertEquals(401, $response->getStatusCode());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
*/
|
2017-03-25 11:37:13 +03:00
|
|
|
public function provideCorrectTokenUpdatesExpirationAndFallsBackToNextMiddleware()
|
2016-07-31 14:01:08 +03:00
|
|
|
{
|
|
|
|
$authToken = 'ABC-abc';
|
|
|
|
$request = ServerRequestFactory::fromGlobals()->withAttribute(
|
|
|
|
RouteResult::class,
|
2018-03-21 03:05:55 +03:00
|
|
|
RouteResult::fromRoute(new Route('bar', $this->dummyMiddleware), [])
|
2016-08-07 20:53:14 +03:00
|
|
|
)->withHeader(CheckAuthenticationMiddleware::AUTHORIZATION_HEADER, 'bearer ' . $authToken);
|
|
|
|
$this->jwtService->verify($authToken)->willReturn(true)->shouldBeCalledTimes(1);
|
|
|
|
$this->jwtService->refresh($authToken)->willReturn($authToken)->shouldBeCalledTimes(1);
|
2016-07-31 14:01:08 +03:00
|
|
|
|
2018-03-26 20:02:41 +03:00
|
|
|
$delegate = $this->prophesize(RequestHandlerInterface::class);
|
2017-03-25 11:37:13 +03:00
|
|
|
/** @var MethodProphecy $process */
|
2018-03-26 20:02:41 +03:00
|
|
|
$process = $delegate->handle($request)->willReturn(new Response());
|
2017-03-25 11:37:13 +03:00
|
|
|
$resp = $this->middleware->process($request, $delegate->reveal());
|
|
|
|
|
|
|
|
$process->shouldHaveBeenCalledTimes(1);
|
|
|
|
$this->assertArrayHasKey(CheckAuthenticationMiddleware::AUTHORIZATION_HEADER, $resp->getHeaders());
|
2016-07-31 14:01:08 +03:00
|
|
|
}
|
2016-07-31 00:26:49 +03:00
|
|
|
}
|