garrison 0.7.3

Garrison - 面向 Rust 生态的一站式身份认证鉴权框架
Documentation
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
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
//! Copyright (c) 2026 Kirky.X. All rights reserved.
//! See LICENSE for full license text.

//! GarrisonSession 实现块(从 mod.rs 迁移)。

use super::*;

impl GarrisonSession {
    /// 创建会话管理器实例。
    ///
    /// # 参数
    /// - `dao`: DAO 引用(oxcache / dbnexus)。
    /// - `timeout`: token 级超时秒数(0 表示永久驻留)。
    /// - `active_timeout`: Account-Session 级 activity 超时秒数。
    ///
    /// # 返回
    /// 新建的 `GarrisonSession` 实例。
    pub fn new(dao: Arc<dyn GarrisonDao>, timeout: u64, active_timeout: u64) -> Self {
        Self {
            dao,
            timeout,
            active_timeout,
            #[cfg(feature = "anonymous-session")]
            anon_session_timeout: crate::config::DEFAULT_ANON_SESSION_TIMEOUT_SECS,
            login_locks: DashMap::new(),
            token_session_locks: DashMap::new(),
            login_token_map: DashMap::new(),
            #[cfg(feature = "listener")]
            listener_manager: None,
            expiry_listeners: Vec::new(),
            last_active_time: DashMap::new(),
        }
    }

    /// 获取 DAO 引用(pub(crate) 供 GarrisonLogicDefault 构造 ApiKeyHandler 等需要 DAO 的协议处理器复用)。
    ///
    /// `GarrisonLogicDefault::check_api_key` 通过此访问器获取 DAO,
    /// 构造 `ApiKeyHandler` 实例进行 API Key 校验。
    ///
    /// 仅在 `protocol-apikey` feature 启用时编译(避免 feature 关闭时的 dead_code 警告)。
    #[cfg(feature = "protocol-apikey")]
    pub(crate) fn dao(&self) -> &Arc<dyn GarrisonDao> {
        &self.dao
    }

    /// 注入监听器管理器。
    ///
    /// 注入后 `kickout_by_device` 会为每个被踢出的 token 广播 `GarrisonEvent::Kickout` 事件。
    ///
    /// # 参数
    /// - `manager`: 监听器管理器实例。
    #[cfg(feature = "listener")]
    pub fn with_listener_manager(
        mut self,
        manager: Arc<crate::listener::GarrisonListenerManager>,
    ) -> Self {
        self.listener_manager = Some(manager);
        self
    }

    /// 设置匿名 Session 超时时间。
    ///
    /// 启用 `anonymous-session` feature 后可用。覆盖默认的 `anon_session_timeout`
    /// (默认值为 `DEFAULT_ANON_SESSION_TIMEOUT_SECS` = 1800 秒 = 30 分钟)。
    ///
    /// # 参数
    /// - `timeout`: 匿名 Session 超时秒数。
    #[cfg(feature = "anonymous-session")]
    pub fn with_anon_session_timeout(mut self, timeout: u64) -> Self {
        self.anon_session_timeout = timeout;
        self
    }

    /// 注册会话过期监听器。
    ///
    /// listener 按注册顺序(FIFO)依次调用。`get_token_session` / `get_account_session`
    /// 发现 session 过期时触发所有已注册的 listener。
    ///
    /// # 参数
    /// - `listener`: 过期监听器实例。
    pub fn add_expiry_listener(&mut self, listener: Arc<dyn SessionExpiryListener>) {
        self.expiry_listeners.push(listener);
    }

    /// 触发所有过期监听器。
    ///
    /// listener 按注册顺序(FIFO)依次调用。单个 listener 失败时记录 `tracing::warn!`
    /// 但继续执行后续 listener。
    async fn trigger_expiry_listeners(&self, login_id: &str, token: &str) {
        for listener in &self.expiry_listeners {
            if let Err(e) = listener.on_session_expired(login_id, token).await {
                // 脱敏:只打印 token 前 8 字符,避免完整 token 泄露到日志
                let token_preview = if token.len() > 8 { &token[..8] } else { token };
                tracing::warn!(
                    "SessionExpiryListener callback failed (login_id={}, token={}...): {}",
                    login_id,
                    token_preview,
                    e
                );
            }
        }
    }

    /// 更新 login_id 的最后活跃时间为当前 unix 毫秒时间戳。
    pub fn update_last_active(&self, login_id: &str) {
        let now = chrono::Utc::now().timestamp_millis();
        self.last_active_time.insert(login_id.to_string(), now);
    }

    /// 更新 login_id 的最后活跃时间为指定时间戳(用于可注入时钟)。
    ///
    /// 与 `update_last_active` 的区别:接受外部传入的时间戳,
    /// 支持 `MockClock` 注入测试场景。
    pub fn update_last_active_at(&self, login_id: &str, timestamp_millis: i64) {
        self.last_active_time
            .insert(login_id.to_string(), timestamp_millis);
    }

    /// 获取 login_id 的最后活跃时间(unix 毫秒),不存在返回 None。
    pub fn get_last_active(&self, login_id: &str) -> Option<i64> {
        self.last_active_time.get(login_id).map(|v| *v)
    }

    /// 检查会话是否因悬停超时应被踢出。
    ///
    /// # 参数
    /// - `login_id`: 登录主体标识。
    /// - `hover_timeout_secs`: 悬停超时秒数(-1 = 不启用,>0 = 启用)。
    ///
    /// # 返回
    /// - `true`: 会话活跃或悬停检查未启用,不应踢出。
    /// - `false`: 会话已悬停超时,应踢出。
    pub fn check_hover_timeout(&self, login_id: &str, hover_timeout_secs: i64) -> bool {
        if hover_timeout_secs <= 0 {
            return true; // 不启用悬停检查
        }
        let now = chrono::Utc::now().timestamp_millis();
        let timeout_millis = hover_timeout_secs * 1000;
        match self.last_active_time.get(login_id) {
            Some(last) => {
                let elapsed = now - *last;
                if elapsed > timeout_millis {
                    return false; // 悬停超时,踢出
                }
                true
            },
            None => true, // 无记录(首次 check_login),不踢出
        }
    }

    /// 获取 per-login_id 锁并执行 future(保护 Account-Session read-modify-write 序列)。
    ///
    /// 锁粒度为 login_id,不影响不同用户的并发。使用 `tokio::sync::Mutex`(持有锁跨 await 点)。
    /// `kickout_by_device` 持有锁后调用 `logout_inner`(不获取锁),避免死锁。
    ///
    /// # pub(crate) 可见性说明
    /// 暴露给 `stp::session::AuthLogicDefault::enforce_max_login_count` 使用,
    /// 将 `enforce_max_login_count` 纳入 per-login_id 锁保护,与 `create_token_session`
    /// 形成原子序列,消除并发 login 时的 TOCTOU 竞态(fix-refresh-race-and-test-contracts / T015)。
    pub(crate) async fn with_login_lock<F, R>(&self, login_id: &str, f: F) -> R
    where
        F: std::future::Future<Output = R>,
    {
        let lock = self
            .login_locks
            .entry(login_id.to_string())
            .or_insert_with(|| Arc::new(TokioMutex::new(())))
            .clone();
        let _guard = lock.lock().await;
        f.await
    }

    /// 获取 per-token 锁并执行 future(保护 Token-Session read-modify-write 序列)。
    ///
    /// 锁粒度为 token,不影响不同 token 的并发。使用 `tokio::sync::Mutex`(持有锁跨 await 点)。
    /// 用于 `set`/`set_device`/`touch`/`set_active_timeout`/`open_safe`/`close_safe` 等
    /// 修改 TokenSession 的操作,避免并发 read-modify-write 导致 lost update
    ///(CRIT-001 / FMEA #5,kueiku RPN=288)。
    ///
    /// 注意:`get_token_session`/`save_token_session` 本身不加锁,调用方需通过此方法
    /// 包裹 read-modify-write 序列。只读操作(如 `is_safe`)不需要锁。
    pub(crate) async fn with_token_session_lock<F, R>(&self, token: &str, f: F) -> R
    where
        F: std::future::Future<Output = R>,
    {
        let lock = self
            .token_session_locks
            .entry(token.to_string())
            .or_insert_with(|| Arc::new(TokioMutex::new(())))
            .clone();
        let _guard = lock.lock().await;
        let result = f.await;
        // 安全审查 MEDIUM 修复(fix-refresh-race-and-test-contracts):
        // 清理无等待者的 entry,防止攻击者用大量随机 token 调用 `set`/`touch` 等
        // 用户面 API 灌满 `token_session_locks` 导致 OOM(CWE-770)。
        // 与 `renew_locks` 的清理模式一致:先 drop guard 再 drop lock(Arc clone),
        // 使 strong_count 减到 1(只剩 DashMap 中的 entry),再 remove_if 移除。
        // 注意:若不 drop(lock),strong_count 仍为 2(DashMap + 本地 lock 变量),
        // remove_if 不会移除,清理失效。
        drop(_guard);
        drop(lock);
        self.token_session_locks
            .remove_if(token, |_, l| Arc::strong_count(l) == 1);
        result
    }

    /// 创建会话(login 时调用):双写 Account-Session + Token-Session。
    ///
    ///
    /// # 参数
    /// - `login_id`: 登录主体标识(接受 `String` / `&str`)。
    /// - `token`: 新创建的 token 字符串。
    ///
    /// # 返回
    /// 成功返回 `Ok(())`。
    ///
    /// # 错误
    /// - 序列化 `TokenSession` / `AccountSession` 失败:`GarrisonError::Session`。
    /// - DAO 写入失败:透传 `GarrisonError`。
    pub async fn create(&self, login_id: impl Into<String>, token: &str) -> GarrisonResult<()> {
        let login_id: String = login_id.into();
        self.create_inner(&login_id, token, None, None, None).await
    }

    /// 创建 Token-Session 并写入 LoginParams 中的 device/ip/user_agent。
    ///
    /// 与 [`create`](Self::create) 的区别:将 `LoginParams` 的 device/ip/user_agent
    /// 直接写入 `TokenSession` 对应字段,无需 login 后再调用 `set_device`。
    ///
    /// # 参数
    /// - `login_id`: 登录主体标识。
    /// - `token`: 新创建的 token 字符串。
    /// - `params`: 登录参数(device/ip/user_agent/remember_me)。
    ///
    /// # 返回
    /// 成功返回 `Ok(())`。
    ///
    /// # 错误
    /// - 序列化 `TokenSession` / `AccountSession` 失败:`GarrisonError::Session`。
    /// - DAO 写入失败:透传 `GarrisonError`。
    pub async fn create_token_session(
        &self,
        login_id: &str,
        token: &str,
        params: &crate::stp::LoginParams,
    ) -> GarrisonResult<()> {
        self.create_inner(
            login_id,
            token,
            params.device.as_deref(),
            params.ip.as_deref(),
            params.user_agent.as_deref(),
        )
        .await
    }

    /// create / create_token_session 共用内部实现。
    ///
    /// 在 per-login_id 锁内双写 Token-Session + Account-Session。
    /// device/ip/user_agent 为 None 时对应字段留空(向后兼容 `create`)。
    async fn create_inner(
        &self,
        login_id: &str,
        token: &str,
        device: Option<&str>,
        ip: Option<&str>,
        user_agent: Option<&str>,
    ) -> GarrisonResult<()> {
        let login_id: String = login_id.to_string();
        self.with_login_lock(&login_id, async {
            self.create_token_session_inner(&login_id, token, device, ip, user_agent)
                .await
        })
        .await
    }

