pub trait AuthBearerCustom: Sized {
    const ERROR_CODE: StatusCode;
    const ERROR_OVERWRITE: Option<&'static str>;

    // Required method
    fn from_header(contents: &str) -> Self;

    // Provided method
    fn decode_request_parts(req: &mut Parts) -> Result<Self, Rejection> { ... }
}
Expand description

Custom extractor trait for bearer allowing you to implement custom responses

This is enabled via the auth-bearer feature

Usage

To create your own bearer auth extractor using this crate, you have to:

  1. Make the extractor struct, something like struct Example(String);
  2. Implement FromRequestParts that links to step 3, copy and paste this from the example below
  3. Implement AuthBearerCustom 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 AuthBearer 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 async_trait::async_trait;
use axum::extract::FromRequestParts;
use axum_auth::{AuthBearerCustom, Rejection};
use http::{request::Parts, StatusCode};

/// Your custom bearer auth returning a fun 418 for errors
struct MyCustomBearerAuth(String);

// this is where you define your custom options
impl AuthBearerCustom for MyCustomBearerAuth {
    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: &str) -> Self {
        Self(contents.to_string())
    }
}

// this is just boilerplate, copy-paste this
#[async_trait]
impl<B> FromRequestParts<B> for MyCustomBearerAuth
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 BearerExample(String); pattern because it makes using it from routes easy

Required Associated Constants§

source

const ERROR_CODE: StatusCode

Error code to use instead of the typical 400 BAD REQUEST error

source

const ERROR_OVERWRITE: Option<&'static str>

Message to overwrite all default ones with if required, leave as None ideally

Required Methods§

source

fn from_header(contents: &str) -> Self

Converts provided header contents to new instance of self; you need to implement this

Example

With the typical struct BearerExample(String); pattern of structures, this can be implemented like so:

use axum_auth::AuthBearerCustom;
use http::StatusCode;

struct BearerExample(String);

impl AuthBearerCustom for BearerExample {
    const ERROR_CODE: StatusCode = StatusCode::BAD_REQUEST;
    const ERROR_OVERWRITE: Option<&'static str> = None;

    fn from_header(contents: &str) -> Self {
        Self(contents.to_string())
    }
}

All this method does is let you put the automatically contents of the header into your resulting structure.

Provided Methods§

source

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

Object Safety§

This trait is not object safe.

Implementors§