6 releases (breaking)

0.7.0 Nov 28, 2023
0.4.1 Oct 4, 2023
0.4.0 Dec 3, 2022
0.3.0 Aug 17, 2022
0.1.1 Mar 27, 2022

#69 in Authentication

Download history 1372/week @ 2023-12-11 1534/week @ 2023-12-18 987/week @ 2023-12-25 938/week @ 2024-01-01 1391/week @ 2024-01-08 1444/week @ 2024-01-15 1364/week @ 2024-01-22 1644/week @ 2024-01-29 1796/week @ 2024-02-05 2046/week @ 2024-02-12 2192/week @ 2024-02-19 2510/week @ 2024-02-26 2256/week @ 2024-03-04 2067/week @ 2024-03-11 1963/week @ 2024-03-18 1940/week @ 2024-03-25

8,425 downloads per month
Used in 6 crates

MIT/Apache

22KB
117 lines

Auth for axum

High-level http auth extractors for axum

🚨 This crate provides an alternative to TypedHeader<Authorization<..>> which you may use instead. Take a look at the fantastic axum-login crate if your looking for more robust session management. I will continue to maintain this crate.

Usage

Bearer Authentication:

use axum_auth::AuthBearer;
 
/// Handler for a typical axum route, takes a `token` and returns it
async fn handler(AuthBearer(token): AuthBearer) -> String {
    format!("Found a bearer token: {}", token)
}

Basic Authentication:

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)
    }
}

You can also define custom extractors, letting you return custom extractors, status codes, and messages to users if the auth fails. Check out the crate documentation for more in-depth information into how everything works!

Installation

Simply place the following inside of your Cargo.toml file for axum:

[dependencies]
axum-auth = "0.7"

Our version follows axum since 0.7. You can also enable just basic/bearer auth via features. To enable just basic auth, you can add this to the Cargo.toml file instead:

[dependencies]
axum-auth = { version = "0.7", default-features = false, features = ["auth-basic"] }

If you're still using axum 0.5, use version 0.3. If you're still using axum 0.6, use version 0.4.

Security

Some essential security considerations to take into account are the following:

  • This crate has not been audited by any security professionals. If you are willing to do or have already done an audit on this crate, please create an issue as it would help out enormously! 😊
  • This crate purposefully does not limit the maximum length of headers arriving so please ensure your webserver configurations are set properly.

Licensing

This project is dual-licensed under both the MIT and Apache, so feel free to use either at your discretion.

Dependencies

~1.9–2.6MB
~51K SLoC