Skip to main content

Crate id_effect_authz

Crate id_effect_authz 

Source
Expand description

Authorization for id_effect services: policies are capabilities.

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 DecisionEvent to the AuthzEvents sink capability (PoliciesBuilder::events, AuthzEvents::observe).
  • Route guards (feature axum) — guard::Guard, a Tower layer answering 401/403 with Problem Details, attached with guard::AuthorizeExt::authorize.
  • Test helpers (feature testing) — testing::AuthzTest with assert_allows / assert_denies and 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): a Guard is a Tower layer that evaluates a policy before the handler runs, and AuthorizeExt::authorize attaches one to a route.
testing
Test helpers: AuthzTest asserts 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 as Rbac.
AclPolicy
See acl_permits.
AllOf
See all_of.
And
See PolicyExt::and.
Anonymous
The unauthenticated principal: no id, no roles, is_authenticated is false.
AnyOf
See any_of.
Authenticated
See authenticated.
AuthzEvents
The decision-event capability: a shared handle to zero or more AuthzSinks. Cheap to clone.
BasicPrincipal
A Principal built from plain values.
ClaimContains
See claim_contains.
ClaimEquals
See claim_equals.
ClaimMatches
See claim_matches.
ClaimPolicy
See claim_equals, claim_contains and claim.
Constant
See allow_all and deny_all.
CurrentPrincipal
The Principal capability: a shared handle to the principal of the current request or job.
DecisionEvent
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.
EffectPolicy
See from_effect.
MemorySink
An in-memory AuthzSink for tests; clones share the same buffer.
Not
See PolicyExt::negate.
Observed
See AuthzEvents::observe.
Or
See PolicyExt::or.
OwnerPolicy
See owner.
Policies
A registry of named policies, each over its own resource type. Cheap to clone (shared).
PoliciesBuilder
Builds a Policies registry.
PredicatePolicy
See predicate.
Rbac
A role → permission table with role inheritance. Cheap to clone (shared).
RbacBuilder
Builds an Rbac table.
RequireInheritedRole
See Rbac::require_role.
RequirePermission
See Rbac::require.
RequireRole
See require_role and require_any_role.
SameTenant
See same_tenant.

Enums§

AuthzError
Authorization failures.
Decision
The outcome of evaluating a policy.
Grantee
Who an Acl entry grants to.
Outcome
What an authorization decision concluded.

Traits§

AuthzSink
Receives decision events. Implementations must be cheap and must not block: they run on the request path.
ClaimCheck
A test of one claim value: ClaimEquals, ClaimContains or any Fn(&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).
PolicyExt
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 Acl grants permission to.
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 with AuthzError::Denied (or AuthzError::Unauthenticated) instead of returning a denial.
authorize_with
Like check_with, failing with AuthzError::Denied (or AuthzError::Unauthenticated) instead of returning a denial.
check
Evaluates the named policy for the CurrentPrincipal in R acting on resource, with the Policies in R.
check_with
Evaluates policy for the CurrentPrincipal in R acting on resource.
claim
Allows when the claim name is present and satisfies check.
claim_contains
Allows when the claim name is an array containing value, or equals it.
claim_equals
Allows when the claim name equals value.
claim_matches
Allows when the claim name is present and check(claim, resource) holds (e.g. the principal’s region claim equals the resource’s region).
current_principal
The CurrentPrincipal in R.
deny_all
A policy that always denies with reason.
emit_decision
Delivers event to the AuthzEvents in R.
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 CurrentPrincipal in R is granted permission by the Rbac in R.
owner
Allows the principal that owns the resource.
permission_matches
Whether the grant granted covers the permission wanted: equal, *, or a prefix:* wildcard covering prefix:anything.
predicate
A policy from a synchronous predicate: allows when check(principal, resource) holds, else denies with reason.
principal_env
A PrincipalEnv wrapping principal (for .provide_all(..) in tests and entry points).
provide_authz_events
A Layer providing events as a capability.
provide_policies
A Layer providing policies as a capability.
provide_principal
A Layer providing principal as the CurrentPrincipal capability (a job’s service account, a test user). Per-request principals come from the authentication middleware.
provide_rbac
A Layer providing rbac as a capability.
require_any_role
Allows a principal holding at least one of roles directly.
require_role
Allows a principal holding role directly (use Rbac::require_role to honour role inheritance).
same_tenant
Allows a principal whose tenant is the resource’s tenant (no tenant never matches).

Type Aliases§

AuthzEventsEnv
The minimal typed environment holding only AuthzEvents.
BoxPolicy
A shared, type-erased policy over Res.
PoliciesEnv
The minimal typed environment holding only Policies.
PolicyEffect
The effect a policy evaluates to.
PrincipalEnv
The minimal typed environment holding only CurrentPrincipal.
RbacEnv
The minimal typed environment holding only Rbac.