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
//! CSRF protection for Ferro framework
//!
//! Provides Laravel-like CSRF protection using per-session tokens.
//!
//! # How it works
//!
//! 1. Each session has a unique CSRF token
//! 2. The token is included in HTML responses via a meta tag
//! 3. JavaScript (Inertia.js) reads the token and sends it with requests
//! 4. The middleware validates the token on state-changing requests
//!
//! # Setup
//!
//! Add the middleware after SessionMiddleware:
//!
//! ```rust,ignore
//! use ferro_rs::{global_middleware, SessionMiddleware, CsrfMiddleware, SessionConfig};
//!
//! pub async fn register() {
//! let config = SessionConfig::from_env();
//! global_middleware!(SessionMiddleware::new(config));
//! global_middleware!(CsrfMiddleware::new());
//! }
//! ```
//!
//! # Frontend Integration
//!
//! Add the CSRF meta tag to your HTML:
//!
//! ```html
//! <meta name="csrf-token" content="{{ csrf_token() }}">
//! ```
//!
//! Configure Axios/fetch to include the token:
//!
//! ```javascript
//! axios.defaults.headers.common['X-CSRF-TOKEN'] =
//! document.querySelector('meta[name="csrf-token"]').content;
//! ```
pub use CsrfMiddleware;
use crateget_csrf_token;
/// Get the current CSRF token
///
/// Returns None if no session is active.
///
/// # Example
///
/// ```rust,ignore
/// use ferro_rs::csrf::csrf_token;
///
/// if let Some(token) = csrf_token() {
/// // Use token in response
/// }
/// ```
/// Generate a CSRF meta tag for HTML responses
///
/// # Example
///
/// ```rust,ignore
/// use ferro_rs::csrf::csrf_meta_tag;
///
/// let meta = csrf_meta_tag();
/// // Returns: <meta name="csrf-token" content="...">
/// ```
/// Generate a hidden CSRF input field for forms
///
/// # Example
///
/// ```rust,ignore
/// use ferro_rs::csrf::csrf_field;
///
/// let field = csrf_field();
/// // Returns: <input type="hidden" name="_token" value="...">
/// ```