refactor: consent checks are performed in AbstractProvider and not in child classes

This commit is contained in:
Maël Gangloff 2024-11-01 00:46:25 +01:00
parent 5be90247f4
commit c7a50eed65
No known key found for this signature in database
GPG Key ID: 11FDC81C24A7F629
29 changed files with 927 additions and 888 deletions

1644
composer.lock generated

File diff suppressed because it is too large Load Diff

View File

@ -41,7 +41,7 @@ enum WebhookScheme: string
WebhookScheme::ZULIP => ZulipTransportFactory::class,
WebhookScheme::PUSHOVER => PushoverTransportFactory::class,
WebhookScheme::NTFY => NtfyTransportFactory::class,
WebhookScheme::ENGAGESPOT => EngagespotTransportFactory::class
WebhookScheme::ENGAGESPOT => EngagespotTransportFactory::class,
};
}
}

View File

@ -24,7 +24,7 @@ class ConnectorController extends AbstractController
private readonly EntityManagerInterface $em,
private readonly LoggerInterface $logger,
#[Autowire(service: 'service_container')]
private ContainerInterface $locator
private ContainerInterface $locator,
) {
}

View File

@ -25,7 +25,7 @@ class DomainRefreshController extends AbstractController
private readonly RDAPService $RDAPService,
private readonly RateLimiterFactory $rdapRequestsLimiter,
private readonly MessageBusInterface $bus,
private readonly LoggerInterface $logger, private readonly KernelInterface $kernel
private readonly LoggerInterface $logger, private readonly KernelInterface $kernel,
) {
}

View File

@ -32,7 +32,7 @@ class RegistrationController extends AbstractController
private readonly EntityManagerInterface $em,
private readonly SerializerInterface $serializer,
private readonly LoggerInterface $logger,
private readonly KernelInterface $kernel
private readonly KernelInterface $kernel,
) {
}

View File

@ -16,7 +16,7 @@ class StatisticsController extends AbstractController
private readonly CacheItemPoolInterface $pool,
private readonly DomainRepository $domainRepository,
private readonly WatchListRepository $watchListRepository,
private readonly KernelInterface $kernel
private readonly KernelInterface $kernel,
) {
}

View File

@ -51,7 +51,7 @@ class WatchListController extends AbstractController
private readonly LoggerInterface $logger,
private readonly ChatNotificationService $chatNotificationService,
#[Autowire(service: 'service_container')]
private ContainerInterface $locator
private ContainerInterface $locator,
) {
}

View File

@ -7,7 +7,7 @@ final class OrderDomain
public function __construct(
public string $watchListToken,
public string $ldhName,
public \DateTimeImmutable $updatedAt
public \DateTimeImmutable $updatedAt,
) {
}
}

View File

@ -7,7 +7,7 @@ final class SendDomainEventNotif
public function __construct(
public string $watchListToken,
public string $ldhName,
public \DateTimeImmutable $updatedAt
public \DateTimeImmutable $updatedAt,
) {
}
}

View File

@ -38,7 +38,7 @@ final readonly class OrderDomainHandler
private StatService $statService,
private ChatNotificationService $chatNotificationService,
#[Autowire(service: 'service_container')]
private ContainerInterface $locator
private ContainerInterface $locator,
) {
$this->sender = new Address($mailerSenderEmail, $mailerSenderName);
}

View File

@ -16,7 +16,7 @@ final readonly class ProcessWatchListsTriggerHandler
{
public function __construct(
private WatchListRepository $watchListRepository,
private MessageBusInterface $bus
private MessageBusInterface $bus,
) {
}

View File

@ -34,7 +34,7 @@ final readonly class SendDomainEventNotifHandler
private StatService $statService,
private DomainRepository $domainRepository,
private WatchListRepository $watchListRepository,
private ChatNotificationService $chatNotificationService
private ChatNotificationService $chatNotificationService,
) {
$this->sender = new Address($mailerSenderEmail, $mailerSenderName);
}

View File

@ -36,7 +36,7 @@ final readonly class UpdateDomainsFromWatchlistHandler
string $mailerSenderName,
private MessageBusInterface $bus,
private WatchListRepository $watchListRepository,
private LoggerInterface $logger
private LoggerInterface $logger,
) {
$this->sender = new Address($mailerSenderEmail, $mailerSenderName);
}

