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 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404
//! # Overview
//!
//! This crate provides user identification, authentication, and authorization
//! as a `tower` middleware for `axum`.
//!
//! If offers:
//!
//! - **User Identification, Authentication, and Authorization**: Leverage
//! [`AuthSession`] to easily manage authentication and authorization. This is
//! also an extractor, so it can be used directly in your `axum` handlers.
//! - **Support for Arbitrary Users and Backends**: Applications implement a
//! couple of traits, [`AuthUser`] and [`AuthnBackend`], allowing for any user
//! type and any user management backend. Your database? Yep. LDAP? Sure. An
//! auth provider? You bet.
//! - **User and Group Permissions**: Authorization is supported via the
//! [`AuthzBackend`] trait, which allows applications to define custom
//! permissions. Both user and group permissions are supported.
//! - **Convenient Route Protection**: Middleware for protecting access to
//! routes are provided via the [`login_required`] and [`permission_required`]
//! macros. Or bring your own by using [`AuthSession`] directly with
//! [`from_fn`](axum::middleware::from_fn).
//! - **Rock-solid Session Management**: Uses [`tower-sessions`](tower_sessions)
//! for high-performing and ergonomic session management. *Look ma, no
//! deadlocks!*
//!
//! # Usage
//!
//! Applications implement two traits, and optionally a third, to enable login
//! workflows: [`AuthUser`] and [`AuthnBackend`]. Respectively, these define a
//! minimal interface for arbitrary user types and an interface with an
//! arbitrary user management backend.
//!
//! ```rust
//! use std::collections::HashMap;
//!
//! use async_trait::async_trait;
//! use axum_login::{AuthUser, AuthnBackend, UserId};
//!
//! #[derive(Debug, Clone)]
//! struct User {
//! id: i64,
//! pw_hash: Vec<u8>,
//! }
//!
//! impl AuthUser for User {
//! type Id = i64;
//!
//! fn id(&self) -> Self::Id {
//! self.id
//! }
//!
//! fn session_auth_hash(&self) -> &[u8] {
//! &self.pw_hash
//! }
//! }
//!
//! #[derive(Clone, Default)]
//! struct Backend {
//! users: HashMap<i64, User>,
//! }
//!
//! #[derive(Clone)]
//! struct Credentials {
//! user_id: i64,
//! }
//!
//! #[async_trait]
//! impl AuthnBackend for Backend {
//! type User = User;
//! type Credentials = Credentials;
//! type Error = std::convert::Infallible;
//!
//! async fn authenticate(
//! &self,
//! Credentials { user_id }: Self::Credentials,
//! ) -> Result<Option<Self::User>, Self::Error> {
//! Ok(self.users.get(&user_id).cloned())
//! }
//!
//! async fn get_user(
//! &self,
//! user_id: &UserId<Self>,
//! ) -> Result<Option<Self::User>, Self::Error> {
//! Ok(self.users.get(user_id).cloned())
//! }
//! }
//! ```
//!
//! Here we've provided implementations for our own user type and a backend (in
//! this case, we use a `HashMap` only as a proxy for something like a
//! database). If we also wanted to support authorization, we could extend with
//! this an implementation of [`AuthzBackend`].
//!
//! It's worth covering a couple of these methods in a little more detail:
//!
//! - `session_auth_hash`, which is used to validate the session; in our example
//! we use
//! a user's password hash, which means changing passwords will invalidate the
//! session.
//! - `get_user`, which is used to load the user from the backend into the
//! session.
//!
//! Note that our example is not realistic and is meant only to provide an
//! illustration of the API. For instance, our implementation of `authenticate`
//! would likely use proper credentials, and not an ID, to positively identify
//! and authenticate a user in a real backend system.
//!
//! ## Writing handlers
//!
//! With the traits implemented, we can write `axum` handlers, leveraging
//! [`AuthSession`] to manage authentication and authorization workflows.
//! Because `AuthSession` is an extractor, we can use it directly in our
//! handlers.
//!
//! ```rust
//! # use std::collections::HashMap;
//! #
//! # use async_trait::async_trait;
//! # use axum_login::{AuthUser, AuthnBackend, UserId};
//! #
//! # #[derive(Debug, Clone)]
//! # struct User {
//! # id: i64,
//! # pw_hash: Vec<u8>,
//! # }
//! #
//! # impl AuthUser for User {
//! # type Id = i64;
//! #
//! # fn id(&self) -> Self::Id {
//! # self.id
//! # }
//! #
//! # fn session_auth_hash(&self) -> &[u8] {
//! # &self.pw_hash
//! # }
//! # }
//! #
//! # #[derive(Clone)]
//! # struct Backend {
//! # users: HashMap<i64, User>,
//! # }
//! #
//! # #[derive(Clone)]
//! # struct Credentials {
//! # user_id: i64,
//! # }
//! #
//! # #[async_trait]
//! # impl AuthnBackend for Backend {
//! # type User = User;
//! # type Credentials = Credentials;
//! # type Error = std::convert::Infallible;
//! #
//! # async fn authenticate(
//! # &self,
//! # Credentials { user_id }: Self::Credentials,
//! # ) -> Result<Option<Self::User>, Self::Error> {
//! # Ok(self.users.get(&user_id).cloned())
//! # }
//! #
//! # async fn get_user(
//! # &self,
//! # user_id: &UserId<Self>,
//! # ) -> Result<Option<Self::User>, Self::Error> {
//! # Ok(self.users.get(user_id).cloned())
//! # }
//! # }
//! use axum::{
//! http::StatusCode,
//! response::{IntoResponse, Redirect},
//! Form,
//! };
//!
//! type AuthSession = axum_login::AuthSession<Backend>;
//!
//! async fn login(
//! mut auth_session: AuthSession,
//! Form(creds): Form<Credentials>,
//! ) -> impl IntoResponse {
//! let user = match auth_session.authenticate(creds.clone()).await {
//! Ok(Some(user)) => user,
//! Ok(None) => return StatusCode::UNAUTHORIZED.into_response(),
//! Err(_) => return StatusCode::INTERNAL_SERVER_ERROR.into_response(),
//! };
//!
//! if auth_session.login(&user).await.is_err() {
//! return StatusCode::INTERNAL_SERVER_ERROR.into_response();
//! }
//!
//! Redirect::to("/protected").into_response()
//! }
//! ```
//!
//! This handler uses a `Form` extractor to retrieve credentials and then uses
//! them to authenticate with our backend. When successful we get back a user
//! and can then log the user in. Such a workflow can be adapted to the specific
//! needs of an application.
//!
//! ## Protecting routes
//!
//! Access to routes can be controlled with [`login_required`] and
//! [`permission_required`]. These produce middleware which may be used directly
//! with application routes.
//!
//! ```rust
//! # use std::collections::HashMap;
//! #
//! # use async_trait::async_trait;
//! # use axum_login::{AuthUser, AuthnBackend, UserId};
//! #
//! # #[derive(Debug, Clone)]
//! # struct User {
//! # id: i64,
//! # pw_hash: Vec<u8>,
//! # }
//! #
//! # impl AuthUser for User {
//! # type Id = i64;
//! #
//! # fn id(&self) -> Self::Id {
//! # self.id
//! # }
//! #
//! # fn session_auth_hash(&self) -> &[u8] {
//! # &self.pw_hash
//! # }
//! # }
//! #
//! # #[derive(Clone)]
//! # struct Backend {
//! # users: HashMap<i64, User>,
//! # }
//! #
//! # #[derive(Clone)]
//! # struct Credentials {
//! # user_id: i64,
//! # }
//! #
//! # #[async_trait]
//! # impl AuthnBackend for Backend {
//! # type User = User;
//! # type Credentials = Credentials;
//! # type Error = std::convert::Infallible;
//! #
//! # async fn authenticate(
//! # &self,
//! # Credentials { user_id }: Self::Credentials,
//! # ) -> Result<Option<Self::User>, Self::Error> {
//! # Ok(self.users.get(&user_id).cloned())
//! # }
//! #
//! # async fn get_user(
//! # &self,
//! # user_id: &UserId<Self>,
//! # ) -> Result<Option<Self::User>, Self::Error> {
//! # Ok(self.users.get(user_id).cloned())
//! # }
//! # }
//! use axum::{routing::get, Router};
//! use axum_login::login_required;
//!
//! fn protected_routes() -> Router {
//! Router::new()
//! .route(
//! "/protected",
//! get(|| async { "Gotta be logged in to see me!" }),
//! )
//! .route_layer(login_required!(Backend, login_url = "/login"))
//! }
//! ```
//!
//! Routes defined in this way can be protected by the middleware, in this case
//! ensuring that a user is logged before accessing the resource. When a user is
//! not logged in, the user agent is redirected to the provided login URL.
//!
//! Likewise, [`permission_required`] can be used to require user or
//! group permissions in order to access the protected resource.
//!
//! ## Setting up an auth service
//!
//! In order to make use of this within our `axum` application, we establish a
//! `tower` service which provides a middleware that attaches `AuthSession` as a
//! request extension.
//!
//! ```rust,no_run
//! # use std::collections::HashMap;
//! #
//! # use async_trait::async_trait;
//! # use axum_login::{AuthUser, AuthnBackend, UserId};
//! #
//! # #[derive(Debug, Clone)]
//! # struct User {
//! # id: i64,
//! # pw_hash: Vec<u8>,
//! # }
//! #
//! # impl AuthUser for User {
//! # type Id = i64;
//! #
//! # fn id(&self) -> Self::Id {
//! # self.id
//! # }
//! #
//! # fn session_auth_hash(&self) -> &[u8] {
//! # &self.pw_hash
//! # }
//! # }
//! #
//! # #[derive(Clone, Default)]
//! # struct Backend {
//! # users: HashMap<i64, User>,
//! # }
//! #
//! # #[derive(Clone)]
//! # struct Credentials {
//! # user_id: i64,
//! # }
//! #
//! # #[async_trait]
//! # impl AuthnBackend for Backend {
//! # type User = User;
//! # type Credentials = Credentials;
//! # type Error = std::convert::Infallible;
//! #
//! # async fn authenticate(
//! # &self,
//! # Credentials { user_id }: Self::Credentials,
//! # ) -> Result<Option<Self::User>, Self::Error> {
//! # Ok(self.users.get(&user_id).cloned())
//! # }
//! #
//! # async fn get_user(
//! # &self,
//! # user_id: &UserId<Self>,
//! # ) -> Result<Option<Self::User>, Self::Error> {
//! # Ok(self.users.get(user_id).cloned())
//! # }
//! # }
//! use axum::{
//! routing::{get, post},
//! Router,
//! };
//! use axum_login::{
//! login_required,
//! tower_sessions::{MemoryStore, SessionManagerLayer},
//! AuthManagerLayerBuilder,
//! };
//!
//! #[tokio::main]
//! async fn main() -> Result<(), Box<dyn std::error::Error>> {
//! // Session layer.
//! let session_store = MemoryStore::default();
//! let session_layer = SessionManagerLayer::new(session_store);
//!
//! // Auth service.
//! let backend = Backend::default();
//! let auth_layer = AuthManagerLayerBuilder::new(backend, session_layer).build();
//!
//! let app = Router::new()
//! .route("/protected", get(todo!()))
//! .route_layer(login_required!(Backend, login_url = "/login"))
//! .route("/login", post(todo!()))
//! .route("/login", get(todo!()))
//! .layer(auth_layer);
//!
//! let listener = tokio::net::TcpListener::bind("0.0.0.0:3000").await.unwrap();
//! axum::serve(listener, app.into_make_service()).await?;
//!
//! Ok(())
//! }
//! ```
//!
//! ## One more thing
//!
//! While this overview of the API aims to give you a sense of how the crate
//! works and how you might use it with your own applications, these snippets
//! are incomplete and as such it's recommended to review a comprehensive
//! implementation as well.
//!
//! A complete example can be found in [`examples/sqlite.rs`](https://github.com/maxcountryman/axum-login/blob/main/examples/sqlite).
#![warn(
clippy::all,
nonstandard_style,
future_incompatible,
missing_docs,
missing_debug_implementations
)]
#![forbid(unsafe_code)]
pub use axum;
pub use backend::{AuthUser, AuthnBackend, AuthzBackend, UserId};
#[doc(hidden)]
pub use middleware::url_with_redirect_query;
pub use service::{AuthManager, AuthManagerLayer, AuthManagerLayerBuilder};
pub use session::{AuthSession, Error};
pub use tower_sessions;
pub use tracing;
mod backend;
mod extract;
mod middleware;
mod service;
mod session;