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
impl Guard
Sourcepub fn new(
identity: Arc<dyn IdentityProvider>,
authorizer: Arc<dyn Authorizer>,
) -> Self
pub fn new( identity: Arc<dyn IdentityProvider>, authorizer: Arc<dyn Authorizer>, ) -> Self
Builds a guard that requires authentication (allow_anonymous = false).
Sourcepub fn allow_anonymous(self, allow: bool) -> Self
pub fn allow_anonymous(self, allow: bool) -> Self
Sets whether unrecognized/absent credentials fall back to anonymous (builder style).
Sourcepub fn disabled() -> Self
pub fn disabled() -> Self
The wide-open policy: anonymous is accepted and everything is allowed. Behaviourally identical to running with no auth.
Sourcepub fn is_disabled(&self) -> bool
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.
Sourcepub fn authenticate(
&self,
credentials: &Credentials,
) -> Result<Principal, AuthError>
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.
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§
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
Source§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
T in a tonic::Request