2016-07-04 14:45:18 +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\Action;
|
2016-07-04 14:45:18 +02:00
|
|
|
|
|
|
|
|
use Psr\Http\Message\ResponseInterface as Response;
|
|
|
|
|
use Psr\Http\Message\ServerRequestInterface as Request;
|
2017-03-25 10:04:48 +01:00
|
|
|
use Psr\Log\LoggerInterface;
|
2016-08-07 19:13:40 +02:00
|
|
|
use Shlinkio\Shlink\Rest\Authentication\JWTServiceInterface;
|
2016-08-07 10:26:34 +02:00
|
|
|
use Shlinkio\Shlink\Rest\Service\ApiKeyService;
|
|
|
|
|
use Shlinkio\Shlink\Rest\Service\ApiKeyServiceInterface;
|
2016-07-19 17:07:59 +02:00
|
|
|
use Shlinkio\Shlink\Rest\Util\RestUtils;
|
2016-07-04 14:45:18 +02:00
|
|
|
use Zend\Diactoros\Response\JsonResponse;
|
2016-07-21 16:41:16 +02:00
|
|
|
use Zend\I18n\Translator\TranslatorInterface;
|
2016-07-04 14:45:18 +02:00
|
|
|
|
2016-07-27 20:22:50 +02:00
|
|
|
class AuthenticateAction extends AbstractRestAction
|
2016-07-04 14:45:18 +02:00
|
|
|
{
|
2016-07-21 16:41:16 +02:00
|
|
|
/**
|
|
|
|
|
* @var TranslatorInterface
|
|
|
|
|
*/
|
|
|
|
|
private $translator;
|
2016-08-07 10:26:34 +02:00
|
|
|
/**
|
|
|
|
|
* @var ApiKeyService|ApiKeyServiceInterface
|
|
|
|
|
*/
|
|
|
|
|
private $apiKeyService;
|
2016-08-07 19:13:40 +02:00
|
|
|
/**
|
|
|
|
|
* @var JWTServiceInterface
|
|
|
|
|
*/
|
|
|
|
|
private $jwtService;
|
2016-07-04 14:45:18 +02:00
|
|
|
|
2016-08-07 19:13:40 +02:00
|
|
|
public function __construct(
|
|
|
|
|
ApiKeyServiceInterface $apiKeyService,
|
|
|
|
|
JWTServiceInterface $jwtService,
|
2017-03-25 10:04:48 +01:00
|
|
|
TranslatorInterface $translator,
|
|
|
|
|
LoggerInterface $logger = null
|
2016-08-07 19:13:40 +02:00
|
|
|
) {
|
2017-03-25 10:04:48 +01:00
|
|
|
parent::__construct($logger);
|
2016-07-21 16:41:16 +02:00
|
|
|
$this->translator = $translator;
|
2016-08-07 10:26:34 +02:00
|
|
|
$this->apiKeyService = $apiKeyService;
|
2016-08-07 19:13:40 +02:00
|
|
|
$this->jwtService = $jwtService;
|
2016-07-04 14:45:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param Request $request
|
2018-03-26 19:05:26 +02:00
|
|
|
* @return Response
|
2017-07-07 13:12:45 +02:00
|
|
|
* @throws \InvalidArgumentException
|
2016-07-04 14:45:18 +02:00
|
|
|
*/
|
2018-03-26 18:49:28 +02:00
|
|
|
public function handle(Request $request): Response
|
2016-07-04 14:45:18 +02:00
|
|
|
{
|
|
|
|
|
$authData = $request->getParsedBody();
|
2016-08-07 10:26:34 +02:00
|
|
|
if (! isset($authData['apiKey'])) {
|
2016-07-04 14:45:18 +02:00
|
|
|
return new JsonResponse([
|
|
|
|
|
'error' => RestUtils::INVALID_ARGUMENT_ERROR,
|
2016-08-06 13:18:27 +02:00
|
|
|
'message' => $this->translator->translate(
|
|
|
|
|
'You have to provide a valid API key under the "apiKey" param name.'
|
|
|
|
|
),
|
2017-03-25 10:04:48 +01:00
|
|
|
], self::STATUS_BAD_REQUEST);
|
2016-07-04 14:45:18 +02:00
|
|
|
}
|
|
|
|
|
|
2016-08-07 10:26:34 +02:00
|
|
|
// Authenticate using provided API key
|
2016-08-07 19:13:40 +02:00
|
|
|
$apiKey = $this->apiKeyService->getByKey($authData['apiKey']);
|
2018-03-26 18:49:28 +02:00
|
|
|
if ($apiKey === null || ! $apiKey->isValid()) {
|
2016-07-04 14:45:18 +02:00
|
|
|
return new JsonResponse([
|
2016-08-07 10:26:34 +02:00
|
|
|
'error' => RestUtils::INVALID_API_KEY_ERROR,
|
|
|
|
|
'message' => $this->translator->translate('Provided API key does not exist or is invalid.'),
|
2017-03-25 10:04:48 +01:00
|
|
|
], self::STATUS_UNAUTHORIZED);
|
2016-07-04 14:45:18 +02:00
|
|
|
}
|
2016-08-07 10:26:34 +02:00
|
|
|
|
2016-08-07 19:13:40 +02:00
|
|
|
// Generate a JSON Web Token that will be used for authorization in next requests
|
|
|
|
|
$token = $this->jwtService->create($apiKey);
|
|
|
|
|
return new JsonResponse(['token' => $token]);
|
2016-07-04 14:45:18 +02:00
|
|
|
}
|
|
|
|
|
}
|