ironbridge-cli 2.0.1

Universal chat session manager - harvest, merge, and analyze AI chat history from VS Code, Cursor, and other editors
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
// Copyright (c) 2024-2026 Nervosys LLC
// SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-IronBridge-Commercial
//! Authentication and Authorization Module
//!
//! Provides JWT-based authentication, user management, and subscription handling
//! for the IRONBRIDGE ecosystem (ironbridge-rust, ironbridge-web, ironbridge-app).

use actix_web::{dev::Payload, web, FromRequest, HttpMessage, HttpRequest, HttpResponse};
use chrono::{Duration, Utc};
use jsonwebtoken::{decode, encode, Algorithm, DecodingKey, EncodingKey, Header, Validation};
use rusqlite::OptionalExtension;
use serde::{Deserialize, Serialize};
use sha2::{Digest, Sha256};
use std::future::{ready, Ready};
use uuid::Uuid;

// =============================================================================
// Configuration
// =============================================================================

/// Environment variable that supplies the JWT signing secret.
const JWT_SECRET_ENV: &str = "IRONBRIDGE_JWT_SECRET";

/// The HMAC secret that signs session tokens, resolved once per process.
///
/// - `IRONBRIDGE_JWT_SECRET` if set and at least 32 bytes: tokens then survive a
///   restart and can be shared across instances behind a load balancer.
/// - otherwise a cryptographically random 32-byte secret generated at startup.
///   Tokens are unforgeable but do not outlive the process; a restart makes
///   everyone log in again, which for a local-first tool is an inconvenience,
///   not a hole.
///
/// What it is never again: a constant compiled into the binary. The previous
/// value -- `ironbridge_jwt_secret_key_change_in_production_2024` -- shipped in the
/// public source, so anyone who read the repository could forge a valid token
/// for any user at any subscription tier. A random default closes that even
/// when the operator sets nothing.
fn jwt_secret() -> &'static [u8] {
    use std::sync::OnceLock;
    static SECRET: OnceLock<Vec<u8>> = OnceLock::new();
    SECRET.get_or_init(|| match std::env::var(JWT_SECRET_ENV) {
        Ok(s) if s.len() >= 32 => s.into_bytes(),
        Ok(s) => {
            eprintln!(
                "[WARN] {JWT_SECRET_ENV} is set but only {} bytes; a signing secret must be \
                 at least 32. Ignoring it and using a random per-process secret instead.",
                s.len()
            );
            random_secret()
        }
        Err(_) => {
            eprintln!(
                "[INFO] {JWT_SECRET_ENV} is not set; signing sessions with a random \
                 per-process secret. Sessions will not survive a restart. Set \
                 {JWT_SECRET_ENV} (>=32 bytes) to keep sessions across restarts or share \
                 them across instances."
            );
            random_secret()
        }
    })
}

fn random_secret() -> Vec<u8> {
    use rand::RngCore;
    let mut buf = vec![0u8; 32];
    rand::thread_rng().fill_bytes(&mut buf);
    buf
}

const JWT_EXPIRY_HOURS: i64 = 24;
const REFRESH_TOKEN_EXPIRY_DAYS: i64 = 30;

// =============================================================================
// Login brute-force throttle
// =============================================================================

/// After this many failed logins from one address within the window, further
/// attempts are refused until the window slides past them.
const LOGIN_MAX_FAILURES: usize = 10;
/// The sliding window for counting failures, in seconds (15 minutes).
const LOGIN_WINDOW_SECS: u64 = 900;

fn login_failures(
) -> &'static std::sync::Mutex<std::collections::HashMap<String, Vec<std::time::Instant>>> {
    use std::sync::OnceLock;
    static FAILURES: OnceLock<
        std::sync::Mutex<std::collections::HashMap<String, Vec<std::time::Instant>>>,
    > = OnceLock::new();
    FAILURES.get_or_init(|| std::sync::Mutex::new(std::collections::HashMap::new()))
}

/// Whether this address has spent its failed-login budget for now.
///
/// Keyed on the socket peer address, not `X-Forwarded-For` or a body field, so
/// it cannot be reset by spoofing a header. An attacker guessing passwords is
/// slowed to `LOGIN_MAX_FAILURES` per window; a legitimate user who mistypes a
/// few times is not affected, and -- because the key is the caller's own
/// address -- no one can lock a victim out by failing *their* login.
fn login_throttled(key: &str) -> bool {
    let mut map = login_failures().lock().unwrap();
    let now = std::time::Instant::now();
    if let Some(times) = map.get_mut(key) {
        times.retain(|t| now.duration_since(*t).as_secs() < LOGIN_WINDOW_SECS);
        times.len() >= LOGIN_MAX_FAILURES
    } else {
        false
    }
}

fn record_login_failure(key: &str) {
    let mut map = login_failures().lock().unwrap();
    let now = std::time::Instant::now();
    let times = map.entry(key.to_string()).or_default();
    times.retain(|t| now.duration_since(*t).as_secs() < LOGIN_WINDOW_SECS);
    times.push(now);
}

fn clear_login_failures(key: &str) {
    login_failures().lock().unwrap().remove(key);
}

/// The throttle key: the connection's peer address (host without port).
fn throttle_key(req: &HttpRequest) -> String {
    req.peer_addr()
        .map(|a| a.ip().to_string())
        .unwrap_or_else(|| "unknown".to_string())
}

// =============================================================================
// Subscription Tiers
// =============================================================================

#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "snake_case")]
#[derive(Default)]
pub enum SubscriptionTier {
    /// Free tier - basic sync, limited features
    #[default]
    Free,
    /// Pro tier - full sync, all features, priority support
    Pro,
    /// Enterprise tier - team features, admin controls, SLA
    Enterprise,
}

impl SubscriptionTier {
    pub fn as_str(&self) -> &'static str {
        match self {
            Self::Free => "free",
            Self::Pro => "pro",
            Self::Enterprise => "enterprise",
        }
    }

    pub fn from_str(s: &str) -> Option<Self> {
        match s.to_lowercase().as_str() {
            "free" => Some(Self::Free),
            "pro" => Some(Self::Pro),
            "enterprise" => Some(Self::Enterprise),
            _ => None,
        }
    }
}

// =============================================================================
// Subscription Features
// =============================================================================

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SubscriptionFeatures {
    /// Maximum number of workspaces
    pub max_workspaces: Option<u32>,
    /// Maximum number of sessions
    pub max_sessions: Option<u32>,
    /// Maximum number of agents
    pub max_agents: Option<u32>,
    /// Maximum number of swarms
    pub max_swarms: Option<u32>,
    /// Real-time sync enabled
    pub realtime_sync: bool,
    /// Cross-device sync enabled
    pub cross_device_sync: bool,
    /// Priority support
    pub priority_support: bool,
    /// Team collaboration features
    pub team_collaboration: bool,
    /// Advanced analytics
    pub analytics: bool,
    /// API access
    pub api_access: bool,
    /// Custom integrations
    pub custom_integrations: bool,
}

impl SubscriptionFeatures {
    pub fn for_tier(tier: SubscriptionTier) -> Self {
        match tier {
            SubscriptionTier::Free => Self {
                max_workspaces: Some(10),
                max_sessions: Some(100),
                max_agents: Some(5),
                max_swarms: Some(1),
                realtime_sync: true,
                cross_device_sync: true,
                priority_support: false,
                team_collaboration: false,
                analytics: false,
                api_access: false,
                custom_integrations: false,
            },
            SubscriptionTier::Pro => Self {
                max_workspaces: Some(100),
                max_sessions: None, // Unlimited
                max_agents: Some(100),
                max_swarms: Some(20),
                realtime_sync: true,
                cross_device_sync: true,
                priority_support: true,
                team_collaboration: false,
                analytics: true,
                api_access: true,
                custom_integrations: false,
            },
            SubscriptionTier::Enterprise => Self {
                max_workspaces: None, // Unlimited
                max_sessions: None,
                max_agents: None,
                max_swarms: None,
                realtime_sync: true,
                cross_device_sync: true,
                priority_support: true,
                team_collaboration: true,
                analytics: true,
                api_access: true,
                custom_integrations: true,
            },
        }
    }
}

