php-agentic/View/Modal/Admin/ApiKeyManager.php

118 lines
3 KiB
PHP
Raw Normal View History

2026-01-27 00:28:29 +00:00
<?php
declare(strict_types=1);
namespace Core\Mod\Agentic\View\Modal\Admin;
2026-01-27 00:28:29 +00:00
use Core\Mod\Agentic\Models\AgentApiKey;
use Core\Mod\Agentic\Services\AgentApiKeyService;
use Core\Tenant\Models\Workspace;
2026-01-27 00:28:29 +00:00
use Livewire\Attributes\Layout;
use Livewire\Component;
/**
* MCP API Key Manager.
*
* Allows workspace owners to create and manage API keys
* for accessing MCP servers via HTTP API.
*/
#[Layout('hub::admin.layouts.app')]
class ApiKeyManager extends Component
{
public Workspace $workspace;
// Create form state
public bool $showCreateModal = false;
public string $newKeyName = '';
public array $newKeyPermissions = [];
2026-01-27 00:28:29 +00:00
public string $newKeyExpiry = 'never';
// Show new key (only visible once after creation)
public ?string $newPlainKey = null;
public bool $showNewKeyModal = false;
public function mount(Workspace $workspace): void
{
$this->workspace = $workspace;
}
public function openCreateModal(): void
{
$this->showCreateModal = true;
$this->newKeyName = '';
$this->newKeyPermissions = [];
2026-01-27 00:28:29 +00:00
$this->newKeyExpiry = 'never';
}
public function closeCreateModal(): void
{
$this->showCreateModal = false;
}
public function availablePermissions(): array
{
return AgentApiKey::availablePermissions();
}
2026-01-27 00:28:29 +00:00
public function createKey(): void
{
$this->validate([
'newKeyName' => 'required|string|max:100',
]);
$expiresAt = match ($this->newKeyExpiry) {
'30days' => now()->addDays(30),
'90days' => now()->addDays(90),
'1year' => now()->addYear(),
default => null,
};
$key = app(AgentApiKeyService::class)->create(
workspace: $this->workspace,
2026-01-27 00:28:29 +00:00
name: $this->newKeyName,
permissions: $this->newKeyPermissions,
2026-01-27 00:28:29 +00:00
expiresAt: $expiresAt,
);
$this->newPlainKey = $key->plainTextKey;
2026-01-27 00:28:29 +00:00
$this->showCreateModal = false;
$this->showNewKeyModal = true;
session()->flash('message', 'API key created successfully.');
}
public function closeNewKeyModal(): void
{
$this->newPlainKey = null;
$this->showNewKeyModal = false;
}
public function revokeKey(int $keyId): void
{
$key = AgentApiKey::forWorkspace($this->workspace)->findOrFail($keyId);
2026-01-27 00:28:29 +00:00
$key->revoke();
session()->flash('message', 'API key revoked.');
}
public function togglePermission(string $permission): void
2026-01-27 00:28:29 +00:00
{
if (in_array($permission, $this->newKeyPermissions)) {
$this->newKeyPermissions = array_values(array_diff($this->newKeyPermissions, [$permission]));
2026-01-27 00:28:29 +00:00
} else {
$this->newKeyPermissions[] = $permission;
2026-01-27 00:28:29 +00:00
}
}
public function render()
{
return view('agentic::admin.api-key-manager', [
'keys' => AgentApiKey::forWorkspace($this->workspace)->orderByDesc('created_at')->get(),
2026-01-27 00:28:29 +00:00
]);
}
}