diff --git a/CHANGELOG.md b/CHANGELOG.md index 2fc644e..82143f2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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. diff --git a/config/packages/security.yaml b/config/packages/security.yaml index 6466847..5a114f1 100644 --- a/config/packages/security.yaml +++ b/config/packages/security.yaml @@ -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 } diff --git a/env.json b/env.json index c40cd36..c62e201 100644 --- a/env.json +++ b/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" } } \ No newline at end of file diff --git a/src/Controller/OgBoot/PxeBootFile/PostAction.php b/src/Controller/OgBoot/PxeBootFile/PostAction.php index 725dad7..46d9a8b 100644 --- a/src/Controller/OgBoot/PxeBootFile/PostAction.php +++ b/src/Controller/OgBoot/PxeBootFile/PostAction.php @@ -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' ] ]; diff --git a/src/Security/ApiKeyAuthenticator.php b/src/Security/ApiKeyAuthenticator.php new file mode 100644 index 0000000..c954021 --- /dev/null +++ b/src/Security/ApiKeyAuthenticator.php @@ -0,0 +1,77 @@ +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); + } +}