mirror of
https://github.com/maelgangloff/domain-watchdog.git
synced 2025-12-29 16:15:04 +00:00
Merge branch 'feat/gandi-provider'
This commit is contained in:
@@ -3,10 +3,13 @@
|
||||
namespace App\Config\Connector;
|
||||
|
||||
use App\Entity\Domain;
|
||||
use Symfony\Contracts\HttpClient\HttpClientInterface;
|
||||
|
||||
interface ConnectorInterface
|
||||
{
|
||||
public static function verifyAuthData(array $authData): array;
|
||||
public function __construct(array $authData, HttpClientInterface $client);
|
||||
|
||||
public function orderDomain(Domain $domain, bool $dryRun): void;
|
||||
|
||||
public static function verifyAuthData(array $authData, HttpClientInterface $client);
|
||||
}
|
||||
|
||||
133
src/Config/Connector/GandiConnector.php
Normal file
133
src/Config/Connector/GandiConnector.php
Normal file
@@ -0,0 +1,133 @@
|
||||
<?php
|
||||
|
||||
namespace App\Config\Connector;
|
||||
|
||||
use App\Entity\Domain;
|
||||
use http\Exception\InvalidArgumentException;
|
||||
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\DecodingExceptionInterface;
|
||||
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
|
||||
use Symfony\Contracts\HttpClient\HttpClientInterface;
|
||||
|
||||
readonly class GandiConnector implements ConnectorInterface
|
||||
{
|
||||
private const BASE_URL = 'https://api.gandi.net/v5';
|
||||
|
||||
public function __construct(private array $authData, private HttpClientInterface $client)
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* Order a domain name with the Gandi API.
|
||||
*
|
||||
* @throws \Exception
|
||||
* @throws TransportExceptionInterface
|
||||
* @throws DecodingExceptionInterface
|
||||
*/
|
||||
public function orderDomain(Domain $domain, bool $dryRun = false): void
|
||||
{
|
||||
if (!$domain->getDeleted()) {
|
||||
throw new InvalidArgumentException('The domain name still appears in the WHOIS database');
|
||||
}
|
||||
|
||||
$ldhName = $domain->getLdhName();
|
||||
if (!$ldhName) {
|
||||
throw new InvalidArgumentException('Domain name cannot be null');
|
||||
}
|
||||
|
||||
$authData = self::verifyAuthData($this->authData, $this->client);
|
||||
|
||||
$user = $this->client->request('GET', '/v5/organization/user-info', (new HttpOptions())
|
||||
->setAuthBearer($authData['token'])
|
||||
->setHeader('Accept', 'application/json')
|
||||
->setBaseUri(self::BASE_URL)
|
||||
->toArray()
|
||||
)->toArray();
|
||||
|
||||
$httpOptions = (new HttpOptions())
|
||||
->setAuthBearer($authData['token'])
|
||||
->setHeader('Accept', 'application/json')
|
||||
->setBaseUri(self::BASE_URL)
|
||||
->setHeader('Dry-Run', $dryRun ? '1' : '0')
|
||||
->setJson([
|
||||
'fqdn' => $ldhName,
|
||||
'owner' => [
|
||||
'email' => $user['email'],
|
||||
'given' => $user['firstname'],
|
||||
'family' => $user['lastname'],
|
||||
'streetaddr' => $user['streetaddr'],
|
||||
'zip' => $user['zip'],
|
||||
'city' => $user['city'],
|
||||
'state' => $user['state'],
|
||||
'phone' => $user['phone'],
|
||||
'country' => $user['country'],
|
||||
'type' => 'individual',
|
||||
],
|
||||
'tld_period' => 'golive',
|
||||
]);
|
||||
|
||||
if (array_key_exists('sharingId', $authData)) {
|
||||
$httpOptions->setQuery([
|
||||
'sharing_id' => $authData['sharingId'],
|
||||
]);
|
||||
}
|
||||
|
||||
$res = $this->client->request('POST', '/domain/domains', $httpOptions->toArray());
|
||||
|
||||
if ((!$dryRun && Response::HTTP_ACCEPTED !== $res->getStatusCode())
|
||||
|| ($dryRun && Response::HTTP_OK !== $res->getStatusCode())) {
|
||||
throw new \Exception($res->toArray()['message']);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws TransportExceptionInterface
|
||||
*/
|
||||
public static function verifyAuthData(array $authData, HttpClientInterface $client): 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');
|
||||
}
|
||||
|
||||
$response = $client->request('GET', '/v5/organization/user-info', (new HttpOptions())
|
||||
->setAuthBearer($token)
|
||||
->setHeader('Accept', 'application/json')
|
||||
->setBaseUri(self::BASE_URL)
|
||||
->toArray()
|
||||
);
|
||||
|
||||
if (Response::HTTP_OK !== $response->getStatusCode()) {
|
||||
throw new BadRequestHttpException('The status of these credentials is not valid');
|
||||
}
|
||||
|
||||
$authDataReturned = [
|
||||
'token' => $token,
|
||||
'acceptConditions' => $acceptConditions,
|
||||
'ownerLegalAge' => $ownerLegalAge,
|
||||
'waiveRetractationPeriod' => $waiveRetractationPeriod,
|
||||
];
|
||||
|
||||
if (array_key_exists('sharingId', $authData)) {
|
||||
$authDataReturned['sharingId'] = $authData['sharingId'];
|
||||
}
|
||||
|
||||
return $authDataReturned;
|
||||
}
|
||||
}
|
||||
@@ -5,6 +5,8 @@ namespace App\Config\Connector;
|
||||
use App\Entity\Domain;
|
||||
use Ovh\Api;
|
||||
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
|
||||
use Symfony\Component\HttpKernel\Exception\HttpException;
|
||||
use Symfony\Contracts\HttpClient\HttpClientInterface;
|
||||
|
||||
readonly class OvhConnector implements ConnectorInterface
|
||||
{
|
||||
@@ -30,7 +32,7 @@ readonly class OvhConnector implements ConnectorInterface
|
||||
],
|
||||
];
|
||||
|
||||
public function __construct(private array $authData)
|
||||
public function __construct(private array $authData, private HttpClientInterface $client)
|
||||
{
|
||||
}
|
||||
|
||||
@@ -39,8 +41,8 @@ readonly class OvhConnector implements ConnectorInterface
|
||||
*
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function orderDomain(Domain $domain, bool $dryRun = false
|
||||
): void {
|
||||
public function orderDomain(Domain $domain, bool $dryRun = false): void
|
||||
{
|
||||
if (!$domain->getDeleted()) {
|
||||
throw new \Exception('The domain name still appears in the WHOIS database');
|
||||
}
|
||||
@@ -50,7 +52,7 @@ readonly class OvhConnector implements ConnectorInterface
|
||||
throw new \Exception('Domain name cannot be null');
|
||||
}
|
||||
|
||||
$authData = self::verifyAuthData($this->authData);
|
||||
$authData = self::verifyAuthData($this->authData, $this->client);
|
||||
|
||||
$acceptConditions = $authData['acceptConditions'];
|
||||
$ownerLegalAge = $authData['ownerLegalAge'];
|
||||
@@ -118,7 +120,7 @@ readonly class OvhConnector implements ConnectorInterface
|
||||
/**
|
||||
* @throws \Exception
|
||||
*/
|
||||
public static function verifyAuthData(array $authData): array
|
||||
public static function verifyAuthData(array $authData, HttpClientInterface $client): array
|
||||
{
|
||||
$appKey = $authData['appKey'];
|
||||
$appSecret = $authData['appSecret'];
|
||||
@@ -137,14 +139,16 @@ readonly class OvhConnector implements ConnectorInterface
|
||||
|| !is_string($apiEndpoint) || empty($apiEndpoint)
|
||||
|| !is_string($ovhSubsidiary) || empty($ovhSubsidiary)
|
||||
|| !is_string($pricingMode) || empty($pricingMode)
|
||||
|
||||
|| true !== $acceptConditions
|
||||
|| true !== $ownerLegalAge
|
||||
|| true !== $waiveRetractationPeriod
|
||||
) {
|
||||
throw new BadRequestHttpException('Bad authData schema');
|
||||
}
|
||||
|
||||
if (true !== $acceptConditions
|
||||
|| true !== $ownerLegalAge
|
||||
|| true !== $waiveRetractationPeriod) {
|
||||
throw new HttpException(451, 'The user has not given explicit consent');
|
||||
}
|
||||
|
||||
$conn = new Api(
|
||||
$appKey,
|
||||
$appSecret,
|
||||
|
||||
@@ -2,7 +2,19 @@
|
||||
|
||||
namespace App\Config;
|
||||
|
||||
use App\Config\Connector\GandiConnector;
|
||||
use App\Config\Connector\OvhConnector;
|
||||
|
||||
enum ConnectorProvider: string
|
||||
{
|
||||
case OVH = 'ovh';
|
||||
case GANDI = 'gandi';
|
||||
|
||||
public function getConnectorProvider(): string
|
||||
{
|
||||
return match ($this) {
|
||||
ConnectorProvider::OVH => OvhConnector::class,
|
||||
ConnectorProvider::GANDI => GandiConnector::class
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,8 +2,7 @@
|
||||
|
||||
namespace App\Controller;
|
||||
|
||||
use App\Config\Connector\OvhConnector;
|
||||
use App\Config\ConnectorProvider;
|
||||
use App\Config\Connector\ConnectorInterface;
|
||||
use App\Entity\Connector;
|
||||
use App\Entity\User;
|
||||
use Doctrine\Common\Collections\Collection;
|
||||
@@ -11,9 +10,10 @@ use Doctrine\ORM\EntityManagerInterface;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
|
||||
use Symfony\Component\Routing\Attribute\Route;
|
||||
use Symfony\Component\Serializer\SerializerInterface;
|
||||
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
|
||||
use Symfony\Contracts\HttpClient\HttpClientInterface;
|
||||
|
||||
class ConnectorController extends AbstractController
|
||||
{
|
||||
@@ -43,6 +43,7 @@ class ConnectorController extends AbstractController
|
||||
|
||||
/**
|
||||
* @throws \Exception
|
||||
* @throws TransportExceptionInterface
|
||||
*/
|
||||
#[Route(
|
||||
path: '/api/connectors',
|
||||
@@ -53,7 +54,7 @@ class ConnectorController extends AbstractController
|
||||
],
|
||||
methods: ['POST']
|
||||
)]
|
||||
public function createConnector(Request $request): Connector
|
||||
public function createConnector(Request $request, HttpClientInterface $client): Connector
|
||||
{
|
||||
$connector = $this->serializer->deserialize($request->getContent(), Connector::class, 'json', ['groups' => 'connector:create']);
|
||||
/** @var User $user */
|
||||
@@ -67,17 +68,21 @@ class ConnectorController extends AbstractController
|
||||
'provider' => $provider->value,
|
||||
]);
|
||||
|
||||
if (ConnectorProvider::OVH === $provider) {
|
||||
$authData = OvhConnector::verifyAuthData($connector->getAuthData());
|
||||
$connector->setAuthData($authData);
|
||||
|
||||
$this->logger->info('User {username} authentication data with the OVH provider has been validated.', [
|
||||
'username' => $user->getUserIdentifier(),
|
||||
]);
|
||||
} else {
|
||||
throw new BadRequestHttpException('Unknown provider');
|
||||
if (null === $provider) {
|
||||
throw new \Exception('Provider not found');
|
||||
}
|
||||
|
||||
/** @var ConnectorInterface $connectorProviderClass */
|
||||
$connectorProviderClass = $provider->getConnectorProvider();
|
||||
|
||||
$authData = $connectorProviderClass::verifyAuthData($connector->getAuthData(), $client);
|
||||
$connector->setAuthData($authData);
|
||||
|
||||
$this->logger->info('User {username} authentication data with the {provider} provider has been validated.', [
|
||||
'username' => $user->getUserIdentifier(),
|
||||
'provider' => $provider->value,
|
||||
]);
|
||||
|
||||
$this->logger->info('The new API connector requested by {username} has been successfully registered.', [
|
||||
'username' => $user->getUserIdentifier(),
|
||||
]);
|
||||
|
||||
@@ -2,8 +2,7 @@
|
||||
|
||||
namespace App\MessageHandler;
|
||||
|
||||
use App\Config\Connector\OvhConnector;
|
||||
use App\Config\ConnectorProvider;
|
||||
use App\Config\Connector\ConnectorInterface;
|
||||
use App\Config\TriggerAction;
|
||||
use App\Entity\Connector;
|
||||
use App\Entity\Domain;
|
||||
@@ -22,6 +21,7 @@ use Symfony\Component\Mailer\MailerInterface;
|
||||
use Symfony\Component\Messenger\Attribute\AsMessageHandler;
|
||||
use Symfony\Component\Mime\Address;
|
||||
use Symfony\Component\Mime\Email;
|
||||
use Symfony\Contracts\HttpClient\HttpClientInterface;
|
||||
|
||||
#[AsMessageHandler]
|
||||
final readonly class ProcessDomainTriggerHandler
|
||||
@@ -33,7 +33,8 @@ final readonly class ProcessDomainTriggerHandler
|
||||
private WatchListRepository $watchListRepository,
|
||||
private DomainRepository $domainRepository,
|
||||
private KernelInterface $kernel,
|
||||
private LoggerInterface $logger
|
||||
private LoggerInterface $logger,
|
||||
private HttpClientInterface $client
|
||||
) {
|
||||
}
|
||||
|
||||
@@ -57,15 +58,19 @@ final readonly class ProcessDomainTriggerHandler
|
||||
'provider' => $connector->getProvider()->value,
|
||||
]);
|
||||
try {
|
||||
if (ConnectorProvider::OVH === $connector->getProvider()) {
|
||||
$ovh = new OvhConnector($connector->getAuthData());
|
||||
$isDebug = $this->kernel->isDebug();
|
||||
|
||||
$ovh->orderDomain($domain, $isDebug);
|
||||
$this->sendEmailDomainOrdered($domain, $connector, $watchList->getUser());
|
||||
} else {
|
||||
throw new \Exception('Unknown provider');
|
||||
$provider = $connector->getProvider();
|
||||
if (null === $provider) {
|
||||
throw new \Exception('Provider not found');
|
||||
}
|
||||
|
||||
$connectorProviderClass = $provider->getConnectorProvider();
|
||||
|
||||
/** @var ConnectorInterface $connectorProvider */
|
||||
$connectorProvider = new $connectorProviderClass($connector->getAuthData(), $this->client);
|
||||
|
||||
$connectorProvider->orderDomain($domain, $this->kernel->isDebug());
|
||||
|
||||
$this->sendEmailDomainOrdered($domain, $connector, $watchList->getUser());
|
||||
} catch (\Throwable) {
|
||||
$this->logger->warning('Unable to complete purchase. An error message is sent to user {username}.', [
|
||||
'username' => $watchList->getUser()->getUserIdentifier(),
|
||||
|
||||
Reference in New Issue
Block a user