actix_security/lib.rs
1//! # Actix Security
2//!
3//! Spring Security-inspired authentication and authorization for Actix Web.
4//!
5//! This crate provides a unified API combining:
6//! - `actix-security-core`: Security middleware, authentication, and authorization
7//! - `actix-security-codegen`: Procedural macros (`#[secured]`, `#[pre_authorize]`, etc.)
8//!
9//! ## Quick Start
10//!
11//! Add to your `Cargo.toml`:
12//!
13//! ```toml
14//! [dependencies]
15//! actix-web = "4"
16//! actix-security = { version = "0.2", features = ["argon2", "http-basic"] }
17//! ```
18//!
19//! ## Example
20//!
21//! ```rust,ignore
22//! use actix_web::{get, App, HttpServer, HttpResponse, Responder};
23//! use actix_security::{secured, pre_authorize};
24//! use actix_security::http::security::{
25//! AuthenticatedUser, AuthenticationManager, AuthorizationManager,
26//! Argon2PasswordEncoder, PasswordEncoder, User,
27//! };
28//! use actix_security::http::security::middleware::SecurityTransform;
29//!
30//! #[secured("ADMIN")]
31//! #[get("/admin")]
32//! async fn admin(user: AuthenticatedUser) -> impl Responder {
33//! HttpResponse::Ok().body(format!("Welcome, Admin {}!", user.get_username()))
34//! }
35//!
36//! #[pre_authorize("hasRole('USER') AND hasAuthority('posts:write')")]
37//! #[post("/posts")]
38//! async fn create_post(user: AuthenticatedUser) -> impl Responder {
39//! HttpResponse::Created().body("Post created")
40//! }
41//! ```
42//!
43//! ## Features
44//!
45//! | Feature | Default | Description |
46//! |---------|---------|-------------|
47//! | `macros` | Yes | Procedural macros (`#[secured]`, `#[pre_authorize]`, etc.) |
48//! | `argon2` | Yes | Argon2 password encoder |
49//! | `http-basic` | Yes | HTTP Basic authentication |
50//! | `jwt` | No | JWT authentication (with RSA support) |
51//! | `session` | No | Session-based authentication with fixation protection |
52//! | `form-login` | No | Form-based login with redirect support |
53//! | `remember-me` | No | Remember-me persistent authentication |
54//! | `csrf` | No | CSRF protection middleware |
55//! | `oauth2` | No | OAuth2/OIDC authentication |
56//! | `user-details` | No | Async UserDetailsService trait |
57//! | `full` | No | All features enabled |
58//!
59//! ## Modules
60//!
61//! The main functionality is available through the `http` module:
62//!
63//! - [`http::security`] - Authentication, authorization, and middleware
64//! - [`http::error`] - Error types
65
66// Re-export everything from actix-security-core
67pub use actix_security_core::*;
68
69// Re-export actix_security_core as a module so macros can find it
70// This is needed because macros generate code like ::actix_security_core::http::error::AuthError
71#[doc(hidden)]
72pub use actix_security_core;
73
74// Re-export procedural macros when the "macros" feature is enabled
75#[cfg(feature = "macros")]
76pub use actix_security_codegen::*;
77
78/// Prelude module for convenient imports
79pub mod prelude {
80 pub use actix_security_core::http::security::{
81 AuthenticatedUser, Authenticator, Authorizer, PasswordEncoder, SecurityContext,
82 SecurityHeaders, User,
83 };
84
85 #[cfg(feature = "argon2")]
86 pub use actix_security_core::http::security::Argon2PasswordEncoder;
87
88 #[cfg(feature = "jwt")]
89 pub use actix_security_core::http::security::{JwtAuthenticator, JwtConfig, JwtTokenService};
90
91 #[cfg(feature = "session")]
92 pub use actix_security_core::http::security::{
93 SessionAuthenticator, SessionConfig, SessionLoginService,
94 };
95
96 #[cfg(feature = "form-login")]
97 pub use actix_security_core::http::security::{
98 FormLoginConfig, FormLoginHandler, FormLoginService,
99 };
100
101 #[cfg(feature = "csrf")]
102 pub use actix_security_core::http::security::{CsrfConfig, CsrfProtection, CsrfToken};
103
104 #[cfg(feature = "remember-me")]
105 pub use actix_security_core::http::security::{RememberMeConfig, RememberMeServices};
106
107 #[cfg(feature = "user-details")]
108 pub use actix_security_core::http::security::{UserDetailsAuthenticator, UserDetailsService};
109
110 #[cfg(feature = "macros")]
111 pub use actix_security_codegen::{
112 deny_all, has_access, permit_all, pre_authorize, roles_allowed, secured,
113 };
114}