mirror of
https://github.com/shlinkio/shlink.git
synced 2025-02-25 18:45:27 -06:00
Implemented ApiKeyHeaderPlugin
This commit is contained in:
@@ -6,17 +6,43 @@ namespace Shlinkio\Shlink\Rest\Authentication\Plugin;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
use Shlinkio\Shlink\Rest\Exception\VerifyAuthenticationException;
|
||||
use Shlinkio\Shlink\Rest\Service\ApiKeyServiceInterface;
|
||||
use Shlinkio\Shlink\Rest\Util\RestUtils;
|
||||
use Zend\I18n\Translator\TranslatorInterface;
|
||||
|
||||
class ApiKeyHeaderPlugin implements AuthenticationPluginInterface
|
||||
{
|
||||
public const HEADER_NAME = 'X-Api-Key';
|
||||
|
||||
/**
|
||||
* @var ApiKeyServiceInterface
|
||||
*/
|
||||
private $apiKeyService;
|
||||
/**
|
||||
* @var TranslatorInterface
|
||||
*/
|
||||
private $translator;
|
||||
|
||||
public function __construct(ApiKeyServiceInterface $apiKeyService, TranslatorInterface $translator)
|
||||
{
|
||||
$this->apiKeyService = $apiKeyService;
|
||||
$this->translator = $translator;
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws VerifyAuthenticationException
|
||||
*/
|
||||
public function verify(ServerRequestInterface $request): void
|
||||
{
|
||||
// TODO: Implement check() method.
|
||||
$apiKey = $request->getHeaderLine(self::HEADER_NAME);
|
||||
if ($this->apiKeyService->check($apiKey)) {
|
||||
return;
|
||||
}
|
||||
|
||||
throw VerifyAuthenticationException::withError(
|
||||
RestUtils::INVALID_API_KEY_ERROR,
|
||||
$this->translator->translate('Provided API key does not exist or is invalid.')
|
||||
);
|
||||
}
|
||||
|
||||
public function update(ServerRequestInterface $request, ResponseInterface $response): ResponseInterface
|
||||
|
||||
Reference in New Issue
Block a user