feat: validate ovh api credentials before using it

This commit is contained in:
Maël Gangloff
2024-07-30 14:09:01 +02:00
parent 61104de1b3
commit 554e840545
2 changed files with 19 additions and 4 deletions

View File

@@ -12,6 +12,6 @@ interface ConnectorInterface
bool $acceptConditions,
bool $ownerLegalAge,
bool $waiveRetractationPeriod,
bool $dryRyn
bool $dryRun
): void;
}

View File

@@ -3,6 +3,7 @@
namespace App\Config\Connector;
use App\Entity\Domain;
use DateTime;
use Exception;
use Ovh\Api;
@@ -22,7 +23,7 @@ readonly class OvhConnector implements ConnectorInterface
bool $acceptConditions,
bool $ownerLegalAge,
bool $waiveRetractationPeriod,
bool $dryRyn = false
bool $dryRun = false
): void
{
if (!$domain->getDeleted()) throw new Exception('The domain name still appears in the WHOIS database');
@@ -89,7 +90,7 @@ readonly class OvhConnector implements ConnectorInterface
}
$conn->get("/order/cart/{$cartId}/checkout");
if ($dryRyn) return;
if ($dryRun) return;
$conn->post("/order/cart/{$cartId}/checkout", [
"autoPayWithPreferredPaymentMethod" => true,
"waiveRetractationPeriod" => $waiveRetractationPeriod
@@ -114,7 +115,21 @@ readonly class OvhConnector implements ConnectorInterface
!is_string($apiEndpoint) || empty($apiEndpoint) ||
!is_string($ovhSubsidiary) || empty($ovhSubsidiary) ||
!is_string($pricingMode) || empty($pricingMode)
) throw new Exception("Bad data schema.");
) throw new Exception("Bad authData schema");
$conn = new Api(
$appKey,
$appSecret,
$apiEndpoint,
$consumerKey
);
$res = $conn->get('/auth/currentCredential');
if ($res['expiration'] !== null && new DateTime($res['expiration']) < new DateTime())
throw new Exception('These credentials have expired');
$status = $res['status'];
if ($status !== 'validated') throw new Exception("The status of these credentials is not valid ($status)");
return [
"appKey" => $appKey,