    /// `create_token_session` 的无锁内部实现(HIGH-1 修复,fix-refresh-race-and-test-contracts)。
    ///
    /// 调用方必须已通过 [`with_login_lock`](Self::with_login_lock) 持有 `login_id` 的锁,
    /// 才能将 `create` 与后续的 `enforce_max_login_count_inner` 组合成原子序列。
    /// 未持锁调用会破坏 Account-Session read-modify-write 的并发安全。
    ///
    /// 双写 Token-Session + Account-Session,device/ip/user_agent 为 None 时对应字段留空。
    pub(crate) async fn create_token_session_inner(
        &self,
        login_id: &str,
        token: &str,
        device: Option<&str>,
        ip: Option<&str>,
        user_agent: Option<&str>,
    ) -> GarrisonResult<()> {
        let now = Utc::now().timestamp();

        // 创建 Token-Session
        let token_session = TokenSession {
            token: token.to_string(),
            login_id: login_id.to_string(),
            created_at: now,
            last_active_at: now,
            attrs: HashMap::new(),
            device: device.map(|s| s.to_string()),
            ip: ip.map(|s| s.to_string()),
            user_agent: user_agent.map(|s| s.to_string()),
            safe_services: HashMap::new(),
            #[cfg(feature = "dynamic-active-timeout")]
            dynamic_active_timeout: None,
            #[cfg(feature = "anonymous-session")]
            is_anon: false,
        };
        let token_json = serde_json::to_string(&token_session)
            .map_err(|e| GarrisonError::Session(format!("session-sim-token-serialize::{}", e)))?;
        self.dao
            .set(&token_key(token), &token_json, self.timeout)
            .await?;

        // 读取或创建 Account-Session
        let mut account = self
            .get_account_session(login_id)
            .await?
            .unwrap_or_else(|| AccountSession {
                login_id: login_id.to_string(),
                tokens: vec![],
                created_at: now,
                last_active_at: now,
            });

        // 添加 token 信息(spec scenario "Account-Session 记录多 token")
        account.tokens.push(TokenInfo {
            token: token.to_string(),
            created_at: now,
            last_active_at: now,
        });
        account.last_active_at = now;

        let account_json = serde_json::to_string(&account)
            .map_err(|e| GarrisonError::Session(format!("session-sim-account-serialize::{}", e)))?;
        self.dao
            .set(&account_key(login_id), &account_json, self.active_timeout)
            .await?;

        // 同步内存索引(login_id → token 列表),用于并发登录控制快速查询
        self.add_login_token(login_id, token);

        Ok(())
    }

    /// 获取 Token-Session。
    ///
    ///
    /// # 参数
    /// - `token`: token 字符串。
    ///
    /// # 返回
    /// - `Some(TokenSession)`: token 存在。
    /// - `None`: token 不存在或已过期。
    ///
    /// # 错误
    /// - 反序列化失败:`GarrisonError::Session`。
    /// - DAO 读取失败:透传 `GarrisonError`。
    pub async fn get_token_session(&self, token: &str) -> GarrisonResult<Option<TokenSession>> {
        match self.dao.get(&token_key(token)).await? {
            Some(json) => {
                let ts: TokenSession = serde_json::from_str(&json).map_err(|e| {
                    GarrisonError::Session(format!("session-sim-token-deserialize::{}", e))
                })?;
                // R-session-lifecycle-003: 检查 session 级过期(last_active_at + timeout < now)
                let now = Utc::now().timestamp();
                if ts.last_active_at + (self.timeout as i64) < now {
                    // 触发过期回调
                    self.trigger_expiry_listeners(&ts.login_id, token).await;
                    // 从 DAO 删除过期 session(清理)
                    if let Err(e) = self.dao.delete(&token_key(token)).await {
                        let token_preview = if token.len() > 8 { &token[..8] } else { token };
                        tracing::warn!(
                            "failed to delete expired Token-Session (token={}...): {}",
                            token_preview,
                            e
                        );
                    }
                    return Ok(None);
                }
                Ok(Some(ts))
            },
            None => Ok(None),
        }
    }

    /// 获取 Token-Session 与剩余 TTL(性能优化版)。
    ///
    /// 单次 DAO 调用同时获取 value 与 TTL,避免 `get_token_session` + `get_token_timeout`
    /// 两次 DAO 往返。用于 `renew_to_equivalent` 热路径。
    ///
    /// # 参数
    /// - `token`: token 字符串。
    ///
    /// # 返回
    /// - `Ok(Some((ts, ttl)))`: token 存在。`ttl` 为 `Some(d)` 表示设置了 TTL,`None` 表示永久驻留。
    /// - `Ok(None)`: token 不存在或已过期。
    ///
    /// # 错误
    /// - 反序列化失败:`GarrisonError::Session`。
    /// - DAO 读取失败:透传 `GarrisonError`。
    pub async fn get_token_session_with_ttl(
        &self,
        token: &str,
    ) -> GarrisonResult<Option<(TokenSession, Option<Duration>)>> {
        let key = token_key(token);
        match self.dao.get_with_ttl(&key).await? {
            Some((json, ttl)) => {
                let ts: TokenSession = serde_json::from_str(&json).map_err(|e| {
                    GarrisonError::Session(format!("session-sim-token-deserialize::{}", e))
                })?;
                // R-session-lifecycle-003: 检查 session 级过期(last_active_at + timeout < now)
                let now = Utc::now().timestamp();
                if ts.last_active_at + (self.timeout as i64) < now {
                    // 触发过期回调
                    self.trigger_expiry_listeners(&ts.login_id, token).await;
                    // 从 DAO 删除过期 session(清理)
                    if let Err(e) = self.dao.delete(&key).await {
                        let token_preview = if token.len() > 8 { &token[..8] } else { token };
                        tracing::warn!(
                            "failed to delete expired Token-Session (token={}...): {}",
                            token_preview,
                            e
                        );
                    }
                    return Ok(None);
                }
                Ok(Some((ts, ttl)))
            },
            None => Ok(None),
        }
    }

    /// 获取 Account-Session。
    ///
    /// # 参数
    /// - `login_id`: 登录主体标识(接受 `String` / `&str`)。
    ///
    /// # 返回
    /// - `Some(AccountSession)`: 账号会话存在。
    /// - `None`: 账号会话不存在或已过期。
    ///
    /// # 错误
    /// - 反序列化失败:`GarrisonError::Session`。
    /// - DAO 读取失败:透传 `GarrisonError`。
    pub async fn get_account_session(
        &self,
        login_id: impl Into<String>,
    ) -> GarrisonResult<Option<AccountSession>> {
        let login_id: String = login_id.into();
        match self.dao.get(&account_key(&login_id)).await? {
            Some(json) => {
                let as_: AccountSession = serde_json::from_str(&json).map_err(|e| {
                    GarrisonError::Session(format!("session-sim-account-deserialize::{}", e))
                })?;
                // R-session-lifecycle-003: 检查 session 级过期(last_active_at + active_timeout < now)
                let now = Utc::now().timestamp();
                if as_.last_active_at + (self.active_timeout as i64) < now {
                    // 触发过期回调(Account-Session 级过期,token 为空字符串)
                    self.trigger_expiry_listeners(&login_id, "").await;
                    // 从 DAO 删除过期 session(清理)
                    if let Err(e) = self.dao.delete(&account_key(&login_id)).await {
                        tracing::warn!(
                            "failed to delete expired Account-Session (login_id={}): {}",
                            login_id,
                            e
                        );
                    }
                    return Ok(None);
                }
                Ok(Some(as_))
            },
            None => Ok(None),
        }
    }

    /// 添加 login_id → token 映射到内存索引。
    ///
    /// 同一 token 不重复添加(去重)。在 `create`/`create_token_session` 成功后调用,
    /// 与 DAO 持久化的 `AccountSession.tokens` 保持同步。
    pub fn add_login_token(&self, login_id: &str, token: &str) {
        let mut entry = self
            .login_token_map
            .entry(login_id.to_string())
            .or_default();
        if !entry.contains(&token.to_string()) {
            entry.push(token.to_string());
        }
    }

    /// 从内存索引中移除指定 login_id 的某个 token。
    ///
    /// 当列表为空时移除整个 entry(避免内存泄漏)。在 `logout`/`kickout_by_device`
    /// (经 `logout_inner`)销毁 Token-Session 后调用。
    pub fn remove_login_token(&self, login_id: &str, token: &str) {
        if let Some(mut entry) = self.login_token_map.get_mut(login_id) {
            entry.retain(|t| t != token);
            if entry.is_empty() {
                drop(entry); // 释放 DashMap 写锁后再 remove,避免死锁
                self.login_token_map.remove(login_id);
            }
        }
    }

    /// 获取指定 login_id 的第一个 token(最旧)。
    ///
    /// 用于并发登录控制等只需取一个代表性 token 的场景。
    pub fn get_token_by_login_id(&self, login_id: &str) -> Option<String> {
        self.login_token_map
            .get(login_id)
            .and_then(|tokens| tokens.first().cloned())
    }

    /// 获取指定 login_id 的所有 token 列表(克隆)。
    ///
    /// 不存在时返回空 `Vec`。
    pub fn get_tokens_by_login_id(&self, login_id: &str) -> Vec<String> {
        self.login_token_map
            .get(login_id)
            .map(|tokens| tokens.clone())
            .unwrap_or_default()
    }

    /// 清理 `login_token_map` 中已过期或已注销的 token。
    ///
    /// 遍历内存索引中所有 login_id 的 token 列表,对每个 token 调用
    /// [`get_token_session`](Self::get_token_session) 检查存在性与过期状态:
    /// - `Ok(None)`:token session 不存在(已注销)或已过期 → 从列表中移除
    /// - `Ok(Some(_))`:token 仍有效 → 保留
    /// - `Err(e)`:DAO 读取错误 → 记录 `tracing::warn!` 并跳过该 token,继续遍历(HIGH-004)
    ///
    /// 若某个 login_id 的 token 列表清理后变空,移除该 login_id 的整个 entry
    ///(与 [`remove_login_token`](Self::remove_login_token) 行为一致)。
    ///
    /// # 返回
    /// 清理的 token 总数(仅统计成功清理的 token,DAO 失败的 token 不计入)。
    ///
    /// # 错误处理(HIGH-004)
    /// 单个 token 的 DAO 读取失败不再透传 `GarrisonError` 中断整个清理周期,
    /// 而是记录 `tracing::warn!` 日志并跳过该 token,继续处理后续 token。
    /// 这样可避免单个 DAO 故障导致整个清理周期中断,最大化清理覆盖率。
    pub async fn cleanup_expired_tokens(&self) -> GarrisonResult<usize> {
        let mut removed = 0usize;
        // 先收集所有 login_id,避免在 await 期间持有 DashMap 读锁
        let login_ids: Vec<String> = self
            .login_token_map
            .iter()
            .map(|r| r.key().clone())
            .collect();

        for login_id in login_ids {
            // 快照当前 login_id 的 token 列表
            let tokens: Vec<String> = match self.login_token_map.get(&login_id) {
                Some(t) => t.clone(),
                None => continue, // entry 已被并发移除
            };

            // 逐个检查 token 的存活性(get_token_session 会处理过期清理与回调)
            // HIGH-004: 单个 token 的 DAO 失败不再中断整个清理周期,改为 warn 日志并跳过
            let mut expired: Vec<String> = Vec::new();
            for token in &tokens {
                match self.get_token_session(token).await {
                    Ok(None) => expired.push(token.clone()),
                    Ok(Some(_)) => {}, // token 仍有效,保留
                    Err(e) => tracing::warn!(
                        "cleanup_expired_tokens: token={} DAO read failed, skipping this token: {}",
                        token,
                        e
                    ),
                }
            }

            if expired.is_empty() {
                continue; // 无需更新
            }

            removed += expired.len();
            // 同步移除过期 token(持有 DashMap 写锁的时间极短)
            // 使用 retain 保留并发期间新增的 token
            if let Some(mut entry) = self.login_token_map.get_mut(&login_id) {
                entry.retain(|x| !expired.contains(x));
                if entry.is_empty() {
                    drop(entry); // 释放写锁后再 remove,避免死锁
                    self.login_token_map.remove(&login_id);
                }
            }
        }

        Ok(removed)
    }