// =============================================================================
// User Model
// =============================================================================

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct User {
    pub id: String,
    pub email: String,
    pub display_name: String,
    #[serde(skip_serializing)]
    #[allow(dead_code)]
    pub password_hash: String,
    pub subscription_tier: SubscriptionTier,
    pub subscription_expires_at: Option<i64>,
    pub created_at: i64,
    pub updated_at: i64,
    pub last_login_at: Option<i64>,
    pub email_verified: bool,
    pub avatar_url: Option<String>,
    pub metadata: Option<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PublicUser {
    pub id: String,
    pub email: String,
    pub display_name: String,
    pub subscription_tier: SubscriptionTier,
    pub subscription_expires_at: Option<i64>,
    pub created_at: i64,
    pub email_verified: bool,
    pub avatar_url: Option<String>,
    pub features: SubscriptionFeatures,
}

impl From<User> for PublicUser {
    fn from(user: User) -> Self {
        Self {
            id: user.id,
            email: user.email,
            display_name: user.display_name,
            subscription_tier: user.subscription_tier,
            subscription_expires_at: user.subscription_expires_at,
            created_at: user.created_at,
            email_verified: user.email_verified,
            avatar_url: user.avatar_url,
            features: SubscriptionFeatures::for_tier(user.subscription_tier),
        }
    }
}

// =============================================================================
// JWT Claims
// =============================================================================

#[derive(Debug, Serialize, Deserialize)]
pub struct Claims {
    /// Subject (user ID)
    pub sub: String,
    /// Email
    pub email: String,
    /// Subscription tier
    pub tier: String,
    /// Issued at
    pub iat: i64,
    /// Expiration
    pub exp: i64,
    /// Token type (access or refresh)
    pub token_type: String,
}

// =============================================================================
// Enforcement middleware
// =============================================================================

/// Opt **out** of `/api` authentication -- for a trusted single-user machine.
pub const DISABLE_AUTH_ENV: &str = "IRONBRIDGE_DISABLE_AUTH";

/// Legacy opt-**in** switch, kept working. Once the default became "on", this
/// is redundant, but a deployment that set it should not suddenly behave
/// differently, so it is still honoured as a no-op-that-confirms-on.
pub const REQUIRE_AUTH_ENV: &str = "IRONBRIDGE_REQUIRE_AUTH";

/// The shortest password accepted, in characters.
pub const MIN_PASSWORD_CHARS: usize = 8;

/// Passwords common enough that an online guesser tries them first.
///
/// NIST SP 800-63B asks for exactly one check beyond length: compare the
/// candidate against passwords known to be compromised, and reject a match.
/// It argues *against* composition rules -- forcing a digit and a symbol
/// pushes people toward `Passw0rd!`, which is on every list already -- so
/// length plus this is the whole policy, deliberately.
///
/// Entries shorter than [`MIN_PASSWORD_CHARS`] are already refused by the
/// length check. They stay listed anyway so the two rules remain independent:
/// a future change to the minimum cannot quietly let `qwerty` back in.
const COMMON_PASSWORDS: &[&str] = &[
    // Sequences and keyboard walks -- the overwhelming majority of real hits.
    "123456",
    "1234567",
    "12345678",
    "123456789",
    "1234567890",
    "12345678910",
    "987654321",
    "qwerty",
    "qwerty123",
    "qwerty1234",
    "qwertyuiop",
    "1q2w3e4r",
    "1q2w3e4r5t",
    "zaq12wsx",
    "qazwsxedc",
    "asdfghjkl",
    "1qaz2wsx",
    "q1w2e3r4",
    // The word itself and its predictable decorations.
    "password",
    "password1",
    "password12",
    "password123",
    "password1234",
    "passw0rd",
    "p@ssw0rd",
    "p@ssword",
    "passwort",
    "contrasena",
    "motdepasse",
    // Access words.
    "letmein",
    "letmein1",
    "letmein123",
    "welcome",
    "welcome1",
    "welcome123",
    "admin123",
    "administrator",
    "root1234",
    "changeme",
    "changeit",
    "secret123",
    "default1",
    "guest123",
    "trustno1",
    "iloveyou",
    "whatever",
    "computer",
    "internet",
    "starwars",
    "superman",
    "batman123",
    "pokemon123",
    "dragon123",
    "monkey123",
    "freedom1",
    "shadow123",
    "master123",
    "hello123",
    "charlie1",
    "jennifer",
    "michael1",
    "jordan23",
    "princess",
    "princess1",
    "sunshine",
    "sunshine1",
    "football",
    "football1",
    "baseball",
    "baseball1",
    "basketball",
    "liverpool",
    "liverpool1",
    "arsenal1",
    "chelsea1",
    "barcelona",
    "juventus1",
    "corvette",
    "harley123",
    "nintendo",
    "creative",
    "midnight",
    "flowerpot",
    "butterfly",
    "chocolate",
    "cocacola",
    "pepsi123",
    "samsung1",
    "michelle",
    "danielle",
    "nicholas",
    "jonathan",
    "samantha",
    "elizabeth",
    "alexander",
    "anthony1",
    "matthew1",
    "andrew123",
    "joshua123",
    "robert123",
    "thomas123",
    "william1",
    "abc12345",
    "abcd1234",
    "a1b2c3d4",
    "asdf1234",
    "test1234",
    "temp1234",
    "demo1234",
    "user1234",
    "login123",
    "access123",
    "system123",
    "server123",
    "database1",
    "backup123",
    "qwe123456",
    "111111111",
    "000000000",
    "121212121",
    "iloveyou1",
    "princess123",
];

/// Whether a password is too common to accept.
///
/// Case-insensitive: `Password` is guessed as readily as `password`, and a
/// list that only caught the lowercase spelling would be trivially sidestepped
/// by the shift key. Comparison is on the raw characters otherwise -- no
/// trimming, since leading or trailing spaces are legitimate password content
/// and silently stripping them would change what the user chose.
pub fn is_common_password(password: &str) -> bool {
    COMMON_PASSWORDS
        .iter()
        .any(|common| password.eq_ignore_ascii_case(common))
}

/// Whether every `/api` route requires a valid Bearer token, resolved once.
///
/// **On by default.** The web and desktop clients present a login screen and
/// attach a token, so an authenticated API is the safe posture to ship. A
/// single-user machine that wants the old open behaviour sets
/// `IRONBRIDGE_DISABLE_AUTH=1` and takes responsibility for it -- appropriate when
/// the server is bound to loopback and nothing else can reach it.
///
/// The precedence is "secure unless explicitly told otherwise": if
/// `IRONBRIDGE_DISABLE_AUTH` is set, auth is off; otherwise it is on, whether or not
/// the old `IRONBRIDGE_REQUIRE_AUTH` is present.
///
/// When on, `require_auth` below rejects any `/api` request (except
/// `/api/health`) that does not carry a valid token.
pub fn auth_required() -> bool {
    use std::sync::OnceLock;
    static REQUIRED: OnceLock<bool> = OnceLock::new();
    *REQUIRED.get_or_init(|| {
        let disabled = std::env::var(DISABLE_AUTH_ENV)
            .map(|v| v == "1" || v.eq_ignore_ascii_case("true"))
            .unwrap_or(false);
        !disabled
    })
}

/// Does this path need a token when enforcement is on?
///
/// Only `/api/*` is gated -- `/auth/login`, `/auth/register` and the root-level
/// scopes must stay reachable, or there would be no way to obtain a token in
/// the first place. `/api/health` is exempt so liveness probes and the client's
/// own "is the server up" check work without credentials.
/// Whether a request to this path needs a token when enforcement is on.
///
/// **Default-deny.** Everything is gated except an explicit open-list. The
/// previous version gated only `/api/`, and that was a real hole: the
/// root-mounted scopes -- `/sync` (which returns every session, workspace and
/// agent in one call), `/recording`, `/audit`, `/retention`, `/webhooks`,
/// `/graphql`, `/ws` -- were reachable with no token even while `/api` itself
/// answered 401. An allowlist cannot grow that kind of hole: a scope added
/// tomorrow is gated until someone deliberately opens it, which is the safe
/// direction to fail.
///
/// Open, and why each has to be:
/// - health/liveness needs no identity;
/// - `/auth/*` is the login handshake -- gating it removes the only way to
///   obtain a token; its own sensitive endpoints self-protect with the
///   `AuthenticatedUser` extractor;
/// - `/sso/*`, `/oidc/*` are the SSO handshake, reached before a session exists;
/// - `/docs` and `/docs/*` are the published API reference and carry no user
///   data.
///
/// Every prefix ends in `/`, and a bare path that must stay open is listed in
/// `OPEN_EXACT` instead. That is deliberate: a prefix without the separator
/// opens every path merely *starting* with those characters, so a plain
/// `"/docs"` would also hand out `/docs-admin` or `/docsecrets` to anyone. No
/// such route exists today -- this is about the hole not being there to fall
/// into when one is added.
fn path_requires_auth(path: &str) -> bool {
    const OPEN_EXACT: &[&str] = &[
        "/health",
        "/api/health",
        "/api/system/health",
        "/api/system/providers/health",
        "/docs",
    ];
    const OPEN_PREFIXES: &[&str] = &["/auth/", "/sso/", "/oidc/", "/docs/"];

    if OPEN_EXACT.contains(&path) {
        return false;
    }
    if OPEN_PREFIXES.iter().any(|p| path.starts_with(p)) {
        return false;
    }
    true
}

/// The bearer token on a request, from the `Authorization` header or, failing
/// that, a `token` query parameter.
///
/// The query parameter exists for WebSocket upgrades: a browser cannot set an
/// `Authorization` header on a `WebSocket` connection, so `/ws` has to carry
/// the token in the URL. It is a lesser channel -- URLs are logged in more
/// places than headers -- so the header is tried first and the query parameter
/// is the fallback, not the norm.
/// True when the request targets a WebSocket route -- the only endpoints that
/// legitimately carry the token in the handshake (subprotocol, or the legacy
/// query string) because a browser cannot set an `Authorization` header on a
/// `WebSocket`.
///
/// This is bound to the request *path*, not the client-settable `Upgrade`
/// header: gating on `Upgrade: websocket` would let a caller set that header on
/// an ordinary endpoint (`GET /api/sessions?token=...`) to smuggle the token in
/// the URL and have it logged -- exactly the leak this guard exists to prevent.
/// The path cannot be spoofed into hitting a non-WS handler.
fn is_websocket_route(req: &actix_web::dev::ServiceRequest) -> bool {
    matches!(req.path(), "/ws" | "/recording/ws")
}

/// The bearer token offered in the `Sec-WebSocket-Protocol` handshake header,
/// if any. Format is `bearer, <token>` (from `new WebSocket(url, ['bearer',
/// token])`). Shared with the WS handler's handshake echo so the two never
/// disagree on what counts as a bearer subprotocol.
pub(crate) fn bearer_subprotocol_token(
    headers: &actix_web::http::header::HeaderMap,
) -> Option<String> {
    headers
        .get("Sec-WebSocket-Protocol")
        .and_then(|h| h.to_str().ok())
        .and_then(|v| {
            let mut parts = v.split(',').map(|s| s.trim());
            match (parts.next(), parts.next()) {
                (Some(scheme), Some(tok)) if scheme.eq_ignore_ascii_case("bearer") => {
                    Some(tok.to_string())
                }
                _ => None,
            }
        })
}

fn request_token(req: &actix_web::dev::ServiceRequest) -> Option<AuthenticatedUser> {
    let claims = req
        .headers()
        .get("Authorization")
        .and_then(|h| h.to_str().ok())
        .and_then(|s| s.strip_prefix("Bearer "))
        .and_then(validate_token_claims)
        .or_else(|| {
            // The subprotocol / query-string token channels are *only* for the
            // WebSocket routes, where a browser cannot set an `Authorization`
            // header. Honouring them elsewhere would let a token ride in the URL
            // of an ordinary API call, where it leaks into access logs, proxy
            // logs, browser history, and the `Referer` of anything the response
            // loads. Bound to the route path (not a spoofable header).
            if !is_websocket_route(req) {
                return None;
            }
            // Preferred WS channel: the token rides in the `Sec-WebSocket-
            // Protocol` handshake header, which — unlike the URL — is not
            // written to request-line access logs.
            let from_subprotocol =
                bearer_subprotocol_token(req.headers()).and_then(|t| validate_token_claims(&t));

            from_subprotocol.or_else(|| {
                // Legacy fallback: token in the query string. Still WS-only.
                req.query_string()
                    .split('&')
                    .find_map(|pair| pair.strip_prefix("token="))
                    .and_then(|t| urlencoding::decode(t).ok())
                    .and_then(|t| validate_token_claims(&t))
            })
        })?;

    // Revocation check: the token passed signature and expiry, but may predate
    // a password change. Look up the user's cut-off when the store is reachable.
    if let Some(state) = req.app_data::<web::Data<crate::api::state::AppState>>() {
        if let Ok(db) = state.db.lock() {
            if !token_not_revoked(&db.conn, &claims.sub, claims.iat) {
                return None;
            }
        }
    }

    Some(AuthenticatedUser {
        user_id: claims.sub,
        email: claims.email,
        tier: SubscriptionTier::from_str(&claims.tier).unwrap_or_default(),
    })
}

/// Reject unauthenticated requests to gated paths. On by default;
/// `IRONBRIDGE_DISABLE_AUTH=1` is the only thing that turns it off.
///
/// A `from_fn` middleware rather than a per-handler extractor: gating every
/// route by hand is a chance to forget one on each new endpoint, and the one
/// forgotten is the hole. One default-deny gate in front of the app cannot be
/// bypassed by adding a scope.
pub async fn require_auth(
    req: actix_web::dev::ServiceRequest,
    next: actix_web::middleware::Next<impl actix_web::body::MessageBody + 'static>,
) -> Result<
    actix_web::dev::ServiceResponse<actix_web::body::EitherBody<impl actix_web::body::MessageBody>>,
    actix_web::Error,
> {
    // CORS preflight carries no credentials by design; gating it would 401 the
    // OPTIONS before the browser ever sends the real, authenticated request.
    let preflight = req.method() == actix_web::http::Method::OPTIONS;
    let gated = auth_required() && !preflight && path_requires_auth(req.path());

    let authorized = !gated || request_token(&req).is_some();

    if authorized {
        next.call(req)
            .await
            .map(actix_web::dev::ServiceResponse::map_into_left_body)
    } else {
        let (request, _payload) = req.into_parts();
        let response = HttpResponse::Unauthorized()
            .json(serde_json::json!({
                "success": false,
                "data": null,
                "error": "Authentication required. Send a valid Bearer token.",
            }))
            .map_into_right_body();
        Ok(actix_web::dev::ServiceResponse::new(request, response))
    }
}

// =============================================================================
// Auth State (for middleware)
// =============================================================================

#[derive(Debug, Clone)]
#[allow(dead_code)]
pub struct AuthenticatedUser {
    pub user_id: String,
    pub email: String,
    pub tier: SubscriptionTier,
}

impl FromRequest for AuthenticatedUser {
    type Error = actix_web::Error;
    type Future = Ready<Result<Self, Self::Error>>;

    fn from_request(req: &HttpRequest, _payload: &mut Payload) -> Self::Future {
        // Try to get user from request extensions (set by middleware)
        if let Some(user) = req.extensions().get::<AuthenticatedUser>() {
            return ready(Ok(user.clone()));
        }

        // Try to extract from Authorization header directly, honouring
        // revocation: a token that predates the user's last password change is
        // no longer accepted here either, matching the middleware.
        if let Some(auth_header) = req.headers().get("Authorization") {
            if let Ok(auth_str) = auth_header.to_str() {
                if let Some(token) = auth_str.strip_prefix("Bearer ") {
                    if let Some(claims) = validate_token_claims(token) {
                        let current =
                            req.app_data::<web::Data<crate::api::state::AppState>>()
                                .and_then(|state| {
                                    state.db.lock().ok().map(|db| {
                                        token_not_revoked(&db.conn, &claims.sub, claims.iat)
                                    })
                                })
                                .unwrap_or(true);
                        if current {
                            return ready(Ok(AuthenticatedUser {
                                user_id: claims.sub,
                                email: claims.email,
                                tier: SubscriptionTier::from_str(&claims.tier).unwrap_or_default(),
                            }));
                        }
                    }
                }
            }
        }

        ready(Ok(AuthenticatedUser {
            user_id: String::new(),
            email: String::new(),
            tier: SubscriptionTier::Free,
        }))
    }
}

// =============================================================================
// Request/Response Types
// =============================================================================

#[derive(Debug, Deserialize)]
pub struct RegisterRequest {
    pub email: String,
    pub password: String,
    pub display_name: String,
}

#[derive(Debug, Deserialize)]
pub struct LoginRequest {
    pub email: String,
    pub password: String,
}

#[derive(Debug, Deserialize)]
pub struct RefreshTokenRequest {
    pub refresh_token: String,
}

#[derive(Debug, Serialize)]
pub struct AuthResponse {
    pub user: PublicUser,
    pub access_token: String,
    pub refresh_token: String,
    pub expires_in: i64,
}

#[derive(Debug, Deserialize)]
pub struct UpdateProfileRequest {
    pub display_name: Option<String>,
    pub avatar_url: Option<String>,
}

#[derive(Debug, Deserialize)]
pub struct ChangePasswordRequest {
    pub current_password: String,
    pub new_password: String,
}

#[derive(Debug, Deserialize)]
pub struct UpgradeSubscriptionRequest {
    pub tier: String,
    /// Payment token from payment provider (Stripe, etc.)
    #[allow(dead_code)]
    pub payment_token: Option<String>,
}

// =============================================================================
// Helper Functions
// =============================================================================

/// Hash a password using SHA-256 with salt
/// Hash a password for storage as an Argon2id PHC string.
///
/// The algorithm, its parameters and a fresh random salt are all embedded in
/// the returned string, so it is self-describing and needs no separate salt
/// column. This replaces `Sha256(password || salt)` -- a *fast* hash a leaked
/// table could be cracked against at billions of guesses a second. Argon2id is
/// memory-hard and deliberately slow.
pub fn hash_password_argon2(password: &str) -> Result<String, String> {
    use argon2::password_hash::{rand_core::OsRng, PasswordHasher, SaltString};
    use argon2::Argon2;
    let salt = SaltString::generate(&mut OsRng);
    Argon2::default()
        .hash_password(password.as_bytes(), &salt)
        .map(|h| h.to_string())
        .map_err(|e| e.to_string())
}

/// The legacy scheme: `Sha256(password || salt)`, hex-encoded.
///
/// Kept only so accounts created before the move to Argon2 can still log in --
/// and be upgraded on that login. Never used to write a new hash.
fn legacy_sha256(password: &str, salt: &str) -> String {
    let mut hasher = Sha256::new();
    hasher.update(password.as_bytes());
    hasher.update(salt.as_bytes());
    format!("{:x}", hasher.finalize())
}

/// A length-independent-ish constant-time byte comparison for the legacy path.
///
/// Argon2 verification is already constant-time internally; this is only for
/// comparing the two hex SHA-256 strings, where the naive `==` leaks a timing
/// signal on how many leading characters matched.
fn constant_time_eq(a: &[u8], b: &[u8]) -> bool {
    if a.len() != b.len() {
        return false;
    }
    let mut diff = 0u8;
    for (x, y) in a.iter().zip(b.iter()) {
        diff |= x ^ y;
    }
    diff == 0
}

/// Verify a password against whatever is stored, old scheme or new.
///
/// Returns `(matches, needs_upgrade)`. `needs_upgrade` is true only when the
/// match succeeded against a legacy SHA-256 hash, signalling the caller to
/// rewrite it as Argon2 -- the transparent upgrade that moves an existing
/// account off the weak hash on its next successful login, without a reset.
pub fn verify_password(password: &str, stored_hash: &str, salt: &str) -> (bool, bool) {
    if stored_hash.starts_with("$argon2") {
        use argon2::password_hash::{PasswordHash, PasswordVerifier};
        use argon2::Argon2;
        let parsed = match PasswordHash::new(stored_hash) {
            Ok(p) => p,
            Err(_) => return (false, false),
        };
        let ok = Argon2::default()
            .verify_password(password.as_bytes(), &parsed)
            .is_ok();
        (ok, false)
    } else {
        let ok = constant_time_eq(
            legacy_sha256(password, salt).as_bytes(),
            stored_hash.as_bytes(),
        );
        (ok, ok)
    }
}

/// Generate a JWT access token
pub fn generate_access_token(user: &User) -> Option<String> {
    let now = Utc::now();
    let exp = now + Duration::hours(JWT_EXPIRY_HOURS);

    let claims = Claims {
        sub: user.id.clone(),
        email: user.email.clone(),
        tier: user.subscription_tier.as_str().to_string(),
        iat: now.timestamp(),
        exp: exp.timestamp(),
        token_type: "access".to_string(),
    };

    encode(
        &Header::default(),
        &claims,
        &EncodingKey::from_secret(jwt_secret()),
    )
    .ok()
}

/// Generate a refresh token
pub fn generate_refresh_token(user: &User) -> Option<String> {
    let now = Utc::now();
    let exp = now + Duration::days(REFRESH_TOKEN_EXPIRY_DAYS);

    let claims = Claims {
        sub: user.id.clone(),
        email: user.email.clone(),
        tier: user.subscription_tier.as_str().to_string(),
        iat: now.timestamp(),
        exp: exp.timestamp(),
        token_type: "refresh".to_string(),
    };

    encode(
        &Header::default(),
        &claims,
        &EncodingKey::from_secret(jwt_secret()),
    )
    .ok()
}

/// Validate a JWT token and return the authenticated user
/// Verify a token's signature and expiry and return its claims.
///
/// This is the stateless half of validation -- it does not know whether the
/// token has been revoked, because that needs the database. `validate_token`
/// wraps it for callers that only need the identity; the middleware and the
/// `AuthenticatedUser` extractor go one step further and check revocation.
pub fn validate_token_claims(token: &str) -> Option<Claims> {
    let validation = Validation::new(Algorithm::HS256);
    let token_data =
        decode::<Claims>(token, &DecodingKey::from_secret(jwt_secret()), &validation).ok()?;
    let claims = token_data.claims;
    if claims.exp < Utc::now().timestamp() {
        return None;
    }
    Some(claims)
}

pub fn validate_token(token: &str) -> Option<AuthenticatedUser> {
    validate_token_claims(token).map(|claims| AuthenticatedUser {
        user_id: claims.sub,
        email: claims.email,
        tier: SubscriptionTier::from_str(&claims.tier).unwrap_or_default(),
    })
}

/// Whether a table has a column, used to add `tokens_valid_after` to an older
/// `users` table without a full migration.
fn user_column_exists(conn: &rusqlite::Connection, col: &str) -> rusqlite::Result<bool> {
    let mut stmt = conn.prepare("PRAGMA table_info(users)")?;
    let names: Vec<String> = stmt
        .query_map([], |r| r.get::<_, String>(1))?
        .collect::<rusqlite::Result<_>>()?;
    Ok(names.iter().any(|n| n == col))
}

/// Whether a token is still current: issued at or after the point the user's
/// tokens were last invalidated (a password change moves that point forward).
///
/// A missing user or a lookup error is treated as *current*, so this can only
/// ever reject a token, never manufacture access for one that failed signature
/// or expiry checks upstream.
fn token_not_revoked(conn: &rusqlite::Connection, user_id: &str, iat: i64) -> bool {
    let valid_after: i64 = conn
        .query_row(
            "SELECT COALESCE(tokens_valid_after, 0) FROM users WHERE id = ?1",
            [user_id],
            |r| r.get(0),
        )
        .unwrap_or(0);
    iat >= valid_after
}

/// Validate refresh token specifically
pub fn validate_refresh_token(token: &str) -> Option<AuthenticatedUser> {
    let validation = Validation::new(Algorithm::HS256);

    let token_data =
        decode::<Claims>(token, &DecodingKey::from_secret(jwt_secret()), &validation).ok()?;

    let claims = token_data.claims;

    // Must be a refresh token
    if claims.token_type != "refresh" {
        return None;
    }

    // Check if token is expired
    if claims.exp < Utc::now().timestamp() {
        return None;
    }

    Some(AuthenticatedUser {
        user_id: claims.sub,
        email: claims.email,
        tier: SubscriptionTier::from_str(&claims.tier).unwrap_or_default(),
    })
}

// =============================================================================
// Database Operations
// =============================================================================

/// Initialize auth tables in the database
pub fn init_auth_tables(conn: &rusqlite::Connection) -> rusqlite::Result<()> {
    conn.execute(
        "CREATE TABLE IF NOT EXISTS users (
            id TEXT PRIMARY KEY,
            email TEXT UNIQUE NOT NULL,
            display_name TEXT NOT NULL,
            password_hash TEXT NOT NULL,
            password_salt TEXT NOT NULL,
            subscription_tier TEXT NOT NULL DEFAULT 'free',
            subscription_expires_at INTEGER,
            created_at INTEGER NOT NULL,
            updated_at INTEGER NOT NULL,
            last_login_at INTEGER,
            email_verified INTEGER DEFAULT 0,
            avatar_url TEXT,
            metadata TEXT,
            tokens_valid_after INTEGER NOT NULL DEFAULT 0
        )",
        [],
    )?;

    // Added after the table shipped: the unix time before which this user's
    // tokens are no longer accepted. A password change bumps it, so tokens
    // minted before the change stop working. Existing databases need the column
    // added; new ones already have it from the CREATE above.
    if !user_column_exists(conn, "tokens_valid_after")? {
        conn.execute(
            "ALTER TABLE users ADD COLUMN tokens_valid_after INTEGER NOT NULL DEFAULT 0",
            [],
        )?;
    }

    conn.execute(
        "CREATE TABLE IF NOT EXISTS refresh_tokens (
            id TEXT PRIMARY KEY,
            user_id TEXT NOT NULL,
            token_hash TEXT NOT NULL,
            expires_at INTEGER NOT NULL,
            created_at INTEGER NOT NULL,
            revoked INTEGER DEFAULT 0,
            device_info TEXT,
            FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE
        )",
        [],
    )?;

    conn.execute(
        "CREATE TABLE IF NOT EXISTS user_sessions (
            id TEXT PRIMARY KEY,
            user_id TEXT NOT NULL,
            device_id TEXT,
            device_name TEXT,
            platform TEXT,
            ip_address TEXT,
            last_active_at INTEGER NOT NULL,
            created_at INTEGER NOT NULL,
            FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE
        )",
        [],
    )?;

    // Create indexes
    conn.execute(
        "CREATE INDEX IF NOT EXISTS idx_users_email ON users(email)",
        [],
    )?;
    conn.execute(
        "CREATE INDEX IF NOT EXISTS idx_refresh_tokens_user_id ON refresh_tokens(user_id)",
        [],
    )?;
    conn.execute(
        "CREATE INDEX IF NOT EXISTS idx_user_sessions_user_id ON user_sessions(user_id)",
        [],
    )?;

    Ok(())
}

