pub struct AuthMiddleware { /* private fields */ }Expand description
The authentication middleware.
Checks all configured authentication methods in order: mTLS → JWT → API key. First success wins.
Implementations§
Source§impl AuthMiddleware
impl AuthMiddleware
Sourcepub fn new(config: AuthConfig) -> Result<Self, AuthError>
pub fn new(config: AuthConfig) -> Result<Self, AuthError>
Create a new authentication middleware from configuration.
§Errors
Returns AuthError::Config if the JWT configuration contains
an unrecognized algorithm string.
Sourcepub fn is_exempt(&self, path: &str) -> bool
pub fn is_exempt(&self, path: &str) -> bool
Check if a path is exempt from authentication.
Uses segment-boundary matching instead of raw prefix matching
to prevent bypass via path manipulation (e.g., /healthz,
/health/../api/v1/write, /health%2F..).
**** Percent-decodes the path before normalization to prevent
bypass via %2F (/) and %2E (.) encoded characters.
A path matches an exempt entry if it is either an exact match
or the exempt path is a proper directory prefix (path starts with
exempt + "/").
Sourcepub fn authenticate(
&self,
bearer_token: Option<&str>,
client_cert_cn: Option<&str>,
) -> Result<AuthContext, AuthError>
pub fn authenticate( &self, bearer_token: Option<&str>, client_cert_cn: Option<&str>, ) -> Result<AuthContext, AuthError>
Authenticate a request.
Parameters:
bearer_token: The value fromAuthorization: Bearer <token>headerclient_cert_cn: The client certificate CN (from mTLS handshake)
Returns the authentication context on success.
Sourcepub async fn authenticate_async(
&self,
bearer_token: Option<&str>,
client_cert_cn: Option<&str>,
) -> Result<AuthContext, AuthError>
pub async fn authenticate_async( &self, bearer_token: Option<&str>, client_cert_cn: Option<&str>, ) -> Result<AuthContext, AuthError>
Authenticate a request, consulting a JWKS endpoint when one is configured.
Identical to authenticate except that the
JWT step can fetch and cache the issuer’s published keys. The two
share every decision — which method wins, when a failure may fall
through to a weaker one — so they cannot drift apart; only the JWT
call differs, because fetching keys needs to await.
§Errors
As authenticate.
Sourcepub fn jwt_validator(&self) -> Option<&JwtValidator>
pub fn jwt_validator(&self) -> Option<&JwtValidator>
The JWT validator, when one is configured.
Sourcepub fn set_jwks_cache(&mut self, cache: Arc<JwksCache>)
pub fn set_jwks_cache(&mut self, cache: Arc<JwksCache>)
Attach a JWKS cache to the JWT validator.
Called at startup once the issuer’s keys have been fetched.
Sourcepub fn is_configured(&self) -> bool
pub fn is_configured(&self) -> bool
Check whether any authentication method is configured.
Trait Implementations§
Source§impl Clone for AuthMiddleware
impl Clone for AuthMiddleware
Source§fn clone(&self) -> AuthMiddleware
fn clone(&self) -> AuthMiddleware
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreAuto Trait Implementations§
impl !RefUnwindSafe for AuthMiddleware
impl !UnwindSafe for AuthMiddleware
impl Freeze for AuthMiddleware
impl Send for AuthMiddleware
impl Sync for AuthMiddleware
impl Unpin for AuthMiddleware
impl UnsafeUnpin for AuthMiddleware
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> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more