    /// 从 DAO 重建内存 `login_token_map`。
    ///
    /// 遍历所有 Account-Session,收集 `tokens` 字段重建内存层。
    /// 用于应用启动时恢复内存索引(重启后内存丢失,DAO 数据仍保留)。
    ///
    /// # 重建流程
    /// 1. 清空现有内存 `login_token_map`(避免与重建数据重叠)
    /// 2. `dao.keys("account:session:*")` 扫描所有 Account-Session key
    /// 3. 逐个 `dao.get` 读取并反序列化为 `AccountSession`
    /// 4. 从 key 解析 `login_id`(key 格式:`account:session:{login_id}`)
    /// 5. 提取 `tokens` 字段中的 token 字符串列表写入内存 map
    ///
    /// # 错误
    /// - DAO `keys()` 失败:透传 `GarrisonError`
    /// - DAO `get()` 失败:透传 `GarrisonError`
    ///
    /// # 反序列化失败处理
    /// 若某个 `AccountSession` 反序列化失败,记录 `tracing::warn!` 并跳过该条目
    /// (不中断重建流程),与 key 格式异常处理一致。
    ///
    /// # key 格式异常处理
    /// 若 key 不符合 `account:session:{login_id}` 模式(`strip_prefix` 返回 `None`),
    /// 记录 `tracing::warn!` 并跳过该 key(不中断重建流程)。
    #[cfg(feature = "login-token-map-persistence")]
    pub async fn rebuild_login_token_map(&self) -> GarrisonResult<()> {
        // 1. 清空现有内存 map(避免与重建数据重叠)
        self.login_token_map.clear();

        // 2. 扫描所有 Account-Session key
        let keys = self.dao.keys("account:session:*").await?;
        for key in keys {
            // 3. 从 key 解析 login_id(key 格式:account:session:{login_id})
            let Some(login_id) = key.strip_prefix("account:session:") else {
                tracing::warn!(
                    "rebuild_login_token_map: skipping key not matching account:session:{{login_id}} pattern: {}",
                    key
                );
                continue;
            };

            // 4. 读取并反序列化 AccountSession
            if let Some(json) = self.dao.get(&key).await? {
                let session: AccountSession = match serde_json::from_str(&json) {
                    Ok(s) => s,
                    Err(e) => {
                        tracing::warn!(
                            key = %key,
                            error = %e,
                            "rebuild_login_token_map: skipping AccountSession that failed deserialization"
                        );
                        continue;
                    },
                };
                // 5. 提取 tokens 字段中的 token 字符串列表,写入内存 map
                let tokens: Vec<String> = session.tokens.into_iter().map(|ti| ti.token).collect();
                self.login_token_map.insert(login_id.to_string(), tokens);
            }
        }
        Ok(())
    }

    /// 持久化添加 login_id → token 映射(双层写入:DAO + 内存)。
    ///
    /// 先写 DAO `AccountSession.tokens`(读取现有 AccountSession → 添加 token → 写回 DAO),
    /// 再写内存 `login_token_map`。DAO 失败时内存不写(返回 Err),保证双层一致性。
    ///
    /// # 参数
    /// - `login_id`: 登录主体标识。
    /// - `token`: token 字符串。
    ///
    /// # 错误
    /// - AccountSession 不存在:`GarrisonError::Session`
    /// - 序列化失败:`GarrisonError::Session`
    /// - DAO update 失败:透传 `GarrisonError`
    #[cfg(feature = "login-token-map-persistence")]
    pub async fn add_login_token_persistent(
        &self,
        login_id: &str,
        token: &str,
    ) -> GarrisonResult<()> {
        let login_id: String = login_id.to_string();
        self.with_login_lock(&login_id, async {
            // 1. 读取现有 AccountSession(必须已存在)
            let mut account = self.get_account_session(&login_id).await?.ok_or_else(|| {
                GarrisonError::Session(format!("session-account-not-found::{}", login_id))
            })?;

            // 2. 添加 token(去重)
            let now = Utc::now().timestamp();
            if !account.tokens.iter().any(|ti| ti.token == token) {
                account.tokens.push(TokenInfo {
                    token: token.to_string(),
                    created_at: now,
                    last_active_at: now,
                });
            }
            account.last_active_at = now;

            // 3. 写回 DAO(用 update 保留原 TTL)
            let json = serde_json::to_string(&account).map_err(|e| {
                GarrisonError::Session(format!("session-sim-account-serialize::{}", e))
            })?;
            self.dao.update(&account_key(&login_id), &json).await?;

            // 4. DAO 成功后写内存 login_token_map
            self.add_login_token(&login_id, token);

            Ok(())
        })
        .await
    }

    /// 持久化移除 login_id → token 映射(双层写入:DAO + 内存)。
    ///
    /// 先写 DAO `AccountSession.tokens`(读取现有 AccountSession → 移除 token → 写回 DAO),
    /// 再写内存 `login_token_map`。DAO 失败时内存不写(返回 Err),保证双层一致性。
    ///
    /// # 参数
    /// - `login_id`: 登录主体标识。
    /// - `token`: token 字符串。
    ///
    /// # 错误
    /// - AccountSession 不存在:`GarrisonError::Session`
    /// - 序列化失败:`GarrisonError::Session`
    /// - DAO update 失败:透传 `GarrisonError`
    #[cfg(feature = "login-token-map-persistence")]
    pub async fn remove_login_token_persistent(
        &self,
        login_id: &str,
        token: &str,
    ) -> GarrisonResult<()> {
        let login_id: String = login_id.to_string();
        self.with_login_lock(&login_id, async {
            // 1. 读取现有 AccountSession(必须已存在)
            let mut account = self.get_account_session(&login_id).await?.ok_or_else(|| {
                GarrisonError::Session(format!("session-account-not-found::{}", login_id))
            })?;

            // 2. 移除 token
            account.tokens.retain(|ti| ti.token != token);

            // 3. 写回 DAO(用 update 保留原 TTL)
            let json = serde_json::to_string(&account).map_err(|e| {
                GarrisonError::Session(format!("session-sim-account-serialize::{}", e))
            })?;
            self.dao.update(&account_key(&login_id), &json).await?;

            // 4. DAO 成功后写内存 login_token_map
            self.remove_login_token(&login_id, token);

            Ok(())
        })
        .await
    }

    /// 设置 Token-Session 自定义属性。
    ///
    ///
    /// # 参数
    /// - `token`: token 字符串。
    /// - `key`: 属性键。
    /// - `value`: 属性值。
    ///
    /// # 返回
    /// 成功返回 `Ok(())`。
    ///
    /// # 错误
    /// - 若 token 不存在,返回 `GarrisonError::InvalidToken`。
    pub async fn set(&self, token: &str, key: &str, value: &str) -> GarrisonResult<()> {
        self.with_token_session_lock(token, async {
            let mut ts = self.get_token_session(token).await?.ok_or_else(|| {
                GarrisonError::InvalidToken("session-token-not-found".to_string())
            })?;
            ts.attrs.insert(key.to_string(), value.to_string());
            ts.last_active_at = Utc::now().timestamp();
            let json = serde_json::to_string(&ts).map_err(|e| {
                GarrisonError::Session(format!("session-sim-token-serialize::{}", e))
            })?;
            // 用 update 保留原 TTL(不重置过期时间)
            self.dao.update(&token_key(token), &json).await?;
            Ok(())
        })
        .await
    }

    /// 获取 Token-Session 自定义属性。
    ///
    ///
    /// # 参数
    /// - `token`: token 字符串。
    /// - `key`: 属性键。
    ///
    /// # 返回
    /// - `Some(String)`: 属性存在。
    /// - `None`: token 不存在或属性不存在。
    ///
    /// # 错误
    /// - DAO 读取失败:透传 `GarrisonError`。
    pub async fn get(&self, token: &str, key: &str) -> GarrisonResult<Option<String>> {
        match self.get_token_session(token).await? {
            Some(ts) => Ok(ts.attrs.get(key).cloned()),
            None => Ok(None),
        }
    }

    /// 保存(更新)Token-Session 到 DAO(保留原 TTL)。
    ///
    /// 供 `AuthLogicDefault::switch_to` / `renew_to_equivalent` 等需要修改
    /// TokenSession 结构(非 attrs)的方法使用。用 `dao.update` 保留原 TTL。
    ///
    /// # 参数
    /// - `token`: token 字符串(用于构造存储 key)。
    /// - `ts`: 修改后的 TokenSession。
    ///
    /// # 错误
    /// - 序列化失败:`GarrisonError::Session`。
    /// - DAO 更新失败:透传 `GarrisonError`。
    pub async fn save_token_session(&self, token: &str, ts: &TokenSession) -> GarrisonResult<()> {
        let json = serde_json::to_string(ts)
            .map_err(|e| GarrisonError::Session(format!("session-sim-token-serialize::{}", e)))?;
        self.dao.update(&token_key(token), &json).await?;
        Ok(())
    }

    /// 确保 token 存在于指定 login_id 的 Account-Session 中。
    ///
    /// 供 `AuthLogicDefault::switch_to` 使用:切换身份后需将 token 添加到
    /// 目标 login_id 的 Account-Session,否则 `is_valid` 检查会失败
    ///(`is_valid` 惰性检查 Account-Session 是否存在)。
    ///
    /// 若 token 已在 Account-Session 中,则仅更新 `last_active_at`,不重复添加。
    ///
    /// # 错误
    /// - 目标 `login_id` 的 Account-Session 不存在时返回
    ///   `GarrisonError::InvalidParam`。强制调用方先通过 `login()` 等路径确保
    ///   目标 Account-Session 已存在。
    pub async fn ensure_token_in_account_session(
        &self,
        login_id: &str,
        token: &str,
    ) -> GarrisonResult<()> {
        // 修复规则 7 风格冲突(fix-refresh-race-and-test-contracts / spec R-refresh-token-002):
        // 与对称方法 `remove_token_from_account_session`(line 858-891)一致,
        // 用 `with_login_lock` 串行化对同一 login_id 的 AccountSession read-modify-write,
        // 避免与同一 login_id 的其它写操作(如 login/logout/renew)竞态。
        let login_id: String = login_id.to_string();
        self.with_login_lock(&login_id, async {
            let now = Utc::now().timestamp();
            // Account-Session 不存在时返回 Err,强制调用方先确保目标 login_id 已存在
            //(如通过 `login()` 创建)。
            let mut account = match self.get_account_session(&login_id).await? {
                Some(acc) => acc,
                None => {
                    return Err(GarrisonError::InvalidParam(format!(
                        "session-account-not-found::{}",
                        login_id
                    )));
                },
            };

            // 若 token 已存在,仅更新 last_active_at;否则添加
            if let Some(ti) = account.tokens.iter_mut().find(|t| t.token == token) {
                ti.last_active_at = now;
            } else {
                account.tokens.push(TokenInfo {
                    token: token.to_string(),
                    created_at: now,
                    last_active_at: now,
                });
            }
            account.last_active_at = now;

            let json = serde_json::to_string(&account).map_err(|e| {
                GarrisonError::Session(format!("session-sim-account-serialize::{}", e))
            })?;
            self.dao
                .set(&account_key(&login_id), &json, self.active_timeout)
                .await?;

            // H1 修复:同步内存 login_token_map(与 create_inner 行为一致)。
            // 否则 `list_devices(target)` 通过 `get_tokens_by_login_id` 读内存索引会漏掉该 token,
            // 与 `ensure_token_in_account_session` 的语义不一致。
            self.add_login_token(&login_id, token);

            Ok(())
        })
        .await
    }