// =============================================================================
// HTTP Handlers
// =============================================================================

/// Register a new user
pub async fn register(
    app_state: web::Data<crate::api::state::AppState>,
    body: web::Json<RegisterRequest>,
) -> HttpResponse {
    let db = app_state.db.lock().unwrap();

    // Initialize tables if needed
    if let Err(e) = init_auth_tables(&db.conn) {
        return HttpResponse::InternalServerError().json(serde_json::json!({
            "success": false,
            "error": format!("Database error: {}", e)
        }));
    }

    // Validate input
    if body.email.is_empty() || !body.email.contains('@') {
        return HttpResponse::BadRequest().json(serde_json::json!({
            "success": false,
            "error": "Invalid email address"
        }));
    }

    if body.password.len() < MIN_PASSWORD_CHARS {
        return HttpResponse::BadRequest().json(serde_json::json!({
            "success": false,
            "error": "Password must be at least 8 characters"
        }));
    }

    if is_common_password(&body.password) {
        return HttpResponse::BadRequest().json(serde_json::json!({
            "success": false,
            "error": "That password is one of the most commonly used ones and is \
                      guessed first in an attack. Please choose another."
        }));
    }

    if body.display_name.is_empty() {
        return HttpResponse::BadRequest().json(serde_json::json!({
            "success": false,
            "error": "Display name is required"
        }));
    }

    // Check if email already exists
    let existing: rusqlite::Result<Option<String>> = db
        .conn
        .query_row(
            "SELECT id FROM users WHERE email = ?1",
            rusqlite::params![body.email.to_lowercase()],
            |row| row.get(0),
        )
        .optional();

    if let Ok(Some(_)) = existing {
        return HttpResponse::Conflict().json(serde_json::json!({
            "success": false,
            "error": "Email already registered"
        }));
    }

    // Create user
    let user_id = Uuid::new_v4().to_string();
    // Argon2 embeds its own salt in the hash string, so the `password_salt`
    // column is vestigial for new rows. It stays NOT NULL in the schema, so
    // write an empty string rather than dropping it.
    let salt = String::new();
    let password_hash = match hash_password_argon2(&body.password) {
        Ok(h) => h,
        Err(e) => {
            return HttpResponse::InternalServerError().json(serde_json::json!({
                "success": false,
                "error": format!("Failed to hash password: {}", e)
            }))
        }
    };
    let now = Utc::now().timestamp();

    let result = db.conn.execute(
        "INSERT INTO users (id, email, display_name, password_hash, password_salt, 
                           subscription_tier, created_at, updated_at)
         VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8)",
        rusqlite::params![
            user_id,
            body.email.to_lowercase(),
            body.display_name,
            password_hash,
            salt,
            "free",
            now,
            now,
        ],
    );

    if let Err(e) = result {
        return HttpResponse::InternalServerError().json(serde_json::json!({
            "success": false,
            "error": format!("Failed to create user: {}", e)
        }));
    }

    // Create user object for token generation
    let user = User {
        id: user_id,
        email: body.email.to_lowercase(),
        display_name: body.display_name.clone(),
        password_hash,
        subscription_tier: SubscriptionTier::Free,
        subscription_expires_at: None,
        created_at: now,
        updated_at: now,
        last_login_at: None,
        email_verified: false,
        avatar_url: None,
        metadata: None,
    };

    // Generate tokens
    let access_token = match generate_access_token(&user) {
        Some(t) => t,
        None => {
            return HttpResponse::InternalServerError().json(serde_json::json!({
                "success": false,
                "error": "Failed to generate access token"
            }))
        }
    };

    let refresh_token = match generate_refresh_token(&user) {
        Some(t) => t,
        None => {
            return HttpResponse::InternalServerError().json(serde_json::json!({
                "success": false,
                "error": "Failed to generate refresh token"
            }))
        }
    };

    HttpResponse::Created().json(serde_json::json!({
        "success": true,
        "data": AuthResponse {
            user: PublicUser::from(user),
            access_token,
            refresh_token,
            expires_in: JWT_EXPIRY_HOURS * 3600,
        }
    }))
}

