#pragma once #include #include #include #include #include namespace svapi { enum class KeyOp { Read, List, Search, Insert, Update, Delete }; std::optional keyOpFromString(const std::string& s); std::string keyOpToString(KeyOp op); struct KeyScopeRule { std::string collection; // exact ("presets") or prefix glob ("catalog_*") std::set ops; bool requireHumanToken = false; }; struct KeyScope { std::vector rules; std::vector origins; // empty = any origin allowed /// CIDR ranges (or bare addresses) the caller must come from. EMPTY = any. /// /// ENFORCED ON FUNCTION POLICIES ONLY, and the key route REFUSES it rather /// than storing it, because a security control that is accepted and /// silently not applied is the worst thing in this file's vocabulary. The /// "refuse what you cannot honour" rule, applied to ourselves. /// /// It is the server-side sibling of `origins`, and it closes a gap that one /// cannot: an Origin header is sent by browsers, so a key meant for /// server-to-server use can never satisfy an origin allow-list and must /// carry none - which leaves exactly the keys with the widest blast radius /// with no network restriction available to them. An address can be /// checked for any caller. /// /// FAILS CLOSED: a non-empty list with no trustworthy client address /// refuses, which mirrors `originAllowed("")` being false for a non-empty /// origin list. The cost is stated rather than discovered - misconfigure /// `trusted_proxy_cidrs` and every caller is refused rather than the wrong /// ones - and the owner accepted that trade deliberately on 2026-11-19. /// /// The MEMBERSHIP TEST is `functions::addressAllowedBy`, not a method here: /// this struct is pure data compiled standalone by several narrow test /// targets, and giving it CIDR parsing broke four of them. std::vector ipAllow; uint32_t rateLimitPerMin = 0; // 0 = unlimited uint64_t expiresAt = 0; // epoch seconds; 0 = never const KeyScopeRule* findRule(const std::string& collection, KeyOp op) const; /// Whether ANY rule mentions this collection, whatever the op. /// /// The question a LISTING asks, and it is deliberately weaker than /// `findRule(name, KeyOp::List)`. If a key may do anything at all with a /// collection - read one document, insert one - then its existence is not /// a secret from that key, and hiding the name buys nothing while costing /// the thing the listing was added for: a consumer had to hardcode four /// collection names into a backup script, so a collection added later was /// silently missed. What the listing must not do is enumerate collections /// the key cannot touch AT ALL. /// /// Pure and op-free, so it also charges no rate limit - unlike /// `requireCapabilityCore`, which would spend one token per collection /// examined and could rate-limit a caller for a single listing. bool reachesCollection(const std::string& collection) const; bool originAllowed(const std::string& origin) const; bool expired(uint64_t nowEpochSec) const; static KeyScope fromJson(const nlohmann::json& j); nlohmann::json toJson() const; }; /// An API key with per-project grants and optional admin privilege. struct ApiKey { std::string id; // stable non-secret identifier (used as DB doc id) std::string key; // secret value (used for authentication only) std::string label; std::vector projects; // may contain "*" (all projects) bool admin = false; uint64_t createdAt = 0; std::optional scope; static ApiKey fromJson(const nlohmann::json& j); nlohmann::json toJson() const; // full (includes key) — for DB storage nlohmann::json toPublicJson() const; // masked: label, projects, admin, created_at, key_prefix /// Returns true if admin, or projects contains "*", or projects contains project. bool canAccess(const std::string& project) const; }; /// Generate a 48-character lowercase hex key (24 random bytes via OpenSSL RAND_bytes). std::string generateApiKey(); } // namespace svapi