    /// 从指定 login_id 的 Account-Session 中移除 token(H1 修复)。
    ///
    /// 供 `AuthLogicDefault::switch_to` 使用:切换身份后需将 token 从
    /// 原 login_id 的 Account-Session 中移除,否则会导致:
    /// 1. `list_devices(original)` 仍返回该 token(数据不一致)
    /// 2. `logout_by_login_id(original)` 会误杀已切到 target 的 token(越权踢出)
    /// 3. `enforce_max_login_count(original)` 会误算已切换的 token
    ///
    /// 与 [`Self::ensure_token_in_account_session`] 对称:一个加、一个减。
    /// 双层写入:先写 DAO `AccountSession.tokens`(用 `dao.update` 保留原 TTL),
    /// 再写内存 `login_token_map`。DAO 失败时内存不写(返回 Err),保证双层一致性。
    ///
    /// 通过 `with_login_lock` 串行化对 original Account-Session 的 read-modify-write,
    /// 避免与 original 的其它写操作(如 login/logout)竞态。
    ///
    /// # 错误
    /// - 目标 `login_id` 的 Account-Session 不存在时返回
    ///   `GarrisonError::InvalidParam`(与 `ensure_token_in_account_session` 对称)。
    /// - 序列化失败:`GarrisonError::Session`。
    /// - DAO update 失败:透传 `GarrisonError`。
    pub async fn remove_token_from_account_session(
        &self,
        login_id: &str,
        token: &str,
    ) -> GarrisonResult<()> {
        let login_id: String = login_id.to_string();
        self.with_login_lock(&login_id, async {
            // 1. 读取现有 AccountSession(必须已存在,与 ensure_token_in_account_session 对称)
            let mut account = match self.get_account_session(&login_id).await? {
                Some(acc) => acc,
                None => {
                    return Err(GarrisonError::InvalidParam(format!(
                        "session-account-not-found::{}",
                        login_id
                    )));
                },
            };

            // 2. 移除 token(若不存在则幂等无操作)
            account.tokens.retain(|ti| ti.token != token);

            // 3. 写回 DAO(用 update 保留原 TTL,不重置过期时间)
            let json = serde_json::to_string(&account).map_err(|e| {
                GarrisonError::Session(format!("session-sim-account-serialize::{}", e))
            })?;
            self.dao.update(&account_key(&login_id), &json).await?;

            // 4. DAO 成功后同步内存 login_token_map(与 logout_inner 行为一致)
            self.remove_login_token(&login_id, token);

            Ok(())
        })
        .await
    }

    /// 查询 token 的剩余 TTL。
    ///
    /// 供 `AuthLogicDefault::renew_to_equivalent` 使用:需要查询旧 token 的
    /// 剩余 TTL 以便新 token 继承相同的过期时间。
    ///
    /// # 返回
    /// - `Ok(Some(remaining))`: 键存在且设置了 TTL。
    /// - `Ok(None)`: 键不存在,或永久键(无 TTL)。
    pub async fn get_token_timeout(&self, token: &str) -> GarrisonResult<Option<Duration>> {
        self.dao.get_timeout(&token_key(token)).await
    }

    /// 创建新的 Token-Session 并指定 TTL。
    ///
    /// 供 `AuthLogicDefault::renew_to_equivalent` 使用:需要用旧 token 的
    /// 剩余 TTL 创建新 token 的 session(而非用默认 timeout)。
    ///
    /// # 参数
    /// - `token`: 新 token 字符串。
    /// - `ts`: 要存储的 TokenSession。
    /// - `ttl_seconds`: TTL 秒数(0 表示永久驻留)。
    pub async fn create_token_session_with_ttl(
        &self,
        token: &str,
        ts: &TokenSession,
        ttl_seconds: u64,
    ) -> GarrisonResult<()> {
        let json = serde_json::to_string(ts)
            .map_err(|e| GarrisonError::Session(format!("session-sim-token-serialize::{}", e)))?;
        self.dao.set(&token_key(token), &json, ttl_seconds).await?;
        Ok(())
    }

    /// 设置 Token-Session 的 TTL(供 remember_me 扩展超时使用)。
    ///
    /// 内部调用 `dao.expire` 重置 TTL。仅在 `create` 之后调用,用于将 Token-Session
    /// 的 TTL 从默认 `timeout` 扩展为 `remember_me_timeout`。
    ///
    /// # 参数
    /// - `token`: token 字符串。
    /// - `ttl_seconds`: TTL 秒数(0 表示永久驻留)。
    ///
    /// # 返回
    /// 成功返回 `Ok(())`;token 不存在时 DAO 返回错误。
    pub async fn set_token_session_ttl(&self, token: &str, ttl_seconds: u64) -> GarrisonResult<()> {
        self.dao.expire(&token_key(token), ttl_seconds).await
    }

    /// 关联 SSO ticket 到 token 会话。
    ///
    /// 将 SSO ticket 存入 Token-Session 的 `sso_ticket` 属性,
    /// 便于 logout 时联动销毁 SSO ticket。
    pub async fn link_sso_ticket(&self, token: &str, ticket: &str) -> GarrisonResult<()> {
        self.set(token, "sso_ticket", ticket).await
    }

    /// 查询 token 关联的 SSO ticket。
    pub async fn get_sso_ticket(&self, token: &str) -> GarrisonResult<Option<String>> {
        self.get(token, "sso_ticket").await
    }

    /// 关联 OAuth2 access_token 到 token 会话。
    ///
    /// 将 OAuth2 access_token 存入 Token-Session 的 `oauth2_access_token` 属性,
    /// 便于业务方在持有内部 token 时访问 OAuth2 资源服务器。
    pub async fn link_oauth2_token(&self, token: &str, access_token: &str) -> GarrisonResult<()> {
        self.set(token, "oauth2_access_token", access_token).await
    }

    /// 查询 token 关联的 OAuth2 access_token。
    pub async fn get_oauth2_token(&self, token: &str) -> GarrisonResult<Option<String>> {
        self.get(token, "oauth2_access_token").await
    }

    /// 关联临时凭证 key 到 token 会话。
    ///
    /// 将临时凭证的完整 dao key 存入 Token-Session 的 `temp_credential_key` 属性。
    /// `is_valid` 会检查该 key 是否仍存在于 dao,若已被删除则会话失效。
    pub async fn link_temp_credential(&self, token: &str, temp_key: &str) -> GarrisonResult<()> {
        self.set(token, "temp_credential_key", temp_key).await
    }

    /// 查询 token 关联的临时凭证 key()。
    pub async fn get_temp_credential(&self, token: &str) -> GarrisonResult<Option<String>> {
        self.get(token, "temp_credential_key").await
    }

    /// 设置 Token-Session 的设备标识。
    ///
    /// 业务方在 `login` 后调用此方法关联 token 与设备,便于后续 `kickout_by_device` 按设备踢出。
    ///
    /// # 参数
    /// - `token`: token 字符串。
    /// - `device`: 设备标识(如 "web-chrome"/"mobile-ios"/"api-client")。
    ///
    /// # 返回
    /// 成功返回 `Ok(())`。
    ///
    /// # 错误
    /// - 若 token 不存在,返回 `GarrisonError::InvalidToken`。
    /// - 序列化失败:`GarrisonError::Session`。
    /// - DAO 更新失败:透传 `GarrisonError`。
    pub async fn set_device(&self, token: &str, device: &str) -> GarrisonResult<()> {
        self.with_token_session_lock(token, async {
            let mut ts = self.get_token_session(token).await?.ok_or_else(|| {
                GarrisonError::InvalidToken("session-token-not-found".to_string())
            })?;
            ts.device = Some(device.to_string());
            ts.last_active_at = Utc::now().timestamp();
            let json = serde_json::to_string(&ts).map_err(|e| {
                GarrisonError::Session(format!("session-sim-token-serialize::{}", e))
            })?;
            self.dao.update(&token_key(token), &json).await?;
            Ok(())
        })
        .await
    }

    /// 设置 per-token 动态 active timeout(秒)。
    ///
    /// 读取现有 TokenSession,设置 `dynamic_active_timeout` 字段后写回 DAO。
    /// 用 `dao.update` 保留原 TTL(不重置过期时间)。
    ///
    /// # 参数
    /// - `token`: 待设置的 token 字符串。
    /// - `timeout_secs`: 超时秒数。`-1` 表示永不过期,`0` 非法。
    ///
    /// # 错误
    /// - `timeout_secs=0`:`GarrisonError::InvalidParam`。
    /// - token 不存在:`GarrisonError::InvalidToken`。
    /// - 序列化失败:`GarrisonError::Session`。
    /// - DAO 更新失败:透传 `GarrisonError`。
    #[cfg(feature = "dynamic-active-timeout")]
    pub async fn set_active_timeout(&self, token: &str, timeout_secs: i64) -> GarrisonResult<()> {
        if timeout_secs == 0 {
            return Err(GarrisonError::InvalidParam(
                "session-timeout-secs-invalid".to_string(),
            ));
        }
        self.with_token_session_lock(token, async {
            let mut ts = self.get_token_session(token).await?.ok_or_else(|| {
                GarrisonError::InvalidToken(format!("session-token-not-found::{}", token))
            })?;
            ts.dynamic_active_timeout = Some(timeout_secs);
            ts.last_active_at = Utc::now().timestamp();
            let json = serde_json::to_string(&ts).map_err(|e| {
                GarrisonError::Session(format!("session-sim-token-serialize::{}", e))
            })?;
            self.dao.update(&token_key(token), &json).await?;
            Ok(())
        })
        .await
    }

    /// 检查 token 是否有效(Token-Session 存在且 Account-Session 未过期)。
    ///
    /// 惰性检查 Account-Session 是否存在——若 Account-Session 已被 oxcache TTL 清理,
    /// 即使 Token-Session 仍存在,也视为无效(spec scenario "Activity 超时")。
    ///
    /// 注意:此方法只读,不更新 last_active_at。活跃续期请调用 `touch`。
    ///
    /// # 参数
    /// - `token`: 待校验的 token 字符串。
    ///
    /// # 返回
    /// - `true`: Token-Session 存在且 Account-Session 未过期。
    /// - `false`: token 不存在、Account-Session 已过期、或(启用 `dynamic-active-timeout` 时)
    ///   per-token 动态活跃超时已到期。
    ///
    /// # 错误
    /// - DAO 读取失败:透传 `GarrisonError`。
    pub async fn is_valid(&self, token: &str) -> GarrisonResult<bool> {
        let ts = match self.get_token_session(token).await? {
            Some(ts) => ts,
            None => return Ok(false),
        };
        // T011: per-token 动态活跃超时检查
        // 优先使用 token_session.dynamic_active_timeout,None 时回退到全局 active_timeout
        #[cfg(feature = "dynamic-active-timeout")]
        {
            let effective_active_timeout = ts
                .dynamic_active_timeout
                .unwrap_or(self.active_timeout as i64);
            // -1 表示永不过期(与全局 active_timeout 语义一致),负值跳过活跃超时检查
            let now = Utc::now().timestamp();
            if effective_active_timeout >= 0 && ts.last_active_at + effective_active_timeout < now {
                return Ok(false);
            }
        }
        // 惰性检查 Account-Session 是否存在
        if self.get_account_session(&ts.login_id).await?.is_none() {
            return Ok(false);
        }
        // 临时凭证过期联动。
        // 若 Token-Session 含 temp_credential_key 属性,检查该 key 是否仍存在于 dao;
        // 临时凭证过期后 token 立即失效,不论 token 自身 timeout 是否到期。
        if let Some(temp_key) = ts.attrs.get("temp_credential_key") {
            if self.dao.get(temp_key).await?.is_none() {
                return Ok(false);
            }
        }
        Ok(true)
    }

    /// 活跃续期:更新 last_active_at 并重置 TTL。
    ///
    /// 同时更新 Token-Session 与 Account-Session 的 last_active_at 和 TTL。
    ///
    /// # 参数
    /// - `token`: 待续期的 token 字符串。
    ///
    /// # 返回
    /// 成功返回 `Ok(())`。
    ///
    /// # 错误
    /// - 若 token 不存在,返回 `GarrisonError::InvalidToken`。
    pub async fn touch(&self, token: &str) -> GarrisonResult<()> {
        self.with_token_session_lock(token, async {
            let mut ts = self.get_token_session(token).await?.ok_or_else(|| {
                GarrisonError::InvalidToken(format!("session-token-not-found::{}", token))
            })?;
            let now = Utc::now().timestamp();
            ts.last_active_at = now;
            let json = serde_json::to_string(&ts).map_err(|e| {
                GarrisonError::Session(format!("session-sim-token-serialize::{}", e))
            })?;
            // 更新值 + 重置 TTL(用 set 覆盖,重置 TTL)
            self.dao.set(&token_key(token), &json, self.timeout).await?;

            // 同时更新 Account-Session 的 last_active_at + 对应 TokenInfo + 重置 TTL
            if let Some(mut account) = self.get_account_session(&ts.login_id).await? {
                account.last_active_at = now;
                for ti in &mut account.tokens {
                    if ti.token == token {
                        ti.last_active_at = now;
                    }
                }
                let account_json = serde_json::to_string(&account).map_err(|e| {
                    GarrisonError::Session(format!("session-sim-account-serialize::{}", e))
                })?;
                self.dao
                    .set(
                        &account_key(&ts.login_id),
                        &account_json,
                        self.active_timeout,
                    )
                    .await?;
            }
            Ok(())
        })
        .await
    }

    /// 主动续期:重置过期时间为完整 timeout。
    ///
    ///
    /// # 参数
    /// - `token`: 待续期的 token 字符串。
    ///
    /// # 返回
    /// 成功返回 `Ok(())`。
    ///
    /// # 错误
    /// - 若 token 不存在,返回 `GarrisonError::InvalidToken`(spec scenario "续期不存在的 token")。
    pub async fn renew(&self, token: &str) -> GarrisonResult<()> {
        // 检查 token 存在(Fail Loud)
        if self.get_token_session(token).await?.is_none() {
            return Err(GarrisonError::InvalidToken(format!(
                "session-token-not-found::{}",
                token
            )));
        }
        // renew 等同于 touch:重置 TTL + 更新 last_active_at
        self.touch(token).await
    }

    /// 登出指定 token。
    ///
    ///
    /// 删除 Token-Session,并从 Account-Session 的 token 列表移除该 token。
    /// 若列表为空,Account-Session 保留(不删除,保留历史)。
    ///
    /// # 参数
    /// - `token`: 待登出的 token 字符串。
    ///
    /// # 返回
    /// 成功返回 `Ok(())`;token 不存在时幂等返回 `Ok(())`。
    ///
    /// # 错误
    /// - 序列化 `AccountSession` 失败:`GarrisonError::Session`。
    /// - DAO 删除/更新失败:透传 `GarrisonError`。
    pub async fn logout(&self, token: &str) -> GarrisonResult<()> {
        // 匿名 token 路由到 logout_anon(key 空间隔离)
        // InvalidParam(空 token / 超长 token)降级为非匿名,保持 logout 幂等契约
        #[cfg(feature = "anonymous-session")]
        {
            let is_anon = match self.is_anon(token).await {
                Ok(v) => v,
                Err(GarrisonError::InvalidParam(_)) => false,
                Err(e) => return Err(e),
            };
            if is_anon {
                return self.logout_anon(token).await;
            }
        }

        // 先读取 token session 获取 login_id(不在锁内,避免锁持有时间过长)
        let ts = self.get_token_session(token).await?;
        match ts {
            Some(ts) => {
                // 获取 per-login_id 锁,保护 Account-Session read-modify-write(R-002, R-004)
                let login_id = ts.login_id.clone();
                self.with_login_lock(
                    &login_id,
                    async move { self.logout_inner(token, &ts).await },
                )
                .await
            },
            None => {
                // token 不存在,幂等删除
                self.dao.delete(&token_key(token)).await
            },
        }
    }

    /// logout 内部实现(不获取 per-login_id 锁)。
    ///
    /// 供 `logout`(获取锁后调用)和 `kickout_by_device`(已持有锁)复用,避免死锁。
    ///
    /// # pub(crate) 可见性说明
    /// 暴露给 `stp::session::AuthLogicDefault::enforce_max_login_count` 使用,
    /// 在 `with_login_lock` 已持锁状态下调用 `logout_inner`(不重入锁),避免死锁
    /// (fix-refresh-race-and-test-contracts / T015)。
    ///
    /// # 调用契约
    /// **调用方必须已持有 `with_login_lock(login_id)` 锁**,否则会破坏 Account-Session
    /// read-modify-write 序列的原子性。`logout` 方法已内部封装了获取锁 + 调用本方法的流程,
    /// 外部调用方应优先使用 `logout`,仅在已持锁场景下使用本方法。
    pub(crate) async fn logout_inner(&self, token: &str, ts: &TokenSession) -> GarrisonResult<()> {
        // 删除 Token-Session
        self.dao.delete(&token_key(token)).await?;

        // SSO ticket 销毁联动。
        // 若 Token-Session 含 sso_ticket 属性,删除 dao 中的 `garrison:sso:ticket:<ticket>` key。
        // 失败仅记录不中断主流程。
        if let Some(ticket) = ts.attrs.get("sso_ticket") {
            let sso_key = format!("garrison:sso:ticket:{}", ticket);
            if let Err(e) = self.dao.delete(&sso_key).await {
                tracing::warn!(
                    "logout failed to delete SSO ticket (key={}): {}",
                    sso_key,
                    e
                );
            }
        }

        // 从 Account-Session 移除该 token
        if let Some(mut account) = self.get_account_session(&ts.login_id).await? {
            account.tokens.retain(|ti| ti.token != token);
            // spec: 若列表为空,Account-Session 标记为空(但不删除,保留历史)
            let account_json = serde_json::to_string(&account).map_err(|e| {
                GarrisonError::Session(format!("session-sim-account-serialize::{}", e))
            })?;
            // 用 update 保留原 TTL(不重置 Account-Session 的过期时间)
            self.dao
                .update(&account_key(&ts.login_id), &account_json)
                .await?;
        }

        // 同步移除内存索引中的该 token(login_token_map)
        self.remove_login_token(&ts.login_id, token);

        Ok(())
    }

    /// 按账号登出:删除所有关联 token + Account-Session。
    ///
    /// 对应 `logout(login_id)` 语义。
    ///
    /// # 参数
    /// - `login_id`: 登录主体标识(接受 `String` / `&str`)。
    ///
    /// # 返回
    /// 成功返回 `Ok(())`。
    ///
    /// # 错误
    /// - DAO 删除失败:透传 `GarrisonError`。
    pub async fn logout_by_login_id(&self, login_id: impl Into<String>) -> GarrisonResult<()> {
        let login_id: String = login_id.into();
        // 获取 per-login_id 锁,保护 Account-Session 读-删序列(R-003, R-004)
        self.with_login_lock(&login_id, async {
            if let Some(account) = self.get_account_session(&login_id).await? {
                for ti in &account.tokens {
                    self.dao.delete(&token_key(&ti.token)).await?;
                }
            }
            self.dao.delete(&account_key(&login_id)).await?;
            // 移除整个 login_id 的内存索引(所有 token 已销毁)
            self.login_token_map.remove(&login_id);
            Ok(())
        })
        .await
    }

    /// 按设备踢出。
    ///
    /// 踢出指定 login_id 在指定 device 上的所有 token session。
    /// 不影响该 login_id 在其他 device 上的 session。
    ///
    /// # 参数
    /// - `login_id`: 登录主体标识(接受 `String` / `&str`)。
    /// - `device`: 设备标识。
    ///
    /// # 返回
    /// 成功返回 `Ok(())`。device 不存在或无匹配 token 时幂等返回 `Ok(())`。
    ///
    /// # 事件广播(R-002)
    /// 若注入了 `listener_manager`,每个被踢出的 token 触发一个 `GarrisonEvent::Kickout` 事件,
    /// `reason` 字段格式为 `"kicked by device: <device>"`。
    ///
    /// # 错误
    /// - DAO 读取/删除失败:透传 `GarrisonError`。
    ///
    /// # account session 维护(R-003)
    /// 踢出后 account session 的 tokens 列表移除被踢出的 token,保留其他 device 的 token。
    pub async fn kickout_by_device(
        &self,
        login_id: impl Into<String>,
        device: &str,
    ) -> GarrisonResult<()> {
        let login_id: String = login_id.into();
        // 获取 per-login_id 锁,保护 Account-Session 读-踢序列(R-009)
        // 内部调用 logout_inner(不获取锁),避免死锁
        self.with_login_lock(&login_id, async {
            let account = match self.get_account_session(&login_id).await? {
                Some(a) => a,
                None => return Ok(()), // 幂等:account session 不存在
            };

            // 收集需要踢出的 token(同时获取 TokenSession 供 logout_inner 使用)
            let mut kicked: Vec<(String, TokenSession)> = Vec::new();
            for ti in &account.tokens {
                if let Some(ts) = self.get_token_session(&ti.token).await? {
                    if ts.device.as_deref() == Some(device) {
                        kicked.push((ti.token.clone(), ts));
                    }
                }
            }

            if kicked.is_empty() {
                return Ok(()); // 幂等:无匹配 device
            }

            // 逐个 logout_inner(不获取锁,因为已持有 login_id 锁)
            for (token, ts) in &kicked {
                self.logout_inner(token, ts).await?;
            }

            // 广播 Kickout 事件(R-002)
            #[cfg(feature = "listener")]
            if let Some(mgr) = &self.listener_manager {
                let reason = format!("kicked by device: {}", device);
                for (token, _) in &kicked {
                    mgr.broadcast(&crate::listener::GarrisonEvent::Kickout {
                        login_id: login_id.clone(),
                        token: token.clone(),
                        reason: reason.clone(),
                        request_context: None,
                    })
                    .await;
                }
            }

            Ok(())
        })
        .await
    }
}

// ============================================================================
// 匿名 Session 委托方法(anonymous-session feature)
// ============================================================================

#[cfg(feature = "anonymous-session")]
impl GarrisonSession {
    /// 获取匿名 Token-Session,不存在则创建。
    ///
    /// 委托到 [`anon::get_anon_token_session`]。
    ///
    /// # 参数
    /// - `token`: token 字符串。
    ///
    /// # 返回
    /// 匿名 TokenSession(`login_id = ""`, `is_anon = true`)。
    pub async fn get_anon_token_session(&self, token: &str) -> GarrisonResult<TokenSession> {
        anon::get_anon_token_session(self, token).await
    }

    /// 判断 token 是否为匿名 Session。
    ///
    /// 委托到 [`anon::is_anon`]。
    ///
    /// # 参数
    /// - `token`: token 字符串。
    ///
    /// # 返回
    /// `true` 表示匿名 Session,`false` 表示非匿名。
    pub async fn is_anon(&self, token: &str) -> GarrisonResult<bool> {
        anon::is_anon(self, token).await
    }

    /// 注销匿名 Session。
    ///
    /// 委托到 [`anon::logout_anon`]。不存在的 anon token 返回 `Ok(())`(幂等)。
    ///
    /// # 参数
    /// - `token`: token 字符串。
    pub async fn logout_anon(&self, token: &str) -> GarrisonResult<()> {
        anon::logout_anon(self, token).await
    }
}

// ============================================================================
// 会话搜索委托方法(session-search feature)
// ============================================================================

#[cfg(feature = "session-search")]
impl GarrisonSession {
    /// 按 token 值搜索 Token-Session。
    ///
    /// 委托到 [`search::search_token_value`]。排除匿名 Session,空 `keyword` 匹配所有。
    ///
    /// # 参数
    /// - `keyword`: 搜索关键字(空字符串匹配所有)。
    /// - `start`: 分页偏移量(0-based)。
    /// - `size`: 返回数量上限。
    /// - `sort_type`: 排序方式。
    ///
    /// # 返回
    /// 匹配的 token 值列表。
    pub async fn search_token_value(
        &self,
        keyword: &str,
        start: usize,
        size: usize,
        sort_type: SearchSortType,
    ) -> GarrisonResult<Vec<String>> {
        search::search_token_value(self, keyword, start, size, sort_type).await
    }

    /// 按 login_id 搜索 Account-Session。
    ///
    /// 委托到 [`search::search_session_id`]。空 `keyword` 匹配所有。
    ///
    /// # 参数
    /// - `keyword`: 搜索关键字(空字符串匹配所有)。
    /// - `start`: 分页偏移量(0-based)。
    /// - `size`: 返回数量上限。
    /// - `sort_type`: 排序方式。
    ///
    /// # 返回
    /// 匹配的 login_id 列表。
    pub async fn search_session_id(
        &self,
        keyword: &str,
        start: usize,
        size: usize,
        sort_type: SearchSortType,
    ) -> GarrisonResult<Vec<String>> {
        search::search_session_id(self, keyword, start, size, sort_type).await
    }

    /// 按 login_id 搜索 Token-Session 的 token。
    ///
    /// 委托到 [`search::search_token_session_id`]。排除匿名 Session,空 `keyword` 匹配所有。
    ///
    /// # 参数
    /// - `keyword`: 搜索关键字(空字符串匹配所有)。
    /// - `start`: 分页偏移量(0-based)。
    /// - `size`: 返回数量上限。
    /// - `sort_type`: 排序方式。
    ///
    /// # 返回
    /// 匹配的 token 值列表。
    pub async fn search_token_session_id(
        &self,
        keyword: &str,
        start: usize,
        size: usize,
        sort_type: SearchSortType,
    ) -> GarrisonResult<Vec<String>> {
        search::search_token_session_id(self, keyword, start, size, sort_type).await
    }
}

// ============================================================================
// 单元测试:覆盖 impl.rs 中尚未被 session::tests 覆盖的路径
// ============================================================================

#[cfg(test)]
mod tests {
    use super::*;
    use crate::dao::tests::MockDao;
    use chrono::Utc;
    use std::collections::HashMap;
    use std::sync::Arc;
    use std::time::Duration;

    /// 辅助函数:创建带 MockDao 的 GarrisonSession(与 session::tests 中保持一致)。
    fn make_session(timeout: u64, active_timeout: u64) -> (Arc<MockDao>, GarrisonSession) {
        let dao = Arc::new(MockDao::new());
        let session = GarrisonSession::new(dao.clone(), timeout, active_timeout);
        (dao, session)
    }

    // ----------------------------------------------------------------
    // update_last_active_at:与 update_last_active 的差异(接受外部时间戳)
    // ----------------------------------------------------------------

    /// 验证 `update_last_active_at` 写入指定时间戳,而非当前时间。
    ///
    /// 覆盖与 `update_last_active` 的差异点:支持可注入时钟(MockClock)。
    #[test]
    fn update_last_active_at_sets_specified_timestamp() {
        let (_dao, session) = make_session(3600, 86400);
        // 固定时间戳,与当前时间无关
        let specified_ts: i64 = 1_700_000_000_000;
        session.update_last_active_at("user_injected", specified_ts);

        let stored = session.get_last_active("user_injected");
        assert_eq!(
            stored,
            Some(specified_ts),
            "update_last_active_at 应写入指定时间戳而非当前时间"
        );
    }

    // ----------------------------------------------------------------
    // add_login_token:去重逻辑
    // ----------------------------------------------------------------

    /// 验证 `add_login_token` 重复添加同一 token 不会复制条目。
    ///
    /// 覆盖 `if !entry.contains(...)` 分支为 false 时的跳过路径。
    #[test]
    fn add_login_token_deduplicates_existing_token() {
        let (_dao, session) = make_session(3600, 86400);
        session.add_login_token("user1", "token1");
        session.add_login_token("user1", "token1"); // 重复
        session.add_login_token("user1", "token2");

        let tokens = session.get_tokens_by_login_id("user1");
        assert_eq!(
            tokens,
            vec!["token1".to_string(), "token2".to_string()],
            "重复 token 不应重复计入列表"
        );
    }

    // ----------------------------------------------------------------
    // ensure_token_in_account_session:token 已存在仅更新 last_active_at
    // ----------------------------------------------------------------

    /// 验证 `ensure_token_in_account_session` 在 token 已存在时不重复添加,
    /// 仅更新对应 TokenInfo 与 AccountSession 的 last_active_at。
    ///
    /// 覆盖 `if let Some(ti) = account.tokens.iter_mut().find(...)` 分支。
    #[tokio::test]
    async fn ensure_token_in_account_session_updates_last_active_for_existing_token() {
        let (_dao, session) = make_session(3600, 86400);
        session.create("1001", "T1").await.unwrap();

        let original = session.get_account_session("1001").await.unwrap().unwrap();
        let original_active = original.last_active_at;

        // 跨秒等待,确保 last_active_at(秒级精度)有可测差异
        tokio::time::sleep(Duration::from_secs(2)).await;

        session
            .ensure_token_in_account_session("1001", "T1")
            .await
            .unwrap();

        let after = session.get_account_session("1001").await.unwrap().unwrap();
        assert_eq!(
            after.tokens.len(),
            1,
            "token 已存在时不应重复添加到 tokens 列表"
        );
        assert!(
            after.last_active_at > original_active,
            "ensure 已存在的 token 应更新 last_active_at(原={}, 新={})",
            original_active,
            after.last_active_at
        );
        // 对应 TokenInfo 的 last_active_at 也应更新
        let ti = after.tokens.iter().find(|t| t.token == "T1").unwrap();
        assert!(
            ti.last_active_at > original_active,
            "TokenInfo.last_active_at 也应被更新"
        );
    }

    // ----------------------------------------------------------------
    // ensure_token_in_account_session:Account-Session 不存在时拒绝
    // ----------------------------------------------------------------
    // Account-Session 不存在时返回 Err,强制调用方先确保目标 login_id 已存在。

    /// 验证 `ensure_token_in_account_session` 在目标 login_id 的
    /// Account-Session 不存在时返回 `Err`,而不是静默创建新会话。
    ///
    /// 覆盖 `match None` 分支。
    #[tokio::test]
    async fn ensure_token_in_account_session_returns_error_for_nonexistent_login_id() {
        let (_dao, session) = make_session(3600, 86400);
        // 直接调用 ensure,不预先 create —— 模拟 switch_to 切到不存在 login_id
        let result = session
            .ensure_token_in_account_session("nonexistent-2002", "T1")
            .await;

        assert!(
            result.is_err(),
            "Account-Session 不存在时应返回 Err,避免静默创建导致提权。实际: {:?}",
            result
        );
        // 验证未创建任何 Account-Session(无副作用)
        let after = session
            .get_account_session("nonexistent-2002")
            .await
            .unwrap();
        assert!(
            after.is_none(),
            "应未创建 Account-Session(避免数据不一致),实际: {:?}",
            after
        );
    }

    // ----------------------------------------------------------------
    // save_token_session:保留原 TTL 持久化修改
    // ----------------------------------------------------------------

    /// 验证 `save_token_session` 通过 `dao.update` 持久化修改后的 TokenSession,
    /// 保留原 TTL(不重置过期时间)。
    ///
    /// 覆盖 `save_token_session` 调用路径。
    #[tokio::test]
    async fn save_token_session_persists_modified_session() {
        let (_dao, session) = make_session(3600, 86400);
        session.create("1001", "T1").await.unwrap();

        // 读取并修改 TokenSession
        let mut ts = session.get_token_session("T1").await.unwrap().unwrap();
        ts.attrs
            .insert("custom_key".to_string(), "custom_value".to_string());

        // save_token_session 用 update(保留原 TTL)
        session.save_token_session("T1", &ts).await.unwrap();

        // 验证修改已持久化
        let reloaded = session.get_token_session("T1").await.unwrap().unwrap();
        assert_eq!(
            reloaded.attrs.get("custom_key").map(|s| s.as_str()),
            Some("custom_value"),
            "save_token_session 后自定义属性应持久化"
        );
    }

    // ----------------------------------------------------------------
    // create_token_session_with_ttl + get_token_timeout + set_token_session_ttl
    // ----------------------------------------------------------------

    /// 验证 TTL 相关三个方法的往返:
    /// - `create_token_session_with_ttl`:用指定 TTL 创建 TokenSession
    /// - `get_token_timeout`:查询剩余 TTL
    /// - `set_token_session_ttl`:重置 TTL
    ///
    /// 覆盖这三个方法未被现有测试覆盖的调用路径。
    #[tokio::test]
    async fn ttl_methods_roundtrip() {
        let (_dao, session) = make_session(3600, 86400);
        session.create("1001", "T1").await.unwrap();

        // 用指定 TTL=600 创建新 TokenSession(构造最小可用 TokenSession)
        let new_ts = TokenSession {
            token: "T2".to_string(),
            login_id: "1001".to_string(),
            created_at: Utc::now().timestamp(),
            last_active_at: Utc::now().timestamp(),
            attrs: HashMap::new(),
            device: None,
            ip: None,
            user_agent: None,
            safe_services: HashMap::new(),
            #[cfg(feature = "dynamic-active-timeout")]
            dynamic_active_timeout: None,
            #[cfg(feature = "anonymous-session")]
            is_anon: false,
        };
        session
            .create_token_session_with_ttl("T2", &new_ts, 600)
            .await
            .unwrap();

        // get_token_timeout 应返回 Some(键存在且设置了 TTL)
        let ttl = session.get_token_timeout("T2").await.unwrap();
        assert!(
            ttl.is_some(),
            "create_token_session_with_ttl 设置了 TTL=600,应返回 Some"
        );
        let ttl_secs = ttl.unwrap().as_secs();
        // 容差:执行时间内可能消耗少量秒,应仍在 590~600 之间
        assert!(
            (590..=600).contains(&ttl_secs),
            "TTL 应接近 600 秒,实际: {}",
            ttl_secs
        );

        // set_token_session_ttl 重置为 1200 秒
        session.set_token_session_ttl("T2", 1200).await.unwrap();
        let ttl_after = session
            .get_token_timeout("T2")
            .await
            .unwrap()
            .expect("set_token_session_ttl 后应仍返回 Some");
        let ttl_after_secs = ttl_after.as_secs();
        assert!(
            (1190..=1200).contains(&ttl_after_secs),
            "set_token_session_ttl 后 TTL 应接近 1200 秒,实际: {}",
            ttl_after_secs
        );
    }

    // ----------------------------------------------------------------
    // with_anon_session_timeout builder(anonymous-session feature)
    // ----------------------------------------------------------------

    /// 验证 `with_anon_session_timeout` builder 设置 `anon_session_timeout` 字段,
    /// 且 `new` 默认使用 `DEFAULT_ANON_SESSION_TIMEOUT_SECS`。
    ///
    /// 覆盖 anonymous-session feature 下的 builder 方法与默认值。
    #[cfg(feature = "anonymous-session")]
    #[test]
    fn with_anon_session_timeout_sets_field() {
        use crate::config::DEFAULT_ANON_SESSION_TIMEOUT_SECS;
        use crate::dao::GarrisonDao;

        let dao: Arc<dyn GarrisonDao> = Arc::new(MockDao::new());
        // 默认值应为 DEFAULT_ANON_SESSION_TIMEOUT_SECS(1800 秒 = 30 分钟)
        let default_session = GarrisonSession::new(dao.clone(), 3600, 86400);
        assert_eq!(
            default_session.anon_session_timeout, DEFAULT_ANON_SESSION_TIMEOUT_SECS,
            "new 默认应使用 DEFAULT_ANON_SESSION_TIMEOUT_SECS"
        );

        // builder 覆盖默认值
        let session = GarrisonSession::new(dao, 3600, 86400).with_anon_session_timeout(99);
        assert_eq!(
            session.anon_session_timeout, 99,
            "with_anon_session_timeout 应设置字段为指定值"
        );
    }

    // ----------------------------------------------------------------
    // with_token_session_lock:per-token 锁(CRIT-001 / FMEA #5)
    // ----------------------------------------------------------------

    /// 验证 `with_token_session_lock` 串行化同一 token 的并发操作。
    ///
    /// 两个并发任务对同一 token 调用 `with_token_session_lock`,应串行执行
    ///(第二个任务在第一个释放锁后才开始),通过共享计数器验证顺序。
    #[tokio::test(flavor = "multi_thread", worker_threads = 4)]
    async fn with_token_session_lock_serializes_same_token() {
        use std::sync::atomic::{AtomicUsize, Ordering};

        let (_dao, session) = make_session(3600, 86400);
        let session = Arc::new(session);
        let counter = Arc::new(AtomicUsize::new(0));
        let max_concurrent = Arc::new(AtomicUsize::new(0));

        let counter_a = counter.clone();
        let max_a = max_concurrent.clone();
        let session_a = session.clone();
        let h1 = tokio::spawn(async move {
            session_a
                .with_token_session_lock("T1", async {
                    let cur = counter_a.fetch_add(1, Ordering::SeqCst) + 1;
                    max_a.fetch_max(cur, Ordering::SeqCst);
                    tokio::time::sleep(Duration::from_millis(50)).await;
                    counter_a.fetch_sub(1, Ordering::SeqCst);
                })
                .await
        });

        let counter_b = counter.clone();
        let max_b = max_concurrent.clone();
        let session_b = session.clone();
        let h2 = tokio::spawn(async move {
            session_b
                .with_token_session_lock("T1", async {
                    let cur = counter_b.fetch_add(1, Ordering::SeqCst) + 1;
                    max_b.fetch_max(cur, Ordering::SeqCst);
                    tokio::time::sleep(Duration::from_millis(50)).await;
                    counter_b.fetch_sub(1, Ordering::SeqCst);
                })
                .await
        });

        h1.await.unwrap();
        h2.await.unwrap();

        // 串行执行时,counter 最大值为 1(不会有两个任务同时进入临界区)
        assert_eq!(
            max_concurrent.load(Ordering::SeqCst),
            1,
            "with_token_session_lock 应串行化同一 token 的并发操作"
        );
    }

    // ----------------------------------------------------------------
    // check_hover_timeout:4 个分支
    // ----------------------------------------------------------------

    /// `check_hover_timeout` 在 `hover_timeout_secs <= 0` 时返回 true(不启用悬停检查)。
    ///
    /// 覆盖 `if hover_timeout_secs <= 0 { return true; }` 分支。
    #[test]
    fn check_hover_timeout_disabled_when_secs_le_zero() {
        let (_dao, session) = make_session(3600, 86400);
        // 不更新 last_active_time,直接检查
        assert!(
            session.check_hover_timeout("user1", 0),
            "hover_timeout_secs=0 应返回 true(不启用)"
        );
        assert!(
            session.check_hover_timeout("user1", -1),
            "hover_timeout_secs=-1 应返回 true(不启用)"
        );
    }

    /// `check_hover_timeout` 在 `last_active_time` 不存在时返回 true(首次 check_login 不踢出)。
    ///
    /// 覆盖 `None => true` 分支。
    #[test]
    fn check_hover_timeout_returns_true_when_no_active_record() {
        let (_dao, session) = make_session(3600, 86400);
        // 不调用 update_last_active,直接检查
        assert!(
            session.check_hover_timeout("never_seen_user", 300),
            "无活跃记录时应返回 true(不踢出首次 check_login)"
        );
    }

    /// `check_hover_timeout` 在活跃时间未超时时返回 true。
    ///
    /// 覆盖 `Some(last) => elapsed <= timeout => true` 分支。
    #[test]
    fn check_hover_timeout_returns_true_when_within_timeout() {
        let (_dao, session) = make_session(3600, 86400);
        // 设置当前时间为最近活跃
        let now = chrono::Utc::now().timestamp_millis();
        session.update_last_active_at("active_user", now);

        // 300 秒悬停超时,刚刚活跃(0 秒前),不应踢出
        assert!(
            session.check_hover_timeout("active_user", 300),
            "刚活跃的用户在悬停超时内应返回 true"
        );
    }

    /// `check_hover_timeout` 在悬停超时后返回 false(应踢出)。
    ///
    /// 覆盖 `Some(last) => elapsed > timeout => false` 分支。
    #[test]
    fn check_hover_timeout_returns_false_when_hover_expired() {
        let (_dao, session) = make_session(3600, 86400);
        // 设置活跃时间为 600 秒前(超过 300 秒悬停超时)
        let old_time = chrono::Utc::now().timestamp_millis() - 600_000; // 600 秒前
        session.update_last_active_at("idle_user", old_time);

        // 300 秒悬停超时,600 秒前活跃,应踢出
        assert!(
            !session.check_hover_timeout("idle_user", 300),
            "悬停超时后应返回 false(应踢出)"
        );
    }

    // ----------------------------------------------------------------
    // update_last_active + get_last_active(nonexistent login_id)
    // ----------------------------------------------------------------

    /// `update_last_active` 写入当前时间戳,`get_last_active` 读取它。
    /// `get_last_active` 对不存在的 login_id 返回 None。
    #[test]
    fn update_last_active_and_get_last_active_roundtrip() {
        let (_dao, session) = make_session(3600, 86400);
        let before = chrono::Utc::now().timestamp_millis();
        session.update_last_active("roundtrip_user");
        let after = chrono::Utc::now().timestamp_millis();

        let stored = session.get_last_active("roundtrip_user");
        assert!(
            stored.is_some(),
            "update_last_active 后应能 get_last_active"
        );
        let ts = stored.unwrap();
        assert!(
            ts >= before && ts <= after,
            "存储的时间戳应在调用前后的范围内"
        );

        // 不存在的 login_id 返回 None
        assert!(
            session.get_last_active("nonexistent_user").is_none(),
            "不存在的 login_id 应返回 None"
        );
    }

    // ----------------------------------------------------------------
    // remove_login_token:entry 为空时移除整个 entry
    // ----------------------------------------------------------------

    /// `remove_login_token` 移除最后一个 token 后整个 entry 被删除。
    #[test]
    fn remove_login_token_removes_entry_when_empty() {
        let (_dao, session) = make_session(3600, 86400);
        session.add_login_token("user1", "token1");
        assert_eq!(
            session.get_tokens_by_login_id("user1"),
            vec!["token1".to_string()]
        );

        session.remove_login_token("user1", "token1");
        // entry 应被完全移除(get_tokens_by_login_id 返回空 Vec)
        assert!(
            session.get_tokens_by_login_id("user1").is_empty(),
            "移除最后一个 token 后 entry 应被删除"
        );
        // get_token_by_login_id 应返回 None
        assert!(
            session.get_token_by_login_id("user1").is_none(),
            "移除最后一个 token 后 get_token_by_login_id 应返回 None"
        );
    }

    /// `remove_login_token` 移除非最后一个 token 时保留其他 token。
    #[test]
    fn remove_login_token_preserves_other_tokens() {
        let (_dao, session) = make_session(3600, 86400);
        session.add_login_token("user1", "token1");
        session.add_login_token("user1", "token2");
        session.add_login_token("user1", "token3");

        session.remove_login_token("user1", "token2");
        let tokens = session.get_tokens_by_login_id("user1");
        assert_eq!(
            tokens,
            vec!["token1".to_string(), "token3".to_string()],
            "移除 token2 后应保留 token1 和 token3"
        );
    }

    // ----------------------------------------------------------------
    // get_token_by_login_id / get_tokens_by_login_id(nonexistent)
    // ----------------------------------------------------------------

    /// `get_token_by_login_id` 返回第一个 token,对不存在的 login_id 返回 None。
    #[test]
    fn get_token_by_login_id_returns_first_or_none() {
        let (_dao, session) = make_session(3600, 86400);
        session.add_login_token("user1", "first_token");
        session.add_login_token("user1", "second_token");

        // 返回第一个 token
        assert_eq!(
            session.get_token_by_login_id("user1"),
            Some("first_token".to_string()),
            "应返回第一个 token"
        );

        // 不存在的 login_id 返回 None
        assert!(
            session.get_token_by_login_id("nonexistent").is_none(),
            "不存在的 login_id 应返回 None"
        );
    }

    /// `get_tokens_by_login_id` 对不存在的 login_id 返回空 Vec。
    #[test]
    fn get_tokens_by_login_id_returns_empty_for_nonexistent() {
        let (_dao, session) = make_session(3600, 86400);
        assert!(
            session
                .get_tokens_by_login_id("nonexistent_user")
                .is_empty(),
            "不存在的 login_id 应返回空 Vec"
        );
    }

    // ----------------------------------------------------------------
    // set / get 自定义属性(error case + success case)
    // ----------------------------------------------------------------

    /// `set` 对不存在的 token 返回 `InvalidToken` 错误。
    #[tokio::test]
    async fn set_attr_on_nonexistent_token_returns_invalid_token() {
        let (_dao, session) = make_session(3600, 86400);
        let result = session.set("nonexistent_token", "key", "value").await;
        assert!(
            matches!(result, Err(GarrisonError::InvalidToken(_))),
            "set 不存在的 token 应返回 InvalidToken 错误"
        );
    }

    /// `set` / `get` 自定义属性的往返测试。
    #[tokio::test]
    async fn set_and_get_custom_attr_roundtrip() {
        let (_dao, session) = make_session(3600, 86400);
        session.create("1001", "T1").await.unwrap();

        // set 属性
        session
            .set("T1", "custom_key", "custom_value")
            .await
            .unwrap();

        // get 属性
        let val = session.get("T1", "custom_key").await.unwrap();
        assert_eq!(val.as_deref(), Some("custom_value"), "应读取已设置的属性");

        // get 不存在的属性
        let missing = session.get("T1", "missing_key").await.unwrap();
        assert!(missing.is_none(), "不存在的属性应返回 None");

        // get 不存在的 token 的属性
        let missing_token = session.get("no_token", "key").await.unwrap();
        assert!(missing_token.is_none(), "不存在的 token 应返回 None");
    }

    // ----------------------------------------------------------------
    // set_device:error case
    // ----------------------------------------------------------------

    /// `set_device` 对不存在的 token 返回 `InvalidToken` 错误。
    #[tokio::test]
    async fn set_device_on_nonexistent_token_returns_invalid_token() {
        let (_dao, session) = make_session(3600, 86400);
        let result = session.set_device("nonexistent_token", "web-chrome").await;
        assert!(
            matches!(result, Err(GarrisonError::InvalidToken(_))),
            "set_device 不存在的 token 应返回 InvalidToken 错误"
        );
    }

    // ----------------------------------------------------------------
    // create_token_session:LoginParams 中的 device/ip/user_agent 写入 TokenSession
    // ----------------------------------------------------------------

    /// `create_token_session` 将 LoginParams 中的 device/ip/user_agent 写入 TokenSession。
    #[tokio::test]
    async fn create_token_session_writes_device_ip_user_agent_from_login_params() {
        let (_dao, session) = make_session(3600, 86400);
        let params = crate::stp::LoginParams {
            device: Some("mobile-ios".to_string()),
            ip: Some("192.168.1.100".to_string()),
            user_agent: Some("Mozilla/5.0 (iPhone)".to_string()),
            remember_me: false,
            require_mfa: false,
        };

        session
            .create_token_session("user1", "T1", &params)
            .await
            .expect("create_token_session 应成功");

        let ts = session
            .get_token_session("T1")
            .await
            .expect("get_token_session 应成功")
            .expect("Token-Session 应存在");
        assert_eq!(ts.device.as_deref(), Some("mobile-ios"), "device 应被写入");
        assert_eq!(ts.ip.as_deref(), Some("192.168.1.100"), "ip 应被写入");
        assert_eq!(
            ts.user_agent.as_deref(),
            Some("Mozilla/5.0 (iPhone)"),
            "user_agent 应被写入"
        );
    }

