pub struct AuthBasic(pub (String, Option<String>));
Expand description
Basic authentication extractor, containing an identifier as well as an optional password
This is enabled via the auth-basic
feature
Example
Though this structure can be used like any other axum extractor, we recommend this pattern:
use axum_auth::AuthBasic;
/// Takes basic auth details and shows a message
async fn handler(AuthBasic((id, password)): AuthBasic) -> String {
if let Some(password) = password {
format!("User '{}' with password '{}'", id, password)
} else {
format!("User '{}' without password", id)
}
}
Errors
There are a few errors which this extractor can make. By default, all invalid responses are 400 BAD REQUEST
with one of these messages:
- `Authorization` header could not be decoded – The header couldn’t be decoded, probably missing a colon
- `Authorization` header must be for basic authentication – Someone tried to use bearer auth instead of basic auth
- `Authorization` header is missing – The header was required but it wasn’t found
- `Authorization` header contains invalid characters – The header couldn’t be processed because of invalid characters
Tuple Fields§
§0: (String, Option<String>)
Trait Implementations§
source§impl AuthBasicCustom for AuthBasic
impl AuthBasicCustom for AuthBasic
source§const ERROR_CODE: StatusCode = ERR_DEFAULT
const ERROR_CODE: StatusCode = ERR_DEFAULT
Error code to use instead of the typical
400 BAD REQUEST
errorsource§const ERROR_OVERWRITE: Option<&'static str> = None
const ERROR_OVERWRITE: Option<&'static str> = None
Message to overwrite all default ones with if required, leave as None ideally
source§impl<B> FromRequestParts<B> for AuthBasicwhere
B: Send + Sync,
impl<B> FromRequestParts<B> for AuthBasicwhere B: Send + Sync,
§type Rejection = (StatusCode, &'static str)
type Rejection = (StatusCode, &'static str)
If the extractor fails it’ll use this “rejection” type. A rejection is
a kind of error that can be converted into a response.
source§impl PartialEq for AuthBasic
impl PartialEq for AuthBasic
impl Eq for AuthBasic
impl StructuralEq for AuthBasic
impl StructuralPartialEq for AuthBasic
Auto Trait Implementations§
impl RefUnwindSafe for AuthBasic
impl Send for AuthBasic
impl Sync for AuthBasic
impl Unpin for AuthBasic
impl UnwindSafe for AuthBasic
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<S, T> FromRequest<S, ViaParts> for Twhere
S: Send + Sync,
T: FromRequestParts<S>,
impl<S, T> FromRequest<S, ViaParts> for Twhere S: Send + Sync, T: FromRequestParts<S>,
§type Rejection = <T as FromRequestParts<S>>::Rejection
type Rejection = <T as FromRequestParts<S>>::Rejection
If the extractor fails it’ll use this “rejection” type. A rejection is
a kind of error that can be converted into a response.