Skip to content

Commit

Permalink
refactor: consent checks are performed in AbstractProvider and not in…
Browse files Browse the repository at this point in the history
… child classes
  • Loading branch information
maelgangloff committed Oct 31, 2024
1 parent 5be9024 commit c7a50ee
Show file tree
Hide file tree
Showing 29 changed files with 928 additions and 889 deletions.
1,648 changes: 846 additions & 802 deletions composer.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/Config/WebhookScheme.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public function getChatTransportFactory(): string
WebhookScheme::ZULIP => ZulipTransportFactory::class,
WebhookScheme::PUSHOVER => PushoverTransportFactory::class,
WebhookScheme::NTFY => NtfyTransportFactory::class,
WebhookScheme::ENGAGESPOT => EngagespotTransportFactory::class
WebhookScheme::ENGAGESPOT => EngagespotTransportFactory::class,
};
}
}
2 changes: 1 addition & 1 deletion src/Controller/ConnectorController.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public function __construct(
private readonly EntityManagerInterface $em,
private readonly LoggerInterface $logger,
#[Autowire(service: 'service_container')]
private ContainerInterface $locator
private ContainerInterface $locator,
) {
}

Expand Down
2 changes: 1 addition & 1 deletion src/Controller/DomainRefreshController.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public function __construct(private readonly DomainRepository $domainRepository,
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,
) {
}

Expand Down
2 changes: 1 addition & 1 deletion src/Controller/RegistrationController.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public function __construct(
private readonly EntityManagerInterface $em,
private readonly SerializerInterface $serializer,
private readonly LoggerInterface $logger,
private readonly KernelInterface $kernel
private readonly KernelInterface $kernel,
) {
}

Expand Down
2 changes: 1 addition & 1 deletion src/Controller/StatisticsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public function __construct(
private readonly CacheItemPoolInterface $pool,
private readonly DomainRepository $domainRepository,
private readonly WatchListRepository $watchListRepository,
private readonly KernelInterface $kernel
private readonly KernelInterface $kernel,
) {
}

Expand Down
2 changes: 1 addition & 1 deletion src/Controller/WatchListController.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public function __construct(
private readonly LoggerInterface $logger,
private readonly ChatNotificationService $chatNotificationService,
#[Autowire(service: 'service_container')]
private ContainerInterface $locator
private ContainerInterface $locator,
) {
}

Expand Down
2 changes: 1 addition & 1 deletion src/Message/OrderDomain.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ final class OrderDomain
public function __construct(
public string $watchListToken,
public string $ldhName,
public \DateTimeImmutable $updatedAt
public \DateTimeImmutable $updatedAt,
) {
}
}
2 changes: 1 addition & 1 deletion src/Message/SendDomainEventNotif.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ final class SendDomainEventNotif
public function __construct(
public string $watchListToken,
public string $ldhName,
public \DateTimeImmutable $updatedAt
public \DateTimeImmutable $updatedAt,
) {
}
}
2 changes: 1 addition & 1 deletion src/MessageHandler/OrderDomainHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public function __construct(
private StatService $statService,
private ChatNotificationService $chatNotificationService,
#[Autowire(service: 'service_container')]
private ContainerInterface $locator
private ContainerInterface $locator,
) {
$this->sender = new Address($mailerSenderEmail, $mailerSenderName);
}
Expand Down
2 changes: 1 addition & 1 deletion src/MessageHandler/ProcessWatchListsTriggerHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
{
public function __construct(
private WatchListRepository $watchListRepository,
private MessageBusInterface $bus
private MessageBusInterface $bus,
) {
}

Expand Down
2 changes: 1 addition & 1 deletion src/MessageHandler/SendDomainEventNotifHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public function __construct(
private StatService $statService,
private DomainRepository $domainRepository,
private WatchListRepository $watchListRepository,
private ChatNotificationService $chatNotificationService
private ChatNotificationService $chatNotificationService,
) {
$this->sender = new Address($mailerSenderEmail, $mailerSenderName);
}
Expand Down
2 changes: 1 addition & 1 deletion src/MessageHandler/UpdateDomainsFromWatchlistHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public function __construct(
string $mailerSenderName,
private MessageBusInterface $bus,
private WatchListRepository $watchListRepository,
private LoggerInterface $logger
private LoggerInterface $logger,
) {
$this->sender = new Address($mailerSenderEmail, $mailerSenderName);
}
Expand Down
2 changes: 1 addition & 1 deletion src/MessageHandler/UpdateRdapServersHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
{
public function __construct(
private RDAPService $RDAPService,
private ParameterBagInterface $bag
private ParameterBagInterface $bag,
) {
}

Expand Down
2 changes: 1 addition & 1 deletion src/Notifier/DomainOrderErrorNotification.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
2 changes: 1 addition & 1 deletion src/Notifier/DomainOrderNotification.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
2 changes: 1 addition & 1 deletion src/Notifier/DomainUpdateErrorNotification.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
2 changes: 1 addition & 1 deletion src/Notifier/DomainUpdateNotification.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
2 changes: 1 addition & 1 deletion src/Security/EmailVerifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
public function __construct(
private VerifyEmailHelperInterface $verifyEmailHelper,
private MailerInterface $mailer,
private EntityManagerInterface $entityManager
private EntityManagerInterface $entityManager,
) {
}

Expand Down
2 changes: 1 addition & 1 deletion src/Security/JWTAuthenticator.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class JWTAuthenticator implements AuthenticationSuccessHandlerInterface
public function __construct(
protected JWTTokenManagerInterface $jwtManager,
protected EventDispatcherInterface $dispatcher,
protected KernelInterface $kernel
protected KernelInterface $kernel,
) {
}

Expand Down
2 changes: 1 addition & 1 deletion src/Security/OAuthAuthenticator.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public function __construct(
private readonly UserRepository $userRepository,
private readonly EntityManagerInterface $em,
private readonly RouterInterface $router,
private readonly JWTTokenManagerInterface $JWTManager
private readonly JWTTokenManagerInterface $JWTManager,
) {
}

Expand Down
2 changes: 1 addition & 1 deletion src/Service/ChatNotificationService.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
readonly class ChatNotificationService
{
public function __construct(
private LoggerInterface $logger
private LoggerInterface $logger,
) {
}

Expand Down
51 changes: 49 additions & 2 deletions src/Service/Connector/AbstractProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -13,21 +16,65 @@
* $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 specific authentication data
*/
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
*/
abstract public function verifyAuthData(array $authData): array;
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
Expand Down
21 changes: 3 additions & 18 deletions src/Service/Connector/AutodnsProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,18 @@
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;
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)
Expand Down Expand Up @@ -166,15 +167,11 @@ public function registerZone(Domain $domain, bool $dryRun = false): void
}
}

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;
}
Expand All @@ -185,22 +182,10 @@ public function verifyAuthData(array $authData): array
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'],
];
}
Expand Down
17 changes: 3 additions & 14 deletions src/Service/Connector/GandiProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
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;
Expand All @@ -16,6 +17,7 @@
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';
Expand Down Expand Up @@ -82,31 +84,18 @@ public function orderDomain(Domain $domain, bool $dryRun = false): void
}
}

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)) {
Expand Down
Loading

0 comments on commit c7a50ee

Please sign in to comment.