From e88468d86747fef0e6b13bc51547b7ad3a279e5e Mon Sep 17 00:00:00 2001
From: Alejandro Celaya <alejandro@alejandrocelaya.com>
Date: Mon, 24 Sep 2018 19:24:23 +0200
Subject: [PATCH] Renamed CheckAuthenticationMiddleware to just
 AuthenticationMiddleware

---
 config/autoload/middleware-pipeline.global.php |  2 +-
 docs/swagger/swagger.json                      |  2 +-
 module/Rest/config/dependencies.config.php     |  4 ++--
 ...leware.php => AuthenticationMiddleware.php} |  7 ++++---
 ...st.php => AuthenticationMiddlewareTest.php} | 18 +++++++++---------
 5 files changed, 17 insertions(+), 16 deletions(-)
 rename module/Rest/src/Middleware/{CheckAuthenticationMiddleware.php => AuthenticationMiddleware.php} (95%)
 rename module/Rest/test/Middleware/{CheckAuthenticationMiddlewareTest.php => AuthenticationMiddlewareTest.php} (88%)

diff --git a/config/autoload/middleware-pipeline.global.php b/config/autoload/middleware-pipeline.global.php
index 308315bb..4bd14e39 100644
--- a/config/autoload/middleware-pipeline.global.php
+++ b/config/autoload/middleware-pipeline.global.php
@@ -39,7 +39,7 @@ return [
                 Rest\Middleware\CrossDomainMiddleware::class,
                 Expressive\Router\Middleware\ImplicitOptionsMiddleware::class,
                 Rest\Middleware\BodyParserMiddleware::class,
-                Rest\Middleware\CheckAuthenticationMiddleware::class,
+                Rest\Middleware\AuthenticationMiddleware::class,
             ],
             'priority' => 5,
         ],
diff --git a/docs/swagger/swagger.json b/docs/swagger/swagger.json
index d3cf0656..1ca741bb 100644
--- a/docs/swagger/swagger.json
+++ b/docs/swagger/swagger.json
@@ -27,7 +27,7 @@
                 "description": "A valid shlink API key",
                 "type": "apiKey",
                 "in": "header",
