2016-07-04 17:54:24 +02:00
|
|
|
<?php
|
2017-10-12 10:13:20 +02:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
2016-07-19 17:07:59 +02:00
|
|
|
namespace Shlinkio\Shlink\Rest\Middleware;
|
2016-07-04 17:54:24 +02:00
|
|
|
|
2018-09-24 19:24:23 +02:00
|
|
|
use Fig\Http\Message\RequestMethodInterface;
|
2017-03-25 09:37:13 +01:00
|
|
|
use Fig\Http\Message\StatusCodeInterface;
|
2016-07-04 17:54:24 +02:00
|
|
|
use Psr\Http\Message\ResponseInterface as Response;
|
|
|
|
|
use Psr\Http\Message\ServerRequestInterface as Request;
|
2018-03-26 18:49:28 +02:00
|
|
|
use Psr\Http\Server\MiddlewareInterface;
|
|
|
|
|
use Psr\Http\Server\RequestHandlerInterface;
|
2016-08-08 12:33:58 +02:00
|
|
|
use Psr\Log\LoggerInterface;
|
|
|
|
|
use Psr\Log\NullLogger;
|
2016-08-07 19:53:14 +02:00
|
|
|
use Shlinkio\Shlink\Rest\Authentication\JWTServiceInterface;
|
|
|
|
|
use Shlinkio\Shlink\Rest\Exception\AuthenticationException;
|
2016-07-19 17:07:59 +02:00
|
|
|
use Shlinkio\Shlink\Rest\Util\RestUtils;
|
2016-07-04 17:54:24 +02:00
|
|
|
use Zend\Diactoros\Response\JsonResponse;
|
|
|
|
|
use Zend\Expressive\Router\RouteResult;
|
2016-07-21 16:41:16 +02:00
|
|
|
use Zend\I18n\Translator\TranslatorInterface;
|
2016-08-29 12:43:02 +02:00
|
|
|
use Zend\Stdlib\ErrorHandler;
|
2016-07-04 17:54:24 +02:00
|
|
|
|
2018-09-24 19:24:23 +02:00
|
|
|
class AuthenticationMiddleware implements MiddlewareInterface, StatusCodeInterface, RequestMethodInterface
|
2016-07-04 17:54:24 +02:00
|
|
|
{
|
2018-03-26 18:49:28 +02:00
|
|
|
public const AUTHORIZATION_HEADER = 'Authorization';
|
2018-09-24 19:24:23 +02:00
|
|
|
public const API_KEY_HEADER = 'X-Api-Key';
|
2016-07-04 17:54:24 +02:00
|
|
|
|
2016-07-21 16:41:16 +02:00
|
|
|
/**
|
|
|
|
|
* @var TranslatorInterface
|
|
|
|
|
*/
|
|
|
|
|
private $translator;
|
2016-08-07 19:53:14 +02:00
|
|
|
/**
|
|
|
|
|
* @var JWTServiceInterface
|
|
|
|
|
*/
|
|
|
|
|
private $jwtService;
|
2016-08-08 12:33:58 +02:00
|
|
|
/**
|
|
|
|
|
* @var LoggerInterface
|
|
|
|
|
*/
|
|
|
|
|
private $logger;
|
2018-05-01 18:35:12 +02:00
|
|
|
/**
|
|
|
|
|
* @var array
|
|
|
|
|
*/
|
|
|
|
|
private $routesWhitelist;
|
2016-07-04 17:54:24 +02:00
|
|
|
|
2016-08-08 12:33:58 +02:00
|
|
|
public function __construct(
|
|
|
|
|
JWTServiceInterface $jwtService,
|
|
|
|
|
TranslatorInterface $translator,
|
2018-05-01 18:35:12 +02:00
|
|
|
array $routesWhitelist,
|
2016-08-08 12:33:58 +02:00
|
|
|
LoggerInterface $logger = null
|
|
|
|
|
) {
|
2016-07-21 16:41:16 +02:00
|
|
|
$this->translator = $translator;
|
2016-08-07 19:53:14 +02:00
|
|
|
$this->jwtService = $jwtService;
|
2018-05-01 18:35:12 +02:00
|
|
|
$this->routesWhitelist = $routesWhitelist;
|
2016-08-08 12:33:58 +02:00
|
|
|
$this->logger = $logger ?: new NullLogger();
|
2016-07-04 17:54:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2017-03-25 09:37:13 +01:00
|
|
|
* Process an incoming server request and return a response, optionally delegating
|
|
|
|
|
* to the next middleware component to create the response.
|
2016-07-04 17:54:24 +02:00
|
|
|
*
|
|
|
|
|
* @param Request $request
|
2018-03-26 18:49:28 +02:00
|
|
|
* @param RequestHandlerInterface $handler
|
2017-03-25 09:37:13 +01:00
|
|
|
*
|
|
|
|
|
* @return Response
|
2017-07-22 14:20:40 +02:00
|
|
|
* @throws \InvalidArgumentException
|
2017-12-27 16:23:54 +01:00
|
|
|
* @throws \ErrorException
|
2016-07-04 17:54:24 +02:00
|
|
|
*/
|
2018-03-26 18:49:28 +02:00
|
|
|
public function process(Request $request, RequestHandlerInterface $handler): Response
|
2016-07-04 17:54:24 +02:00
|
|
|
{
|
2017-12-27 16:23:54 +01:00
|
|
|
/** @var RouteResult|null $routeResult */
|
2016-07-04 17:54:24 +02:00
|
|
|
$routeResult = $request->getAttribute(RouteResult::class);
|
2017-12-27 16:23:54 +01:00
|
|
|
if ($routeResult === null
|
2016-07-26 19:09:54 +02:00
|
|
|
|| $routeResult->isFailure()
|
2018-09-24 19:24:23 +02:00
|
|
|
|| $request->getMethod() === self::METHOD_OPTIONS
|
2018-05-01 18:35:12 +02:00
|
|
|
|| \in_array($routeResult->getMatchedRouteName(), $this->routesWhitelist, true)
|
2016-07-05 19:19:23 +02:00
|
|
|
) {
|
2018-03-26 18:49:28 +02:00
|
|
|
return $handler->handle($request);
|
2016-07-04 17:54:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Check that the auth header was provided, and that it belongs to a non-expired token
|
2016-08-07 19:53:14 +02:00
|
|
|
if (! $request->hasHeader(self::AUTHORIZATION_HEADER)) {
|
2016-07-04 17:54:24 +02:00
|
|
|
return $this->createTokenErrorResponse();
|
|
|
|
|
}
|
|
|
|
|
|
2016-08-07 19:53:14 +02:00
|
|
|
// Get token making sure the an authorization type is provided
|
|
|
|
|
$authToken = $request->getHeaderLine(self::AUTHORIZATION_HEADER);
|
2018-03-27 23:56:55 +02:00
|
|
|
$authTokenParts = \explode(' ', $authToken);
|
|
|
|
|
if (\count($authTokenParts) === 1) {
|
2016-08-07 19:53:14 +02:00
|
|
|
return new JsonResponse([
|
|
|
|
|
'error' => RestUtils::INVALID_AUTHORIZATION_ERROR,
|
2018-03-27 23:56:55 +02:00
|
|
|
'message' => \sprintf($this->translator->translate(
|
2016-08-07 19:53:14 +02:00
|
|
|
'You need to provide the Bearer type in the %s header.'
|
|
|
|
|
), self::AUTHORIZATION_HEADER),
|
2017-03-25 09:37:13 +01:00
|
|
|
], self::STATUS_UNAUTHORIZED);
|
2016-08-07 19:53:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Make sure the authorization type is Bearer
|
2018-03-27 23:56:55 +02:00
|
|
|
[$authType, $jwt] = $authTokenParts;
|
|
|
|
|
if (\strtolower($authType) !== 'bearer') {
|
2016-08-07 19:53:14 +02:00
|
|
|
return new JsonResponse([
|
|
|
|
|
'error' => RestUtils::INVALID_AUTHORIZATION_ERROR,
|
2018-03-27 23:56:55 +02:00
|
|
|
'message' => \sprintf($this->translator->translate(
|
2016-08-07 19:53:14 +02:00
|
|
|
'Provided authorization type %s is not supported. Use Bearer instead.'
|
|
|
|
|
), $authType),
|
2017-03-25 09:37:13 +01:00
|
|
|
], self::STATUS_UNAUTHORIZED);
|
2016-08-07 19:53:14 +02:00
|
|
|
}
|
|
|
|
|
|
2016-07-04 17:54:24 +02:00
|
|
|
try {
|
2016-08-29 12:43:02 +02:00
|
|
|
ErrorHandler::start();
|
2016-08-07 19:53:14 +02:00
|
|
|
if (! $this->jwtService->verify($jwt)) {
|
2016-07-04 17:54:24 +02:00
|
|
|
return $this->createTokenErrorResponse();
|
|
|
|
|
}
|
2016-08-29 12:43:02 +02:00
|
|
|
ErrorHandler::stop(true);
|
2016-07-04 17:54:24 +02:00
|
|
|
|
|
|
|
|
// Update the token expiration and continue to next middleware
|
2016-08-07 19:53:14 +02:00
|
|
|
$jwt = $this->jwtService->refresh($jwt);
|
2018-03-26 18:49:28 +02:00
|
|
|
$response = $handler->handle($request);
|
2016-08-07 19:53:14 +02:00
|
|
|
|
|
|
|
|
// Return the response with the updated token on it
|
|
|
|
|
return $response->withHeader(self::AUTHORIZATION_HEADER, 'Bearer ' . $jwt);
|
|
|
|
|
} catch (AuthenticationException $e) {
|
2016-08-08 12:33:58 +02:00
|
|
|
$this->logger->warning('Tried to access API with an invalid JWT.' . PHP_EOL . $e);
|
2016-07-04 17:54:24 +02:00
|
|
|
return $this->createTokenErrorResponse();
|
2016-08-29 12:43:02 +02:00
|
|
|
} finally {
|
|
|
|
|
ErrorHandler::clean();
|
2016-07-04 17:54:24 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-03-27 23:56:55 +02:00
|
|
|
/**
|
|
|
|
|
* @return JsonResponse
|
|
|
|
|
* @throws \InvalidArgumentException
|
|
|
|
|
*/
|
|
|
|
|
private function createTokenErrorResponse(): JsonResponse
|
2016-07-04 17:54:24 +02:00
|
|
|
{
|
|
|
|
|
return new JsonResponse([
|
|
|
|
|
'error' => RestUtils::INVALID_AUTH_TOKEN_ERROR,
|
2018-03-27 23:56:55 +02:00
|
|
|
'message' => \sprintf(
|
2016-07-21 16:41:16 +02:00
|
|
|
$this->translator->translate(
|
|
|
|
|
'Missing or invalid auth token provided. Perform a new authentication request and send provided '
|
|
|
|
|
. 'token on every new request on the "%s" header'
|
|
|
|
|
),
|
2016-08-07 19:53:14 +02:00
|
|
|
self::AUTHORIZATION_HEADER
|
2016-07-04 17:54:24 +02:00
|
|
|
),
|
2017-03-25 09:37:13 +01:00
|
|
|
], self::STATUS_UNAUTHORIZED);
|
2016-07-04 17:54:24 +02:00
|
|
|
}
|
|
|
|
|
}
|