Files
Library/include/Void/Security/Authorization.hpp
T

171 lines
4.4 KiB
C++

#pragma once
#include <Void/Functional/Result.hpp>
#include <Void/Security/Types.hpp>
#include <cstddef>
#include <cstdint>
#include <map>
#include <memory>
#include <string>
#include <string_view>
#include <vector>
namespace Void::Security
{
struct Principal {
std::string subject{};
std::vector<std::string> roles{};
std::uint64_t policy_generation{0};
std::map<std::string, std::string, std::less<>> attributes{};
};
struct ResourceAttributes {
std::string resource_type{};
std::string resource_id{};
std::map<std::string, std::string, std::less<>> attributes{};
};
enum class Effect : std::uint8_t { Deny = 0, Allow = 1 };
/// Native typed policy predicate AST with bounded depth (<= 16) and zero
/// script/filesystem/network side effects.
struct Predicate {
enum class Kind : std::uint8_t {
AlwaysTrue = 0,
AlwaysFalse,
HasRole,
SubjectEquals,
ResourceTypeEquals,
ResourceIdEquals,
ResourceAttrEquals,
PrincipalAttrEquals,
ResourceOwnerMatchesSubject,
AllOf,
AnyOf,
Not
};
Kind kind{Kind::AlwaysFalse};
std::string key{};
std::string value{};
std::vector<Predicate> children{};
[[nodiscard]] static Predicate always_true()
{
return Predicate{.kind = Kind::AlwaysTrue};
}
[[nodiscard]] static Predicate always_false()
{
return Predicate{.kind = Kind::AlwaysFalse};
}
[[nodiscard]] static Predicate has_role(std::string role)
{
return Predicate{.kind = Kind::HasRole,
.value = std::move(role)};
}
[[nodiscard]] static Predicate subject_equals(std::string subject)
{
return Predicate{.kind = Kind::SubjectEquals,
.value = std::move(subject)};
}
[[nodiscard]] static Predicate resource_type(std::string type)
{
return Predicate{.kind = Kind::ResourceTypeEquals,
.value = std::move(type)};
}
[[nodiscard]] static Predicate resource_id(std::string id)
{
return Predicate{.kind = Kind::ResourceIdEquals,
.value = std::move(id)};
}
[[nodiscard]] static Predicate resource_attr(std::string k,
std::string v)
{
return Predicate{.kind = Kind::ResourceAttrEquals,
.key = std::move(k),
.value = std::move(v)};
}
[[nodiscard]] static Predicate principal_attr(std::string k,
std::string v)
{
return Predicate{.kind = Kind::PrincipalAttrEquals,
.key = std::move(k),
.value = std::move(v)};
}
[[nodiscard]] static Predicate
owner_is_subject(std::string owner_attr = "owner")
{
return Predicate{.kind = Kind::ResourceOwnerMatchesSubject,
.key = std::move(owner_attr)};
}
[[nodiscard]] static Predicate all_of(std::vector<Predicate> items)
{
return Predicate{.kind = Kind::AllOf,
.children = std::move(items)};
}
[[nodiscard]] static Predicate any_of(std::vector<Predicate> items)
{
return Predicate{.kind = Kind::AnyOf,
.children = std::move(items)};
}
[[nodiscard]] static Predicate negate(Predicate child)
{
Predicate p{.kind = Kind::Not};
p.children.push_back(std::move(child));
return p;
}
};
struct PolicyRule {
std::string id{};
Effect effect{Effect::Deny};
std::string action{};
Predicate condition{Predicate::always_false()};
};
struct Decision {
bool allowed{false};
std::string matched_rule_id{};
std::string reason{};
std::uint64_t policy_generation{0};
};
/// Immutable validated authorization policy snapshot.
/// Enforces default-deny and deny-overrides-allow with bounded rules
/// (<= 4096), roles (<= 64), and predicate nesting depth (<= 16).
class PolicySnapshot
{
public:
[[nodiscard]] static Void::Result::Result<PolicySnapshot>
create(std::uint64_t generation, std::vector<PolicyRule> rules);
[[nodiscard]] std::uint64_t generation() const noexcept
{
return m_generation;
}
[[nodiscard]] std::size_t rule_count() const noexcept
{
return m_rules.size();
}
[[nodiscard]] Decision
authorize(const Principal& principal, std::string_view action,
const ResourceAttributes& resource) const;
private:
PolicySnapshot(std::uint64_t generation,
std::vector<PolicyRule> rules) noexcept
: m_generation(generation), m_rules(std::move(rules))
{
}
std::uint64_t m_generation{0};
std::vector<PolicyRule> m_rules{};
};
} // namespace Void::Security