Files
domain-watchdog/src/Config/Provider/GandiProvider.php

161 lines
5.6 KiB
PHP
Raw Normal View History

2024-08-06 03:38:00 +02:00
<?php
2024-08-23 02:35:09 +02:00
namespace App\Config\Provider;
2024-08-06 03:38:00 +02:00
use App\Entity\Domain;
2024-08-23 02:35:09 +02:00
use Psr\Cache\CacheItemInterface;
2024-08-06 03:38:00 +02:00
use Symfony\Component\HttpClient\HttpOptions;
use Symfony\Component\HttpFoundation\Response;
2024-08-06 21:52:35 +02:00
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
use Symfony\Component\HttpKernel\Exception\HttpException;
use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface;
2024-08-06 03:38:00 +02:00
use Symfony\Contracts\HttpClient\Exception\DecodingExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface;
2024-08-06 03:38:00 +02:00
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
use Symfony\Contracts\HttpClient\HttpClientInterface;
2024-08-23 02:35:09 +02:00
class GandiProvider extends AbstractProvider
2024-08-06 03:38:00 +02:00
{
private const BASE_URL = 'https://api.gandi.net';
2024-08-06 03:38:00 +02:00
/**
* 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()) {
2024-08-23 03:02:46 +02:00
throw new \Exception('The domain name still appears in the WHOIS database');
2024-08-06 03:38:00 +02:00
}
$ldhName = $domain->getLdhName();
if (!$ldhName) {
2024-08-23 03:02:46 +02:00
throw new \Exception('Domain name cannot be null');
2024-08-06 03:38:00 +02:00
}
$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())) {
2024-08-07 16:21:41 +02:00
throw new \HttpException($res->toArray()['message']);
2024-08-06 03:38:00 +02:00
}
}
/**
* @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']))
) {
2024-08-06 21:52:35 +02:00
throw new BadRequestHttpException('Bad authData schema');
2024-08-06 03:38:00 +02:00
}
if (true !== $acceptConditions
|| true !== $ownerLegalAge
|| true !== $waiveRetractationPeriod) {
2024-08-07 01:10:56 +02:00
throw new HttpException(451, 'The user has not given explicit consent');
}
2024-08-06 03:38:00 +02:00
$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()) {
2024-08-06 21:52:35 +02:00
throw new BadRequestHttpException('The status of these credentials is not valid');
2024-08-06 03:38:00 +02:00
}
$authDataReturned = [
'token' => $token,
'acceptConditions' => $acceptConditions,
'ownerLegalAge' => $ownerLegalAge,
'waiveRetractationPeriod' => $waiveRetractationPeriod,
];
if (array_key_exists('sharingId', $authData)) {
$authDataReturned['sharingId'] = $authData['sharingId'];
}
return $authDataReturned;
}
/**
* @throws TransportExceptionInterface
* @throws ServerExceptionInterface
* @throws RedirectionExceptionInterface
* @throws DecodingExceptionInterface
* @throws ClientExceptionInterface
*/
2024-08-23 02:35:09 +02:00
protected function getSupportedTldList(): array
{
$authData = self::verifyAuthData($this->authData, $this->client);
$response = $this->client->request('GET', '/v5/domain/tlds', (new HttpOptions())
->setAuthBearer($authData['token'])
->setHeader('Accept', 'application/json')
->setBaseUri(self::BASE_URL)
->toArray())->toArray();
2024-08-23 02:35:09 +02:00
return array_map(fn ($tld) => $tld['name'], $response);
}
2024-08-23 02:35:09 +02:00
/**
* @throws \Psr\Cache\InvalidArgumentException
*/
protected function getCachedTldList(): CacheItemInterface
{
return $this->cacheItemPool->getItem('app.provider.ovh.supported-tld');
}
2024-08-06 03:38:00 +02:00
}