1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
//! HTTP Basic Authentication middleware and extractor.
//!
//! This module provides [`BasicAuthLayer`], a Tower middleware that decodes the
//! `Authorization: Basic ...` header, calls your [`BasicAuthenticator`] implementation
//! to verify credentials, and stores the authenticated user in request extensions.
//! Use [`BasicAuth<U>`] to extract the user in handlers.
//!
//! # Example
//!
//! ```rust
//! use axum::{Router, http::StatusCode, routing::get};
//! use axum_security::basic_auth::{BasicAuth, BasicAuthLayer, BasicAuthenticator};
//!
//! #[derive(Clone)]
//! struct User {
//! username: String,
//! }
//!
//! struct MyAuth;
//!
//! impl BasicAuthenticator for MyAuth {
//! type User = User;
//! type Error = StatusCode;
//!
//! async fn authenticate(
//! &self,
//! username: &str,
//! password: &str,
//! ) -> Result<Option<User>, StatusCode> {
//! if username == "admin" && password == "secret" {
//! Ok(Some(User { username: username.to_owned() }))
//! } else {
//! Ok(None)
//! }
//! }
//! }
//!
//! async fn hello(BasicAuth(user): BasicAuth<User>) -> String {
//! format!("Hello, {}!", user.username)
//! }
//!
//! let app = Router::<()>::new()
//! .route("/hello", get(hello))
//! .layer(BasicAuthLayer::new(MyAuth));
//! ```
pub use ;
use ;
use ;
/// Implement this trait to verify Basic Auth credentials.
///
/// Return `Ok(Some(user))` on success, `Ok(None)` when credentials are wrong,
/// and `Err(e)` when the verification itself fails (e.g. database error).
///
/// # Example
///
/// ```rust
/// use axum::http::StatusCode;
/// use axum_security::basic_auth::BasicAuthenticator;
///
/// #[derive(Clone)]
/// struct User {
/// username: String,
/// }
///
/// struct MyAuth;
///
/// impl BasicAuthenticator for MyAuth {
/// type User = User;
/// type Error = StatusCode;
///
/// async fn authenticate(
/// &self,
/// username: &str,
/// password: &str,
/// ) -> Result<Option<User>, StatusCode> {
/// if username == "admin" && password == "secret" {
/// Ok(Some(User { username: username.to_owned() }))
/// } else {
/// Ok(None)
/// }
/// }
/// }
/// ```
/// Authenticated user extracted from the Basic Auth header.
///
/// Use this as a handler parameter to require authentication. Returns
/// `401 Unauthorized` if no valid credentials were provided.
///
/// For optional authentication, use `Option<BasicAuth<U>>` instead — it
/// never rejects and returns `None` when the user is unauthenticated.
///
/// # Examples
///
/// Require authentication:
///
/// ```rust,ignore
/// async fn handler(BasicAuth(user): BasicAuth<User>) -> String {
/// format!("Hello, {}!", user.username)
/// }
/// ```
///
/// Optional authentication:
///
/// ```rust,ignore
/// async fn handler(auth: Option<BasicAuth<User>>) -> String {
/// if let Some(BasicAuth(user)) = auth {
/// format!("Welcome back, {}!", user.username)
/// } else {
/// "Welcome, guest!".to_owned()
/// }
/// }
/// ```
;