pub struct BasicAuth { /* private fields */ }Expand description
Extracts HTTP Basic authentication credentials from the Authorization header.
This implements the HTTP Basic authentication scheme as defined in RFC 7617.
The Authorization header should contain Basic <base64(username:password)>.
§Example
ⓘ
use fastapi_core::BasicAuth;
async fn protected_route(auth: BasicAuth) -> impl IntoResponse {
format!("Hello, {}!", auth.username())
}§Error Handling
When credentials are missing or invalid, a 401 Unauthorized response is returned
with a WWW-Authenticate: Basic header, following RFC 7617.
§Optional Extraction
Wrap in Option to make authentication optional:
ⓘ
async fn maybe_auth(auth: Option<BasicAuth>) -> impl IntoResponse {
match auth {
Some(a) => format!("Hello, {}!", a.username()),
None => "Anonymous access".to_string(),
}
}§OpenAPI
This extractor generates the following OpenAPI security scheme:
securitySchemes:
BasicAuth:
type: http
scheme: basicImplementations§
Trait Implementations§
Source§impl FromRequest for BasicAuth
impl FromRequest for BasicAuth
Source§type Error = BasicAuthError
type Error = BasicAuthError
Error type when extraction fails.
Source§async fn from_request(
_ctx: &RequestContext,
req: &mut Request,
) -> Result<Self, Self::Error>
async fn from_request( _ctx: &RequestContext, req: &mut Request, ) -> Result<Self, Self::Error>
Extract a value from the request. Read more
impl Eq for BasicAuth
impl StructuralPartialEq for BasicAuth
Auto Trait Implementations§
impl Freeze for BasicAuth
impl RefUnwindSafe for BasicAuth
impl Send for BasicAuth
impl Sync for BasicAuth
impl Unpin for BasicAuth
impl UnwindSafe for BasicAuth
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
Mutably borrows from an owned value. Read more
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: NoopSpan) -> Self
fn instrument(self, _span: NoopSpan) -> Self
Instruments this future with a span (no-op when disabled).
Source§fn in_current_span(self) -> Self
fn in_current_span(self) -> Self
Instruments this future with the current span (no-op when disabled).