diff --git a/module/Rest/src/Authentication/RequestToHttpAuthPlugin.php b/module/Rest/src/Authentication/RequestToHttpAuthPlugin.php index 9e3e28e0..c10c0321 100644 --- a/module/Rest/src/Authentication/RequestToHttpAuthPlugin.php +++ b/module/Rest/src/Authentication/RequestToHttpAuthPlugin.php @@ -4,9 +4,8 @@ declare(strict_types=1); namespace Shlinkio\Shlink\Rest\Authentication; -use Psr\Container; use Psr\Http\Message\ServerRequestInterface; -use Shlinkio\Shlink\Rest\Exception\NoAuthenticationException; +use Shlinkio\Shlink\Rest\Exception\MissingAuthenticationException; use function array_filter; use function array_reduce; @@ -30,13 +29,12 @@ class RequestToHttpAuthPlugin implements RequestToHttpAuthPluginInterface } /** - * @throws Container\ContainerExceptionInterface - * @throws NoAuthenticationException + * @throws MissingAuthenticationException */ public function fromRequest(ServerRequestInterface $request): Plugin\AuthenticationPluginInterface { if (! $this->hasAnySupportedHeader($request)) { - throw NoAuthenticationException::fromExpectedTypes(self::SUPPORTED_AUTH_HEADERS); + throw MissingAuthenticationException::fromExpectedTypes(self::SUPPORTED_AUTH_HEADERS); } return $this->authPluginManager->get($this->getFirstAvailableHeader($request)); diff --git a/module/Rest/src/Authentication/RequestToHttpAuthPluginInterface.php b/module/Rest/src/Authentication/RequestToHttpAuthPluginInterface.php index 18e68ee2..b8002431 100644 --- a/module/Rest/src/Authentication/RequestToHttpAuthPluginInterface.php +++ b/module/Rest/src/Authentication/RequestToHttpAuthPluginInterface.php @@ -4,15 +4,13 @@ declare(strict_types=1); namespace Shlinkio\Shlink\Rest\Authentication; -use Psr\Container; use Psr\Http\Message\ServerRequestInterface; -use Shlinkio\Shlink\Rest\Exception\NoAuthenticationException; +use Shlinkio\Shlink\Rest\Exception\MissingAuthenticationException; interface RequestToHttpAuthPluginInterface { /** - * @throws Container\ContainerExceptionInterface - * @throws NoAuthenticationException + * @throws MissingAuthenticationException */ public function fromRequest(ServerRequestInterface $request): Plugin\AuthenticationPluginInterface; } diff --git a/module/Rest/src/Exception/MissingAuthenticationException.php b/module/Rest/src/Exception/MissingAuthenticationException.php new file mode 100644 index 00000000..6ab55458 --- /dev/null +++ b/module/Rest/src/Exception/MissingAuthenticationException.php @@ -0,0 +1,36 @@ +detail = $e->getMessage(); + $e->title = self::TITLE; + $e->type = self::TYPE; + $e->status = StatusCodeInterface::STATUS_UNAUTHORIZED; + $e->additional = ['expectedTypes' => $expectedTypes]; + + return $e; + } +} diff --git a/module/Rest/src/Exception/NoAuthenticationException.php b/module/Rest/src/Exception/NoAuthenticationException.php deleted file mode 100644 index b5e8bfc8..00000000 --- a/module/Rest/src/Exception/NoAuthenticationException.php +++ /dev/null @@ -1,19 +0,0 @@ -logger = $logger ?: new NullLogger(); } - /** - * Process an incoming server request and return a response, optionally delegating - * to the next middleware component to create the response. - * - * @param Request $request - * @param RequestHandlerInterface $handler - * - * @return Response - * @throws \InvalidArgumentException - */ public function process(Request $request, RequestHandlerInterface $handler): Response { /** @var RouteResult|null $routeResult */ @@ -67,15 +52,7 @@ class AuthenticationMiddleware implements MiddlewareInterface, StatusCodeInterfa return $handler->handle($request); } - try { - $plugin = $this->requestToAuthPlugin->fromRequest($request); - } catch (ContainerExceptionInterface | NoAuthenticationException $e) { - $this->logger->warning('Invalid or no authentication provided. {e}', ['e' => $e]); - return $this->createErrorResponse(sprintf( - 'Expected one of the following authentication headers, but none were provided, ["%s"]', - implode('", "', RequestToHttpAuthPlugin::SUPPORTED_AUTH_HEADERS) - )); - } + $plugin = $this->requestToAuthPlugin->fromRequest($request); try { $plugin->verify($request); diff --git a/module/Rest/src/Util/RestUtils.php b/module/Rest/src/Util/RestUtils.php index 6e2b77f0..1dad3908 100644 --- a/module/Rest/src/Util/RestUtils.php +++ b/module/Rest/src/Util/RestUtils.php @@ -28,7 +28,8 @@ class RestUtils public const INVALID_CREDENTIALS_ERROR = 'INVALID_CREDENTIALS'; public const INVALID_AUTH_TOKEN_ERROR = 'INVALID_AUTH_TOKEN'; - public const INVALID_AUTHORIZATION_ERROR = 'INVALID_AUTHORIZATION'; + /** @deprecated */ + public const INVALID_AUTHORIZATION_ERROR = Rest\MissingAuthenticationException::TYPE; public const INVALID_API_KEY_ERROR = 'INVALID_API_KEY'; /** @deprecated */ diff --git a/module/Rest/test/Authentication/RequestToAuthPluginTest.php b/module/Rest/test/Authentication/RequestToAuthPluginTest.php index e9d68489..d9005261 100644 --- a/module/Rest/test/Authentication/RequestToAuthPluginTest.php +++ b/module/Rest/test/Authentication/RequestToAuthPluginTest.php @@ -11,7 +11,7 @@ use Shlinkio\Shlink\Rest\Authentication\Plugin\ApiKeyHeaderPlugin; use Shlinkio\Shlink\Rest\Authentication\Plugin\AuthenticationPluginInterface; use Shlinkio\Shlink\Rest\Authentication\Plugin\AuthorizationHeaderPlugin; use Shlinkio\Shlink\Rest\Authentication\RequestToHttpAuthPlugin; -use Shlinkio\Shlink\Rest\Exception\NoAuthenticationException; +use Shlinkio\Shlink\Rest\Exception\MissingAuthenticationException; use Zend\Diactoros\ServerRequest; use function implode; @@ -35,7 +35,7 @@ class RequestToAuthPluginTest extends TestCase { $request = new ServerRequest(); - $this->expectException(NoAuthenticationException::class); + $this->expectException(MissingAuthenticationException::class); $this->expectExceptionMessage(sprintf( 'None of the valid authentication mechanisms where provided. Expected one of ["%s"]', implode('", "', RequestToHttpAuthPlugin::SUPPORTED_AUTH_HEADERS) diff --git a/module/Rest/test/Middleware/AuthenticationMiddlewareTest.php b/module/Rest/test/Middleware/AuthenticationMiddlewareTest.php index e373fbea..054db85e 100644 --- a/module/Rest/test/Middleware/AuthenticationMiddlewareTest.php +++ b/module/Rest/test/Middleware/AuthenticationMiddlewareTest.php @@ -19,7 +19,7 @@ use Shlinkio\Shlink\Rest\Action\AuthenticateAction; use Shlinkio\Shlink\Rest\Authentication\Plugin\AuthenticationPluginInterface; use Shlinkio\Shlink\Rest\Authentication\RequestToHttpAuthPlugin; use Shlinkio\Shlink\Rest\Authentication\RequestToHttpAuthPluginInterface; -use Shlinkio\Shlink\Rest\Exception\NoAuthenticationException; +use Shlinkio\Shlink\Rest\Exception\MissingAuthenticationException; use Shlinkio\Shlink\Rest\Exception\VerifyAuthenticationException; use Shlinkio\Shlink\Rest\Middleware\AuthenticationMiddleware; use Shlinkio\Shlink\Rest\Util\RestUtils; @@ -128,7 +128,7 @@ class AuthenticationMiddlewareTest extends TestCase }; yield 'container exception' => [$containerException]; - yield 'authentication exception' => [NoAuthenticationException::fromExpectedTypes([])]; + yield 'authentication exception' => [MissingAuthenticationException::fromExpectedTypes([])]; } /** @test */