/// Login with email and password
pub async fn login(
    req: HttpRequest,
    app_state: web::Data<crate::api::state::AppState>,
    body: web::Json<LoginRequest>,
) -> HttpResponse {
    // Refuse before touching the database or the password hash: an address that
    // has burned its failure budget is answered 429 without an Argon2
    // verification, so the throttle also caps the CPU a guesser can spend.
    let key = throttle_key(&req);
    if login_throttled(&key) {
        return HttpResponse::TooManyRequests().json(serde_json::json!({
            "success": false,
            "error": "Too many failed login attempts. Try again later.",
        }));
    }

    let db = app_state.db.lock().unwrap();

    // Initialize tables if needed
    if let Err(e) = init_auth_tables(&db.conn) {
        return HttpResponse::InternalServerError().json(serde_json::json!({
            "success": false,
            "error": format!("Database error: {}", e)
        }));
    }

    // Find user by email
    let user_result: rusqlite::Result<(
        String,
        String,
        String,
        String,
        String,
        String,
        Option<i64>,
        i64,
        i64,
        Option<i64>,
        i32,
        Option<String>,
    )> = db.conn.query_row(
        "SELECT id, email, display_name, password_hash, password_salt, 
                    subscription_tier, subscription_expires_at, created_at, updated_at,
                    last_login_at, email_verified, avatar_url
             FROM users WHERE email = ?1",
        rusqlite::params![body.email.to_lowercase()],
        |row| {
            Ok((
                row.get(0)?,
                row.get(1)?,
                row.get(2)?,
                row.get(3)?,
                row.get(4)?,
                row.get(5)?,
                row.get(6)?,
                row.get(7)?,
                row.get(8)?,
                row.get(9)?,
                row.get(10)?,
                row.get(11)?,
            ))
        },
    );

    let (
        id,
        email,
        display_name,
        stored_hash,
        salt,
        tier_str,
        sub_expires,
        created_at,
        updated_at,
        _last_login,
        verified,
        avatar,
    ) = match user_result {
        Ok(data) => data,
        Err(_) => {
            record_login_failure(&key);
            return HttpResponse::Unauthorized().json(serde_json::json!({
                "success": false,
                "error": "Invalid email or password"
            }));
        }
    };

    // Verify password against whatever scheme it was stored under.
    let (matches, needs_upgrade) = verify_password(&body.password, &stored_hash, &salt);
    if !matches {
        record_login_failure(&key);
        return HttpResponse::Unauthorized().json(serde_json::json!({
            "success": false,
            "error": "Invalid email or password"
        }));
    }

    // A correct password clears this address's failure budget.
    clear_login_failures(&key);

    // A legacy SHA-256 account that just proved its password: rewrite the hash
    // as Argon2 now, while we have the plaintext, so it never has to again.
    if needs_upgrade {
        if let Ok(upgraded) = hash_password_argon2(&body.password) {
            let _ = db.conn.execute(
                "UPDATE users SET password_hash = ?1, password_salt = ?2 WHERE id = ?3",
                rusqlite::params![upgraded, "", id],
            );
        }
    }

    // Update last login time
    let now = Utc::now().timestamp();
    let _ = db.conn.execute(
        "UPDATE users SET last_login_at = ?1 WHERE id = ?2",
        rusqlite::params![now, id],
    );

    let user = User {
        id,
        email,
        display_name,
        password_hash: stored_hash,
        subscription_tier: SubscriptionTier::from_str(&tier_str).unwrap_or_default(),
        subscription_expires_at: sub_expires,
        created_at,
        updated_at,
        last_login_at: Some(now),
        email_verified: verified == 1,
        avatar_url: avatar,
        metadata: None,
    };

    // Generate tokens
    let access_token = match generate_access_token(&user) {
        Some(t) => t,
        None => {
            return HttpResponse::InternalServerError().json(serde_json::json!({
                "success": false,
                "error": "Failed to generate access token"
            }))
        }
    };

    let refresh_token = match generate_refresh_token(&user) {
        Some(t) => t,
        None => {
            return HttpResponse::InternalServerError().json(serde_json::json!({
                "success": false,
                "error": "Failed to generate refresh token"
            }))
        }
    };

    HttpResponse::Ok().json(serde_json::json!({
        "success": true,
        "data": AuthResponse {
            user: PublicUser::from(user),
            access_token,
            refresh_token,
            expires_in: JWT_EXPIRY_HOURS * 3600,
        }
    }))
}

