This commit is contained in:
HueByte
2026-07-23 21:16:08 +00:00
parent 28cdc265fd
commit 596dbb301a
7 changed files with 1022 additions and 387 deletions
@@ -10,13 +10,12 @@ public class ServerController : ControllerBase
```
ServerController is an ASP.NET Core API controller that exposes server-wide information and administrative operations under the `/api/server` route. It wires together runtime configuration, persistence, and directory-state to provide a concise snapshot of the server and a small admin surface for privileged tasks. The public `GetInfo` endpoint returns a [`ServerStatusDto`](../../EchoHub.Core/DTOs/ServerDtos.cs.md) containing the server name, description, user and channel counts, and the current registration mode derived from config. The `GetEncryptionKey` endpoint is protected by `[Authorize]` and returns an [`EncryptionKeyResponse`](../../EchoHub.Core/DTOs/ServerDtos.cs.md) containing the configured key, or a 503 if encryption is not configured. The `GetDirectoryStatus` endpoint is admin-only and surfaces directory registration state, including the server identifier and whether a claim token exists, while never exposing the token itself. A private helper `GetCallerAsync` centralizes authentication and authorization checks for admin actions.
ServerController is an ASP.NET Core API controller that exposes server-related admin endpoints under `/api/server`. It assembles live server state by querying the database context for `Users` and `Channels`, reading server metadata from `IConfiguration` (name, description, and registration mode), and deriving the server version from the executing assembly. It returns a `ServerStatusDto` via the `GetInfo` endpoint. The protected endpoints `GetEncryptionKey` and `GetDirectoryStatus` require authentication (and admin privileges for directory status) and return either the configured encryption key or directory-registration state, respectively, without exposing the claim token. A small helper, `GetCallerAsync`, enforces the required role before performing admin-only operations.
## Remarks
By centralizing server-wide information and admin operations in a single controller, the architecture cleanly separates concerns: data access ([`EchoHubDbContext`](../Data/EchoHubDbContext.cs.md)), configuration (`IConfiguration`), and directory registration state ([`DirectoryClaimStore`](../Services/DirectoryClaimStore.cs.md)) are coordinated behind stable, contract-driven DTOs ([`ServerStatusDto`](../../EchoHub.Core/DTOs/ServerDtos.cs.md), [`EncryptionKeyResponse`](../../EchoHub.Core/DTOs/ServerDtos.cs.md)). Authorization boundaries are explicit: open information through `GetInfo`, authenticated access for the encryption key, and admin-only access for directory status. The internal `GetCallerAsync` encapsulates common identity/role validation, reducing duplication and potential security gaps across admin endpoints.
ServerController acts as a focused orchestration boundary that surfaces operator-facing server state by weaving together data from the data layer, configuration, and directory claim store. It centralizes admin concerns (health, configuration, and directory registration) behind clear HTTP endpoints, enabling simple client UIs and tooling. The design emphasizes guarded access for sensitive data (encryption keys and directory status) and relies on role-based checks to restrict those capabilities to admins.
## Notes
- The admin surface is guarded: `GetDirectoryStatus` relies on `GetCallerAsync` to enforce that the caller has at least `ServerRole.Admin`; non-admins will receive an appropriate 403/Unauthorized response.
- If encryption is not configured on the server, the `GetEncryptionKey` endpoint returns a 503 Service Unavailable, signaling to clients that encryption is not currently available despite the endpoint being accessible.
- Access to `/api/server/encryption-key` and `/api/server/directory` is protected by authentication; admins only for the latter.
- The code path for `GetCallerAsync` relies on the `NameIdentifier` claim being a valid GUID; malformed claims could cause an exception at runtime.
- If encryption is not configured, `/api/server/encryption-key` responds with HTTP 503 to indicate the service is not ready.