Created action to get mercure integration info

This commit is contained in:
Alejandro Celaya
2020-04-12 13:59:10 +02:00
parent 85440c1c5f
commit 2ffbf03cf8
8 changed files with 196 additions and 0 deletions

View File

@@ -9,6 +9,7 @@ use Laminas\ServiceManager\AbstractFactory\ConfigAbstractFactory;
use Laminas\ServiceManager\Factory\InvokableFactory;
use Mezzio\Router\Middleware\ImplicitOptionsMiddleware;
use Psr\Log\LoggerInterface;
use Shlinkio\Shlink\Common\Mercure\LcobucciJwtProvider;
use Shlinkio\Shlink\Core\Options\AppOptions;
use Shlinkio\Shlink\Core\Service;
use Shlinkio\Shlink\Rest\Service\ApiKeyService;
@@ -20,6 +21,7 @@ return [
ApiKeyService::class => ConfigAbstractFactory::class,
Action\HealthAction::class => ConfigAbstractFactory::class,
Action\MercureAction::class => ConfigAbstractFactory::class,
Action\ShortUrl\CreateShortUrlAction::class => ConfigAbstractFactory::class,
Action\ShortUrl\SingleStepCreateShortUrlAction::class => ConfigAbstractFactory::class,
Action\ShortUrl\EditShortUrlAction::class => ConfigAbstractFactory::class,
@@ -46,6 +48,7 @@ return [
ApiKeyService::class => ['em'],
Action\HealthAction::class => [Connection::class, AppOptions::class, 'Logger_Shlink'],
Action\MercureAction::class => [LcobucciJwtProvider::class, 'config.mercure', 'Logger_Shlink'],
Action\ShortUrl\CreateShortUrlAction::class => [
Service\UrlShortener::class,
'config.url_shortener.domain',

View File

@@ -33,6 +33,8 @@ return [
Action\Tag\DeleteTagsAction::getRouteDef(),
Action\Tag\CreateTagsAction::getRouteDef(),
Action\Tag\UpdateTagAction::getRouteDef(),
Action\MercureAction::getRouteDef(),
],
];

View File

@@ -0,0 +1,56 @@
<?php
declare(strict_types=1);
namespace Shlinkio\Shlink\Rest\Action;
use Cake\Chronos\Chronos;
use Laminas\Diactoros\Response\JsonResponse;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Log\LoggerInterface;
use Shlinkio\Shlink\Common\Mercure\JwtProviderInterface;
use Shlinkio\Shlink\Rest\Exception\MercureException;
use Throwable;
class MercureAction extends AbstractRestAction
{
protected const ROUTE_PATH = '/mercure-info';
protected const ROUTE_ALLOWED_METHODS = [self::METHOD_GET];
private JwtProviderInterface $jwtProvider;
private array $mercureConfig;
public function __construct(
JwtProviderInterface $jwtProvider,
array $mercureConfig,
?LoggerInterface $logger = null
) {
parent::__construct($logger);
$this->jwtProvider = $jwtProvider;
$this->mercureConfig = $mercureConfig;
}
public function handle(ServerRequestInterface $request): ResponseInterface
{
$hubUrl = $this->mercureConfig['public_hub_url'] ?? null;
if ($hubUrl === null) {
throw MercureException::mercureNotConfigured();
}
$days = $this->mercureConfig['jwt_days_duration'] ?? 3;
$expiresAt = Chronos::now()->addDays($days);
try {
$jwt = $this->jwtProvider->buildSubscriptionToken($expiresAt);
} catch (Throwable $e) {
throw MercureException::mercureNotConfigured($e);
}
return new JsonResponse([
'mercureHubUrl' => $hubUrl,
'token' => $jwt,
'jwtExpiration' => $expiresAt->toAtomString(),
]);
}
}

View File

@@ -0,0 +1,30 @@
<?php
declare(strict_types=1);
namespace Shlinkio\Shlink\Rest\Exception;
use Fig\Http\Message\StatusCodeInterface;
use Mezzio\ProblemDetails\Exception\CommonProblemDetailsExceptionTrait;
use Mezzio\ProblemDetails\Exception\ProblemDetailsExceptionInterface;
use Throwable;
class MercureException extends RuntimeException implements ProblemDetailsExceptionInterface
{
use CommonProblemDetailsExceptionTrait;
private const TITLE = 'Mercure integration not configured';
private const TYPE = 'MERCURE_NOT_CONFIGURED';
public static function mercureNotConfigured(?Throwable $prev = null): self
{
$e = new self('This Shlink instance is not integrated with a mercure hub.', 1, $prev);
$e->detail = $e->getMessage();
$e->title = self::TITLE;
$e->type = self::TYPE;
$e->status = StatusCodeInterface::STATUS_NOT_IMPLEMENTED;
return $e;
}
}