principal.hpp 1.8 KB

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