Trait axum_auth::AuthBasicCustom
source · pub trait AuthBasicCustom: Sized {
const ERROR_CODE: StatusCode;
const ERROR_OVERWRITE: Option<&'static str>;
// Required method
fn from_header(contents: (String, Option<String>)) -> Self;
// Provided method
fn decode_request_parts(req: &mut Parts) -> Result<Self, Rejection> { ... }
}
Expand description
Custom extractor trait for basic auth allowing you to implement custom responses
This is enabled via the auth-basic
feature
Usage
To create your own basic auth extractor using this create, you have to:
- Make the extractor struct, something like
struct Example((String, Option<String>));
- Implement FromRequestParts that links to step 3, copy and paste this from the example below
- Implement AuthBasicCustom to generate your extractor with your custom options, see the example below
Once you’ve completed these steps, you should have a new extractor which is just as easy to use as AuthBasic but has all of your custom configuration options inside of it!
Example
This is what a typical custom extractor should look like in full, copy-paste this and edit it:
use axum_auth::{AuthBasicCustom, Rejection};
use http::{request::Parts, StatusCode};
use axum::extract::FromRequestParts;
use async_trait::async_trait;
/// Your custom basic auth returning a fun 418 for errors
struct MyCustomBasicAuth((String, Option<String>));
// this is where you define your custom options
impl AuthBasicCustom for MyCustomBasicAuth {
const ERROR_CODE: StatusCode = StatusCode::IM_A_TEAPOT; // <-- define custom status code here
const ERROR_OVERWRITE: Option<&'static str> = None; // <-- define overwriting message here
fn from_header(contents: (String, Option<String>)) -> Self {
Self(contents)
}
}
// this is just boilerplate, copy-paste this
#[async_trait]
impl<B> FromRequestParts<B> for MyCustomBasicAuth
where
B: Send + Sync,
{
type Rejection = Rejection;
async fn from_request_parts(parts: &mut Parts, _: &B) -> Result<Self, Self::Rejection> {
Self::decode_request_parts(parts)
}
}
Some notes about this example for some more insight:
- There’s no reason for the FromRequestParts to ever change out of this pattern unless you’re doing something special
- It’s recommended to use the
struct BasicExample((String, Option<String>));
pattern because it makes using it from routes easy
Required Associated Constants§
sourceconst ERROR_CODE: StatusCode
const ERROR_CODE: StatusCode
Error code to use instead of the typical 400 BAD REQUEST
error
sourceconst ERROR_OVERWRITE: Option<&'static str>
const ERROR_OVERWRITE: Option<&'static str>
Message to overwrite all default ones with if required, leave as None ideally
Required Methods§
sourcefn from_header(contents: (String, Option<String>)) -> Self
fn from_header(contents: (String, Option<String>)) -> Self
Converts provided header contents to new instance of self; you need to implement this
Example
With the typical struct BasicExample((String, Option<String>));
pattern of structures, this can be implemented like so:
use axum_auth::AuthBasicCustom;
use http::StatusCode;
struct BasicExample((String, Option<String>));
impl AuthBasicCustom for BasicExample {
const ERROR_CODE: StatusCode = StatusCode::BAD_REQUEST;
const ERROR_OVERWRITE: Option<&'static str> = None;
fn from_header(contents: (String, Option<String>)) -> Self {
Self(contents)
}
}
All this method does is let you put the automatically contents of the header into your resulting structure.
Provided Methods§
sourcefn decode_request_parts(req: &mut Parts) -> Result<Self, Rejection>
fn decode_request_parts(req: &mut Parts) -> Result<Self, Rejection>
Decodes bearer token content into new instance of self from axum body parts; this is automatically implemented