mirror of
https://github.com/maelgangloff/domain-watchdog.git
synced 2025-12-29 16:15:04 +00:00
chore: progress towards namecheap
This commit is contained in:
@@ -1,15 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace App\Config\Connector;
|
||||
|
||||
use App\Entity\Domain;
|
||||
use Symfony\Contracts\HttpClient\HttpClientInterface;
|
||||
|
||||
interface ConnectorInterface
|
||||
{
|
||||
public function __construct(array $authData, HttpClientInterface $client);
|
||||
|
||||
public function orderDomain(Domain $domain, bool $dryRun): void;
|
||||
|
||||
public static function verifyAuthData(array $authData, HttpClientInterface $client): array;
|
||||
}
|
||||
@@ -1,133 +0,0 @@
|
||||
<?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 HttpException($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;
|
||||
}
|
||||
}
|
||||
@@ -1,204 +0,0 @@
|
||||
<?php
|
||||
|
||||
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
|
||||
{
|
||||
public const REQUIRED_ROUTES = [
|
||||
[
|
||||
'method' => 'GET',
|
||||
'path' => '/order/cart',
|
||||
], [
|
||||
'method' => 'GET',
|
||||
'path' => '/order/cart/*',
|
||||
],
|
||||
[
|
||||
'method' => 'POST',
|
||||
'path' => '/order/cart',
|
||||
],
|
||||
[
|
||||
'method' => 'POST',
|
||||
'path' => '/order/cart/*',
|
||||
],
|
||||
[
|
||||
'method' => 'DELETE',
|
||||
'path' => '/order/cart/*',
|
||||
],
|
||||
];
|
||||
|
||||
public function __construct(private array $authData, private HttpClientInterface $client)
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* Order a domain name with the OVH API.
|
||||
*
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function orderDomain(Domain $domain, bool $dryRun = false): void
|
||||
{
|
||||
if (!$domain->getDeleted()) {
|
||||
throw new \Exception('The domain name still appears in the WHOIS database');
|
||||
}
|
||||
|
||||
$ldhName = $domain->getLdhName();
|
||||
if (!$ldhName) {
|
||||
throw new \Exception('Domain name cannot be null');
|
||||
}
|
||||
|
||||
$authData = self::verifyAuthData($this->authData, $this->client);
|
||||
|
||||
$acceptConditions = $authData['acceptConditions'];
|
||||
$ownerLegalAge = $authData['ownerLegalAge'];
|
||||
$waiveRetractationPeriod = $authData['waiveRetractationPeriod'];
|
||||
|
||||
$conn = new Api(
|
||||
$authData['appKey'],
|
||||
$authData['appSecret'],
|
||||
$authData['apiEndpoint'],
|
||||
$authData['consumerKey']
|
||||
);
|
||||
|
||||
$cart = $conn->post('/order/cart', [
|
||||
'ovhSubsidiary' => $authData['ovhSubsidiary'],
|
||||
'description' => 'Domain Watchdog',
|
||||
]);
|
||||
$cartId = $cart['cartId'];
|
||||
|
||||
$offers = $conn->get("/order/cart/{$cartId}/domain", [
|
||||
'domain' => $ldhName,
|
||||
]);
|
||||
|
||||
$pricingModes = ['create-default'];
|
||||
if ('create-default' !== $authData['pricingMode']) {
|
||||
$pricingModes[] = $authData['pricingMode'];
|
||||
}
|
||||
|
||||
$offer = array_filter($offers, fn ($offer) => 'create' === $offer['action']
|
||||
&& true === $offer['orderable']
|
||||
&& in_array($offer['pricingMode'], $pricingModes)
|
||||
);
|
||||
if (empty($offer)) {
|
||||
$conn->delete("/order/cart/{$cartId}");
|
||||
throw new \Exception('Cannot buy this domain name');
|
||||
}
|
||||
|
||||
$item = $conn->post("/order/cart/{$cartId}/domain", [
|
||||
'domain' => $ldhName,
|
||||
'duration' => 'P1Y',
|
||||
]);
|
||||
$itemId = $item['itemId'];
|
||||
|
||||
// $conn->get("/order/cart/{$cartId}/summary");
|
||||
$conn->post("/order/cart/{$cartId}/assign");
|
||||
$conn->get("/order/cart/{$cartId}/item/{$itemId}/requiredConfiguration");
|
||||
|
||||
$configuration = [
|
||||
'ACCEPT_CONDITIONS' => $acceptConditions,
|
||||
'OWNER_LEGAL_AGE' => $ownerLegalAge,
|
||||
];
|
||||
|
||||
foreach ($configuration as $label => $value) {
|
||||
$conn->post("/order/cart/{$cartId}/item/{$itemId}/configuration", [
|
||||
'cartId' => $cartId,
|
||||
'itemId' => $itemId,
|
||||
'label' => $label,
|
||||
'value' => $value,
|
||||
]);
|
||||
}
|
||||
$conn->get("/order/cart/{$cartId}/checkout");
|
||||
|
||||
if ($dryRun) {
|
||||
return;
|
||||
}
|
||||
$conn->post("/order/cart/{$cartId}/checkout", [
|
||||
'autoPayWithPreferredPaymentMethod' => true,
|
||||
'waiveRetractationPeriod' => $waiveRetractationPeriod,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws \Exception
|
||||
*/
|
||||
public static function verifyAuthData(array $authData, HttpClientInterface $client): array
|
||||
{
|
||||
$appKey = $authData['appKey'];
|
||||
$appSecret = $authData['appSecret'];
|
||||
$apiEndpoint = $authData['apiEndpoint'];
|
||||
$consumerKey = $authData['consumerKey'];
|
||||
$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)
|
||||
|| !is_string($apiEndpoint) || empty($apiEndpoint)
|
||||
|| !is_string($ovhSubsidiary) || empty($ovhSubsidiary)
|
||||
|| !is_string($pricingMode) || empty($pricingMode)
|
||||
) {
|
||||
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,
|
||||
$apiEndpoint,
|
||||
$consumerKey
|
||||
);
|
||||
|
||||
$res = $conn->get('/auth/currentCredential');
|
||||
if (null !== $res['expiration'] && new \DateTime($res['expiration']) < new \DateTime()) {
|
||||
throw new \Exception('These credentials have expired');
|
||||
}
|
||||
|
||||
$status = $res['status'];
|
||||
if ('validated' !== $status) {
|
||||
throw new \Exception("The status of these credentials is not valid ($status)");
|
||||
}
|
||||
|
||||
foreach (self::REQUIRED_ROUTES as $requiredRoute) {
|
||||
$ok = false;
|
||||
|
||||
foreach ($res['rules'] as $allowedRoute) {
|
||||
if (
|
||||
$requiredRoute['method'] === $allowedRoute['method']
|
||||
&& fnmatch($allowedRoute['path'], $requiredRoute['path'])
|
||||
) {
|
||||
$ok = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (!$ok) {
|
||||
throw new BadRequestHttpException('The credentials provided do not have enough permissions to purchase a domain name.');
|
||||
}
|
||||
}
|
||||
|
||||
return [
|
||||
'appKey' => $appKey,
|
||||
'appSecret' => $appSecret,
|
||||
'apiEndpoint' => $apiEndpoint,
|
||||
'consumerKey' => $consumerKey,
|
||||
'ovhSubsidiary' => $ovhSubsidiary,
|
||||
'pricingMode' => $pricingMode,
|
||||
'acceptConditions' => $acceptConditions,
|
||||
'ownerLegalAge' => $ownerLegalAge,
|
||||
'waiveRetractationPeriod' => $waiveRetractationPeriod,
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -2,19 +2,22 @@
|
||||
|
||||
namespace App\Config;
|
||||
|
||||
use App\Config\Connector\GandiConnector;
|
||||
use App\Config\Connector\OvhConnector;
|
||||
use App\Service\Connector\OvhConnector;
|
||||
use App\Service\Connector\GandiConnector;
|
||||
use App\Service\Connector\NamecheapConnector;
|
||||
|
||||
enum ConnectorProvider: string
|
||||
{
|
||||
case OVH = 'ovh';
|
||||
case GANDI = 'gandi';
|
||||
case NAMECHEAP = 'namecheap';
|
||||
|
||||
public function getConnectorProvider(): string
|
||||
{
|
||||
return match ($this) {
|
||||
ConnectorProvider::OVH => OvhConnector::class,
|
||||
ConnectorProvider::GANDI => GandiConnector::class
|
||||
ConnectorProvider::GANDI => GandiConnector::class,
|
||||
ConnectorProvider::NAMECHEAP => NamecheapConnector::class
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user