/// Refresh access token using refresh token
pub async fn refresh_token(
    app_state: web::Data<crate::api::state::AppState>,
    body: web::Json<RefreshTokenRequest>,
) -> HttpResponse {
    // Validate refresh token: signature, expiry, and that it is a refresh
    // token (not an access token replayed here).
    let claims = match validate_token_claims(&body.refresh_token) {
        Some(c) if c.token_type == "refresh" => c,
        _ => {
            return HttpResponse::Unauthorized().json(serde_json::json!({
                "success": false,
                "error": "Invalid or expired refresh token"
            }))
        }
    };

    let db = app_state.db.lock().unwrap();

    // Revocation applies to refresh tokens too, or it does not apply at all: a
    // refresh token minted before a password change would otherwise let its
    // holder keep issuing fresh access tokens indefinitely, defeating the point
    // of changing the password.
    if !token_not_revoked(&db.conn, &claims.sub, claims.iat) {
        return HttpResponse::Unauthorized().json(serde_json::json!({
            "success": false,
            "error": "Invalid or expired refresh token"
        }));
    }
    let auth_user = AuthenticatedUser {
        user_id: claims.sub,
        email: claims.email,
        tier: SubscriptionTier::from_str(&claims.tier).unwrap_or_default(),
    };

    // Get current user data
    let user_result: rusqlite::Result<(
        String,
        String,
        String,
        String,
        Option<i64>,
        i64,
        i64,
        i32,
        Option<String>,
    )> = db.conn.query_row(
        "SELECT id, email, display_name, subscription_tier, subscription_expires_at,
                    created_at, updated_at, email_verified, avatar_url
             FROM users WHERE id = ?1",
        rusqlite::params![auth_user.user_id],
        |row| {
            Ok((
                row.get(0)?,
                row.get(1)?,
                row.get(2)?,
                row.get(3)?,
                row.get(4)?,
                row.get(5)?,
                row.get(6)?,
                row.get(7)?,
                row.get(8)?,
            ))
        },
    );

    let (id, email, display_name, tier_str, sub_expires, created_at, updated_at, verified, avatar) =
        match user_result {
            Ok(data) => data,
            Err(_) => {
                return HttpResponse::NotFound().json(serde_json::json!({
                    "success": false,
                    "error": "User not found"
                }))
            }
        };

    let user = User {
        id,
        email,
        display_name,
        password_hash: String::new(), // Not needed for token generation
        subscription_tier: SubscriptionTier::from_str(&tier_str).unwrap_or_default(),
        subscription_expires_at: sub_expires,
        created_at,
        updated_at,
        last_login_at: None,
        email_verified: verified == 1,
        avatar_url: avatar,
        metadata: None,
    };

    // Generate new tokens
    let access_token = match generate_access_token(&user) {
        Some(t) => t,
        None => {
            return HttpResponse::InternalServerError().json(serde_json::json!({
                "success": false,
                "error": "Failed to generate access token"
            }))
        }
    };

    let new_refresh_token = match generate_refresh_token(&user) {
        Some(t) => t,
        None => {
            return HttpResponse::InternalServerError().json(serde_json::json!({
                "success": false,
                "error": "Failed to generate refresh token"
            }))
        }
    };

    HttpResponse::Ok().json(serde_json::json!({
        "success": true,
        "data": AuthResponse {
            user: PublicUser::from(user),
            access_token,
            refresh_token: new_refresh_token,
            expires_in: JWT_EXPIRY_HOURS * 3600,
        }
    }))
}

/// Get current user profile
pub async fn get_profile(
    app_state: web::Data<crate::api::state::AppState>,
    auth_user: AuthenticatedUser,
) -> HttpResponse {
    if auth_user.user_id.is_empty() {
        return HttpResponse::Unauthorized().json(serde_json::json!({
            "success": false,
            "error": "Not authenticated"
        }));
    }

    let db = app_state.db.lock().unwrap();

    let user_result: rusqlite::Result<(
        String,
        String,
        String,
        String,
        Option<i64>,
        i64,
        i64,
        i32,
        Option<String>,
    )> = db.conn.query_row(
        "SELECT id, email, display_name, subscription_tier, subscription_expires_at,
                    created_at, updated_at, email_verified, avatar_url
             FROM users WHERE id = ?1",
        rusqlite::params![auth_user.user_id],
        |row| {
            Ok((
                row.get(0)?,
                row.get(1)?,
                row.get(2)?,
                row.get(3)?,
                row.get(4)?,
                row.get(5)?,
                row.get(6)?,
                row.get(7)?,
                row.get(8)?,
            ))
        },
    );

    match user_result {
        Ok((
            id,
            email,
            display_name,
            tier_str,
            sub_expires,
            created_at,
            updated_at,
            verified,
            avatar,
        )) => {
            let user = User {
                id,
                email,
                display_name,
                password_hash: String::new(),
                subscription_tier: SubscriptionTier::from_str(&tier_str).unwrap_or_default(),
                subscription_expires_at: sub_expires,
                created_at,
                updated_at,
                last_login_at: None,
                email_verified: verified == 1,
                avatar_url: avatar,
                metadata: None,
            };

            HttpResponse::Ok().json(serde_json::json!({
                "success": true,
                "data": PublicUser::from(user)
            }))
        }
        Err(_) => HttpResponse::NotFound().json(serde_json::json!({
            "success": false,
            "error": "User not found"
        })),
    }
}

/// Update user profile
pub async fn update_profile(
    app_state: web::Data<crate::api::state::AppState>,
    auth_user: AuthenticatedUser,
    body: web::Json<UpdateProfileRequest>,
) -> HttpResponse {
    if auth_user.user_id.is_empty() {
        return HttpResponse::Unauthorized().json(serde_json::json!({
            "success": false,
            "error": "Not authenticated"
        }));
    }

    // Scope the db lock so it's released before calling get_profile
    {
        let db = app_state.db.lock().unwrap();
        let now = Utc::now().timestamp();

        // Build update query
        let mut updates = Vec::new();
        let mut params: Vec<Box<dyn rusqlite::ToSql>> = Vec::new();

        if let Some(ref name) = body.display_name {
            updates.push("display_name = ?");
            params.push(Box::new(name.clone()));
        }

        if let Some(ref avatar) = body.avatar_url {
            updates.push("avatar_url = ?");
            params.push(Box::new(avatar.clone()));
        }

        if updates.is_empty() {
            return HttpResponse::BadRequest().json(serde_json::json!({
                "success": false,
                "error": "No fields to update"
            }));
        }

        updates.push("updated_at = ?");
        params.push(Box::new(now));

        let query = format!("UPDATE users SET {} WHERE id = ?", updates.join(", "));
        params.push(Box::new(auth_user.user_id.clone()));

        let params_refs: Vec<&dyn rusqlite::ToSql> = params.iter().map(|p| p.as_ref()).collect();

        if let Err(e) = db.conn.execute(&query, params_refs.as_slice()) {
            return HttpResponse::InternalServerError().json(serde_json::json!({
                "success": false,
                "error": format!("Failed to update profile: {}", e)
            }));
        }
    } // db lock released here

    // Return updated profile
    get_profile(app_state, auth_user).await
}

/// Change password
pub async fn change_password(
    app_state: web::Data<crate::api::state::AppState>,
    auth_user: AuthenticatedUser,
    body: web::Json<ChangePasswordRequest>,
) -> HttpResponse {
    if auth_user.user_id.is_empty() {
        return HttpResponse::Unauthorized().json(serde_json::json!({
            "success": false,
            "error": "Not authenticated"
        }));
    }

    if body.new_password.len() < MIN_PASSWORD_CHARS {
        return HttpResponse::BadRequest().json(serde_json::json!({
            "success": false,
            "error": "New password must be at least 8 characters"
        }));
    }

    if is_common_password(&body.new_password) {
        return HttpResponse::BadRequest().json(serde_json::json!({
            "success": false,
            "error": "That password is one of the most commonly used ones and is \
                      guessed first in an attack. Please choose another."
        }));
    }

    let db = app_state.db.lock().unwrap();

    // Get current password hash and salt
    let creds: rusqlite::Result<(String, String)> = db.conn.query_row(
        "SELECT password_hash, password_salt FROM users WHERE id = ?1",
        rusqlite::params![auth_user.user_id],
        |row| Ok((row.get(0)?, row.get(1)?)),
    );

    let (stored_hash, salt) = match creds {
        Ok(c) => c,
        Err(_) => {
            return HttpResponse::NotFound().json(serde_json::json!({
                "success": false,
                "error": "User not found"
            }))
        }
    };

    // Verify current password (old scheme or new).
    let (matches, _) = verify_password(&body.current_password, &stored_hash, &salt);
    if !matches {
        return HttpResponse::Unauthorized().json(serde_json::json!({
            "success": false,
            "error": "Current password is incorrect"
        }));
    }

    // Update password. The new hash is always Argon2; the salt column goes
    // empty because Argon2 carries its own.
    let new_salt = String::new();
    let new_hash = match hash_password_argon2(&body.new_password) {
        Ok(h) => h,
        Err(e) => {
            return HttpResponse::InternalServerError().json(serde_json::json!({
                "success": false,
                "error": format!("Failed to hash password: {}", e)
            }))
        }
    };
    let now = Utc::now().timestamp();

    // `tokens_valid_after = now` invalidates every token issued before this
    // change. Changing a password is how you respond to it being compromised;
    // if the old tokens kept working, the response would not actually lock the
    // attacker out. The `- 1` guards a same-second race: a token minted in the
    // same second as the change (iat == now) should still be rejected, and iat
    // >= valid_after is the accept test, so the cut-off sits one second ahead.
    if let Err(e) = db.conn.execute(
        "UPDATE users SET password_hash = ?1, password_salt = ?2, updated_at = ?3,
                tokens_valid_after = ?4 WHERE id = ?5",
        rusqlite::params![new_hash, new_salt, now, now + 1, auth_user.user_id],
    ) {
        return HttpResponse::InternalServerError().json(serde_json::json!({
            "success": false,
            "error": format!("Failed to update password: {}", e)
        }));
    }

    HttpResponse::Ok().json(serde_json::json!({
        "success": true,
        "message": "Password updated successfully"
    }))
}

/// Get subscription details
pub async fn get_subscription(
    app_state: web::Data<crate::api::state::AppState>,
    auth_user: AuthenticatedUser,
) -> HttpResponse {
    if auth_user.user_id.is_empty() {
        return HttpResponse::Unauthorized().json(serde_json::json!({
            "success": false,
            "error": "Not authenticated"
        }));
    }

    let db = app_state.db.lock().unwrap();

    let result: rusqlite::Result<(String, Option<i64>)> = db.conn.query_row(
        "SELECT subscription_tier, subscription_expires_at FROM users WHERE id = ?1",
        rusqlite::params![auth_user.user_id],
        |row| Ok((row.get(0)?, row.get(1)?)),
    );

    match result {
        Ok((tier_str, expires_at)) => {
            let tier = SubscriptionTier::from_str(&tier_str).unwrap_or_default();
            let features = SubscriptionFeatures::for_tier(tier);

            HttpResponse::Ok().json(serde_json::json!({
                "success": true,
                "data": {
                    "tier": tier_str,
                    "expiresAt": expires_at,
                    "features": features,
                    "isActive": expires_at.map(|exp| exp > Utc::now().timestamp()).unwrap_or(true),
                }
            }))
        }
        Err(_) => HttpResponse::NotFound().json(serde_json::json!({
            "success": false,
            "error": "User not found"
        })),
    }
}

/// Upgrade subscription (simulated - in production would integrate with payment provider)
pub async fn upgrade_subscription(
    app_state: web::Data<crate::api::state::AppState>,
    auth_user: AuthenticatedUser,
    body: web::Json<UpgradeSubscriptionRequest>,
) -> HttpResponse {
    if auth_user.user_id.is_empty() {
        return HttpResponse::Unauthorized().json(serde_json::json!({
            "success": false,
            "error": "Not authenticated"
        }));
    }

    let new_tier = match SubscriptionTier::from_str(&body.tier) {
        Some(t) => t,
        None => {
            return HttpResponse::BadRequest().json(serde_json::json!({
                "success": false,
                "error": "Invalid subscription tier"
            }))
        }
    };

    // In production, this would:
    // 1. Validate the payment token with Stripe/PayPal
    // 2. Create a subscription in the payment provider
    // 3. Set up webhooks for subscription events

    let db = app_state.db.lock().unwrap();
    let now = Utc::now().timestamp();
    let expires_at = if new_tier == SubscriptionTier::Free {
        None
    } else {
        // 1 year subscription
        Some(now + (365 * 24 * 3600))
    };

    if let Err(e) = db.conn.execute(
        "UPDATE users SET subscription_tier = ?1, subscription_expires_at = ?2, updated_at = ?3 WHERE id = ?4",
        rusqlite::params![new_tier.as_str(), expires_at, now, auth_user.user_id],
    ) {
        return HttpResponse::InternalServerError().json(serde_json::json!({
            "success": false,
            "error": format!("Failed to upgrade subscription: {}", e)
        }));
    }

    let features = SubscriptionFeatures::for_tier(new_tier);

    HttpResponse::Ok().json(serde_json::json!({
        "success": true,
        "data": {
            "tier": new_tier.as_str(),
            "expiresAt": expires_at,
            "features": features,
            "message": "Subscription upgraded successfully"
        }
    }))
}

