Expand description
Authorization for id_effect services: policies are capabilities.
- Who — the
Principaltrait (implemented byid_effect_auth’s principals and byBasicPrincipal), installed per request or job as theCurrentPrincipalcapability. - Rules — a
Policy<Res>evaluates the principal against a resource as an effect returning aDecision; policies compose (PolicyExt,all_of,any_of). - Roles and permissions —
Rbac: role → permission table with inheritance andarea:*wildcards (Rbac::require,has_permission). - Resources — ownership (
owner), tenant match (same_tenant) and access lists (Acl,acl_permits). - Attributes — claim predicates (
claim_equals,claim_contains,claim,claim_matches). - Named policies —
Policies, a registry capability;authorizeandcheckevaluate a rule by name for the principal in the environment.
A denial is a value (Decision::Deny with a Denial reason) until
authorize / authorize_with turn it into an AuthzError whose
http_status is 401 for an anonymous principal and 403 otherwise.
use id_effect::{Effect, Exit, HCons, HNil, hlist};
use id_effect_authz::{
AuthzError, BasicPrincipal, CurrentPrincipal, Policies, Rbac, authorize,
};
let rbac = Rbac::builder().grant("accountant", ["ledger:post"]).build();
let policies = Policies::builder().define::<()>("ledger.post", rbac.require("ledger:post")).build();
fn post_entry() -> Effect<(), AuthzError, HCons<CurrentPrincipal, HCons<Policies, HNil>>> {
authorize("ledger.post", ())
}
let carol = CurrentPrincipal::new(BasicPrincipal::new("carol").with_role("accountant"));
assert_eq!(post_entry().provide_all(hlist![carol, policies]).block_on(), Exit::Success(()));- Decision events — every decision can be recorded as a serialisable
DecisionEventto theAuthzEventssink capability (PoliciesBuilder::events,AuthzEvents::observe). - Route guards (feature
axum) —guard::Guard, a Tower layer answering 401/403 with Problem Details, attached withguard::AuthorizeExt::authorize. - Test helpers (feature
testing) —testing::AuthzTestwithassert_allows/assert_deniesand the decisions it recorded.
Tier platform (ADR 0010 Amendment 5): the Ledger reference app under
examples/reference/ depends on it.
Re-exports§
pub use guard::AuthorizeExt;pub use guard::AuthorizedEndpoint;pub use guard::Guard;pub use guard::GuardService;
Modules§
- guard
- Route guards (feature
axum): aGuardis a Tower layer that evaluates a policy before the handler runs, andAuthorizeExt::authorizeattaches one to a route. - testing
- Test helpers:
AuthzTestasserts what a policy decides and keeps every decision it saw.
Structs§
- Acl
- A per-resource access-control list: (grantee, permission) entries; permissions accept the
same
area:*/*wildcards asRbac. - AclPolicy
- See
acl_permits. - AllOf
- See
all_of. - And
- See
PolicyExt::and. - Anonymous
- The unauthenticated principal: no id, no roles,
is_authenticatedisfalse. - AnyOf
- See
any_of. - Authenticated
- See
authenticated. - Authz
Events - The decision-event capability: a shared handle to zero or more
AuthzSinks. Cheap to clone. - Basic
Principal - A
Principalbuilt from plain values. - Claim
Contains - See
claim_contains. - Claim
Equals - See
claim_equals. - Claim
Matches - See
claim_matches. - Claim
Policy - See
claim_equals,claim_containsandclaim. - Constant
- See
allow_allanddeny_all. - Current
Principal - The
Principalcapability: a shared handle to the principal of the current request or job. - Decision
Event - One authorization decision: who, which policy, on what, and the outcome with its reason.
- Denial
- Why a policy denied: a human-readable reason and, when evaluated by name through
Policies, the policy’s name. - Effect
Policy - See
from_effect. - Memory
Sink - An in-memory
AuthzSinkfor tests; clones share the same buffer. - Not
- See
PolicyExt::negate. - Observed
- See
AuthzEvents::observe. - Or
- See
PolicyExt::or. - Owner
Policy - See
owner. - Policies
- A registry of named policies, each over its own resource type. Cheap to clone (shared).
- Policies
Builder - Builds a
Policiesregistry. - Predicate
Policy - See
predicate. - Rbac
- A role → permission table with role inheritance. Cheap to clone (shared).
- Rbac
Builder - Builds an
Rbactable. - Require
Inherited Role - See
Rbac::require_role. - Require
Permission - See
Rbac::require. - Require
Role - See
require_roleandrequire_any_role. - Same
Tenant - See
same_tenant.
Enums§
- Authz
Error - Authorization failures.
- Decision
- The outcome of evaluating a policy.
- Grantee
- Who an
Aclentry grants to. - Outcome
- What an authorization decision concluded.
Traits§
- Authz
Sink - Receives decision events. Implementations must be cheap and must not block: they run on the request path.
- Claim
Check - A test of one claim value:
ClaimEquals,ClaimContainsor anyFn(&Value) -> bool. - HasAcl
- A resource carrying its own
Acl. - Owned
- A resource with an owning principal.
- Policy
- A rule deciding whether a principal may act on a resource of type
Res(()for rules that look only at the principal). - Policy
Ext - Combinators for policies.
- Principal
- An authenticated (or anonymous) subject: its id, roles, direct permissions and claims.
- Tenanted
- A resource that belongs to a tenant.
Functions§
- acl_
permits - Allows principals the resource’s
Aclgrantspermissionto. - all_of
- Allows when every policy allows (in order, stopping at the first denial); an empty list allows.
- allow_
all - A policy that always allows.
- any_of
- Allows when any policy allows (in order, stopping at the first allow); when all deny, the last denial is kept; an empty list denies.
- authenticated
- Allows any authenticated principal (denies
Anonymous). - authorize
- Like
check, failing withAuthzError::Denied(orAuthzError::Unauthenticated) instead of returning a denial. - authorize_
with - Like
check_with, failing withAuthzError::Denied(orAuthzError::Unauthenticated) instead of returning a denial. - check
- Evaluates the named policy for the
CurrentPrincipalinRacting onresource, with thePoliciesinR. - check_
with - Evaluates
policyfor theCurrentPrincipalinRacting onresource. - claim
- Allows when the claim
nameis present and satisfiescheck. - claim_
contains - Allows when the claim
nameis an array containingvalue, or equals it. - claim_
equals - Allows when the claim
nameequalsvalue. - claim_
matches - Allows when the claim
nameis present andcheck(claim, resource)holds (e.g. the principal’sregionclaim equals the resource’s region). - current_
principal - The
CurrentPrincipalinR. - deny_
all - A policy that always denies with
reason. - emit_
decision - Delivers
eventto theAuthzEventsinR. - from_
effect - A policy from a function returning the decision effect (for policies that look something
up). Copy what the effect needs out of
resource: the effect outlives the borrow. - has_
permission - Whether the
CurrentPrincipalinRis grantedpermissionby theRbacinR. - owner
- Allows the principal that owns the resource.
- permission_
matches - Whether the grant
grantedcovers the permissionwanted: equal,*, or aprefix:*wildcard coveringprefix:anything. - predicate
- A policy from a synchronous predicate: allows when
check(principal, resource)holds, else denies withreason. - principal_
env - A
PrincipalEnvwrappingprincipal(for.provide_all(..)in tests and entry points). - provide_
authz_ events - A
Layerprovidingeventsas a capability. - provide_
policies - A
Layerprovidingpoliciesas a capability. - provide_
principal - A
Layerprovidingprincipalas theCurrentPrincipalcapability (a job’s service account, a test user). Per-request principals come from the authentication middleware. - provide_
rbac - A
Layerprovidingrbacas a capability. - require_
any_ role - Allows a principal holding at least one of
rolesdirectly. - require_
role - Allows a principal holding
roledirectly (useRbac::require_roleto honour role inheritance). - same_
tenant - Allows a principal whose tenant is the resource’s tenant (no tenant never matches).
Type Aliases§
- Authz
Events Env - The minimal typed environment holding only
AuthzEvents. - BoxPolicy
- A shared, type-erased policy over
Res. - Policies
Env - The minimal typed environment holding only
Policies. - Policy
Effect - The effect a policy evaluates to.
- Principal
Env - The minimal typed environment holding only
CurrentPrincipal. - RbacEnv
- The minimal typed environment holding only
Rbac.