Añadido ApiKeyAuthenticator para acceso seguro de clientes #73
|
|
@ -1,4 +1,11 @@
|
|||
# Changelog
|
||||
## [1.2.4] - 2026-03-16
|
||||
### Added
|
||||
- Se ha implementado un nuevo [ApiKeyAuthenticator] para dotar de acceso seguro al endpoint `/commands` mediante la cabecera `X-API-KEY`.
|
||||
- Incorporación de la variable global de entorno `OGCLIENT_API_KEY` en env.json para gestionar el token de acceso de clientes desde la interfaz gráfica.
|
||||
### Changed
|
||||
- Se ha modificado el controlador PostAction de `PxeBootFile` para inyectar y propagar la clave `ogclient_api_key` al payload que se envía a `ogBoot` durante la creación del arranque.
|
||||
|
||||
## [1.2.3] - 2026-03-11
|
||||
### Added
|
||||
- Se ha añadido una nueva funcionalidad en el DTO `MenuInput` (campo `menuHtmlContent`) para recibir la plantilla HTML íntegra del menú codificada en Base64.
|
||||
|
|
|
|||
|
|
@ -22,6 +22,8 @@ security:
|
|||
success_handler: lexik_jwt_authentication.handler.authentication_success
|
||||
failure_handler: lexik_jwt_authentication.handler.authentication_failure
|
||||
jwt: ~
|
||||
custom_authenticators:
|
||||
- App\Security\ApiKeyAuthenticator
|
||||
refresh_jwt:
|
||||
check_path: refresh_token
|
||||
|
||||
|
|
@ -29,6 +31,7 @@ security:
|
|||
- { path: ^/$, roles: PUBLIC_ACCESS } # Allows accessing the Swagger UI
|
||||
- { path: ^/docs, roles: PUBLIC_ACCESS } # Allows accessing the Swagger UI docs
|
||||
- { path: ^/auth/login, roles: PUBLIC_ACCESS }
|
||||
- { path: ^/commands, roles: [ROLE_OGCLIENT_API, ROLE_ADMIN] }
|
||||
- { path: ^/opengnsys/rest, roles: PUBLIC_ACCESS }
|
||||
- { path: ^/og-repository/webhook, roles: PUBLIC_ACCESS }
|
||||
- { path: ^/og-lives/install/webhook, roles: PUBLIC_ACCESS }
|
||||
|
|
|
|||
3
env.json
3
env.json
|
|
@ -10,6 +10,7 @@
|
|||
"SSL_ENABLED": "false",
|
||||
"OG_BOOT_IP": "127.0.0.1",
|
||||
"OG_BOOT_API_PORT": "8082",
|
||||
"OG_BOOT_PXE_PORT": "8082"
|
||||
"OG_BOOT_PXE_PORT": "8082",
|
||||
"OGCLIENT_API_KEY": "ogclient-super-secret-token"
|
||||
}
|
||||
}
|
||||
|
|
@ -58,7 +58,8 @@ class PostAction extends AbstractOgBootController
|
|||
'ogntp' => $client->getOrganizationalUnit()->getNetworkSettings()?->getNtp(),
|
||||
'ogdns' => $client->getOrganizationalUnit()->getNetworkSettings()?->getDns(),
|
||||
'ogProxy' => $client->getOrganizationalUnit()->getNetworkSettings()?->getProxy(),
|
||||
'resolution' => $client->getResolution() ?? '791'
|
||||
'resolution' => $client->getResolution() ?? '791',
|
||||
'ogclient_api_key' => $_ENV['OGCLIENT_API_KEY'] ?? 'ogclient-super-secret-token'
|
||||
]
|
||||
];
|
||||
|
||||
|
|
|
|||
|
|
@ -0,0 +1,77 @@
|
|||
<?php
|
||||
|
||||
namespace App\Security;
|
||||
|
||||
use App\Entity\User;
|
||||
use Symfony\Component\HttpFoundation\JsonResponse;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
|
||||
use Symfony\Component\Security\Core\Exception\AuthenticationException;
|
||||
use Symfony\Component\Security\Core\Exception\CustomUserMessageAuthenticationException;
|
||||
use Symfony\Component\Security\Http\Authenticator\AbstractAuthenticator;
|
||||
use Symfony\Component\Security\Http\Authenticator\Passport\Badge\UserBadge;
|
||||
use Symfony\Component\Security\Http\Authenticator\Passport\Passport;
|
||||
use Symfony\Component\Security\Http\Authenticator\Passport\SelfValidatingPassport;
|
||||
use Symfony\Component\HttpKernel\KernelInterface;
|
||||
|
||||
class ApiKeyAuthenticator extends AbstractAuthenticator
|
||||
{
|
||||
private string $projectDir;
|
||||
|
||||
public function __construct(KernelInterface $kernel)
|
||||
{
|
||||
$this->projectDir = $kernel->getProjectDir();
|
||||
}
|
||||
|
||||
/**
|
||||
* Called on every request to decide if this authenticator should be used for the request.
|
||||
* Returning `false` will cause this authenticator to be skipped.
|
||||
*/
|
||||
public function supports(Request $request): ?bool
|
||||
{
|
||||
return $request->headers->has('X-API-KEY');
|
||||
}
|
||||
|
||||
public function authenticate(Request $request): Passport
|
||||
{
|
||||
$apiToken = $request->headers->get('X-API-KEY');
|
||||
|
||||
$envFile = $this->projectDir . DIRECTORY_SEPARATOR . 'env.json';
|
||||
if (!is_file($envFile)) {
|
||||
throw new CustomUserMessageAuthenticationException('Configuration file not found.');
|
||||
}
|
||||
|
||||
$config = json_decode(file_get_contents($envFile), true);
|
||||
$expectedToken = $config['vars']['OGCLIENT_API_KEY'] ?? null;
|
||||
|
||||
if (null === $expectedToken || $apiToken !== $expectedToken) {
|
||||
throw new CustomUserMessageAuthenticationException('Invalid API Token.');
|
||||
}
|
||||
|
||||
// Usamos tu propia entidad User para crear un usuario virtual "al vuelo"
|
||||
return new SelfValidatingPassport(new UserBadge('ogclient_api_user', function() {
|
||||
$apiUser = new User();
|
||||
$apiUser->setUsername('ogclient_api_user');
|
||||
$apiUser->setRoles(['ROLE_OGCLIENT_API']);
|
||||
$apiUser->setPassword(''); // Evitamos nulos en propiedades requeridas si las hay
|
||||
|
||||
return $apiUser;
|
||||
}));
|
||||
}
|
||||
|
||||
public function onAuthenticationSuccess(Request $request, TokenInterface $token, string $firewallName): ?Response
|
||||
{
|
||||
// on success, let the request continue
|
||||
return null;
|
||||
}
|
||||
|
||||
public function onAuthenticationFailure(Request $request, AuthenticationException $exception): ?Response
|
||||
{
|
||||
$data = [
|
||||
'message' => strtr($exception->getMessageKey(), $exception->getMessageData())
|
||||
];
|
||||
|
||||
return new JsonResponse($data, Response::HTTP_UNAUTHORIZED);
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue