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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
use TokenStream;
use quote;
use ;
use ;
/// Derive macro for implementing the `Claim` trait for JWT claims.
///
/// This macro automatically derives the `Claim` trait for your claims struct,
/// enabling validation of required fields and JWT expiration times.
///
/// # Attributes
///
/// - `#[required]` - Mark a field as required (must not be empty)
/// - `#[exp]` - Mark a field as the expiration timestamp (checks against current time)
///
/// # Example: Simple Claims
///
/// ```rust,ignore
/// use feather::jwt::Claim;
///
/// #[derive(Claim, Clone)]
/// struct MyClaims {
/// user_id: String,
/// username: String,
/// }
/// ```
///
/// # Example: With Validation
///
/// ```rust,ignore
/// use feather::jwt::Claim;
///
/// #[derive(Claim, Clone)]
/// struct AuthClaims {
/// #[required]
/// user_id: String,
/// #[required]
/// username: String,
/// }
/// ```
///
/// # Example: With Expiration
///
/// ```rust,ignore
/// use feather::jwt::Claim;
///
/// #[derive(Claim, Clone)]
/// struct TokenClaims {
/// #[required]
/// user_id: String,
/// #[exp]
/// expires_at: usize, // Unix timestamp
/// }
/// ```
///
/// # How It Works
///
/// The macro generates a `validate()` method that:
/// 1. Checks all `#[required]` fields are non-empty
/// 2. Checks `#[exp]` fields contain timestamps greater than current time
/// 3. Returns `Err` if any validation fails
///
/// This is automatically called by the JWT manager when decoding tokens.
///
/// # See Also
///
/// - [`SimpleClaims`](https://docs.rs/feather/latest/feather/jwt/struct.SimpleClaims.html) for a built-in claims struct
/// - [Authentication Guide](https://docs.rs/feather/latest/feather/guides/authentication/) for JWT patterns
/// Attribute macro for defining middleware functions with automatic signature injection.
///
/// This macro eliminates boilerplate by automatically providing `req`, `res`, and `ctx` parameters
/// to your middleware function. It transforms a simple function into a proper Feather middleware.
///
/// # What This Macro Does
///
/// The `#[middleware_fn]` macro injects three parameters into your function:
/// - `req: &mut Request` - The HTTP request
/// - `res: &mut Response` - The HTTP response
/// - `ctx: &AppContext` - Application context for accessing state
///
/// Your function must return `Outcome` (which is `Result<MiddlewareResult, Box<dyn Error>>`).
///
/// # Basic Example
///
/// ```rust,ignore
/// use feather::middleware_fn;
///
/// #[middleware_fn]
/// fn log_requests() {
/// println!("{} {}", req.method, req.uri);
/// next!()
/// }
///
/// app.use_middleware(log_requests);
/// ```
///
/// # With Route Handlers
///
/// ```rust,ignore
/// use feather::{App, middleware_fn};
///
/// #[middleware_fn]
/// fn greet() {
/// let name = req.param("name").unwrap_or("Guest".to_string());
/// res.send_text(format!("Hello, {}!", name));
/// next!()
/// }
///
/// let mut app = App::new();
/// app.get("/greet/:name", greet);
/// ```
///
/// # Compared to the middleware! Macro
///
/// Both `#[middleware_fn]` and `middleware!` work similarly, but `#[middleware_fn]` is
/// best for reusable, named middleware functions, while `middleware!` is best for inline closures:
///
/// ```rust,ignore
/// // Using #[middleware_fn] - for reusable middleware
/// #[middleware_fn]
/// fn validate_auth() {
/// if !req.headers.contains_key("Authorization") {
/// res.set_status(401);
/// res.send_text("Unauthorized");
/// return next!();
/// }
/// next!()
/// }
///
/// app.use_middleware(validate_auth);
///
/// // Using middleware! - for inline middleware
/// app.get("/", middleware!(|_req, res, _ctx| {
/// res.send_text("Hello!");
/// next!()
/// }));
/// ```
///
/// # Accessing Application State
///
/// ```rust,ignore
/// use feather::{State, middleware_fn};
///
/// #[derive(Clone)]
/// struct Config {
/// api_key: String,
/// }
///
/// #[middleware_fn]
/// fn check_api_key() {
/// let config = ctx.get_state::<State<Config>>();
/// let is_valid = config.with_scope(|cfg| cfg.api_key == "secret");
///
/// if !is_valid {
/// res.set_status(403);
/// res.send_text("Forbidden");
/// return next!();
/// }
/// next!()
/// }
/// ```
///
/// # Error Handling
///
/// ```rust,ignore
/// use feather::middleware_fn;
///
/// #[middleware_fn]
/// fn parse_json() {
/// if let Ok(body) = String::from_utf8(req.body.clone()) {
/// // Process body
/// next!()
/// } else {
/// res.set_status(400);
/// res.send_text("Invalid UTF-8");
/// next!()
/// }
/// }
/// ```
///
/// # See Also
///
/// - Use `#[jwt_required]` together with `#[middleware_fn]` for JWT-protected routes
/// - See the [Middlewares Guide](https://docs.rs/feather/latest/feather/guides/middlewares/) for more patterns
/// Attribute macro for creating JWT-protected middleware.
///
/// Combines with `#[middleware_fn]` to automatically extract and validate JWT claims
/// from the `Authorization` header. Only works with `#[middleware_fn]`.
///
/// # How It Works
///
/// This macro:
/// 1. Extracts the JWT token from the `Authorization: Bearer <token>` header
/// 2. Decodes and validates the token using the app's JWT manager
/// 3. Validates claims using the `Claim` trait
/// 4. Injects the decoded claims into your function
///
/// If any step fails, it returns a 401 Unauthorized response automatically.
///
/// # Syntax
///
/// ```rust,ignore
/// #[jwt_required]
/// #[middleware_fn]
/// fn your_handler(claims: YourClaimsType) {
/// // claims are now available
/// next!()
/// }
/// ```
///
/// # Example: Protecting a Route
///
/// ```rust,ignore
/// use feather::{jwt_required, middleware_fn, Claim};
///
/// #[derive(Claim, Clone)]
/// struct AuthClaims {
/// #[required]
/// user_id: String,
/// username: String,
/// }
///
/// #[jwt_required]
/// #[middleware_fn]
/// fn protected_profile() {
/// res.send_text(format!("Profile for: {}", claims.username));
/// next!()
/// }
///
/// let mut app = App::new();
/// app.get("/profile", protected_profile);
/// ```
///
/// # Example: With SimpleClaims
///
/// ```rust,ignore
/// use feather::{jwt_required, middleware_fn};
/// use feather::jwt::SimpleClaims;
///
/// #[jwt_required]
/// #[middleware_fn]
/// fn get_user() {
/// res.send_text(format!("User: {}", claims.sub));
/// next!()
/// }
/// ```
///
/// # Example: Accessing Claim Fields
///
/// ```rust,ignore
/// #[jwt_required]
/// #[middleware_fn]
/// fn protected_route(claims: AuthClaims) {
/// // Access claim fields
/// let user_id = &claims.user_id;
/// let username = &claims.username;
///
/// // Store in response or context
/// ctx.set_state(State::new(user_id.clone()));
/// res.send_text(format!("Welcome, {}!", username));
/// next!()
/// }
/// ```
///
/// # Integration with the App
///
/// Remember to configure the JWT manager:
/// ```rust,ignore
/// use feather::App;
/// use feather::jwt::JwtManager;
///
/// let mut app = App::new();
/// let jwt_manager = JwtManager::new("your-secret-key");
/// app.context().set_state(State::new(jwt_manager));
/// ```
///
/// # Error Handling
///
/// Automatic 401 responses are sent if:
/// - `Authorization` header is missing or malformed
/// - Token is invalid or expired
/// - Claims fail validation
///
/// To customize error responses, use `#[middleware_fn]` with manual JWT handling.
///
/// # See Also
///
/// - [`#[middleware_fn]`](attr.middleware_fn.html) - The companion macro required with `#[jwt_required]`
/// - [`JwtManager`](https://docs.rs/feather/latest/feather/jwt/struct.JwtManager.html) - JWT token management
/// - [Authentication Guide](https://docs.rs/feather/latest/feather/guides/authentication/) - JWT patterns and examples