[Web] Rework API keys: multiple read-only/read-write keys, service keys from mailcow.conf

This commit is contained in:
FreddleSpl0it committed 2026-10-23 11:20:17 +02:00
1 parent c145f858e2
commit 0dadf49934
21 files changed
+942 -335

No files matched your search

@@ -0,0 +1,40 @@
<?php
declare(strict_types=1);
namespace Mailcow\ApiKey;
use Mailcow\ApiKey\Controller\ApiKeyApiController;
use Mailcow\ApiKey\Repository\ApiKeyRepository;
use Mailcow\ApiKey\Service\ApiKeyService;
use Mailcow\Core\ApiResponse;
use Mailcow\Core\Context;
use PDO;
/**
* Composition root of the API key module.
*
* process_add_return(ApiKeyModule::fromGlobals()->api->addKey($attr));
* ApiKeyModule::create($pdo, Context::system())->keys->syncServiceKeys(getenv()); // php-fpm start
*/
class ApiKeyModule
{
/** Request keys whose values never reach a log */
private const SECRET_KEYS = ['api_key'];
public function __construct(public readonly ApiKeyService $keys, public readonly ApiKeyApiController $api)
{
}
public static function create(PDO $pdo, Context $context): self
{
$keys = new ApiKeyService(new ApiKeyRepository($pdo), $context);
return new self($keys, new ApiKeyApiController($keys, new ApiResponse(self::SECRET_KEYS)));
}
/** Legacy entry point for procedural callers: reads $pdo and $_SESSION; $context overrides the session caller */
public static function fromGlobals(?Context $context = null): self
{
return self::create($GLOBALS['pdo'], $context ?? Context::fromSession($_SESSION ?? []));
}
}