    /// `create_token_session` 中 LoginParams 的 device/ip/user_agent 为 None 时
    /// TokenSession 对应字段也为 None(向后兼容 `create`)。
    #[tokio::test]
    async fn create_token_session_with_none_params_leaves_fields_none() {
        let (_dao, session) = make_session(3600, 86400);
        let params = crate::stp::LoginParams::default();

        session
            .create_token_session("user1", "T1", &params)
            .await
            .expect("create_token_session 应成功");

        let ts = session
            .get_token_session("T1")
            .await
            .expect("get_token_session 应成功")
            .expect("Token-Session 应存在");
        assert!(ts.device.is_none(), "device 应为 None");
        assert!(ts.ip.is_none(), "ip 应为 None");
        assert!(ts.user_agent.is_none(), "user_agent 应为 None");
    }

    // ----------------------------------------------------------------
    // link_sso_ticket / link_oauth2_token / link_temp_credential(success cases)
    // ----------------------------------------------------------------

    /// `link_sso_ticket` + `get_sso_ticket` 往返测试。
    #[tokio::test]
    async fn link_and_get_sso_ticket_roundtrip() {
        let (_dao, session) = make_session(3600, 86400);
        session.create("user1", "T1").await.unwrap();

        session
            .link_sso_ticket("T1", "ticket-abc-123")
            .await
            .expect("link_sso_ticket 应成功");

        let ticket = session
            .get_sso_ticket("T1")
            .await
            .expect("get_sso_ticket 应成功");
        assert_eq!(
            ticket.as_deref(),
            Some("ticket-abc-123"),
            "应读取已关联的 SSO ticket"
        );
    }

    /// `link_oauth2_token` + `get_oauth2_token` 往返测试。
    #[tokio::test]
    async fn link_and_get_oauth2_token_roundtrip() {
        let (_dao, session) = make_session(3600, 86400);
        session.create("user1", "T1").await.unwrap();

        session
            .link_oauth2_token("T1", "oauth2-access-token-xyz")
            .await
            .expect("link_oauth2_token 应成功");

        let token = session
            .get_oauth2_token("T1")
            .await
            .expect("get_oauth2_token 应成功");
        assert_eq!(
            token.as_deref(),
            Some("oauth2-access-token-xyz"),
            "应读取已关联的 OAuth2 access_token"
        );
    }

    /// `link_temp_credential` + `get_temp_credential` 往返测试。
    #[tokio::test]
    async fn link_and_get_temp_credential_roundtrip() {
        let (_dao, session) = make_session(3600, 86400);
        session.create("user1", "T1").await.unwrap();

        let temp_key = "temp:cred:user1:20260714";
        session
            .link_temp_credential("T1", temp_key)
            .await
            .expect("link_temp_credential 应成功");

        let stored = session
            .get_temp_credential("T1")
            .await
            .expect("get_temp_credential 应成功");
        assert_eq!(
            stored.as_deref(),
            Some(temp_key),
            "应读取已关联的临时凭证 key"
        );
    }

    // ----------------------------------------------------------------
    // renew:error case(token 不存在)
    // ----------------------------------------------------------------

    /// `renew` 对不存在的 token 返回 `InvalidToken` 错误。
    #[tokio::test]
    async fn renew_nonexistent_token_returns_invalid_token() {
        let (_dao, session) = make_session(3600, 86400);
        let result = session.renew("nonexistent_token").await;
        assert!(
            matches!(result, Err(GarrisonError::InvalidToken(_))),
            "renew 不存在的 token 应返回 InvalidToken 错误"
        );
    }

    // ----------------------------------------------------------------
    // logout:幂等性(不存在的 token)
    // ----------------------------------------------------------------

    /// `logout` 对不存在的 token 幂等返回 `Ok(())`。
    #[tokio::test]
    async fn logout_nonexistent_token_is_idempotent() {
        let (_dao, session) = make_session(3600, 86400);
        let result = session.logout("nonexistent_token").await;
        assert!(result.is_ok(), "logout 不存在的 token 应幂等返回 Ok(())");
    }

    // ----------------------------------------------------------------
    // dao() 访问器(protocol-apikey feature)
    // ----------------------------------------------------------------

    /// `dao()` 返回内部 DAO 引用(protocol-apikey feature)。
    #[cfg(feature = "protocol-apikey")]
    #[test]
    fn dao_accessor_returns_dao_reference() {
        use crate::dao::GarrisonDao;

        let dao: Arc<dyn GarrisonDao> = Arc::new(MockDao::new());
        let session = GarrisonSession::new(dao.clone(), 3600, 86400);
        let returned = session.dao();
        // 验证返回的是同一个 Arc(通过比较指针是否指向同一对象)
        let returned_ptr = Arc::as_ptr(returned);
        let original_ptr = Arc::as_ptr(&dao);
        assert!(
            std::ptr::eq(returned_ptr, original_ptr),
            "dao() 应返回内部 DAO 的引用"
        );
    }

    // ----------------------------------------------------------------
    // FMEA #5:并发 read-modify-write lost update 防护
    // ----------------------------------------------------------------

    /// FMEA #5: 验证 `set` 在并发调用下不会 lost update。
    ///
    /// 10 个并发任务对同一 token 调用 `set` 写入不同 key,
    /// 完成后所有 10 个 key 都应存在(无 lost update)。
    #[tokio::test(flavor = "multi_thread", worker_threads = 4)]
    async fn set_concurrent_no_lost_update() {
        let (_dao, session) = make_session(3600, 86400);
        let session = Arc::new(session);
        session.create("user1", "T1").await.unwrap();

        let mut handles = Vec::new();
        for i in 0..10 {
            let s = session.clone();
            handles.push(tokio::spawn(async move {
                s.set("T1", &format!("key{}", i), &format!("val{}", i))
                    .await
                    .unwrap();
            }));
        }
        for h in handles {
            h.await.unwrap();
        }

        // 验证所有 10 个 key 都存在(无 lost update)
        for i in 0..10 {
            let val = session.get("T1", &format!("key{}", i)).await.unwrap();
            assert_eq!(
                val,
                Some(format!("val{}", i)),
                "key{} 应存在(无 lost update),实际: {:?}",
                i,
                val
            );
        }
    }

    /// FMEA #5: 验证 `set_device` 与 `set` 并发调用不会互相覆盖。
    ///
    /// 一个任务调用 `set_device`,另一个调用 `set`,
    /// 完成后 device 和 attr 都应存在。
    #[tokio::test(flavor = "multi_thread", worker_threads = 4)]
    async fn set_device_concurrent_with_set_no_lost_update() {
        let (_dao, session) = make_session(3600, 86400);
        let session = Arc::new(session);
        session.create("user1", "T1").await.unwrap();

        let s1 = session.clone();
        let s2 = session.clone();
        let h1 = tokio::spawn(async move {
            s1.set_device("T1", "device-A").await.unwrap();
        });
        let h2 = tokio::spawn(async move {
            s2.set("T1", "attr1", "val1").await.unwrap();
        });
        h1.await.unwrap();
        h2.await.unwrap();

        // 验证 device 和 attr 都存在
        let ts = session.get_token_session("T1").await.unwrap().unwrap();
        assert_eq!(ts.device, Some("device-A".to_string()));
        assert_eq!(ts.attrs.get("attr1"), Some(&"val1".to_string()));
    }

    /// FMEA #5: 验证 `touch` 在并发调用下不会损坏 session。
    ///
    /// 10 个并发任务对同一 token 调用 `touch`,
    /// 完成后 session 应仍然存在且 last_active_at 已更新。
    #[tokio::test(flavor = "multi_thread", worker_threads = 4)]
    async fn touch_concurrent_no_corruption() {
        let (_dao, session) = make_session(3600, 86400);
        let session = Arc::new(session);
        session.create("user1", "T1").await.unwrap();

        let mut handles = Vec::new();
        for _ in 0..10 {
            let s = session.clone();
            handles.push(tokio::spawn(async move {
                s.touch("T1").await.unwrap();
            }));
        }
        for h in handles {
            h.await.unwrap();
        }

        // 验证 session 仍然存在
        let ts = session.get_token_session("T1").await.unwrap();
        assert!(ts.is_some(), "touch 并发后 session 应仍然存在");
    }

    // ----------------------------------------------------------------
    // with_token_session_lock:entry 清理逻辑(安全审查 MEDIUM 修复验证)
    // ----------------------------------------------------------------

    /// `with_token_session_lock` 完成后清理无等待者的 entry(CWE-770 防护)。
    ///
    /// 安全审查 MEDIUM 修复验证:调用 `with_token_session_lock` 后,
    /// `token_session_locks` 不应残留无引用 entry(防止攻击者用大量随机 token 灌满)。
    #[tokio::test]
    async fn with_token_session_lock_cleans_entry_after_completion() {
        let (_dao, session) = make_session(3600, 86400);
        assert!(
            session.token_session_locks.is_empty(),
            "调用前 token_session_locks 应为空"
        );
        // 调用 with_token_session_lock 执行任意操作
        let _ = session
            .with_token_session_lock("test-token-cleanup", async { 42 })
            .await;
        assert!(
            session.token_session_locks.is_empty(),
            "调用后 token_session_locks 应清理为空(无等待者)"
        );
    }

    /// `with_token_session_lock` 并发调用完成后清理所有 entry。
    ///
    /// 验证:多个不同 token 的并发调用完成后,所有 entry 都应被清理。
    #[tokio::test(flavor = "multi_thread", worker_threads = 4)]
    async fn with_token_session_lock_cleans_all_entries_after_concurrent_calls() {
        let (_dao, session) = make_session(3600, 86400);
        let session = Arc::new(session);
        assert!(
            session.token_session_locks.is_empty(),
            "并发调用前 token_session_locks 应为空"
        );

        let mut handles = Vec::new();
        for i in 0..10 {
            let s = session.clone();
            handles.push(tokio::spawn(async move {
                let token = format!("concurrent-token-{}", i);
                s.with_token_session_lock(&token, async { i }).await
            }));
        }
        for h in handles {
            h.await.unwrap();
        }

        assert!(
            session.token_session_locks.is_empty(),
            "并发调用全部完成后 token_session_locks 应清理为空,实际残留 {} 个 entry",
            session.token_session_locks.len()
        );
    }

    /// `with_token_session_lock` 持锁期间 entry 不被清理(有等待者)。
    ///
    /// 验证:当一个 task 持锁时,另一个 task 等待锁期间 entry 不应被清理
    ///(strong_count == 2,remove_if 不移除)。
    #[tokio::test(flavor = "multi_thread", worker_threads = 4)]
    async fn with_token_session_lock_keeps_entry_while_contended() {
        let (_dao, session) = make_session(3600, 86400);
        let session = Arc::new(session);

        let barrier = Arc::new(tokio::sync::Barrier::new(2));
        let b1 = barrier.clone();
        let s1 = session.clone();
        let h1 = tokio::spawn(async move {
            s1.with_token_session_lock("contended-token", async move {
                b1.wait().await;
                tokio::time::sleep(Duration::from_millis(50)).await;
            })
            .await
        });

        let b2 = barrier.clone();
        let s2 = session.clone();
        let h2 = tokio::spawn(async move {
            b2.wait().await;
            s2.with_token_session_lock("contended-token", async { 42 })
                .await
        });

        h1.await.unwrap();
        h2.await.unwrap();

        assert!(
            session.token_session_locks.is_empty(),
            "两个 task 都完成后 token_session_locks 应清理为空"
        );
    }
}