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
29 changed files with 927 additions and 888 deletions

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,
) {
}