-                "name": "X-API-KEY"
+                "name": "X-Api-Key"
             },
             "Bearer": {
                 "description": "**[Deprecated]** The JWT identifying a previously authenticated API key",
diff --git a/module/Rest/config/dependencies.config.php b/module/Rest/config/dependencies.config.php
index c9a9da98..a8cca560 100644
--- a/module/Rest/config/dependencies.config.php
+++ b/module/Rest/config/dependencies.config.php
@@ -35,7 +35,7 @@ return [
             Middleware\BodyParserMiddleware::class => InvokableFactory::class,
             Middleware\CrossDomainMiddleware::class => InvokableFactory::class,
             Middleware\PathVersionMiddleware::class => InvokableFactory::class,
-            Middleware\CheckAuthenticationMiddleware::class => ConfigAbstractFactory::class,
+            Middleware\AuthenticationMiddleware::class => ConfigAbstractFactory::class,
             Middleware\ShortUrl\CreateShortUrlContentNegotiationMiddleware::class => InvokableFactory::class,
             Middleware\ShortUrl\ShortCodePathMiddleware::class => InvokableFactory::class,
         ],
@@ -92,7 +92,7 @@ return [
         Action\Tag\CreateTagsAction::class => [Service\Tag\TagService::class, LoggerInterface::class],
         Action\Tag\UpdateTagAction::class => [Service\Tag\TagService::class, Translator::class, LoggerInterface::class],
 
-        Middleware\CheckAuthenticationMiddleware::class => [
+        Middleware\AuthenticationMiddleware::class => [
             Authentication\JWTService::class,
             'translator',
             'config.auth.routes_whitelist',
diff --git a/module/Rest/src/Middleware/CheckAuthenticationMiddleware.php b/module/Rest/src/Middleware/AuthenticationMiddleware.php
similarity index 95%
rename from module/Rest/src/Middleware/CheckAuthenticationMiddleware.php
rename to module/Rest/src/Middleware/AuthenticationMiddleware.php
index dd0adf3b..124b4f03 100644
--- a/module/Rest/src/Middleware/CheckAuthenticationMiddleware.php
+++ b/module/Rest/src/Middleware/AuthenticationMiddleware.php
@@ -3,6 +3,7 @@ declare(strict_types=1);
 
 namespace Shlinkio\Shlink\Rest\Middleware;
 
+use Fig\Http\Message\RequestMethodInterface;
 use Fig\Http\Message\StatusCodeInterface;
 use Psr\Http\Message\ResponseInterface as Response;
 use Psr\Http\Message\ServerRequestInterface as Request;
@@ -18,9 +19,10 @@ use Zend\Expressive\Router\RouteResult;
 use Zend\I18n\Translator\TranslatorInterface;
 use Zend\Stdlib\ErrorHandler;
 
-class CheckAuthenticationMiddleware implements MiddlewareInterface, StatusCodeInterface
+class AuthenticationMiddleware implements MiddlewareInterface, StatusCodeInterface, RequestMethodInterface
 {
     public const AUTHORIZATION_HEADER = 'Authorization';
+    public const API_KEY_HEADER = 'X-Api-Key';
 
     /**
      * @var TranslatorInterface
@@ -64,12 +66,11 @@ class CheckAuthenticationMiddleware implements MiddlewareInterface, StatusCodeIn
      */
     public function process(Request $request, RequestHandlerInterface $handler): Response
     {
-        // If current route is the authenticate route or an OPTIONS request, continue to the next middleware
         /** @var RouteResult|null $routeResult */
         $routeResult = $request->getAttribute(RouteResult::class);
         if ($routeResult === null
             || $routeResult->isFailure()
-            || $request->getMethod() === 'OPTIONS'
+            || $request->getMethod() === self::METHOD_OPTIONS
             || \in_array($routeResult->getMatchedRouteName(), $this->routesWhitelist, true)
         ) {
             return $handler->handle($request);
diff --git a/module/Rest/test/Middleware/CheckAuthenticationMiddlewareTest.php b/module/Rest/test/Middleware/AuthenticationMiddlewareTest.php
similarity index 88%
rename from module/Rest/test/Middleware/CheckAuthenticationMiddlewareTest.php
rename to module/Rest/test/Middleware/AuthenticationMiddlewareTest.php
index 7c74a6aa..ee64eaa6 100644
--- a/module/Rest/test/Middleware/CheckAuthenticationMiddlewareTest.php
+++ b/module/Rest/test/Middleware/AuthenticationMiddlewareTest.php
@@ -9,7 +9,7 @@ use Prophecy\Prophecy\ObjectProphecy;
 use Psr\Http\Server\RequestHandlerInterface;
 use Shlinkio\Shlink\Rest\Action\AuthenticateAction;
 use Shlinkio\Shlink\Rest\Authentication\JWTService;
-use Shlinkio\Shlink\Rest\Middleware\CheckAuthenticationMiddleware;
+use Shlinkio\Shlink\Rest\Middleware\AuthenticationMiddleware;
 use ShlinkioTest\Shlink\Common\Util\TestUtils;
 use Zend\Diactoros\Response;
 use Zend\Diactoros\ServerRequestFactory;
@@ -18,10 +18,10 @@ use Zend\Expressive\Router\RouteResult;
 use Zend\I18n\Translator\Translator;
 use function Zend\Stratigility\middleware;
 
-class CheckAuthenticationMiddlewareTest extends TestCase
+class AuthenticationMiddlewareTest extends TestCase
 {
     /**
-     * @var CheckAuthenticationMiddleware
+     * @var AuthenticationMiddleware
      */
     protected $middleware;
     /**
@@ -37,7 +37,7 @@ class CheckAuthenticationMiddlewareTest extends TestCase
     public function setUp()
     {
         $this->jwtService = $this->prophesize(JWTService::class);
-        $this->middleware = new CheckAuthenticationMiddleware($this->jwtService->reveal(), Translator::factory([]), [
+        $this->middleware = new AuthenticationMiddleware($this->jwtService->reveal(), Translator::factory([]), [
             AuthenticateAction::class,
         ]);
         $this->dummyMiddleware = middleware(function () {
@@ -116,7 +116,7 @@ class CheckAuthenticationMiddlewareTest extends TestCase
         $request = ServerRequestFactory::fromGlobals()->withAttribute(
             RouteResult::class,
             RouteResult::fromRoute(new Route('bar', $this->dummyMiddleware), [])
-        )->withHeader(CheckAuthenticationMiddleware::AUTHORIZATION_HEADER, $authToken);
+        )->withHeader(AuthenticationMiddleware::AUTHORIZATION_HEADER, $authToken);
 
         $response = $this->middleware->process($request, TestUtils::createReqHandlerMock()->reveal());
 
@@ -133,7 +133,7 @@ class CheckAuthenticationMiddlewareTest extends TestCase
         $request = ServerRequestFactory::fromGlobals()->withAttribute(
             RouteResult::class,
             RouteResult::fromRoute(new Route('bar', $this->dummyMiddleware), [])
-        )->withHeader(CheckAuthenticationMiddleware::AUTHORIZATION_HEADER, 'Basic ' . $authToken);
+        )->withHeader(AuthenticationMiddleware::AUTHORIZATION_HEADER, 'Basic ' . $authToken);
 
         $response = $this->middleware->process($request, TestUtils::createReqHandlerMock()->reveal());
 
@@ -152,7 +152,7 @@ class CheckAuthenticationMiddlewareTest extends TestCase
         $request = ServerRequestFactory::fromGlobals()->withAttribute(
             RouteResult::class,
             RouteResult::fromRoute(new Route('bar', $this->dummyMiddleware), [])
-        )->withHeader(CheckAuthenticationMiddleware::AUTHORIZATION_HEADER, 'Bearer ' . $authToken);
+        )->withHeader(AuthenticationMiddleware::AUTHORIZATION_HEADER, 'Bearer ' . $authToken);
         $this->jwtService->verify($authToken)->willReturn(false)->shouldBeCalledTimes(1);
 
         $response = $this->middleware->process($request, TestUtils::createReqHandlerMock()->reveal());
@@ -168,7 +168,7 @@ class CheckAuthenticationMiddlewareTest extends TestCase
         $request = ServerRequestFactory::fromGlobals()->withAttribute(
             RouteResult::class,
             RouteResult::fromRoute(new Route('bar', $this->dummyMiddleware), [])
-        )->withHeader(CheckAuthenticationMiddleware::AUTHORIZATION_HEADER, 'bearer ' . $authToken);
+        )->withHeader(AuthenticationMiddleware::AUTHORIZATION_HEADER, 'bearer ' . $authToken);
         $this->jwtService->verify($authToken)->willReturn(true)->shouldBeCalledTimes(1);
         $this->jwtService->refresh($authToken)->willReturn($authToken)->shouldBeCalledTimes(1);
 
@@ -178,6 +178,6 @@ class CheckAuthenticationMiddlewareTest extends TestCase
         $resp = $this->middleware->process($request, $delegate->reveal());
 
         $process->shouldHaveBeenCalledTimes(1);
-        $this->assertArrayHasKey(CheckAuthenticationMiddleware::AUTHORIZATION_HEADER, $resp->getHeaders());
+        $this->assertArrayHasKey(AuthenticationMiddleware::AUTHORIZATION_HEADER, $resp->getHeaders());
     }
 }