Praetor
Praetor is a small, strongly typed authorization library for Rust built around composable
canandcannotrules.
Praetor defines authorization mechanics, not your application's authorization model. Actions, subjects, claims, roles, permissions, and JWT formats all stay in your application. Praetor just composes rules, matches them, and returns a decision.
claims + rules + action + subject/resource -> allow / deny
Install
[]
= "0.1"
Praetor targets Rust 1.88+.
Quick start
use ;
let ability = builder
.can
.can_if
.cannot_if
.build;
let mine = Message ;
// Subject-level check (no instance).
assert!;
// Instance-level check (predicate runs against the resource).
assert!;
// `authorize` maps the same decision to a `Result`.
assert!;
How it works
A single can / authorize interface accepts either a logical subject
(Subject::Message) or a concrete resource (&message). A resource type
opts in by implementing AbilitySubject, which maps it to its logical subject.
- Unconditional rules (
can/cannot) match on action and subject. - Conditional rules (
can_if/cannot_if) additionally run a strongly typed closureFn(&C, &R) -> boolagainst the resource instance. A conditional rule can only match when a concrete instance is supplied — a subject-only check never matches one.
Predicates are ordinary Rust closures. Heterogeneous resource types
(Message, Room, ...) are stored in one ability via internal type erasure
that is invisible to callers.
Evaluation semantics
- Default deny — if no rule matches, access is denied.
- Last matching rule wins — later rules override earlier ones, so a
cannot_ifafter acancan carve out an exception.
let ability = builder
.can
.cannot_if
.build;
let deleted = Message ;
assert!; // later deny wins
What Praetor is not
Praetor deliberately does not provide:
- JWT validation, OAuth/OIDC, or a claims schema
- Built-in roles, permissions, or capability registries
- A policy DSL, predicate AST, or JSON condition format
- Serialized policies or remote/database-backed evaluation
- Built-in
Manage/wildcard semantics
Authorization is a pure, in-memory operation. Load whatever state you need first, then ask Praetor for a decision. Your application owns its model; Praetor owns the mechanics.
Links
- Website: https://praetor.frazier.software
- API docs: https://docs.rs/praetor
- Source: https://github.com/chrisfrazier0/praetor
License
Licensed under the MIT License.