#pragma once #include "apikey.hpp" #include #include namespace svapi { /// Who is asking. /// /// Authorization used to be expressed directly in terms of ApiKey, which was /// fine while every caller held one. A server-side function holds no key: it /// never authenticates over the wire, it IS the server, and giving it one would /// mean storing a usable secret inside a function definition. It still needs /// the same questions answered - may it reach this project, may it perform this /// op on this collection - and those answers must come from the SAME code, or /// function access and HTTP access will drift apart. /// /// So Principal is the one notion of a caller: an ApiKey converts to it, and so /// does a function identity. /// /// Holds non-owning pointers, so it must not outlive whatever it was built /// from. That is always a stack-local key or identity for the duration of one /// request or one function run. struct Principal { /// Identifies the caller for rate limiting and attribution: a key id, or a /// function id. Distinct namespaces, so a function cannot consume a key's /// rate-limit budget. std::string id; /// May contain "*". Never null. const std::vector* projects = nullptr; bool admin = false; /// Null means unscoped: full access within the granted projects. A scoped /// caller is restricted to the rules it carries. const KeyScope* scope = nullptr; /// True if admin, or the project list contains "*" or this project. /// Deliberately mirrors ApiKey::canAccess, and a test pins them together. bool canAccess(const std::string& project) const; /// Explicit on purpose: an implicit conversion would make every existing /// requireAdmin(key) call ambiguous against the Principal overload. static Principal of(const ApiKey& k); }; } // namespace svapi