Skip to main content

Guard

Struct Guard 

Source
pub struct Guard { /* private fields */ }
Expand description

A chosen (IdentityProvider, Authorizer) pair — the policy a surface enforces. One Guard serves every request on a surface; identity is derived per request, so a single guard handles many concurrent callers.

allow_anonymous decides what happens when no provider recognizes a request’s credentials: true admits it as Principal::anonymous (auth is optional), false rejects it (auth is required). Authorization still runs on the resulting principal, so “optional authn + strict authz” is expressible — anonymous simply holds no roles.

Implementations§

Source§

impl Guard

Source

pub fn new( identity: Arc<dyn IdentityProvider>, authorizer: Arc<dyn Authorizer>, ) -> Self

Builds a guard that requires authentication (allow_anonymous = false).

Source

pub fn allow_anonymous(self, allow: bool) -> Self

Sets whether unrecognized/absent credentials fall back to anonymous (builder style).

Source

pub fn disabled() -> Self

The wide-open policy: anonymous is accepted and everything is allowed. Behaviourally identical to running with no auth.

Source

pub fn is_disabled(&self) -> bool

Whether this guard enforces nothing (anonymous provider + allow-all), so callers can skip the identity plumbing on hot paths if they wish.

Source

pub fn authenticate( &self, credentials: &Credentials, ) -> Result<Principal, AuthError>

Authenticates a request’s credentials into a principal.

§Errors

Propagates a provider’s AuthError, or returns AuthError::Unauthenticated when no provider accepts and anonymous access is not allowed.

Source

pub async fn authorize( &self, principal: &Principal, access: &Access, ) -> Result<Option<Arc<dyn ViewFilter>>, AuthError>

Authorizes access for principal, returning any view restriction.

Async because Authorizer may consult an external policy oracle; local authorizers resolve without awaiting.

§Errors

Returns AuthError::Forbidden when the authorizer denies the access.

Trait Implementations§

Source§

impl Clone for Guard

Source§

fn clone(&self) -> Guard

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more

Auto Trait Implementations§

§

impl !RefUnwindSafe for Guard

§

impl !UnwindSafe for Guard

§

impl Freeze for Guard

§

impl Send for Guard

§

impl Sync for Guard

§

impl Unpin for Guard

§

impl UnsafeUnpin for Guard

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> FromRef<T> for T
where T: Clone,

Source§

fn from_ref(input: &T) -> T

Converts to this type from a reference to the input type.
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> IntoRequest<T> for T

Source§

fn into_request(self) -> Request<T>

Wrap the input message T in a tonic::Request
Source§

impl<L> LayerExt<L> for L

Source§

fn named_layer<S>(&self, service: S) -> Layered<<L as Layer<S>>::Service, S>
where L: Layer<S>,

Applies the layer to a service and wraps it in Layered.
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. 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