View File

@ -17,7 +17,7 @@ final readonly class UpdateRdapServersHandler
{
public function __construct(
private RDAPService $RDAPService,
private ParameterBagInterface $bag
private ParameterBagInterface $bag,
) {
}

View File

@ -17,7 +17,7 @@ class DomainOrderErrorNotification extends DomainWatchdogNotification
{
public function __construct(
private readonly Address $sender,
private readonly Domain $domain
private readonly Domain $domain,
) {
parent::__construct();
}

View File

@ -19,7 +19,7 @@ class DomainOrderNotification extends DomainWatchdogNotification
public function __construct(
private readonly Address $sender,
private readonly Domain $domain,
private readonly Connector $connector
private readonly Connector $connector,
) {
parent::__construct();
}

View File

@ -16,7 +16,7 @@ class DomainUpdateErrorNotification extends DomainWatchdogNotification
{
public function __construct(
private readonly Address $sender,
private readonly Domain $domain
private readonly Domain $domain,
) {
parent::__construct();
}

View File

@ -17,7 +17,7 @@ class DomainUpdateNotification extends DomainWatchdogNotification
{
public function __construct(
private readonly Address $sender,
private readonly DomainEvent $domainEvent
private readonly DomainEvent $domainEvent,
) {
parent::__construct();
}

View File

@ -15,7 +15,7 @@ readonly class EmailVerifier
public function __construct(
private VerifyEmailHelperInterface $verifyEmailHelper,
private MailerInterface $mailer,
private EntityManagerInterface $entityManager
private EntityManagerInterface $entityManager,
) {
}

View File

@ -22,7 +22,7 @@ class JWTAuthenticator implements AuthenticationSuccessHandlerInterface
public function __construct(
protected JWTTokenManagerInterface $jwtManager,
protected EventDispatcherInterface $dispatcher,
protected KernelInterface $kernel
protected KernelInterface $kernel,
) {
}

View File

@ -27,7 +27,7 @@ class OAuthAuthenticator extends OAuth2Authenticator implements AuthenticationEn
private readonly UserRepository $userRepository,
private readonly EntityManagerInterface $em,
private readonly RouterInterface $router,
private readonly JWTTokenManagerInterface $JWTManager
private readonly JWTTokenManagerInterface $JWTManager,
) {
}

View File

@ -15,7 +15,7 @@ use Symfony\Component\Notifier\Transport\Dsn;
readonly class ChatNotificationService
{
public function __construct(
private LoggerInterface $logger
private LoggerInterface $logger,
) {
}

View File

@ -3,8 +3,11 @@
namespace App\Service\Connector;
use App\Entity\Domain;
use Exception;
use Psr\Cache\CacheItemInterface;
use Psr\Cache\CacheItemPoolInterface;
use Symfony\Component\DependencyInjection\Attribute\Autoconfigure;
use Symfony\Component\HttpKernel\Exception\HttpException;
/**
* The typical flow of a provider will go as follows:
@ -13,21 +16,65 @@ use Psr\Cache\CacheItemPoolInterface;
* $provider->authenticate($authData);
* $provider->orderDomain($domain, $dryRun);
*/
#[Autoconfigure(public: true)]
abstract class AbstractProvider
{
protected array $authData;
public function __construct(
protected CacheItemPoolInterface $cacheItemPool
protected CacheItemPoolInterface $cacheItemPool,
) {
}
/**
* Perform a static check of the connector data.
* To be valid, the data fields must match the Provider and the conditions must be accepted.
* User consent is checked here.
*
* @param array $authData raw authentication data as supplied by the user
*
* @return array a cleaned up version of the authentication data
*
* @throws HttpException when the user does not accept the necessary conditions
*/
public function verifyAuthData(array $authData): array
{
return [
...$this->verifySpecificAuthData($this->verifyLegalAuthData($authData)),
'acceptConditions' => $authData['acceptConditions'],
'ownerLegalAge' => $authData['ownerLegalAge'],
'waiveRetractationPeriod' => $authData['waiveRetractationPeriod'],
];
}
/**
* @param array $authData raw authentication data as supplied by the user
*
* @return array a cleaned up version of the authentication data
* @return array specific authentication data
*/
abstract public function verifyAuthData(array $authData): array;
abstract protected function verifySpecificAuthData(array $authData): array;
/**
* @param array $authData raw authentication data as supplied by the user
*
* @return array raw authentication data as supplied by the user
*
* @throws HttpException when the user does not accept the necessary conditions
*/
private function verifyLegalAuthData(array $authData): array
{
$acceptConditions = $authData['acceptConditions'];
$ownerLegalAge = $authData['ownerLegalAge'];
$waiveRetractationPeriod = $authData['waiveRetractationPeriod'];
if (true !== $acceptConditions
|| true !== $ownerLegalAge
|| true !== $waiveRetractationPeriod) {
throw new HttpException(451, 'The user has not given explicit consent');
}
return $authData;
}
/**
* @throws \Exception when the registrar denies the authentication

View File

@ -6,10 +6,10 @@ use App\Entity\Domain;
use Psr\Cache\CacheItemInterface;
use Psr\Cache\CacheItemPoolInterface;
use Psr\Cache\InvalidArgumentException;
use Symfony\Component\DependencyInjection\Attribute\Autoconfigure;
use Symfony\Component\HttpClient\HttpOptions;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
use Symfony\Component\HttpKernel\Exception\HttpException;
use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\DecodingExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface;
@ -17,6 +17,7 @@ use Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
use Symfony\Contracts\HttpClient\HttpClientInterface;
#[Autoconfigure(public: true)]
class AutodnsProvider extends AbstractProvider
{
public function __construct(CacheItemPoolInterface $cacheItemPool, private readonly HttpClientInterface $client)
@ -166,15 +167,11 @@ class AutodnsProvider extends AbstractProvider
}
}
public function verifyAuthData(array $authData): array
public function verifySpecificAuthData(array $authData): array
{
$username = $authData['username'];
$password = $authData['password'];
$acceptConditions = $authData['acceptConditions'];
$ownerLegalAge = $authData['ownerLegalAge'];
$waiveRetractationPeriod = $authData['waiveRetractationPeriod'];
if (empty($authData['context'])) {
$authData['context'] = 4;
}
@ -185,22 +182,10 @@ class AutodnsProvider extends AbstractProvider
throw new BadRequestHttpException('Bad authData schema');
}
if (
true !== $acceptConditions
|| true !== $authData['ownerConfirm']
|| true !== $ownerLegalAge
|| true !== $waiveRetractationPeriod
) {
throw new HttpException(451, 'The user has not given explicit consent');
}
return [
'username' => $authData['username'],
'password' => $authData['password'],
'acceptConditions' => $authData['acceptConditions'],
'ownerLegalAge' => $authData['ownerLegalAge'],
'ownerConfirm' => $authData['ownerConfirm'],
'waiveRetractationPeriod' => $authData['waiveRetractationPeriod'],
'context' => $authData['context'],
];
}

View File

@ -5,6 +5,7 @@ namespace App\Service\Connector;
use App\Entity\Domain;
use Psr\Cache\CacheItemInterface;
use Psr\Cache\CacheItemPoolInterface;
use Symfony\Component\DependencyInjection\Attribute\Autoconfigure;
use Symfony\Component\HttpClient\HttpOptions;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
@ -16,6 +17,7 @@ use Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
use Symfony\Contracts\HttpClient\HttpClientInterface;
#[Autoconfigure(public: true)]
class GandiProvider extends AbstractProvider
{
private const BASE_URL = 'https://api.gandi.net';
@ -82,31 +84,18 @@ class GandiProvider extends AbstractProvider
}
}
public function verifyAuthData(array $authData): array
public function verifySpecificAuthData(array $authData): array
{
$token = $authData['token'];
$acceptConditions = $authData['acceptConditions'];
$ownerLegalAge = $authData['ownerLegalAge'];
$waiveRetractationPeriod = $authData['waiveRetractationPeriod'];
if (!is_string($token) || empty($token)
|| (array_key_exists('sharingId', $authData) && !is_string($authData['sharingId']))
) {
throw new BadRequestHttpException('Bad authData schema');
}
if (true !== $acceptConditions
|| true !== $ownerLegalAge
|| true !== $waiveRetractationPeriod) {
throw new HttpException(451, 'The user has not given explicit consent');
}
$authDataReturned = [
'token' => $token,
'acceptConditions' => $acceptConditions,
'ownerLegalAge' => $ownerLegalAge,
'waiveRetractationPeriod' => $waiveRetractationPeriod,
];
if (array_key_exists('sharingId', $authData)) {

View File

@ -8,7 +8,6 @@ use Psr\Cache\CacheItemPoolInterface;
use Psr\Cache\InvalidArgumentException;
use Symfony\Component\DependencyInjection\Attribute\Autoconfigure;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
use Symfony\Component\HttpKernel\Exception\HttpException;
use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface;
@ -99,33 +98,20 @@ class NamecheapProvider extends AbstractProvider
return $data->CommandResponse;
}
public function verifyAuthData(array $authData): array
public function verifySpecificAuthData(array $authData): array
{
$apiUser = $authData['ApiUser'];
$apiKey = $authData['ApiKey'];
$acceptConditions = $authData['acceptConditions'];
$ownerLegalAge = $authData['ownerLegalAge'];
$waiveRetractationPeriod = $authData['waiveRetractationPeriod'];
if (!is_string($apiUser) || empty($apiUser)
|| !is_string($apiKey) || empty($apiKey)
) {
throw new BadRequestHttpException('Bad authData schema');
}
if (true !== $acceptConditions
|| true !== $ownerLegalAge
|| true !== $waiveRetractationPeriod) {
throw new HttpException(451, 'The user has not given explicit consent');
}
return [
'ApiUser' => $authData['ApiUser'],
'ApiKey' => $authData['ApiKey'],
'acceptConditions' => $authData['acceptConditions'],
'ownerLegalAge' => $authData['ownerLegalAge'],
'waiveRetractationPeriod' => $authData['waiveRetractationPeriod'],
];
}

View File

@ -9,9 +9,10 @@ use Ovh\Exceptions\InvalidParameterException;
use Psr\Cache\CacheItemInterface;
use Psr\Cache\CacheItemPoolInterface;
use Psr\Cache\InvalidArgumentException;
use Symfony\Component\DependencyInjection\Attribute\Autoconfigure;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
use Symfony\Component\HttpKernel\Exception\HttpException;
#[Autoconfigure(public: true)]
class OvhProvider extends AbstractProvider
{
public const REQUIRED_ROUTES = [
@ -130,7 +131,7 @@ class OvhProvider extends AbstractProvider
/**
* @throws \Exception
*/
public function verifyAuthData(array $authData): array
public function verifySpecificAuthData(array $authData): array
{
$appKey = $authData['appKey'];
$appSecret = $authData['appSecret'];
@ -139,10 +140,6 @@ class OvhProvider extends AbstractProvider
$ovhSubsidiary = $authData['ovhSubsidiary'];
$pricingMode = $authData['pricingMode'];
$acceptConditions = $authData['acceptConditions'];
$ownerLegalAge = $authData['ownerLegalAge'];
$waiveRetractationPeriod = $authData['waiveRetractationPeriod'];
if (!is_string($appKey) || empty($appKey)
|| !is_string($appSecret) || empty($appSecret)
|| !is_string($consumerKey) || empty($consumerKey)
@ -153,12 +150,6 @@ class OvhProvider extends AbstractProvider
throw new BadRequestHttpException('Bad authData schema');
}
if (true !== $acceptConditions
|| true !== $ownerLegalAge
|| true !== $waiveRetractationPeriod) {
throw new HttpException(451, 'The user has not given explicit consent');
}
return [
'appKey' => $appKey,
'appSecret' => $appSecret,
@ -166,9 +157,6 @@ class OvhProvider extends AbstractProvider
'consumerKey' => $consumerKey,
'ovhSubsidiary' => $ovhSubsidiary,
'pricingMode' => $pricingMode,
'acceptConditions' => $acceptConditions,
'ownerLegalAge' => $ownerLegalAge,
'waiveRetractationPeriod' => $waiveRetractationPeriod,
];
}

View File

@ -98,7 +98,7 @@ readonly class RDAPService
private TldRepository $tldRepository,
private EntityManagerInterface $em,
private LoggerInterface $logger,
private StatService $statService
private StatService $statService,
) {
}

View File

@ -7,7 +7,7 @@ use Psr\Cache\CacheItemPoolInterface;
readonly class StatService
{
public function __construct(
private CacheItemPoolInterface $pool
private CacheItemPoolInterface $pool,
) {
}