Skip to main content

Capability

Struct Capability 

Source
pub struct Capability<P, R>
where P: Permission, R: Resource,
{ /* private fields */ }
Expand description

An unforgeable proof that subject subject holds permission P on resource R.

Construct via PolicyEngine::mint_capability. The phantom parameters P and R are erased at runtime but enforced at compile time.

Implementations§

Source§

impl<P, R> Capability<P, R>
where P: Permission, R: Resource,

Source

pub fn subject(&self) -> &str

The subject that holds this capability.

Source

pub fn resource_id(&self) -> &str

The resource identifier this capability covers.

Source

pub fn issued_at(&self) -> SystemTime

When the policy engine minted this capability.

A capability is a point-in-time decision: policy changes after this instant are not reflected in the token. Long-lived holders should re-request rather than cache, or gate use on is_fresh.

Source

pub fn expires_at(&self) -> SystemTime

When this capability expires.

Source

pub fn is_fresh(&self, max_age: Duration) -> bool

Whether this capability was minted within the last max_age.

Use this to bound the window between the policy check and the action (TOCTOU): cap.is_fresh(Duration::from_secs(60)).

Source

pub fn is_expired(&self) -> bool

Whether this capability’s lease has expired.

Source

pub fn is_revoked(&self) -> bool

Whether this capability was revoked via its RevocationEpoch.

Always false for capabilities minted without a revocation binding.

Source

pub fn ensure_active(&self) -> Result<(), CapabilityUseError>

Validate that this capability can still be used (not expired, not revoked).

Source

pub fn permission_name() -> &'static str

The permission name (from the type parameter).

Source§

impl<P, R> Capability<P, R>
where P: Permission, R: Resource,

Source

pub fn coerce<Q>(self) -> Capability<Q, R>
where Q: Permission, P: Implies<Q>,

Downcast this capability to a less-privileged one.

Only callable when P: Implies<Q> — the compiler enforces the lattice. This is a zero-cost operation: subject and resource are preserved; only the permission type parameter changes.

§Example
let write_cap: Capability<CanWrite, Report> =
    agent.request_capability(&report).await?;
// CanWrite → CanRead is a valid lattice relationship:
let read_cap: Capability<CanRead, Report> = write_cap.coerce();
Source

pub fn coerce_ref<Q>(&self) -> Capability<Q, R>
where Q: Permission, P: Implies<Q>,

Like coerce, but borrows — the original (higher) capability is retained.

This is safe for the same reason coerce is: P: Implies<Q> means the holder of P already has every right Q grants, so deriving a Q token grants nothing new.

Trait Implementations§

Source§

impl<P, R> Debug for Capability<P, R>
where P: Permission, R: Resource,

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
Source§

impl<P, R> Display for Capability<P, R>
where P: Permission, R: Resource,

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<P, R> Freeze for Capability<P, R>

§

impl<P, R> RefUnwindSafe for Capability<P, R>

§

impl<P, R> Send for Capability<P, R>

§

impl<P, R> Sync for Capability<P, R>

§

impl<P, R> Unpin for Capability<P, R>

§

impl<P, R> UnsafeUnpin for Capability<P, R>

§

impl<P, R> UnwindSafe for Capability<P, R>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more