axum_auth/
lib.rs

1//! High-level [http auth](https://developer.mozilla.org/en-US/docs/Web/HTTP/Authentication) extractors for [axum](https://github.com/tokio-rs/axum)
2//!
3//! 🚨 This crate provides an alternative to `TypedHeader<Authorization<..>>` which you should probably [use](https://docs.rs/axum/latest/axum/struct.TypedHeader.html) instead. Take a look at the fantastic [axum-login](https://github.com/maxcountryman/axum-login) crate if your looking for more robust session management. I will continue to maintain this crate.
4//!
5//! # Usage
6//!
7//! Take a look at the following structures:
8//!
9//! - **Basic auth: [AuthBasic]**
10//! - **Bearer auth: [AuthBearer]**
11//!
12//! If you need to implement custom errors (i.e., status codes and messages), use these:
13//!
14//! - Custom basic auth: [AuthBasicCustom]
15//! - Custom bearer auth: [AuthBearerCustom]
16//!
17//! That's all there is to it! Check out the [repository](https://github.com/owez/axum-auth) for contributing or some more documentation.
18
19#[cfg(not(any(feature = "auth-basic", feature = "auth-bearer")))]
20compile_error!(r#"At least one feature must be enabled!"#);
21
22#[cfg(feature = "auth-basic")]
23mod auth_basic;
24#[cfg(feature = "auth-bearer")]
25mod auth_bearer;
26
27#[cfg(feature = "auth-basic")]
28pub use auth_basic::{AuthBasic, AuthBasicCustom};
29#[cfg(feature = "auth-bearer")]
30pub use auth_bearer::{AuthBearer, AuthBearerCustom};
31
32use http::{header::AUTHORIZATION, request::Parts, StatusCode};
33
34/// Rejection error used in the [AuthBasicCustom] and [AuthBearerCustom] extractors
35pub type Rejection = (StatusCode, &'static str);
36
37/// Default error status code used for the basic extractors
38pub(crate) const ERR_DEFAULT: StatusCode = StatusCode::BAD_REQUEST;
39
40/// The header is completely missing
41pub(crate) const ERR_MISSING: &str = "`Authorization` header is missing";
42
43/// The header has some invalid characters in it
44pub(crate) const ERR_CHARS: &str = "`Authorization` header contains invalid characters";
45
46/// The header couldn't be decoded properly for basic auth, might not have had a colon in the header
47pub(crate) const ERR_DECODE: &str = "`Authorization` header could not be decoded";
48
49/// The header was set as bearer authentication when we're expecting basic
50pub(crate) const ERR_WRONG_BASIC: &str = "`Authorization` header must be for basic authentication";
51
52/// The header was set as basic authentication when we're expecting bearer
53pub(crate) const ERR_WRONG_BEARER: &str = "`Authorization` header must be a bearer token";
54
55// NOTE: Never used as of axum 0.8.0, remove this block in >=0.9.0
56// /// Helper trait for decoding [Parts] to a final extractor; this is the main interface into the decoding system
57// pub(crate) trait DecodeRequestParts: Sized {
58//     /// Decodes all provided [Parts] into a new instance of self, going through the entire decoding cycle
59//     ///
60//     /// To add custom errors here internally, set the `err_code` as something different
61//     fn decode_request_parts(req: &mut Parts, err_code: StatusCode) -> Result<Self, Rejection>;
62// }
63
64/// Gets the auth header from [Parts] of the request or errors with [ERR_CHARS] or [ERR_MISSING] if wrong
65pub(crate) fn get_header(parts: &mut Parts, err_code: StatusCode) -> Result<&str, Rejection> {
66    parts
67        .headers
68        .get(AUTHORIZATION)
69        .ok_or((err_code, ERR_MISSING))?
70        .to_str()
71        .map_err(|_| (err_code, ERR_CHARS))
72}