2018-09-28 23:08:01 +03:00
|
|
|
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
2018-09-29 09:16:40 +03:00
|
|
|
namespace ShlinkioTest\Shlink\Rest\Authentication\Plugin;
|
2018-09-28 23:08:01 +03:00
|
|
|
|
|
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
use Prophecy\Prophecy\ObjectProphecy;
|
|
|
|
use Shlinkio\Shlink\Rest\Authentication\JWTServiceInterface;
|
|
|
|
use Shlinkio\Shlink\Rest\Authentication\Plugin\AuthorizationHeaderPlugin;
|
|
|
|
use Shlinkio\Shlink\Rest\Exception\VerifyAuthenticationException;
|
|
|
|
use Zend\Diactoros\Response;
|
2018-12-26 01:01:30 +03:00
|
|
|
use Zend\Diactoros\ServerRequest;
|
2019-02-27 00:56:43 +03:00
|
|
|
|
2018-10-28 10:34:02 +03:00
|
|
|
use function sprintf;
|
2018-09-28 23:08:01 +03:00
|
|
|
|
|
|
|
class AuthorizationHeaderPluginTest extends TestCase
|
|
|
|
{
|
2018-11-20 21:30:27 +03:00
|
|
|
/** @var AuthorizationHeaderPlugin */
|
2018-09-28 23:08:01 +03:00
|
|
|
private $plugin;
|
2018-11-20 21:30:27 +03:00
|
|
|
/** @var ObjectProphecy */
|
2018-11-20 21:37:22 +03:00
|
|
|
private $jwtService;
|
2018-09-28 23:08:01 +03:00
|
|
|
|
2019-02-16 12:53:45 +03:00
|
|
|
public function setUp(): void
|
2018-09-28 23:08:01 +03:00
|
|
|
{
|
|
|
|
$this->jwtService = $this->prophesize(JWTServiceInterface::class);
|
2018-11-18 18:28:04 +03:00
|
|
|
$this->plugin = new AuthorizationHeaderPlugin($this->jwtService->reveal());
|
2018-09-28 23:08:01 +03:00
|
|
|
}
|
|
|
|
|
2019-02-17 22:28:34 +03:00
|
|
|
/** @test */
|
2018-09-28 23:08:01 +03:00
|
|
|
public function verifyAnAuthorizationWithoutBearerTypeThrowsException()
|
|
|
|
{
|
|
|
|
$authToken = 'ABC-abc';
|
2018-12-26 01:01:30 +03:00
|
|
|
$request = (new ServerRequest())->withHeader(
|
2018-09-28 23:08:01 +03:00
|
|
|
AuthorizationHeaderPlugin::HEADER_NAME,
|
|
|
|
$authToken
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->expectException(VerifyAuthenticationException::class);
|
|
|
|
$this->expectExceptionMessage(sprintf(
|
|
|
|
'You need to provide the Bearer type in the %s header.',
|
|
|
|
AuthorizationHeaderPlugin::HEADER_NAME
|
|
|
|
));
|
|
|
|
|
|
|
|
$this->plugin->verify($request);
|
|
|
|
}
|
|
|
|
|
2019-02-17 22:28:34 +03:00
|
|
|
/** @test */
|
2018-09-28 23:08:01 +03:00
|
|
|
public function verifyAnAuthorizationWithWrongTypeThrowsException()
|
|
|
|
{
|
|
|
|
$authToken = 'Basic ABC-abc';
|
2018-12-26 01:01:30 +03:00
|
|
|
$request = (new ServerRequest())->withHeader(
|
2018-09-28 23:08:01 +03:00
|
|
|
AuthorizationHeaderPlugin::HEADER_NAME,
|
|
|
|
$authToken
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->expectException(VerifyAuthenticationException::class);
|
|
|
|
$this->expectExceptionMessage(
|
|
|
|
'Provided authorization type Basic is not supported. Use Bearer instead.'
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->plugin->verify($request);
|
|
|
|
}
|
|
|
|
|
2019-02-17 22:28:34 +03:00
|
|
|
/** @test */
|
2018-09-28 23:08:01 +03:00
|
|
|
public function verifyAnExpiredTokenThrowsException()
|
|
|
|
{
|
|
|
|
$authToken = 'Bearer ABC-abc';
|
2018-12-26 01:01:30 +03:00
|
|
|
$request = (new ServerRequest())->withHeader(
|
2018-09-28 23:08:01 +03:00
|
|
|
AuthorizationHeaderPlugin::HEADER_NAME,
|
|
|
|
$authToken
|
|
|
|
);
|
|
|
|
$jwtVerify = $this->jwtService->verify('ABC-abc')->willReturn(false);
|
|
|
|
|
|
|
|
$this->expectException(VerifyAuthenticationException::class);
|
|
|
|
$this->expectExceptionMessage(sprintf(
|
|
|
|
'Missing or invalid auth token provided. Perform a new authentication request and send provided '
|
|
|
|
. 'token on every new request on the %s header',
|
|
|
|
AuthorizationHeaderPlugin::HEADER_NAME
|
|
|
|
));
|
|
|
|
|
|
|
|
$this->plugin->verify($request);
|
|
|
|
|
2018-11-11 15:18:21 +03:00
|
|
|
$jwtVerify->shouldHaveBeenCalledOnce();
|
2018-09-28 23:08:01 +03:00
|
|
|
}
|
|
|
|
|
2019-02-17 22:28:34 +03:00
|
|
|
/** @test */
|
2018-09-28 23:08:01 +03:00
|
|
|
public function verifyValidTokenDoesNotThrowException()
|
|
|
|
{
|
|
|
|
$authToken = 'Bearer ABC-abc';
|
2018-12-26 01:01:30 +03:00
|
|
|
$request = (new ServerRequest())->withHeader(
|
2018-09-28 23:08:01 +03:00
|
|
|
AuthorizationHeaderPlugin::HEADER_NAME,
|
|
|
|
$authToken
|
|
|
|
);
|
|
|
|
$jwtVerify = $this->jwtService->verify('ABC-abc')->willReturn(true);
|
|
|
|
|
|
|
|
$this->plugin->verify($request);
|
|
|
|
|
2018-11-11 15:18:21 +03:00
|
|
|
$jwtVerify->shouldHaveBeenCalledOnce();
|
2018-09-28 23:08:01 +03:00
|
|
|
}
|
|
|
|
|
2019-02-17 22:28:34 +03:00
|
|
|
/** @test */
|
2018-09-28 23:08:01 +03:00
|
|
|
public function updateReturnsAnUpdatedResponseWithNewJwt()
|
|
|
|
{
|
|
|
|
$authToken = 'Bearer ABC-abc';
|
2018-12-26 01:01:30 +03:00
|
|
|
$request = (new ServerRequest())->withHeader(
|
2018-09-28 23:08:01 +03:00
|
|
|
AuthorizationHeaderPlugin::HEADER_NAME,
|
|
|
|
$authToken
|
|
|
|
);
|
|
|
|
$jwtRefresh = $this->jwtService->refresh('ABC-abc')->willReturn('DEF-def');
|
|
|
|
|
|
|
|
$response = $this->plugin->update($request, new Response());
|
|
|
|
|
|
|
|
$this->assertTrue($response->hasHeader(AuthorizationHeaderPlugin::HEADER_NAME));
|
|
|
|
$this->assertEquals('Bearer DEF-def', $response->getHeaderLine(AuthorizationHeaderPlugin::HEADER_NAME));
|
2018-11-11 15:18:21 +03:00
|
|
|
$jwtRefresh->shouldHaveBeenCalledOnce();
|
2018-09-28 23:08:01 +03:00
|
|
|
}
|
|
|
|
}
|