#pragma once #include #include #include #include #include #include #include #include // 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 from_jwks(std::string_view jwks, AccessTokenRules rules); [[nodiscard]] std::expected 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 public_key; // EVP_PKEY, freed by its deleter. }; TokenVerifier(std::vector keys, AccessTokenRules rules) : keys_{std::move(keys)}, rules_{std::move(rules)} {} std::vector keys_; AccessTokenRules rules_; }; [[nodiscard]] std::optional> base64url_decode(std::string_view text); [[nodiscard]] std::string base64_encode(std::span bytes); } // namespace evo::server::broker