Files
dingoandClaude Opus 5.5 54beceb823 feat(admin): read-only admin portal and API (ADR-0182 step 1, schema 61)
evo-admin-api serves the portal at srs.aeternalisgames.com and read-only JSON
for world 100 behind Keycloak (realm role aevolution-admin, audience
aevolution-admin, client aevolution-admin-portal): accounts with session,
status, lives, deaths, resets and mutation choices; one account's lives; the
world's regions; and a live 2D region view of cells, wildlife, pools and
bubbles. The portal is plain HTML/CSS/JS with PKCE sign-in, a strict CSP and
no third-party code.

- Schema 61: three bounded SECURITY DEFINER read functions, fenced in the
  privilege audit; no table or existing function changes.
- Cellular regions answer GET /admin/view on the ops port (NetworkPolicy:
  Prometheus and the admin portal only); CellularLives::extent_of reads the
  existing phenotype budget. No simulation, checkpoint or wire change.
- TokenVerifier takes an optional realm role and returns the email; the ops
  server's response bound is configurable (16 KiB default unchanged).
- DEV component admin-portal-dev (Deployment, Service, NetworkPolicies); the
  Ingress and the aevolution-admin-pg Secret are created out of band.
- CI scripts pinned to schema 60 now expect 61.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-27 12:39:28 -04:00

71 lines
2.4 KiB
C++

#pragma once
#include <cstdint>
#include <expected>
#include <memory>
#include <optional>
#include <span>
#include <string>
#include <string_view>
#include <vector>
// ADR-0131: verifies Keycloak access tokens (RS256 JWTs) against the realm's public
// keys, offline. Only what the broker needs is checked and returned.
namespace evo::server::broker {
struct AccessTokenRules {
std::string issuer; // The realm's issuer URL, exact.
std::string audience; // Must appear in `aud` (string or array).
std::string authorized_party; // `azp`: the game's public client.
std::uint64_t leeway_seconds{30};
// ADR-0182: when set, the role must appear in `realm_access.roles` (the admin portal).
std::string realm_role{};
};
struct AccessTokenClaims {
std::string issuer, subject;
std::uint64_t expires_at{};
std::string email{}; // Empty when absent; shown to the admin, never used as identity.
};
enum class TokenError : std::uint8_t {
malformed,
unsupported_algorithm,
unknown_key,
bad_signature,
wrong_issuer,
wrong_audience,
wrong_party,
wrong_type,
expired,
not_yet_valid,
unverified_email,
missing_subject,
missing_role
};
inline constexpr std::size_t max_access_token_bytes = 6144;
class TokenVerifier {
public:
// RSA signing keys (`use` sig or absent, `alg` RS256 or absent, at least 2048 bits)
// from a JWKS document; other keys are ignored. Fails when none is usable.
[[nodiscard]] static std::expected<TokenVerifier, TokenError> from_jwks(std::string_view jwks,
AccessTokenRules rules);
[[nodiscard]] std::expected<AccessTokenClaims, TokenError>
verify(std::string_view token, std::uint64_t now_seconds) const;
[[nodiscard]] std::size_t keys() const noexcept {
return keys_.size();
}
private:
struct Key {
std::string kid;
std::shared_ptr<void> public_key; // EVP_PKEY, freed by its deleter.
};
TokenVerifier(std::vector<Key> keys, AccessTokenRules rules)
: keys_{std::move(keys)}, rules_{std::move(rules)} {}
std::vector<Key> keys_;
AccessTokenRules rules_;
};
[[nodiscard]] std::optional<std::vector<std::uint8_t>> base64url_decode(std::string_view text);
[[nodiscard]] std::string base64_encode(std::span<const std::uint8_t> bytes);
} // namespace evo::server::broker