/// Logout, invalidating the caller's tokens server-side.
///
/// This moves the user's `tokens_valid_after` forward, the same revocation
/// point a password change uses, so both the access token and the refresh
/// token presented up to now stop being accepted. Until this was wired up,
/// logout only returned 200: the client dropped its copy of the token while
/// the token itself stayed valid until expiry, so anyone who had captured it
/// still had a working credential and "log out" was a false assurance.
///
/// Note the scope. `tokens_valid_after` is a single per-user instant, so this
/// is a global sign-out: every session that user has, on every device, ends
/// here. That is the honest behaviour for the primitive available -- `Claims`
/// carries no `jti`, so there is nothing to revoke an individual token by.
/// Per-device logout would need a token id in the claims plus somewhere to
/// record which ones were withdrawn.
///
/// The `- 1`-style `now + 1` cut-off is the same same-second race guard as in
/// `change_password`: a token minted in the same second as the logout
/// (`iat == now`) must still be rejected, and `iat >= valid_after` is the
/// accept test, so the cut-off sits one second ahead.
pub async fn logout(
    app_state: web::Data<crate::api::state::AppState>,
    auth_user: AuthenticatedUser,
) -> HttpResponse {
    let now = Utc::now().timestamp();
    let db = app_state.db.lock().unwrap();

    if let Err(e) = db.conn.execute(
        "UPDATE users SET tokens_valid_after = ?1 WHERE id = ?2",
        rusqlite::params![now + 1, auth_user.user_id],
    ) {
        // Report the failure rather than answering 200. A client told its
        // logout succeeded will discard the token and stop showing a signed-in
        // state, which would leave a live credential behind with nobody aware.
        return HttpResponse::InternalServerError().json(serde_json::json!({
            "success": false,
            "error": format!("Failed to invalidate session: {}", e)
        }));
    }

    HttpResponse::Ok().json(serde_json::json!({
        "success": true,
        "message": "Logged out successfully"
    }))
}

// =============================================================================
// Route Configuration
// =============================================================================

pub fn configure_auth_routes(cfg: &mut web::ServiceConfig) {
    cfg.service(
        web::scope("/auth")
            .route("/register", web::post().to(register))
            .route("/login", web::post().to(login))
            .route("/refresh", web::post().to(refresh_token))
            .route("/logout", web::post().to(logout))
            .route("/profile", web::get().to(get_profile))
            .route("/profile", web::put().to(update_profile))
            .route("/password", web::put().to(change_password))
            .route("/subscription", web::get().to(get_subscription))
            .route(
                "/subscription/upgrade",
                web::post().to(upgrade_subscription),
            ),
    );
}

#[cfg(test)]
mod security_tests {
    use super::*;

    /// A token is accepted until the user's `tokens_valid_after` moves past its
    /// `iat`, and rejected after -- the mechanism a password change uses to
    /// invalidate outstanding tokens. A missing user is treated as current, so
    /// the check can only reject, never invent access.
    #[test]
    fn a_token_is_revoked_once_valid_after_passes_its_issue_time() {
        let conn = rusqlite::Connection::open_in_memory().unwrap();
        init_auth_tables(&conn).unwrap();
        conn.execute(
            "INSERT INTO users (id, email, display_name, password_hash, password_salt,
                                created_at, updated_at, tokens_valid_after)
             VALUES ('u1','u1@e.co','U','h','', 0, 0, 0)",
            [],
        )
        .unwrap();

        // Fresh table: cut-off 0, any token is current.
        assert!(token_not_revoked(&conn, "u1", 1000));

        // Password change moves the cut-off to 1500.
        conn.execute(
            "UPDATE users SET tokens_valid_after = 1500 WHERE id = 'u1'",
            [],
        )
        .unwrap();
        assert!(
            !token_not_revoked(&conn, "u1", 1000),
            "old token still current"
        );
        assert!(
            token_not_revoked(&conn, "u1", 1500),
            "cut-off itself accepted"
        );
        assert!(token_not_revoked(&conn, "u1", 2000), "newer token current");

        // Unknown user -> current (reject-only guarantee).
        assert!(token_not_revoked(&conn, "nobody", 1));
    }

    /// The common-password check catches the passwords that actually get
    /// guessed, ignores case, and leaves strong passwords alone.
    ///
    /// `password` and `12345678` both clear the eight-character minimum, so
    /// length alone accepted the two most-guessed passwords there are. The
    /// case-insensitivity matters as much as the list: a check that only
    /// matched the lowercase spelling would be sidestepped by the shift key.
    #[test]
    fn the_most_guessed_passwords_are_refused_whatever_their_case() {
        // The two that motivated this: long enough, and still top of every list.
        assert!(is_common_password("password"));
        assert!(is_common_password("12345678"));

        // Case must not be an escape hatch.
        assert!(is_common_password("Password"));
        assert!(is_common_password("PASSWORD"));
        assert!(is_common_password("PaSsWoRd"));
        assert!(is_common_password("QwErTy123"));

        // Decorations that feel clever and are not.
        assert!(is_common_password("password123"));
        assert!(is_common_password("passw0rd"));
        assert!(is_common_password("p@ssw0rd"));
        assert!(is_common_password("letmein123"));
        assert!(is_common_password("qwertyuiop"));

        // Entries below the length minimum stay listed so the two rules stay
        // independent -- raising or lowering the minimum must not change this.
        assert!(is_common_password("qwerty"));
        assert!(is_common_password("123456"));

        // Strong passwords must pass untouched; a false positive here locks a
        // user out of a password that was fine.
        assert!(!is_common_password("Aud1t-Probe-Passw0rd!"));
        assert!(!is_common_password("correct horse battery staple"));
        assert!(!is_common_password("wR7#pq2Lm!vZ"));
        // Contains a listed entry but is not one: matching is whole-string.
        assert!(!is_common_password("mypasswordislong"));
        assert!(!is_common_password("password!extra"));
    }

    /// Logout has to move the same cut-off a password change moves, and it has
    /// to land *ahead* of a token minted in the same second.
    ///
    /// This is the regression guard for logout having been a no-op: it
    /// answered 200 while the token it "invalidated" kept working until
    /// expiry, so a captured credential survived the one action a user takes
    /// to shut it down. The `now + 1` is what makes the same-second case fail
    /// closed -- `iat >= valid_after` is the accept test, so writing `now`
    /// would leave a token issued that same second still valid.
    #[test]
    fn logout_revokes_tokens_issued_up_to_and_including_that_second() {
        let conn = rusqlite::Connection::open_in_memory().unwrap();
        init_auth_tables(&conn).unwrap();
        conn.execute(
            "INSERT INTO users (id, email, display_name, password_hash, password_salt,
                                created_at, updated_at, tokens_valid_after)
             VALUES ('u1','u1@e.co','U','h','', 0, 0, 0)",
            [],
        )
        .unwrap();

        let now = 1_000_000i64;
        assert!(
            token_not_revoked(&conn, "u1", now),
            "token is current before logout"
        );

        // Exactly what the logout handler writes.
        conn.execute(
            "UPDATE users SET tokens_valid_after = ?1 WHERE id = ?2",
            rusqlite::params![now + 1, "u1"],
        )
        .unwrap();

        assert!(
            !token_not_revoked(&conn, "u1", now - 60),
            "token from before logout must be rejected"
        );
        assert!(
            !token_not_revoked(&conn, "u1", now),
            "token minted in the same second as logout must be rejected"
        );
        assert!(
            token_not_revoked(&conn, "u1", now + 1),
            "a token issued after logout must still work, or login is broken"
        );
    }

    /// The login throttle blocks an address after its failure budget, and a
    /// success (modelled here as a clear) resets it. Uses a unique key per test
    /// so the process-global map does not couple tests.
    #[test]
    fn login_throttle_blocks_after_the_budget_and_resets_on_success() {
        let key = format!("test-key-{}", uuid::Uuid::new_v4());
        assert!(!login_throttled(&key));
        for _ in 0..LOGIN_MAX_FAILURES {
            record_login_failure(&key);
        }
        assert!(login_throttled(&key), "not throttled after the budget");
        clear_login_failures(&key);
        assert!(
            !login_throttled(&key),
            "a success did not reset the counter"
        );
    }

    /// One failure short of the budget is still allowed -- an ordinary user who
    /// mistypes is not locked out.
    #[test]
    fn login_throttle_allows_up_to_the_budget() {
        let key = format!("test-key-{}", uuid::Uuid::new_v4());
        for _ in 0..(LOGIN_MAX_FAILURES - 1) {
            record_login_failure(&key);
        }
        assert!(!login_throttled(&key));
    }

    /// A token this process signs must verify; a byte-flipped one must not.
    #[test]
    fn a_token_signed_now_round_trips_and_tampering_is_rejected() {
        let user = User {
            id: "u1".into(),
            email: "u1@example.com".into(),
            display_name: "U".into(),
            password_hash: String::new(),
            subscription_tier: SubscriptionTier::Free,
            subscription_expires_at: None,
            created_at: 0,
            updated_at: 0,
            last_login_at: None,
            email_verified: false,
            avatar_url: None,
            metadata: None,
        };
        let token = generate_access_token(&user).expect("signed");
        assert!(validate_token(&token).is_some());

        let mut bad = token.clone();
        bad.pop();
        bad.push(if token.ends_with('a') { 'b' } else { 'a' });
        assert!(validate_token(&bad).is_none(), "a tampered token verified");
    }

    /// The exploit that motivated this: a token signed with the old
    /// compiled-in constant must NOT validate, because that constant is no
    /// longer the signing secret.
    #[test]
    fn a_token_forged_with_the_old_hardcoded_secret_is_rejected() {
        use jsonwebtoken::{encode, EncodingKey, Header};
        const OLD_SECRET: &[u8] = b"ironbridge_jwt_secret_key_change_in_production_2024";
        let claims = Claims {
            sub: "attacker".into(),
            email: "attacker@evil.test".into(),
            tier: "enterprise".into(),
            iat: Utc::now().timestamp(),
            exp: (Utc::now() + Duration::hours(1)).timestamp(),
            token_type: "access".into(),
        };
        let forged = encode(
            &Header::default(),
            &claims,
            &EncodingKey::from_secret(OLD_SECRET),
        )
        .expect("forge");
        assert!(
            validate_token(&forged).is_none(),
            "a token forged with the leaked constant still validated"
        );
    }

    /// New passwords are stored as Argon2, never the legacy SHA-256.
    #[test]
    fn new_hashes_are_argon2_and_verify() {
        let hash = hash_password_argon2("correct horse battery staple").expect("hash");
        assert!(
            hash.starts_with("$argon2"),
            "not an argon2 PHC string: {hash}"
        );
        let (ok, upgrade) = verify_password("correct horse battery staple", &hash, "");
        assert!(ok);
        assert!(!upgrade, "an argon2 hash should not ask to be upgraded");
        let (bad, _) = verify_password("wrong password", &hash, "");
        assert!(!bad);
    }

    /// Two hashes of the same password differ (per-hash random salt), so the
    /// store is not a lookup table of equal passwords to equal hashes.
    #[test]
    fn argon2_salts_are_unique_per_hash() {
        let a = hash_password_argon2("same").unwrap();
        let b = hash_password_argon2("same").unwrap();
        assert_ne!(a, b);
    }

    /// A legacy SHA-256 account still logs in, and asks to be upgraded.
    #[test]
    fn a_legacy_hash_verifies_and_requests_upgrade() {
        let salt = "legacy-salt";
        let legacy = legacy_sha256("hunter2", salt);
        assert!(!legacy.starts_with("$argon2"));

        let (ok, upgrade) = verify_password("hunter2", &legacy, salt);
        assert!(ok, "legacy password did not verify");
        assert!(upgrade, "legacy verify did not flag an upgrade");

        let (bad, _) = verify_password("not it", &legacy, salt);
        assert!(!bad);
    }

    /// The legacy comparison is length-checked; a truncated hash never matches.
    #[test]
    fn legacy_compare_rejects_mismatched_lengths() {
        assert!(!constant_time_eq(b"abc", b"abcd"));
        assert!(constant_time_eq(b"abcd", b"abcd"));
        assert!(!constant_time_eq(b"abcd", b"abce"));
    }
}

