10#include <unordered_map>
13namespace morph::session {
51 std::unordered_map<std::string, std::string>
metadata;
86 std::unordered_map<std::string, std::string>
claims;
91 [[nodiscard]]
bool hasRole(std::string_view role)
const {
return std::ranges::find(
roles, role) !=
roles.end(); }
151 std::string_view modelType, std::string_view actionType)
const = 0;
166 [[nodiscard]]
virtual std::optional<std::string>
authenticate([[maybe_unused]]
const Context& ctx)
const {
196 std::string_view modelType, std::string_view actionType)
const {
198 return Admission{.allowed =
authorize(ctx, modelType, actionType), .principal = std::move(principal)};
234 [[maybe_unused]] std::string_view modelType,
235 [[maybe_unused]] std::string_view actionType,
236 [[maybe_unused]] std::uint64_t modelId,
237 [[maybe_unused]] std::string_view ownerPrincipal)
const {
270 [[maybe_unused]] std::string_view modelType)
const {
288 [[maybe_unused]] std::string_view modelType,
289 [[maybe_unused]] std::string_view actionType)
const override {
295inline std::shared_ptr<IAuthorizer> allowAllAuthorizer() {
296 static auto instance = std::make_shared<AllowAllAuthorizer>();
309inline const Context*& tlsCurrent() {
310 thread_local const Context* tls =
nullptr;
337inline const Context* current() noexcept {
return detail::tlsCurrent(); }
RAII helper that sets the thread-local Context for its scope.
~ScopedContext()
Restores the thread-local context that was active at construction.
ScopedContext(const Context &ctx)
Installs ctx as the thread-local context until the scope exits.
Default authorizer that permits everything.
bool authorize(const Context &ctx, std::string_view modelType, std::string_view actionType) const override
Permits every call.
Per-call session context carried through the bridge to the model.
std::string token
Bearer credential verified server-side (empty if unauthenticated).
std::unordered_map< std::string, std::string > metadata
Free-form bag of string→string metadata (feature flags, A/B buckets, …).
std::string principal
Auth principal (user id).
std::string requestId
Stable id used for distributed tracing / log correlation. Empty if unused.
std::string locale
BCP-47 locale tag (e.g. en-US, fr-FR) for i18n. Empty for app default.
What admit decides about one call: whether it may run, and under which verified principal.
bool allowed
authorize's answer.
std::optional< std::string > principal
Authorizes incoming actions on a RemoteServer.
virtual bool authorizeRegister(const Context &ctx, std::string_view modelType) const
Optional gate on model creation — bounds who may create an instance.
virtual Admission admit(const Context &ctx, std::string_view modelType, std::string_view actionType) const
Authenticates and authorizes one call together.
virtual bool authorizeInstance(const Context &ctx, std::string_view modelType, std::string_view actionType, std::uint64_t modelId, std::string_view ownerPrincipal) const
Optional per-**instance** authorization hook — the multi-tenant gate.
virtual std::optional< std::string > authenticate(const Context &ctx) const
Returns the authenticated principal for ctx, or nullopt.
virtual bool authorize(const Context &ctx, std::string_view modelType, std::string_view actionType) const =0
Returns true if ctx is allowed to invoke actionType on modelType.
The signed-in user's verified identity, readable outside a dispatch.
std::vector< std::string > roles
Coarse-grained roles the app can gate UI on (e.g. "editor", "admin").
std::string id
Verified identity (e.g. username), as returned by the server. Empty if signed out.
std::unordered_map< std::string, std::string > claims
Free-form bag of string→string claims beyond id/roles.
bool hasRole(std::string_view role) const
Returns true if roles contains role.