pub struct BasicAuth {
pub username: String,
pub password: String,
}Expand description
HTTP Basic Authentication credentials extractor.
Extracts username and password from the Authorization header using
the Basic authentication scheme (RFC 7617). The header value is expected
to be Basic <base64(username:password)>.
§Example
ⓘ
use fastapi_core::BasicAuth;
async fn protected_route(auth: BasicAuth) -> impl IntoResponse {
format!("Hello, {}!", auth.username)
}§Error Responses
Returns 401 Unauthorized with WWW-Authenticate: Basic header when:
- Authorization header is missing
- Header doesn’t use Basic scheme
- Base64 decoding fails
- Decoded value doesn’t contain a colon separator
§Optional Extraction
Use Option<BasicAuth> to make authentication optional:
ⓘ
async fn maybe_protected(auth: Option<BasicAuth>) -> impl IntoResponse {
match auth {
Some(creds) => format!("Hello, {}!", creds.username),
None => "Hello, anonymous!".to_string(),
}
}Fields§
§username: StringThe extracted username.
password: StringThe extracted password (may be empty).
Implementations§
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
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).