#[cfg(test)]
mod enforcement_tests {
    use super::*;

    #[test]
    fn the_gate_is_default_deny_with_a_small_open_list() {
        // Gated: the API, and -- the bug this replaced -- every sensitive
        // root-mounted scope that used to be reachable with no token.
        assert!(path_requires_auth("/api/sessions"));
        assert!(path_requires_auth("/api/swarms/x/agents"));
        assert!(path_requires_auth("/sync/snapshot"));
        assert!(path_requires_auth("/recording/sessions"));
        assert!(path_requires_auth("/audit"));
        assert!(path_requires_auth("/retention/policies"));
        assert!(path_requires_auth("/webhooks"));
        assert!(path_requires_auth("/graphql"));
        assert!(path_requires_auth("/ws"));
        // A scope nobody has thought of yet is gated by default.
        assert!(path_requires_auth("/some/future/scope"));

        // Open: liveness, the login handshake, the SSO handshake, the docs.
        assert!(!path_requires_auth("/api/health"));
        assert!(!path_requires_auth("/health"));
        assert!(!path_requires_auth("/api/system/health"));
        assert!(!path_requires_auth("/auth/login"));
        assert!(!path_requires_auth("/auth/register"));
        assert!(!path_requires_auth("/sso/login"));
        assert!(!path_requires_auth("/oidc/callback"));
        assert!(!path_requires_auth("/docs"));
        assert!(!path_requires_auth("/docs/openapi.yaml"));

        // An open prefix must not extend past the path separator. `/docs` was
        // once listed as a prefix rather than an exact path, which opened
        // every route merely starting with those characters. Nothing named
        // like this exists yet, which is the point: the gate should already be
        // shut when one is added.
        assert!(path_requires_auth("/docs-admin"));
        assert!(path_requires_auth("/docsecrets"));
        assert!(path_requires_auth("/authorize"));
        assert!(path_requires_auth("/ssoconfig"));
        assert!(path_requires_auth("/oidcadmin"));
        assert!(path_requires_auth("/healthz"));
    }

    /// The middleware, end to end, in both modes.
    ///
    /// A `from_fn` wrap is only meaningful mounted on an app, so this drives it
    /// through one: with enforcement off every route is open; with it on, a
    /// bare `/api` route is 401 without a token, 200 with a freshly signed one,
    /// and `/api/health` stays open either way.
    #[tokio::test]
    async fn the_gate_opens_and_closes_with_a_valid_token() {
        use actix_web::{middleware::from_fn, web, App, HttpResponse};

        async fn ok() -> HttpResponse {
            HttpResponse::Ok().json(serde_json::json!({ "ok": true }))
        }

        let app = actix_web::test::init_service(
            App::new().wrap(from_fn(require_auth)).service(
                web::scope("/api")
                    .route("/health", web::get().to(ok))
                    .route("/sessions", web::get().to(ok)),
            ),
        )
        .await;

        let user = User {
            id: "u1".into(),
            email: "u1@example.com".into(),
            display_name: "U".into(),
            password_hash: String::new(),
            subscription_tier: SubscriptionTier::Free,
            subscription_expires_at: None,
            created_at: 0,
            updated_at: 0,
            last_login_at: None,
            email_verified: false,
            avatar_url: None,
            metadata: None,
        };
        let token = generate_access_token(&user).expect("token");

        let get = |uri: &str, bearer: Option<&str>| {
            let mut r = actix_web::test::TestRequest::get().uri(uri);
            if let Some(b) = bearer {
                r = r.insert_header(("Authorization", format!("Bearer {b}")));
            }
            r.to_request()
        };

        // `auth_required()` reads the env once and memoizes. The test asserts
        // the two branches directly rather than fighting that global, so it is
        // deterministic regardless of the runner's environment.
        if auth_required() {
            // Enforcement is on in this environment.
            let s = actix_web::test::call_service(&app, get("/api/sessions", None)).await;
            assert_eq!(s.status(), 401);
            let s = actix_web::test::call_service(&app, get("/api/sessions", Some(&token))).await;
            assert_eq!(s.status(), 200);
            let s = actix_web::test::call_service(&app, get("/api/health", None)).await;
            assert_eq!(s.status(), 200);
        } else {
            // IRONBRIDGE_DISABLE_AUTH is set in this environment: everything open.
            let s = actix_web::test::call_service(&app, get("/api/sessions", None)).await;
            assert_eq!(s.status(), 200);
            let s = actix_web::test::call_service(&app, get("/api/health", None)).await;
            assert_eq!(s.status(), 200);
        }
    }

    #[test]
    fn query_token_accepted_only_on_websocket_routes() {
        let user = User {
            id: "u1".into(),
            email: "u1@example.com".into(),
            display_name: "U".into(),
            password_hash: String::new(),
            subscription_tier: SubscriptionTier::Free,
            subscription_expires_at: None,
            created_at: 0,
            updated_at: 0,
            last_login_at: None,
            email_verified: false,
            avatar_url: None,
            metadata: None,
        };
        let token = generate_access_token(&user).expect("token");

        // A bare `?token=` on an ordinary request must NOT authenticate: the
        // query fallback exists only so WebSocket handshakes (which cannot set
        // an Authorization header) can carry a token. Honouring it elsewhere
        // would leak tokens into logs, history, and Referer headers.
        let plain = actix_web::test::TestRequest::get()
            .uri(&format!("/api/sessions?token={token}"))
            .to_srv_request();
        assert!(
            request_token(&plain).is_none(),
            "a ?token= on a non-WebSocket route must not authenticate"
        );

        // Spoof guard: setting `Upgrade: websocket` on an ordinary route must
        // still NOT authenticate from the URL. The gate is the route path, not
        // this client-settable header -- otherwise the token would ride in (and
        // be logged from) the URL of a normal API request.
        let spoof = actix_web::test::TestRequest::get()
            .uri(&format!("/api/sessions?token={token}"))
            .insert_header(("Upgrade", "websocket"))
            .to_srv_request();
        assert!(
            request_token(&spoof).is_none(),
            "a forged Upgrade header on a non-WS route must not authenticate from the URL"
        );

        // On a WebSocket route the query fallback IS accepted (legacy path).
        let ws_query = actix_web::test::TestRequest::get()
            .uri(&format!("/ws?token={token}"))
            .to_srv_request();
        assert!(
            request_token(&ws_query).is_some(),
            "a ?token= on the /ws route must authenticate"
        );

        // Preferred WS channel: token in the Sec-WebSocket-Protocol header.
        let ws_subproto = actix_web::test::TestRequest::get()
            .uri("/ws")
            .insert_header(("Sec-WebSocket-Protocol", format!("bearer, {token}")))
            .to_srv_request();
        assert!(
            request_token(&ws_subproto).is_some(),
            "a bearer subprotocol on /ws must authenticate"
        );

        // The Authorization header path is unaffected on ordinary requests.
        let hdr = actix_web::test::TestRequest::get()
            .uri("/api/sessions")
            .insert_header(("Authorization", format!("Bearer {token}")))
            .to_srv_request();
        assert!(
            request_token(&hdr).is_some(),
            "the Authorization header must still authenticate ordinary requests"
        );
    }
}