domain-watchdog/src/Controller/ConnectorController.php

125 lines
4.2 KiB
PHP
Raw Normal View History

2024-07-29 15:28:05 +02:00
<?php
namespace App\Controller;
2025-02-21 16:20:19 +01:00
use App\Config\ConnectorProvider;
2024-07-29 15:28:05 +02:00
use App\Entity\Connector;
use App\Entity\User;
2024-09-18 13:37:07 +02:00
use App\Service\Connector\AbstractProvider;
2025-02-21 16:20:19 +01:00
use DateTimeImmutable;
2024-07-29 15:28:05 +02:00
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\EntityManagerInterface;
2025-02-21 16:20:19 +01:00
use Exception;
2024-08-04 14:45:27 +02:00
use Psr\Log\LoggerInterface;
2024-07-29 15:28:05 +02:00
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
2024-09-30 13:48:15 +02:00
use Symfony\Component\DependencyInjection\Attribute\Autowire;
use Symfony\Component\DependencyInjection\ContainerInterface;
2025-02-21 16:20:19 +01:00
use Symfony\Component\Filesystem\Filesystem;
2024-07-29 15:28:05 +02:00
use Symfony\Component\HttpFoundation\Request;
2024-08-30 12:54:42 +02:00
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
2024-07-29 15:28:05 +02:00
use Symfony\Component\Routing\Attribute\Route;
use Symfony\Component\Serializer\SerializerInterface;
class ConnectorController extends AbstractController
{
public function __construct(
2025-02-21 16:20:19 +01:00
private readonly SerializerInterface $serializer,
2024-08-04 14:45:27 +02:00
private readonly EntityManagerInterface $em,
2025-02-21 16:20:19 +01:00
private readonly LoggerInterface $logger,
2024-09-30 13:48:15 +02:00
#[Autowire(service: 'service_container')]
2025-02-21 16:20:19 +01:00
private readonly ContainerInterface $locator,
)
{
2024-07-29 15:28:05 +02:00
}
#[Route(
path: '/api/connectors',
name: 'connector_get_all_mine',
defaults: [
'_api_resource_class' => Connector::class,
'_api_operation_name' => 'get_all_mine',
],
methods: ['GET']
)]
public function getConnector(): Collection
{
/** @var User $user */
$user = $this->getUser();
2024-08-02 23:24:52 +02:00
2024-07-29 15:28:05 +02:00
return $user->getConnectors();
}
/**
2025-02-21 16:20:19 +01:00
* @throws Exception
2024-07-29 15:28:05 +02:00
*/
#[Route(
path: '/api/connectors',
name: 'connector_create',
defaults: [
'_api_resource_class' => Connector::class,
'_api_operation_name' => 'create',
],
methods: ['POST']
)]
2025-02-21 16:20:19 +01:00
public function createConnector(Request $request): Connector
2024-07-29 15:28:05 +02:00
{
2025-02-21 16:20:19 +01:00
/** @var Connector $connector */
2024-07-29 15:28:05 +02:00
$connector = $this->serializer->deserialize($request->getContent(), Connector::class, 'json', ['groups' => 'connector:create']);
2024-08-03 00:06:38 +02:00
/** @var User $user */
$user = $this->getUser();
$connector->setUser($user);
2024-07-29 15:28:05 +02:00
2024-08-04 14:45:27 +02:00
$provider = $connector->getProvider();
$this->logger->info('User {username} wants to register a connector from provider {provider}.', [
'username' => $user->getUserIdentifier(),
'provider' => $provider->value,
]);
2024-08-07 01:10:56 +02:00
if (null === $provider) {
2024-08-30 12:54:42 +02:00
throw new BadRequestHttpException('Provider not found');
2024-08-02 23:24:52 +02:00
}
2024-07-29 15:28:05 +02:00
2025-02-21 16:20:19 +01:00
if ($provider === ConnectorProvider::EPP) {
$directory = sprintf('var/epp-certificates/%s/', $connector->getId());
$filesystem = new Filesystem();
$filesystem->mkdir($directory);
$authData = $connector->getAuthData();
if (!isset($authData['certificate_pem'], $authData['certificate_key'])) {
throw new BadRequestHttpException('EPP certificates are required');
}
$pemPath = $directory . 'certificate.pem';
$keyPath = $directory . 'certificate.key';
$filesystem->dumpFile($pemPath, $authData['certificate_pem']);
$filesystem->dumpFile($keyPath, $authData['certificate_key']);
$connector->setAuthData([...$authData, 'files' => ['pem' => $pemPath, 'key' => $keyPath]]);
}
2024-09-30 13:48:15 +02:00
/** @var AbstractProvider $providerClient */
$providerClient = $this->locator->get($provider->getConnectorProvider());
$authData = $providerClient->verifyAuthData($connector->getAuthData());
2024-08-06 03:38:00 +02:00
$connector->setAuthData($authData);
$providerClient->authenticate($authData);
2024-08-06 03:38:00 +02:00
$this->logger->info('User {username} authentication data with the {provider} provider has been validated.', [
'username' => $user->getUserIdentifier(),
'provider' => $provider->value,
]);
2024-08-04 14:45:27 +02:00
$this->logger->info('The new API connector requested by {username} has been successfully registered.', [
'username' => $user->getUserIdentifier(),
]);
2025-02-21 16:20:19 +01:00
$connector->setCreatedAt(new DateTimeImmutable('now'));
2024-07-29 15:28:05 +02:00
$this->em->persist($connector);
$this->em->flush();
return $connector;
}
2024-08-02 23:24:52 +02:00
}