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
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
// Author: 金书记
//
//! Nonce Manager | Nonce 管理器
//!
//! Prevents replay attacks by tracking used nonces
//! 通过跟踪已使用的 nonce 来防止重放攻击
//!
//! ## Overview | 概述
//!
//! A **nonce** (number used once) is a unique value that can only be used one time,
//! preventing replay attacks where an attacker reuses a valid request.
//! **nonce**(一次性数字)是一个只能使用一次的唯一值,防止攻击者重用有效请求的重放攻击。
//!
//! ## Integration with Sa-Token | 与 Sa-Token 的集成
//!
//! Nonce is used in several Sa-Token scenarios:
//! Nonce 在 Sa-Token 的多个场景中使用:
//!
//! 1. **Login with Nonce** | 带 Nonce 的登录
//! - Prevents replay of login requests
//! - 防止登录请求的重放
//!
//! 2. **Token Creation** | Token 创建
//! - Each token can have an associated nonce
//! - 每个 token 可以关联一个 nonce
//!
//! 3. **OAuth2 / SSO** | OAuth2 / SSO
//! - Used in authorization codes and state parameters
//! - 用于授权码和状态参数
//!
//! 4. **Sensitive Operations** | 敏感操作
//! - Password changes, account deletion, etc.
//! - 密码修改、账户删除等
//!
//! ## Workflow | 工作流程
//!
//! ```text
//! ┌─────────────────────────────────────────────────────────────┐
//! │ Nonce Lifecycle │
//! │ Nonce 生命周期 │
//! └─────────────────────────────────────────────────────────────┘
//!
//! Client NonceManager Storage
//! 客户端 Nonce管理器 存储
//! │ │ │
//! │ 1. Request nonce │ │
//! │ 请求 nonce │ │
//! │────────────────────────────▶│ │
//! │ │ │
//! │ 2. generate() │ │
//! │ │ 生成唯一 nonce │
//! │ │ nonce_TIMESTAMP_UUID │
//! │ │ │
//! │ 3. Return nonce │ │
//! │ 返回 nonce │ │
//! │◀────────────────────────────│ │
//! │ │ │
//! │ 4. Use nonce in request │ │
//! │ 在请求中使用 nonce │ │
//! │────────────────────────────▶│ │
//! │ │ │
//! │ 5. validate_and_consume() │ │
//! │ │ Check not used │
//! │ │ 检查未使用 │
//! │ │─────────────────────▶ │
//! │ │ Get nonce key │
//! │ │ │
//! │ │ Not found = valid │
//! │ │ 未找到 = 有效 │
//! │ │◀───────────────────── │
//! │ │ │
//! │ │ Store nonce (TTL) │
//! │ │ 存储 nonce │
//! │ │─────────────────────▶ │
//! │ │ │
//! │ 6. Request processed │ │
//! │ 请求已处理 │ │
//! │◀────────────────────────────│ │
//! │ │ │
//! │ 7. Reuse same nonce (ATTACK) │
//! │ 重用相同 nonce(攻击) │ │
//! │────────────────────────────▶│ │
//! │ │ Check if used │
//! │ │ 检查是否已使用 │
//! │ │─────────────────────▶ │
//! │ │ Found = already used │
//! │ │ 找到 = 已使用 │
//! │ │◀───────────────────── │
//! │ │ │
//! │ ❌ Reject (NonceAlreadyUsed) │
//! │ 拒绝(Nonce已使用) │ │
//! │◀────────────────────────────│ │
//! │ │ │
//! │ [After TTL expires] │
//! │ [TTL 过期后] │
//! │ │ Auto cleanup │
//! │ │ 自动清理 │
//! │ │ X──────────────│
//! ```
//!
//! ## Storage Keys | 存储键格式
//!
//! ```text
//! sa:nonce:{nonce_value}
//! - Stores: { "login_id": "...", "created_at": "..." }
//! - TTL: Configured timeout (default: 60 seconds)
//! - Purpose: Mark nonce as used
//!
//! 存储:{ "login_id": "...", "created_at": "..." }
//! TTL:配置的超时时间(默认:60秒)
//! 目的:标记 nonce 为已使用
//! ```
//!
//! ## Security Considerations | 安全考虑
//!
//! ```text
//! 1. ✅ One-Time Use | 一次性使用
//! - Nonce can only be used once
//! - Stored after first use to prevent reuse
//!
//! 2. ✅ Time-Limited | 时间限制
//! - Nonces expire after timeout (default: 60s)
//! - Prevents storage bloat
//!
//! 3. ✅ Unique Generation | 唯一生成
//! - UUID + timestamp ensures uniqueness
//! - Collision probability: negligible
//!
//! 4. ✅ Timestamp Validation | 时间戳验证
//! - check_timestamp() validates time window
//! - Prevents time-based attacks
//!
//! 5. ✅ Atomic Operations | 原子操作
//! - validate_and_consume() is atomic
//! - Prevents race conditions
//! ```
//!
//! ## Usage Examples | 使用示例
//!
//! ### Example 1: Login with Nonce | 带 Nonce 的登录
//!
//! ```rust,ignore
//! use sa_token_core::manager::SaTokenManager;
//!
//! // Client requests nonce
//! let nonce = nonce_manager.generate();
//! // Returns: "nonce_1234567890123_abc123def456"
//!
//! // Client sends login request with nonce
//! let token = manager.login_with_options(
//! "user_123",
//! None,
//! None,
//! None,
//! Some(nonce.clone()), // ← Nonce here
//! None,
//! ).await?;
//!
//! // Server validates and consumes nonce (inside login_with_token_info)
//! nonce_manager.validate_and_consume(&nonce, "user_123").await?;
//! // ✅ First use: OK
//! // ❌ Second use: NonceAlreadyUsed error
//! ```
//!
//! ### Example 2: Sensitive Operation with Nonce | 带 Nonce 的敏感操作
//!
//! ```rust,ignore
//! // Change password with nonce protection
//! async fn change_password(
//! user_id: &str,
//! new_password: &str,
//! nonce: &str,
//! ) -> Result<()> {
//! // Validate nonce
//! nonce_manager.validate_and_consume(nonce, user_id).await?;
//!
//! // Proceed with password change
//! update_password(user_id, new_password).await?;
//!
//! Ok(())
//! }
//! ```
//!
//! ## Best Practices | 最佳实践
//!
//! 1. **Always generate nonces server-side** | 始终在服务端生成 nonce
//! - Don't let clients generate their own nonces
//! - 不要让客户端生成自己的 nonce
//!
//! 2. **Use appropriate timeout** | 使用适当的超时时间
//! - Short timeout (30-60s) for most operations
//! - Longer timeout (5-10min) for complex flows
//! - 大多数操作使用短超时(30-60秒)
//! - 复杂流程使用较长超时(5-10分钟)
//!
//! 3. **Validate timestamp** | 验证时间戳
//! - Use check_timestamp() for additional validation
//! - 使用 check_timestamp() 进行额外验证
//!
//! 4. **One nonce per operation** | 每个操作一个 nonce
//! - Don't reuse nonces across different operations
//! - 不要在不同操作间重用 nonce
//!
//! 5. **Combine with other security measures** | 与其他安全措施结合
//! - Use nonces WITH authentication, not instead of it
//! - 将 nonce 与认证结合使用,而不是替代认证
//! ```
use Arc;
use ;
use SaStorage;
use crate;
use Uuid;
/// Nonce Manager | Nonce 管理器
///
/// Manages nonce generation and validation to prevent replay attacks
/// 管理 nonce 的生成和验证以防止重放攻击