2016-07-04 14:45:18 +02:00
|
|
|
<?php
|
2016-07-19 17:07:59 +02:00
|
|
|
namespace Shlinkio\Shlink\Rest\Action;
|
2016-07-04 14:45:18 +02:00
|
|
|
|
|
|
|
|
use Acelaya\ZsmAnnotatedServices\Annotation\Inject;
|
2016-08-07 14:44:33 +02:00
|
|
|
use Firebase\JWT\JWT;
|
2016-07-04 14:45:18 +02:00
|
|
|
use Psr\Http\Message\ResponseInterface as Response;
|
|
|
|
|
use Psr\Http\Message\ServerRequestInterface as Request;
|
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-07-04 14:45:18 +02:00
|
|
|
|
|
|
|
|
/**
|
2016-07-27 20:22:50 +02:00
|
|
|
* AuthenticateAction constructor.
|
2016-08-07 10:26:34 +02:00
|
|
|
* @param ApiKeyServiceInterface|ApiKeyService $apiKeyService
|
2016-07-21 16:41:16 +02:00
|
|
|
* @param TranslatorInterface $translator
|
2016-07-04 14:45:18 +02:00
|
|
|
*
|
2016-08-07 10:26:34 +02:00
|
|
|
* @Inject({ApiKeyService::class, "translator"})
|
2016-07-04 14:45:18 +02:00
|
|
|
*/
|
2016-08-07 10:26:34 +02:00
|
|
|
public function __construct(ApiKeyServiceInterface $apiKeyService, TranslatorInterface $translator)
|
2016-07-04 14:45:18 +02:00
|
|
|
{
|
2016-07-21 16:41:16 +02:00
|
|
|
$this->translator = $translator;
|
2016-08-07 10:26:34 +02:00
|
|
|
$this->apiKeyService = $apiKeyService;
|
2016-07-04 14:45:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param Request $request
|
|
|
|
|
* @param Response $response
|
2016-07-05 19:19:23 +02:00
|
|
|
* @param callable|null $out
|
2016-07-04 14:45:18 +02:00
|
|
|
* @return null|Response
|
|
|
|
|
*/
|
2016-07-05 19:19:23 +02:00
|
|
|
public function dispatch(Request $request, Response $response, callable $out = null)
|
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.'
|
|
|
|
|
),
|
2016-07-04 14:45:18 +02:00
|
|
|
], 400);
|
|
|
|
|
}
|
|
|
|
|
|
2016-08-07 10:26:34 +02:00
|
|
|
// Authenticate using provided API key
|
|
|
|
|
if (! $this->apiKeyService->check($authData['apiKey'])) {
|
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.'),
|
2016-07-04 14:45:18 +02:00
|
|
|
], 401);
|
|
|
|
|
}
|
2016-08-07 10:26:34 +02:00
|
|
|
|
|
|
|
|
// TODO Generate a JSON Web Token that will be used for authorization in next requests
|
|
|
|
|
|
|
|
|
|
return new JsonResponse(['token' => '']);
|
2016-07-04 14:45:18 +02:00
|
|
|
}
|
|
|
|
|
}
|