lific 2.8.0

Local-first, lightweight issue tracker. Single binary, SQLite-backed, MCP-native.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
use axum::{
    body::Body,
    extract::State,
    http::{HeaderMap, Method, Request, StatusCode},
    middleware::Next,
    response::{IntoResponse, Response},
};
use rusqlite::params;
use tracing::{info, warn};

use api_keys_simplified::{ApiKeyManagerV0, Environment, ExposeSecret, KeyStatus};

use crate::db::DbPool;
use crate::db::models::AuthUser;

#[derive(Clone)]
pub struct AuthState {
    pub db: DbPool,
    pub manager: ApiKeyManagerV0,
    pub public_url: String,
    /// LIF-294: mirror of `[auth] required`. When false, a request with no
    /// credential at all passes as operator-equivalent; see `require_api_key`.
    pub required: bool,
}

/// Encode bytes as a lowercase hex string.
///
/// LIF-383: the one hex encoder in the tree. Four copies of this loop used to
/// live in oauth.rs, mcp/mod.rs, auth.rs and db/queries/users.rs.
pub(crate) fn hex_encode(bytes: &[u8]) -> String {
    use std::fmt::Write as _;

    let mut encoded = String::with_capacity(bytes.len() * 2);
    for byte in bytes {
        write!(&mut encoded, "{byte:02x}").expect("writing to a String cannot fail");
    }
    encoded
}

/// SHA-256 a byte slice and return the lowercase hex digest. This is how every
/// bearer credential (session tokens, OAuth access tokens, device codes) is
/// stored and looked up.
pub(crate) fn sha256_hex(bytes: &[u8]) -> String {
    use sha2::{Digest, Sha256};
    hex_encode(&Sha256::digest(bytes))
}

/// Create the API key manager with our prefix.
pub fn create_key_manager() -> Result<ApiKeyManagerV0, String> {
    ApiKeyManagerV0::init_default_config("lific_sk")
        .map_err(|e| format!("failed to init key manager: {e}"))
}

/// Generate a new API key, store the hash, return the plaintext (shown once).
///
/// `user_id` binds the key to a user in the same insert (LIF-391). `None`
/// leaves it unbound, which resolves as the operator identity, so pass a user
/// whenever the key belongs to one.
pub fn create_api_key(
    db: &DbPool,
    manager: &ApiKeyManagerV0,
    name: &str,
    user_id: Option<i64>,
) -> Result<String, crate::error::LificError> {
    create_api_key_with_expiry(db, manager, name, None, user_id)
}

/// Like [`create_api_key`] but writes an optional `expires_at` (ISO 8601). Once
/// past, the auth path (LIF-131) refuses the key. `None` means never expires.
pub fn create_api_key_with_expiry(
    db: &DbPool,
    manager: &ApiKeyManagerV0,
    name: &str,
    expires_at: Option<&str>,
    user_id: Option<i64>,
) -> Result<String, crate::error::LificError> {
    let prepared = PreparedApiKey::generate(manager)?;
    // One immediate transaction, not a bare `write()`. `insert` reads the
    // name's existing rows, deletes a matching tombstone and inserts: three
    // statements that must be one atomic unit, and that must serialize against
    // an account lockdown running in *another process* (the CLI resetting a
    // password while the server is up). A bare writer guard only serializes
    // within this process.
    db.transaction(|tx| prepared.insert(tx, name, expires_at, user_id))
}

/// Key material generated but not yet written.
///
/// Splitting generation from the insert lets a caller do the expensive,
/// side-effect-free half (CSPRNG draw plus the manager's hashing) *outside* the
/// writer, then take the writer once and revalidate its authorization in the
/// same transaction that stores the key. Nothing here is persisted until
/// [`PreparedApiKey::insert`] runs, so an abandoned preparation leaves no trace
/// and no live credential.
///
/// The plaintext lives in this struct and is handed to the caller exactly once,
/// by `insert`. It is never written to the database, which stores only the
/// hash and the derived lookup id.
pub struct PreparedApiKey {
    plaintext: String,
    hash: String,
    key_id: String,
}

impl PreparedApiKey {
    /// Draw fresh key material. Touches no connection.
    pub fn generate(manager: &ApiKeyManagerV0) -> Result<Self, crate::error::LificError> {
        let api_key = manager.generate(Environment::production()).map_err(|e| {
            crate::error::LificError::Internal(format!("key generation failed: {e}"))
        })?;
        Ok(Self {
            plaintext: api_key.key().expose_secret().to_string(),
            hash: api_key.expose_hash().hash().to_string(),
            key_id: api_key.expose_hash().key_id().to_string(),
        })
    }

    /// Claim the name and store the key on `conn`, returning the plaintext.
    ///
    /// Takes a borrowed connection rather than the pool so the caller controls
    /// the transaction: claiming the name and inserting are several statements
    /// and must not be separable, and a caller that revalidated its
    /// authorization moments earlier needs that revalidation to be inside the
    /// same transaction as this write.
    ///
    /// `api_keys.name` is globally `UNIQUE`, not unique-per-active-key, so a
    /// revoked row keeps its name reserved forever. After an account lockdown
    /// revoked `opencode-blake`, reconnecting that tool hit the UNIQUE
    /// constraint and failed with an opaque database error, and the account
    /// could not get its tools back without shell access. Names are therefore
    /// reusable, but only on terms narrow enough that reuse is never a way to
    /// reach somebody else's row:
    ///
    /// - any **active** row with this exact name is refused, the rule every
    ///   caller has always had;
    /// - a **revoked** row with this exact name is reusable only when its
    ///   ownership matches the key being created: same `user_id`, or both
    ///   `NULL` for the unbound operator key. `NULL` and `Some(id)` are
    ///   different owners, in both directions.
    /// - anything else is refused without touching the row.
    ///
    /// The owner check matters because the name is the whole handle here. Bot
    /// usernames are derived (`{tool}-{owner}`), so `opencode-blake`'s
    /// tombstone is exactly what a second account would have to displace to
    /// take that name, and deleting another owner's row is a write on their
    /// account's history that the caller has no claim to. Refusing is also
    /// what stops name reuse becoming an oracle: the caller learns the name is
    /// taken, not by whom.
    ///
    /// The delete is exact-name (never a `LIKE` or prefix), revoked-only, and
    /// owner-matched, so it cannot touch a live credential or another
    /// account's. It carries no key material anywhere: the row is dropped, not
    /// copied. The audit rows that recorded the revocation survive it,
    /// deliberately, and nothing references `api_keys` by foreign key, so there
    /// is nothing else to cascade.
    ///
    /// LIF-391: the user binding is part of the insert, not a follow-up
    /// update. A key is never briefly on disk unbound, so a crash mid-creation
    /// cannot leave behind an orphan key that resolves as the operator.
    pub fn insert(
        self,
        conn: &rusqlite::Connection,
        name: &str,
        expires_at: Option<&str>,
        user_id: Option<i64>,
    ) -> Result<String, crate::error::LificError> {
        let mut stmt = conn.prepare("SELECT user_id, revoked FROM api_keys WHERE name = ?1")?;
        let existing = stmt
            .query_map(params![name], |row| {
                Ok((row.get::<_, Option<i64>>(0)?, row.get::<_, bool>(1)?))
            })?
            .collect::<Result<Vec<_>, _>>()?;
        drop(stmt);

        if existing.iter().any(|(_, revoked)| !revoked) {
            return Err(crate::error::LificError::BadRequest(format!(
                "an active key named '{name}' already exists"
            )));
        }
        if existing.iter().any(|(owner, _)| *owner != user_id) {
            return Err(crate::error::LificError::BadRequest(format!(
                "the key name '{name}' is already reserved by another owner"
            )));
        }

        if !existing.is_empty() {
            conn.execute(
                "DELETE FROM api_keys WHERE name = ?1 AND revoked = 1",
                params![name],
            )?;
        }

        conn.execute(
            "INSERT INTO api_keys (name, key_hash, key_id, expires_at, user_id) \
             VALUES (?1, ?2, ?3, ?4, ?5)",
            params![name, self.hash, self.key_id, expires_at, user_id],
        )?;

        Ok(self.plaintext)
    }
}

/// The error every failure of [`recent_session_token`] /
/// [`revalidate_recent_session`] returns. One string on purpose: which of
/// "no token", "not a session token", "expired", "too old" or "wrong user"
/// applies is not something an unauthorized caller should get to probe for.
fn recent_auth_required() -> crate::error::LificError {
    crate::error::LificError::Forbidden("recent authentication required".into())
}

/// Pull the browser session token out of an `Authorization: Bearer` header for
/// a route that mints durable credentials.
///
/// Minting an API key or connecting a tool is an account-level action, so it
/// requires the thing a human just typed a password into: a browser session.
/// An API key, an OAuth access token, a cookie, or no credential at all is
/// refused here regardless of what the authentication middleware made of it.
/// This is a *shape* check only; validity, freshness and ownership are
/// re-established by [`revalidate_recent_session`] inside the write
/// transaction, because anything checked before that transaction opens can be
/// revoked out from under the write.
pub fn recent_session_token(headers: &HeaderMap) -> Result<String, crate::error::LificError> {
    session_bearer_token(headers).map_err(|_| recent_auth_required())
}

/// The browser session token from an `Authorization: Bearer` header, or a
/// `Forbidden` naming what is missing.
///
/// Shape only: this says the caller presented something session-shaped, not
/// that it is live or whose it is. Split out from [`recent_session_token`] so
/// `POST /api/auth/me/refresh` can require a session without also requiring it
/// to be recent, which is the one thing it exists to fix.
pub fn session_bearer_token(headers: &HeaderMap) -> Result<String, crate::error::LificError> {
    let token = headers
        .get("authorization")
        .and_then(|value| value.to_str().ok())
        .and_then(|value| value.strip_prefix("Bearer "))
        .map(str::trim)
        .ok_or_else(|| {
            crate::error::LificError::Forbidden(
                "this action requires a signed-in browser session".into(),
            )
        })?;

    if !token.starts_with("lific_sess_") {
        return Err(crate::error::LificError::Forbidden(
            "this action requires a signed-in browser session".into(),
        ));
    }
    Ok(token.to_string())
}

/// Re-establish, on `conn`, that `token` is a live browser session belonging to
/// `expected_user_id` and created inside the recent-authentication window, and
/// hand back **the user as the database has them right now**.
///
/// Callers run this as the first statement of the writer transaction that
/// grants something. SQLite serializes writers, so an account lockdown is
/// either wholly before this check (which then fails, because the session row
/// is gone) or wholly after the write (which then revokes what was granted).
/// There is no interleaving that leaves a live credential behind a revoked
/// session.
///
/// `expected_user_id` is the identity the authentication middleware resolved.
/// Comparing it to the session's own user closes the gap where a token is
/// swapped between the middleware's read and this write.
///
/// **Use the returned user, not the middleware's copy, for any authorization
/// decision this transaction makes.** The `AuthUser` the middleware attached
/// was read before the request was routed; `is_admin` on it is a snapshot, and
/// a demotion committed since is invisible to it. The value returned here was
/// read inside this transaction and is the only trustworthy one. Callers that
/// need nothing but "the session belongs to this person" may ignore it.
pub fn revalidate_recent_session(
    conn: &rusqlite::Connection,
    token: &str,
    expected_user_id: i64,
) -> Result<crate::db::models::User, crate::error::LificError> {
    let user = crate::db::queries::users::validate_session(conn, token)
        .map_err(|_| recent_auth_required())?;
    if user.id != expected_user_id {
        return Err(recent_auth_required());
    }
    if !crate::db::queries::users::session_is_recent(conn, token)? {
        return Err(recent_auth_required());
    }
    Ok(user)
}

/// The freshly-read user as an `AuthUser`, for re-running an authorization gate
/// inside the transaction that is about to write.
pub fn fresh_auth_user(user: &crate::db::models::User) -> AuthUser {
    AuthUser {
        id: user.id,
        username: user.username.clone(),
        display_name: user.display_name.clone(),
        is_admin: user.is_admin,
    }
}

/// The freshly-read user as a `ResolvedIdentity` for `authz::require_role_conn`.
///
/// A session caller is always a human, so there is no bot-to-owner resolution
/// to redo: the session's own user *is* the effective user.
pub fn fresh_identity(
    user: &crate::db::models::User,
    transport: crate::actor::Transport,
) -> crate::resolve_caller::ResolvedIdentity {
    crate::resolve_caller::ResolvedIdentity {
        user: fresh_auth_user(user),
        transport,
    }
}

/// Re-read the calling user inside a transaction, and refuse if the account
/// can no longer authenticate.
///
/// The destructive routes (revoke a key, disconnect or delete a tool, demote,
/// deactivate) deliberately do NOT require a recent sign-in: they only ever
/// take access away, and they are what an admin reaches for mid-incident. They
/// do still need their *authorization* to be current, because
/// `AuthUser.is_admin` came off a snapshot taken before the request was
/// routed, and "can this caller act on somebody else's credential" is decided
/// by that flag.
pub fn fresh_caller(
    conn: &rusqlite::Connection,
    caller_id: i64,
) -> Result<crate::db::models::User, crate::error::LificError> {
    let user = crate::db::queries::users::get_user_by_id(conn, caller_id)
        .map_err(|_| crate::error::LificError::Forbidden("authentication required".into()))?;
    if !crate::db::queries::users::credential_is_live(conn, &user)? {
        return Err(crate::error::LificError::Forbidden(
            "authentication required".into(),
        ));
    }
    Ok(user)
}

/// Inside a granting transaction, require that the freshly-read session user
/// still holds instance admin.
///
/// The handler's `require_admin` preflight runs on the middleware's snapshot so
/// an unauthorized caller gets a clean 403 without touching the writer. This is
/// the authoritative one.
pub fn require_fresh_admin(user: &crate::db::models::User) -> Result<(), crate::error::LificError> {
    if user.is_admin {
        Ok(())
    } else {
        Err(crate::error::LificError::Forbidden(
            "only an admin can do this".into(),
        ))
    }
}

/// List all API keys (never returns the key itself, just metadata).
pub fn list_api_keys(db: &DbPool) -> Result<Vec<ApiKeyInfo>, crate::error::LificError> {
    let conn = db.read()?;
    let mut stmt = conn.prepare(
        "SELECT id, name, created_at, expires_at, revoked FROM api_keys ORDER BY created_at",
    )?;
    let rows = stmt.query_map([], |row| {
        Ok(ApiKeyInfo {
            id: row.get(0)?,
            name: row.get(1)?,
            created_at: row.get(2)?,
            expires_at: row.get(3)?,
            revoked: row.get(4)?,
        })
    })?;
    rows.collect::<Result<Vec<_>, _>>()
        .map_err(crate::error::LificError::Database)
}

/// Revoke a key by name.
pub fn revoke_api_key(db: &DbPool, name: &str) -> Result<(), crate::error::LificError> {
    let conn = db.write()?;
    let changed = conn.execute(
        "UPDATE api_keys SET revoked = 1 WHERE name = ?1 AND revoked = 0",
        params![name],
    )?;
    drop(conn);
    if changed == 0 {
        return Err(crate::error::LificError::NotFound(format!(
            "no active key named '{name}'"
        )));
    }
    info!(name, "API key revoked");
    Ok(())
}

/// Rotate a key: delete the old one, create a new one, return the new plaintext.
/// The old key's user binding carries over to the new key (LIF-132) — rotating
/// a bot/user key must not silently de-attribute it.
pub fn rotate_api_key(
    db: &DbPool,
    manager: &ApiKeyManagerV0,
    name: &str,
) -> Result<String, crate::error::LificError> {
    rotate_api_key_bound(db, manager, name, None)
}

/// Like [`rotate_api_key`], but binds the new key to `user_id` rather than
/// carrying the old binding over. `None` keeps the old binding. Used where
/// the caller already knows the owner, e.g. `lific connect` re-minting a
/// bot's key.
pub fn rotate_api_key_bound(
    db: &DbPool,
    manager: &ApiKeyManagerV0,
    name: &str,
    user_id: Option<i64>,
) -> Result<String, crate::error::LificError> {
    // Draw the material first, outside any lock, then do the lookup, the
    // delete and the insert in ONE transaction. This used to be a `write()`
    // guard that read and deleted, then dropped the guard and called
    // `create_api_key`, which took the writer again: two commits with a gap in
    // between where the key simply did not exist. A crash or a competing write
    // landing in that gap left the tool with no credential at all and no way
    // to tell that from a rotation that had not started.
    let prepared = PreparedApiKey::generate(manager)?;
    db.transaction(|tx| {
        // Capture the user binding before deleting so it can be re-applied.
        // If multiple rows share the name (revoked leftovers), prefer the
        // binding of an active row.
        let previous_user_id: Option<i64> = tx
            .query_row(
                "SELECT user_id FROM api_keys WHERE name = ?1 ORDER BY revoked ASC, id DESC LIMIT 1",
                params![name],
                |row| row.get(0),
            )
            .map_err(|e| match e {
                rusqlite::Error::QueryReturnedNoRows => {
                    crate::error::LificError::NotFound(format!("no key named '{name}'"))
                }
                other => other.into(),
            })?;

        // Delete old key entirely (not just revoke) so the name can be reused.
        // This clears every row for the name, so `insert`'s active-name and
        // owner checks see a free name: rotation is explicitly allowed to
        // replace a live key, which is the whole point of it.
        tx.execute("DELETE FROM api_keys WHERE name = ?1", params![name])?;

        prepared.insert(tx, name, None, user_id.or(previous_user_id))
    })
}

/// Check if any API keys exist.
pub fn has_any_keys(db: &DbPool) -> bool {
    if let Ok(conn) = db.read() {
        conn.query_row("SELECT COUNT(*) FROM api_keys", [], |row| {
            row.get::<_, i64>(0)
        })
        .unwrap_or(0)
            > 0
    } else {
        false
    }
}

/// Whether a first human (non-bot) operator exists yet.
pub fn has_human_operator(db: &DbPool) -> bool {
    if let Ok(conn) = db.read() {
        crate::db::queries::users::has_human_users(&conn).unwrap_or(false)
    } else {
        false
    }
}

/// LIFIC-9: whether to auto-mint the "default" unbound API key at startup.
///
/// This is the single decision both `lific init` and `lific start` share. Under
/// the new design a human operator is created at `init` (passwordless mode), so
/// an unbound operator-style key should no longer be minted as the default path
/// — the operator *is* a real user now. The "default" key is still available on
/// demand via `lific key create`. We mint it only for the genuinely empty
/// bootstrap (no users at all, no keys) so a headless/agent-first install can
/// still get a credential before any human exists — and once a human exists we
/// never auto-mint it again, even if all keys were later revoked.
pub fn should_mint_initial_key(db: &DbPool) -> bool {
    !has_any_keys(db) && !has_human_operator(db)
}

#[derive(Debug)]
#[allow(dead_code)]
pub struct ApiKeyInfo {
    pub id: i64,
    pub name: String,
    pub created_at: String,
    pub expires_at: Option<String>,
    pub revoked: bool,
}

/// LIF-267: parse the `lific_token` session cookie a browser sends on same-site
/// GETs. Splits the `Cookie` header on `;`, trims each pair, and returns the
/// `lific_token` value ONLY when it looks like a session token (`lific_sess_`
/// prefix). API keys (`lific_sk`) and OAuth tokens (`lific_at_`) are never
/// accepted via cookie — the cookie path authenticates the browser session and
/// nothing else. Returns `None` when the header/cookie is absent or the value
/// isn't a session token.
fn session_cookie_token(headers: &HeaderMap) -> Option<String> {
    let cookies = headers.get("cookie").and_then(|v| v.to_str().ok())?;
    let value = cookies.split(';').find_map(|c| {
        c.trim()
            .strip_prefix("lific_token=")
            .map(|v| v.trim().to_string())
    })?;
    value.starts_with("lific_sess_").then_some(value)
}

/// LIF-267: is this request a `GET /api/attachments/{id}` download, where `{id}`
/// is a single numeric segment (trailing slash tolerated)? Only this exact
/// shape is eligible for the session-cookie fallback: it's the browser-native
/// `<img src>` subresource path. The list route `/api/attachments` (no id) and
/// any deeper path like `/api/attachments/5/extra` are excluded, so the
/// fallback never widens beyond a single read-only download.
///
/// LIF-418 adds one sibling, `/api/attachments/{id}/thumbnail`. It is the same
/// read of the same blob, downscaled, and it is consumed the same way: an
/// `<img src>` cannot set an Authorization header, so without the fallback
/// every thumbnail in the UI would 401. Nothing else under `{id}/` is
/// eligible, so `/links` and `/preview` still require a real credential.
fn is_attachment_download(method: &Method, path: &str) -> bool {
    if method != Method::GET {
        return false;
    }
    let Some(rest) = path.strip_prefix("/api/attachments/") else {
        return false;
    };
    let rest = rest.strip_suffix('/').unwrap_or(rest);
    let id = rest.strip_suffix("/thumbnail").unwrap_or(rest);
    !id.is_empty() && id.bytes().all(|b| b.is_ascii_digit())
}

/// LIF-403: which kind of credential authenticated the current request.
///
/// Inserted into the request extensions at every success branch of
/// [`require_api_key`], next to `Option<AuthUser>` and the resolved identity.
/// Downstream code that needs to treat a credential *kind* differently — the
/// OAuth deny-list below is the first such case — reads this instead of
/// re-sniffing the `Authorization` header or the request path.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum CredentialKind {
    /// Browser session token (`lific_sess_`), from the header or the
    /// `lific_token` cookie.
    Session,
    /// API key (`lific_sk`), including the unbound operator key.
    ApiKey,
    /// OAuth 2.1 access token (`lific_at_`) issued by the connector flow.
    Oauth,
    /// No credential at all — the `[auth] required = false` path (LIF-294).
    Anonymous,
}

/// LIF-403: the REST surfaces an OAuth access token may not reach.
///
/// An OAuth token is the MCP connector credential: it expires, it is
/// revocable from Connected Tools, and it is bound to a per-tool bot. Those
/// properties are the entire point of it, and they evaporate the moment the
/// token can mint a credential that outlives it. Until this gate existed, a
/// bot token could `POST /api/auth/keys` (`api::auth::create_key`) and walk
/// away with a permanent, never-expiring API key — a leash traded for a
/// forever key.
///
/// OAuth tokens remain accepted on ordinary REST routes on purpose: that is
/// parity with what the same token can already do through the MCP tools.
/// Only credential-management and account/user administration is closed off.
/// Returns the reason a route is on the list, or `None` when the request is
/// allowed. Each rule is a path prefix (so `{id}` sub-routes are covered
/// without re-implementing route matching) plus the methods it applies to.
fn oauth_blocked_reason(method: &Method, path: &str) -> Option<&'static str> {
    // Normalize one trailing slash so `/api/auth/keys/` can't slip past a
    // comparison the router itself would still resolve.
    let path = path.strip_suffix('/').unwrap_or(path);

    // API keys — create / list / revoke. THE escalation this issue is about:
    // a credential that expires must not mint one that never does. Listing is
    // blocked too; key inventory is credential management, not app data.
    if path == "/api/auth/keys" || path.starts_with("/api/auth/keys/") {
        return Some("API key management is not available to OAuth-token callers");
    }

    // Connected tools (bots) — creating one mints an API key for it, and
    // disconnect/delete revoke *other* agents' credentials. Same class of
    // authority as the key routes above.
    if path == "/api/auth/bots" || path.starts_with("/api/auth/bots/") {
        return Some("connected-tool management is not available to OAuth-token callers");
    }

    // Password change and "sign every session out". A tool token acts for a
    // bot; rewriting the human's login credential, or logging them out
    // everywhere, is not within its remit.
    // `/me/refresh` joins them: it mints a browser session, which is the one
    // credential shape allowed to perform the granting actions an OAuth token
    // is kept out of. The handler refuses non-session bearers on its own; this
    // is the same rule stated where the rest of the credential surface is.
    if path == "/api/auth/me/password"
        || path == "/api/auth/me/sessions"
        || path == "/api/auth/me/refresh"
    {
        return Some("account credential changes are not available to OAuth-token callers");
    }

    // Profile mutation on the caller's own account. `GET /api/auth/me` stays
    // open — reading who you are is not an account change.
    if path == "/api/auth/me" && method != Method::GET {
        return Some("account changes are not available to OAuth-token callers");
    }

    // User administration — create an account, grant/revoke instance admin,
    // deactivate/reactivate. `GET /api/users` stays open: the roster read is
    // a plain read that drives assignee pick-lists.
    if (path == "/api/users" && method != Method::GET) || path.starts_with("/api/users/") {
        return Some("user administration is not available to OAuth-token callers");
    }

    // OAuth client and token administration. These live in a router merged
    // OUTSIDE this middleware today (`oauth::router` in src/server.rs), so
    // nothing reaches here; the rule is here so moving them behind auth later
    // cannot hand a token authority over the flow that issued it.
    if path == "/oauth" || path.starts_with("/oauth/") {
        return Some(
            "OAuth client and token administration is not available to OAuth-token callers",
        );
    }

    None
}

/// LIFIC-8: resolve the caller's identity and stamp it into the request
/// extension *alongside* the legacy `Option<AuthUser>`. Best-effort by design
/// during the expand step — a resolve failure (DB fault, read-lock poisoning)
/// is logged and degrades to `None` so auth itself never breaks. LIFIC-10
/// will make the downstream gates read this; until then nothing consumes it.
///
/// LIF-403: also inserts the [`CredentialKind`] marker, so every success
/// branch of `require_api_key` stamps the credential kind through this one
/// call and a new branch cannot forget it.
///
/// `default` is the transport the credential-type implies when the request
/// is NOT aimed at `/mcp` (session → web, key/oauth → api).
fn insert_identity_extensions(
    request: &mut Request<Body>,
    db: &DbPool,
    credential_user: Option<crate::db::models::AuthUser>,
    is_mcp_request: bool,
    default: crate::actor::Transport,
    kind: CredentialKind,
) {
    let transport = if is_mcp_request {
        crate::actor::Transport::Mcp
    } else {
        default
    };
    let resolved = match crate::resolve_caller::resolve_caller(db, credential_user, transport) {
        Ok(id) => id,
        Err(e) => {
            warn!(error = %e, "resolved-identity lookup failed; degrading to None");
            None
        }
    };
    request.extensions_mut().insert(resolved);
    request.extensions_mut().insert(kind);
}

/// Axum middleware that validates Bearer tokens and resolves user identity.
///
/// After successful auth, inserts `Extension<Option<AuthUser>>` into the request:
/// - `Some(user)` if the token resolves to a user (session, or API key with user_id)
/// - `None` if the token is valid but has no user association (legacy keys, OAuth)
///
/// It also inserts `Extension<CredentialKind>` (LIF-403) so downstream code
/// can tell *how* the caller authenticated, and refuses an OAuth token that
/// is aimed at a credential-management or account-administration route (see
/// [`oauth_blocked_reason`]) with a 403.
pub async fn require_api_key(
    State(auth): State<AuthState>,
    mut request: Request<Body>,
    next: Next,
) -> Response {
    // Extract Bearer token from Authorization header
    let token = request
        .headers()
        .get("authorization")
        .and_then(|v| v.to_str().ok())
        .and_then(|v| v.strip_prefix("Bearer "))
        .map(|s| s.trim().to_string());

    // Targeted diagnostics for the MCP endpoint only (keeps REST traffic quiet).
    // Lets us see, post-OAuth, whether Claude actually presents the bearer token
    // it was issued — distinguishing a server-side token rejection from the
    // documented claude.ai-web bug where the token is dropped and the
    // authenticated /mcp request is never sent.
    let is_mcp_request = request.uri().path() == "/mcp";
    if is_mcp_request {
        let token_kind = match token.as_deref() {
            Some(t) if t.starts_with("lific_sess_") => "session",
            Some(t) if t.starts_with("lific_at_") => "oauth",
            Some(t) if t.starts_with("lific_sk") => "api_key",
            Some(_) => "unknown",
            None => "none",
        };
        info!(method = %request.method(), token_kind, "/mcp request received");
    }

    // RFC 9728 §3.1: for a resource URL with a path component (`/mcp`), the
    // canonical protected-resource metadata lives at the path-aware well-known
    // location. Point Claude there so the `resource` it reads matches the URL
    // the user entered.
    let www_auth = format!(
        "Bearer resource_metadata=\"{}/.well-known/oauth-protected-resource/mcp\"",
        auth.public_url
    );

    let Some(token) = token else {
        // LIF-267: session-cookie fallback, scoped to GET /api/attachments/{id}.
        // A browser-native `<img src="/api/attachments/N">` cannot attach an
        // Authorization header, so inline attachment images arrived here
        // credential-less and 401'd. When (and only when) this is the
        // read-only attachment download route on a GET, accept the browser's
        // `lific_token` session cookie in lieu of the header and resolve it
        // exactly like a header-borne session token. This reopens NO CSRF
        // surface (GET is a safe method; every mutation stays header-only) and
        // leaks nothing cross-site (the cookie is SameSite=Lax, so it is never
        // sent on cross-site subresource requests). The download handler still
        // runs its own project-scoped `authorize_read`, so gating is unchanged
        // — this just lets the browser present the credential it can.
        if is_attachment_download(request.method(), request.uri().path())
            && let Some(cookie_token) = session_cookie_token(request.headers())
        {
            let user = {
                // LIF-139: validation is a pure read — take a pooled read
                // connection instead of the single writer mutex.
                let conn = match auth.db.read() {
                    Ok(c) => c,
                    Err(_) => {
                        return (StatusCode::INTERNAL_SERVER_ERROR, "database error")
                            .into_response();
                    }
                };
                crate::db::queries::users::validate_session(&conn, &cookie_token)
            };
            if let Ok(u) = user {
                let auth_user = crate::db::models::AuthUser {
                    id: u.id,
                    username: u.username,
                    display_name: u.display_name,
                    is_admin: u.is_admin,
                };
                let actor = crate::actor::ActorCtx {
                    user_id: Some(auth_user.id),
                    transport: crate::actor::Transport::Web,
                };
                insert_identity_extensions(
                    &mut request,
                    &auth.db,
                    Some(auth_user.clone()),
                    false,
                    crate::actor::Transport::Web,
                    CredentialKind::Session,
                );
                request.extensions_mut().insert(Some(auth_user));
                return crate::actor::scope(actor, next.run(request)).await;
            }
            // Missing/invalid/expired cookie session falls through to 401 below.
        }

        // LIF-294: `[auth] required = false` — a credential-less request is
        // the operator (same trust rail as an unbound API key, LIF-261).
        // ONLY this no-credential path is affected: a presented-but-invalid
        // token still falls through to the 401s below, so a broken client
        // config surfaces as an error instead of silently degrading to
        // anonymous-with-admin-powers.
        if !auth.required {
            let default = if is_mcp_request {
                crate::actor::Transport::Mcp
            } else {
                crate::actor::Transport::Api
            };
            let actor = crate::actor::ActorCtx {
                user_id: None,
                transport: default,
            };
            // LIFIC-8: resolve the passwordless identity (first-admin
            // fallback). resolve_caller handles the operator bypass — no
            // separate carrier needed (LIFIC-14 deleted the last of them).
            insert_identity_extensions(
                &mut request,
                &auth.db,
                None,
                is_mcp_request,
                default,
                CredentialKind::Anonymous,
            );
            request
                .extensions_mut()
                .insert(Option::<crate::db::models::AuthUser>::None);
            return crate::actor::scope(actor, next.run(request)).await;
        }

        if is_mcp_request {
            info!("/mcp rejected: no Authorization header (discovery probe or dropped token)");
        }
        return (
            StatusCode::UNAUTHORIZED,
            [("WWW-Authenticate", www_auth.as_str())],
            "Missing Authorization: Bearer <key> header",
        )
            .into_response();
    };

    // ── Session tokens (lific_sess_ prefix) ──────────────────────
    if token.starts_with("lific_sess_") {
        let user = {
            // LIF-139: session validation no longer writes, so every
            // session-authenticated request reads from the pool instead of
            // serializing on the exclusive writer.
            let conn = match auth.db.read() {
                Ok(c) => c,
                Err(_) => {
                    return (StatusCode::INTERNAL_SERVER_ERROR, "database error").into_response();
                }
            };
            crate::db::queries::users::validate_session(&conn, &token)
        };

        match user {
            Ok(u) => {
                let auth_user = crate::db::models::AuthUser {
                    id: u.id,
                    username: u.username,
                    display_name: u.display_name,
                    is_admin: u.is_admin,
                };
                // LIF-155: session tokens are the browser — audit as 'web'
                // (or 'mcp' if a session token is ever pointed at /mcp).
                let actor = crate::actor::ActorCtx {
                    user_id: Some(auth_user.id),
                    transport: if is_mcp_request {
                        crate::actor::Transport::Mcp
                    } else {
                        crate::actor::Transport::Web
                    },
                };
                insert_identity_extensions(
                    &mut request,
                    &auth.db,
                    Some(auth_user.clone()),
                    is_mcp_request,
                    crate::actor::Transport::Web,
                    CredentialKind::Session,
                );
                request.extensions_mut().insert(Some(auth_user));
                return crate::actor::scope(actor, next.run(request)).await;
            }
            Err(_) => {
                return (
                    StatusCode::UNAUTHORIZED,
                    [("WWW-Authenticate", www_auth.as_str())],
                    "Invalid or expired session",
                )
                    .into_response();
            }
        }
    }

    // ── OAuth tokens (lific_at_ prefix) ──────────────────────────
    if token.starts_with("lific_at_") {
        // One resolution, one database snapshot (`resolve_oauth_credential`).
        // This used to be three calls on three pooled connections: is it
        // valid, whose is it, is that user live. A token revoked between the
        // first two answered "valid" and then "unbound", and an unbound OAuth
        // token takes the operator fallback, so revoking a tool's credential
        // could promote it. The typed outcome makes that state unrepresentable.
        match crate::oauth::resolve_oauth_credential(&auth.db, &token) {
            Ok(credential) => {
                if is_mcp_request {
                    info!("/mcp authorized: OAuth token accepted");
                }
                // A bound token carries its user. `LegacyUnbound` is a
                // pre-LIF-79 row with genuinely no binding, and keeps the
                // documented operator fallback (`None`); nothing issued since
                // can be in that state.
                let auth_user = match credential {
                    crate::oauth::OAuthCredential::Bound(user) => Some(user),
                    crate::oauth::OAuthCredential::LegacyUnbound => None,
                };
                // LIF-403: the credential authenticates, but it may not be
                // pointed at credential management or account administration.
                // Enforced here, in the middleware, rather than in the handlers:
                // one list next to the credential check beats a gate per route
                // that a new route can be added without.
                if let Some(reason) = oauth_blocked_reason(request.method(), request.uri().path()) {
                    warn!(
                        method = %request.method(),
                        path = %request.uri().path(),
                        "OAuth token refused on a credential-management route"
                    );
                    return (
                        StatusCode::FORBIDDEN,
                        format!(
                            "{reason}. Authenticate with a session or API key for this endpoint."
                        ),
                    )
                        .into_response();
                }
                // LIF-155: OAuth tokens are programmatic access — 'mcp' when
                // aimed at /mcp (the normal case), 'api' against REST.
                let actor = crate::actor::ActorCtx {
                    user_id: auth_user.as_ref().map(|u| u.id),
                    transport: if is_mcp_request {
                        crate::actor::Transport::Mcp
                    } else {
                        crate::actor::Transport::Api
                    },
                };
                insert_identity_extensions(
                    &mut request,
                    &auth.db,
                    auth_user.clone(),
                    is_mcp_request,
                    crate::actor::Transport::Api,
                    CredentialKind::Oauth,
                );
                request.extensions_mut().insert(auth_user);
                return crate::actor::scope(actor, next.run(request)).await;
            }
            Err(crate::oauth::OAuthReject::DeadBinding) => {
                // Bound to an identity that may not authenticate: the user is
                // gone, deactivated, or is a bot whose owner is deactivated. A
                // bot's authority is its owner's, so an owner switched off must
                // not keep acting through the tool token they approved
                // earlier. Rejected outright, never degraded to unbound, which
                // would hand it the operator fallback (PR #23 review).
                if is_mcp_request {
                    warn!("/mcp rejected: OAuth token bound to a missing or deactivated user");
                }
                return (
                    StatusCode::UNAUTHORIZED,
                    [("WWW-Authenticate", www_auth.as_str())],
                    "OAuth token bound to a missing or deactivated user",
                )
                    .into_response();
            }
            Err(crate::oauth::OAuthReject::Unavailable) => {
                return (StatusCode::INTERNAL_SERVER_ERROR, "database error").into_response();
            }
            Err(crate::oauth::OAuthReject::Invalid) => {
                if is_mcp_request {
                    warn!("/mcp rejected: OAuth token invalid or expired");
                }
                return (
                    StatusCode::UNAUTHORIZED,
                    [("WWW-Authenticate", www_auth.as_str())],
                    "Invalid or expired OAuth token",
                )
                    .into_response();
            }
        }
    }

    // ── API keys (lific_sk- prefix) ──────────────────────────────
    // LIFIC-18: shared with the stdio LIFIC_TOKEN resolver so the
    // checksum/lookup/backfill/hash logic lives in one place (see
    // `validate_api_key` just below `ApiKeyRow`).
    let auth_user = match validate_api_key(&auth.db, &auth.manager, &token) {
        Ok(user) => user,
        Err(ApiKeyReject::BadChecksum) => {
            warn!("rejected API key with invalid checksum");
            return (
                StatusCode::UNAUTHORIZED,
                [("WWW-Authenticate", www_auth.as_str())],
                "Invalid API key",
            )
                .into_response();
        }
        Err(ApiKeyReject::Db) => {
            return (StatusCode::INTERNAL_SERVER_ERROR, "database error").into_response();
        }
        Err(ApiKeyReject::NotFound) => {
            warn!("rejected invalid API key");
            return (
                StatusCode::UNAUTHORIZED,
                [("WWW-Authenticate", www_auth.as_str())],
                "Invalid API key",
            )
                .into_response();
        }
        Err(ApiKeyReject::HashMismatch) => {
            warn!("API key hash verification failed");
            return (
                StatusCode::UNAUTHORIZED,
                [("WWW-Authenticate", www_auth.as_str())],
                "Invalid API key",
            )
                .into_response();
        }
        Err(ApiKeyReject::Inactive) => {
            warn!("rejected API key: deactivated account, or a bot with a deactivated owner");
            return (
                StatusCode::UNAUTHORIZED,
                [("WWW-Authenticate", www_auth.as_str())],
                "This account is deactivated",
            )
                .into_response();
        }
    };

    // LIF-155: API keys are programmatic — 'mcp' on the /mcp path, 'api' for
    // direct REST usage. The LIF-261 operator bypass for an unbound key
    // (user_id = None) now lives entirely in `resolve_caller` (inserted above),
    // which falls back to the first admin — read as `identity.user.is_admin` by
    // the gates. The old credential-type-specific `OperatorCredential` marker
    // and `operator_scope` task-local are gone (LIFIC-14).
    let actor = crate::actor::ActorCtx {
        user_id: auth_user.as_ref().map(|u| u.id),
        transport: if is_mcp_request {
            crate::actor::Transport::Mcp
        } else {
            crate::actor::Transport::Api
        },
    };
    insert_identity_extensions(
        &mut request,
        &auth.db,
        auth_user.clone(),
        is_mcp_request,
        crate::actor::Transport::Api,
        CredentialKind::ApiKey,
    );
    request.extensions_mut().insert(auth_user);
    crate::actor::scope(actor, next.run(request)).await
}

/// Internal struct for loading API key rows during auth.
#[derive(Debug)]
struct ApiKeyRow {
    id: i64,
    hash: String,
    user_id: Option<i64>,
}

/// Why an API key failed to authenticate. Maps to both the HTTP response and
/// the stdio-resolver error, so both callers share one decision.
#[derive(Debug, Clone, Copy)]
enum ApiKeyReject {
    /// A database read/write failed (backend fault, not a bad key).
    Db,
    /// The key didn't pass the format checksum.
    BadChecksum,
    /// The key is well-formed but matches no active key.
    NotFound,
    /// A matching key exists but the stored hash didn't verify.
    HashMismatch,
    /// The key verified, but the identity it names may not authenticate: the
    /// account is deactivated, or it is a bot whose owner is deactivated
    /// (LIF-214 follow-up, see `queries::users::credential_is_live`).
    Inactive,
}

/// Shared API-key authentication for both the HTTP middleware and the stdio
/// `LIFIC_TOKEN` resolver. Verifies the checksum, resolves the key row by
/// derived key_id (with the pre-migration-010 scan-and-backfill fallback), and
/// verifies the stored hash — exactly one copy of that logic (LIFIC-18 review:
/// previously duplicated between `require_api_key` and `resolve_api_key_user`).
///
/// Returns `Ok(Some(user))` for a valid bound key, `Ok(None)` for a valid but
/// unbound key (the caller falls that back to the operator), and
/// `Err(reject)` when the key does not authenticate.
fn validate_api_key(
    db: &DbPool,
    manager: &ApiKeyManagerV0,
    token: &str,
) -> Result<Option<AuthUser>, ApiKeyReject> {
    use api_keys_simplified::SecureString;

    let secure_token = SecureString::from(token.to_string());

    // Fast checksum pre-check: reject malformed keys in ~20μs without touching DB.
    match manager.verify_checksum(&secure_token) {
        Ok(true) => {}
        _ => return Err(ApiKeyReject::BadChecksum),
    }

    // Compute deterministic key ID (BLAKE3, ~microseconds) for O(1) DB lookup.
    let key_id = manager.extract_key_id(&secure_token);

    // Look up the single matching key by key_id (indexed query).
    let key_row: Option<ApiKeyRow> = {
        let conn = db.read().map_err(|_| ApiKeyReject::Db)?;
        conn.query_row(
            "SELECT id, key_hash, user_id FROM api_keys WHERE key_id = ?1 AND revoked = 0 \
             AND (expires_at IS NULL OR datetime(expires_at) > datetime('now'))",
            params![key_id],
            |row| {
                Ok(ApiKeyRow {
                    id: row.get(0)?,
                    hash: row.get(1)?,
                    user_id: row.get(2)?,
                })
            },
        )
        .ok()
    };

    // Fallback: keys created before migration 010 have no key_id — scan those.
    let key_row = key_row.or_else(|| {
        let conn = db.read().ok()?;
        let mut stmt = conn
            .prepare(
                "SELECT id, key_hash, user_id FROM api_keys WHERE key_id IS NULL AND revoked = 0 \
                 AND (expires_at IS NULL OR datetime(expires_at) > datetime('now'))",
            )
            .ok()?;
        let rows: Vec<ApiKeyRow> = stmt
            .query_map([], |row| {
                Ok(ApiKeyRow {
                    id: row.get(0)?,
                    hash: row.get(1)?,
                    user_id: row.get(2)?,
                })
            })
            .ok()?
            .filter_map(|r| r.ok())
            .collect();
        for row in rows {
            if let Ok(KeyStatus::Valid) = manager.verify(&secure_token, &row.hash) {
                // Backfill the key_id so future lookups are O(1).
                if let Ok(wconn) = db.write() {
                    let _ = wconn.execute(
                        "UPDATE api_keys SET key_id = ?1 WHERE id = ?2",
                        params![key_id, row.id],
                    );
                }
                return Some(row);
            }
        }
        None
    });

    let Some(key) = key_row else {
        return Err(ApiKeyReject::NotFound);
    };

    match manager.verify(&secure_token, &key.hash) {
        Ok(KeyStatus::Valid) => {
            // Resolve the user if the key has a user_id. A valid-but-unbound
            // key (legacy, or a fresh-install unassigned key) is Ok(None) — the
            // caller falls back to the operator.
            let auth_user = match key.user_id {
                None => None,
                Some(uid) => {
                    let conn = db.read().map_err(|_| ApiKeyReject::Db)?;
                    match crate::db::queries::users::get_user_by_id(&conn, uid) {
                        // LIF-214 follow-up: a key bound to a deactivated
                        // account, or to a bot whose *owner* is deactivated,
                        // is a dead credential. It must be rejected outright
                        // rather than degraded to `None`, which would hand it
                        // the unbound-key operator fallback (first admin) and
                        // turn deactivation into a promotion.
                        Ok(u) => match crate::db::queries::users::credential_is_live(&conn, &u) {
                            Ok(true) => Some(crate::db::models::AuthUser {
                                id: u.id,
                                username: u.username,
                                display_name: u.display_name,
                                is_admin: u.is_admin,
                            }),
                            Ok(false) => return Err(ApiKeyReject::Inactive),
                            Err(_) => return Err(ApiKeyReject::Db),
                        },
                        // Unchanged: a binding to a user row that no longer
                        // exists stays unresolved.
                        Err(_) => None,
                    }
                }
            };
            Ok(auth_user)
        }
        _ => Err(ApiKeyReject::HashMismatch),
    }
}

/// LIFIC-18: resolve an API key (e.g. the `LIFIC_TOKEN` carried by a stdio
/// agent) to its bound user, without an HTTP request context.
///
/// Returns `Some(user)` when the key is valid AND bound to a user; `Ok(None)`
/// for a valid-but-unbound key (the stdio session then falls back to the
/// operator). An invalid/unrecognized key is an error so the caller can warn
/// loudly and still degrade to the operator fallback.
pub fn resolve_api_key_user(
    db: &DbPool,
    manager: &ApiKeyManagerV0,
    token: &str,
) -> Result<Option<AuthUser>, String> {
    // Reuse the shared validator (same checksum/lookup/backfill/hash logic the
    // HTTP middleware runs). Mapping the typed rejection to a human string
    // keeps the stdio resolver vendoring nothing of its own.
    validate_api_key(db, manager, token).map_err(|reject| match reject {
        ApiKeyReject::Db => "database error".to_string(),
        ApiKeyReject::BadChecksum => "invalid API key checksum".to_string(),
        ApiKeyReject::NotFound => "invalid API key".to_string(),
        ApiKeyReject::HashMismatch => "API key hash verification failed".to_string(),
        ApiKeyReject::Inactive => "this account is deactivated".to_string(),
    })
}

/// LIFIC-18: resolve the `LIFIC_TOKEN` a stdio agent carries into its bound
/// user. `Ok(None)` when the token is absent, empty, or valid-but-unbound —
/// the session runs as the operator. `Err` when a token was present but
/// invalid (checksum/DB/hash failure), so the stdio entrypoint can emit a
/// distinct warning while still degrading to the operator fallback.
pub fn resolve_stdio_token(
    db: &DbPool,
    manager: &ApiKeyManagerV0,
) -> Result<Option<AuthUser>, String> {
    let raw = std::env::var("LIFIC_TOKEN").unwrap_or_default();
    let token = raw.trim();
    if token.is_empty() {
        return Ok(None);
    }
    resolve_api_key_user(db, manager, token)
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::db;
    use api_keys_simplified::SecureString;
    use axum::{Extension, Router, middleware, routing::get};
    use http_body_util::BodyExt;
    use tower::ServiceExt;

    fn test_db() -> db::DbPool {
        db::open_memory().expect("test db")
    }

    // Serializes env mutation (LIFIC_TOKEN) across the stdio-token tests.
    // The process env is global, so every test that touches it must share
    // one lock — and "every test" spans modules: the credentials and doctor
    // tests read the same variable, which is why this is the crate-wide
    // lock from `test_env` rather than a module-local static (LIF-401).
    use crate::test_env::lock_lific_token_env_blocking;

    // ── Expiry is compared as a datetime, not as text ────────
    //
    // `expires_at` accepts ISO 8601, and `chrono`'s `to_rfc3339` writes
    // '2026-08-20T12:00:00+00:00'. SQLite's `datetime('now')` produces
    // '2026-08-20 12:00:00'. Compared as raw text those disagree within the
    // same day, because 'T' (0x54) sorts after every digit: a same-day RFC
    // 3339 timestamp reads as *later* than any SQLite-format one, so an
    // expired key looked live and a live one could look expired. Wrapping the
    // column in `datetime()` normalizes both sides.
    //
    // Times are a minute either side of now, never seconds, so the tests do
    // not sit on a boundary.

    fn rfc3339_from_now(minutes: i64) -> String {
        (chrono::Utc::now() + chrono::Duration::minutes(minutes)).to_rfc3339()
    }

    /// The indexed lookup path (`key_id` present).
    #[test]
    fn an_iso8601_expiry_is_honoured_on_the_indexed_lookup() {
        let pool = test_db();
        let manager = create_key_manager().unwrap();

        let live =
            create_api_key_with_expiry(&pool, &manager, "live", Some(&rfc3339_from_now(1)), None)
                .unwrap();
        let dead =
            create_api_key_with_expiry(&pool, &manager, "dead", Some(&rfc3339_from_now(-1)), None)
                .unwrap();

        assert!(
            validate_api_key(&pool, &manager, &live).is_ok(),
            "a key expiring in a minute must still authenticate"
        );
        assert!(
            validate_api_key(&pool, &manager, &dead).is_err(),
            "a key that expired a minute ago must not authenticate"
        );
    }

    /// The pre-migration-010 fallback path, which scans rows with a NULL
    /// `key_id`. It carries its own copy of the predicate, so it needs its own
    /// proof.
    #[test]
    fn an_iso8601_expiry_is_honoured_on_the_legacy_null_key_id_path() {
        let pool = test_db();
        let manager = create_key_manager().unwrap();

        let live = create_api_key_with_expiry(
            &pool,
            &manager,
            "legacy-live",
            Some(&rfc3339_from_now(1)),
            None,
        )
        .unwrap();
        let dead = create_api_key_with_expiry(
            &pool,
            &manager,
            "legacy-dead",
            Some(&rfc3339_from_now(-1)),
            None,
        )
        .unwrap();
        // Make both look like pre-010 rows so the scan is the only way in.
        pool.write()
            .unwrap()
            .execute("UPDATE api_keys SET key_id = NULL", [])
            .unwrap();

        assert!(
            validate_api_key(&pool, &manager, &live).is_ok(),
            "the legacy scan must honour a live ISO 8601 expiry"
        );
        assert!(
            validate_api_key(&pool, &manager, &dead).is_err(),
            "the legacy scan must reject an expired ISO 8601 key"
        );
    }

    /// A key with no expiry is unaffected either way.
    #[test]
    fn a_key_without_an_expiry_still_authenticates() {
        let pool = test_db();
        let manager = create_key_manager().unwrap();
        let key = create_api_key(&pool, &manager, "forever", None).unwrap();
        assert!(validate_api_key(&pool, &manager, &key).is_ok());
    }

    #[test]
    fn create_key_returns_valid_format() {
        let pool = test_db();
        let manager = create_key_manager().unwrap();
        let key = create_api_key(&pool, &manager, "test-key", None).unwrap();
        assert!(key.starts_with("lific_sk-live-"));
    }

    // ── Name reuse after revocation, scoped to the owner ────────
    //
    // `api_keys.name` is globally UNIQUE, so a revoked row keeps its name
    // reserved. An account lockdown revokes `opencode-blake`; without reuse in
    // `PreparedApiKey::insert`, reconnecting that tool hits the UNIQUE
    // constraint and the account cannot get its tools back.
    //
    // Reuse is owner-scoped, so the matrix below is the actual contract: same
    // owner reuses, any other owner is refused and the tombstone is left
    // exactly as it was. `NULL` (the unbound operator key) is its own owner in
    // both directions.

    /// `api_keys.user_id` is a real foreign key, so a binding needs a real row.
    fn seed_key_owner(pool: &db::DbPool, username: &str) -> i64 {
        let conn = pool.write().unwrap();
        crate::db::queries::users::create_user(
            &conn,
            &crate::db::models::CreateUser {
                username: username.into(),
                email: format!("{username}@test.local"),
                password: "testpassword1".into(),
                display_name: None,
                is_admin: false,
                is_bot: false,
            },
        )
        .unwrap()
        .id
    }

    /// Full snapshot of a name's row, so a refusal can be shown to have
    /// changed nothing at all: not the id, not the stored hash, not the owner,
    /// not the creation timestamp.
    #[derive(Debug, PartialEq)]
    struct KeyRow {
        id: i64,
        user_id: Option<i64>,
        revoked: bool,
        key_hash: String,
        created_at: String,
    }

    fn key_rows(pool: &db::DbPool, name: &str) -> Vec<KeyRow> {
        let conn = pool.read().unwrap();
        let mut stmt = conn
            .prepare(
                "SELECT id, user_id, revoked, key_hash, created_at
                 FROM api_keys WHERE name = ?1 ORDER BY id",
            )
            .unwrap();
        let rows = stmt
            .query_map(params![name], |r| {
                Ok(KeyRow {
                    id: r.get(0)?,
                    user_id: r.get(1)?,
                    revoked: r.get(2)?,
                    key_hash: r.get(3)?,
                    created_at: r.get(4)?,
                })
            })
            .unwrap();
        rows.map(Result::unwrap).collect()
    }

    fn revoke_all(pool: &db::DbPool) {
        pool.write()
            .unwrap()
            .execute("UPDATE api_keys SET revoked = 1", [])
            .unwrap();
    }

    /// The reconnect case this whole rule exists for: a lockdown revoked the
    /// bot's key, and the bot claims its own derived name back.
    #[test]
    fn the_same_bot_reclaims_its_own_revoked_name() {
        let pool = test_db();
        let manager = create_key_manager().unwrap();
        let bot = seed_key_owner(&pool, "opencode-blake");
        let old = create_api_key(&pool, &manager, "opencode-blake", Some(bot)).unwrap();
        revoke_all(&pool);

        let fresh = create_api_key(&pool, &manager, "opencode-blake", Some(bot))
            .expect("a bot reclaims its own revoked name");
        assert_ne!(fresh, old);

        let rows = key_rows(&pool, "opencode-blake");
        assert_eq!(rows.len(), 1, "the dead row was swept, not accumulated");
        assert_eq!(rows[0].user_id, Some(bot));
        assert!(!rows[0].revoked);
        // The old plaintext is gone for good; only the new one authenticates.
        assert!(validate_api_key(&pool, &manager, &old).is_err());
        assert!(validate_api_key(&pool, &manager, &fresh).is_ok());
    }

    #[test]
    fn a_human_reclaims_their_own_revoked_name() {
        let pool = test_db();
        let manager = create_key_manager().unwrap();
        let blake = seed_key_owner(&pool, "blake");
        create_api_key(&pool, &manager, "laptop", Some(blake)).unwrap();
        revoke_all(&pool);

        let fresh = create_api_key(&pool, &manager, "laptop", Some(blake))
            .expect("the same human reuses their own name");
        assert!(validate_api_key(&pool, &manager, &fresh).is_ok());
        assert_eq!(key_rows(&pool, "laptop")[0].user_id, Some(blake));
    }

    #[test]
    fn an_unbound_key_reclaims_its_own_revoked_name() {
        let pool = test_db();
        let manager = create_key_manager().unwrap();
        create_api_key(&pool, &manager, "default", None).unwrap();
        revoke_all(&pool);

        let fresh = create_api_key(&pool, &manager, "default", None)
            .expect("the operator key reuses its own name");
        assert!(validate_api_key(&pool, &manager, &fresh).is_ok());
        let rows = key_rows(&pool, "default");
        assert_eq!(rows.len(), 1);
        assert_eq!(rows[0].user_id, None);
    }

    /// Every way one owner could try to take a name another owner's tombstone
    /// still holds. In each case the refusal must leave the tombstone byte for
    /// byte as it was and write no new row.
    #[test]
    fn a_revoked_name_belonging_to_another_owner_is_refused_untouched() {
        for claimant in ["different-human", "unbound", "bound"] {
            let pool = test_db();
            let manager = create_key_manager().unwrap();
            let blake = seed_key_owner(&pool, "blake");
            let mallory = seed_key_owner(&pool, "mallory");

            // Who holds the tombstone, and who tries to take it.
            let (holder, claimer) = match claimant {
                // A second human cannot displace Blake's revoked key.
                "different-human" => (Some(blake), Some(mallory)),
                // A human cannot displace the unbound operator key's name,
                // which would hand them a name the operator still owns.
                "unbound" => (None, Some(mallory)),
                // Nor the reverse: minting an unbound operator key must not
                // consume a name a real account is still holding.
                "bound" => (Some(blake), None),
                _ => unreachable!(),
            };

            create_api_key(&pool, &manager, "contested", holder).unwrap();
            revoke_all(&pool);
            let before = key_rows(&pool, "contested");

            let err = match create_api_key(&pool, &manager, "contested", claimer) {
                Ok(_) => panic!("{claimant}: another owner's tombstone must not be claimable"),
                Err(err) => err,
            };
            match err {
                crate::error::LificError::BadRequest(message) => assert!(
                    message.contains("reserved by another owner"),
                    "{claimant}: {message}"
                ),
                other => panic!("{claimant}: expected BadRequest, got {other:?}"),
            }

            assert_eq!(
                key_rows(&pool, "contested"),
                before,
                "{claimant}: the refusal must not touch the row or add one"
            );
        }
    }

    #[test]
    fn an_active_name_is_still_refused_and_leaves_the_live_key_alone() {
        let pool = test_db();
        let manager = create_key_manager().unwrap();
        let owner = seed_key_owner(&pool, "blake");
        let other = seed_key_owner(&pool, "mallory");
        let live = create_api_key(&pool, &manager, "opencode-blake", Some(owner)).unwrap();
        let before = key_rows(&pool, "opencode-blake");

        // Refused for both a different owner and the same one: an active name
        // is taken regardless of who is asking, and the message says only that.
        for claimer in [Some(other), Some(owner), None] {
            let err = create_api_key(&pool, &manager, "opencode-blake", claimer)
                .expect_err("an active name is taken");
            match err {
                crate::error::LificError::BadRequest(message) => {
                    assert!(message.contains("already exists"), "{message}");
                }
                other => panic!("expected BadRequest, got {other:?}"),
            }
        }

        assert_eq!(
            key_rows(&pool, "opencode-blake"),
            before,
            "the refusal wrote nothing and did not rebind the live key"
        );
        assert!(
            validate_api_key(&pool, &manager, &live).is_ok(),
            "the live key still authenticates"
        );
    }

    #[test]
    fn reuse_matches_the_exact_name_only() {
        let pool = test_db();
        let manager = create_key_manager().unwrap();
        let owner = seed_key_owner(&pool, "blake");
        create_api_key(&pool, &manager, "opencode-blake", Some(owner)).unwrap();
        create_api_key(&pool, &manager, "opencode-blake-laptop", Some(owner)).unwrap();
        let neighbour = create_api_key(&pool, &manager, "cursor-blake", Some(owner)).unwrap();
        pool.write()
            .unwrap()
            .execute(
                "UPDATE api_keys SET revoked = 1 WHERE name = 'opencode-blake'",
                [],
            )
            .unwrap();

        create_api_key(&pool, &manager, "opencode-blake", Some(owner)).unwrap();

        assert_eq!(key_rows(&pool, "opencode-blake").len(), 1);
        assert_eq!(
            key_rows(&pool, "opencode-blake-laptop").len(),
            1,
            "a name this one is a prefix of is untouched"
        );
        assert!(validate_api_key(&pool, &manager, &neighbour).is_ok());
    }

    /// The revocation is a historical fact and stays in the log after the row
    /// it described is swept. Nothing joins the two, so nothing breaks, and no
    /// key material was ever in the log to leak.
    #[test]
    fn sweeping_a_revoked_row_leaves_its_audit_trail_intact() {
        let pool = test_db();
        let manager = create_key_manager().unwrap();
        let user_id = {
            let conn = pool.write().unwrap();
            let user = crate::db::queries::users::create_user(
                &conn,
                &crate::db::models::CreateUser {
                    username: "blake".into(),
                    email: "blake@test.local".into(),
                    password: "testpassword1".into(),
                    display_name: None,
                    is_admin: true,
                    is_bot: false,
                },
            )
            .unwrap();
            user.id
        };
        create_api_key(&pool, &manager, "opencode-blake", Some(user_id)).unwrap();
        {
            let conn = pool.write().unwrap();
            crate::db::queries::users::lock_down_account(&conn, user_id).unwrap();
        }

        create_api_key(&pool, &manager, "opencode-blake", Some(user_id)).unwrap();

        let conn = pool.read().unwrap();
        let audited: i64 = conn
            .query_row(
                "SELECT COUNT(*) FROM audit_log
                 WHERE entity_type = 'api_key' AND action = 'revoke'
                   AND entity_label = 'opencode-blake'",
                [],
                |r| r.get(0),
            )
            .unwrap();
        assert_eq!(audited, 1, "the revocation is still on the record");
        let leaked: i64 = conn
            .query_row(
                "SELECT COUNT(*) FROM audit_log WHERE new_value LIKE 'lific_sk%'",
                [],
                |r| r.get(0),
            )
            .unwrap();
        assert_eq!(leaked, 0);
    }

    /// Rotation is the one path allowed to replace a *live* key, and it must
    /// still carry the owner over. It is now one transaction, so the name is
    /// never briefly absent.
    #[test]
    fn rotation_replaces_a_live_key_and_keeps_its_owner() {
        let pool = test_db();
        let manager = create_key_manager().unwrap();
        let bot = seed_key_owner(&pool, "opencode-blake");
        let old = create_api_key(&pool, &manager, "opencode-blake", Some(bot)).unwrap();

        let fresh = rotate_api_key(&pool, &manager, "opencode-blake").unwrap();
        assert_ne!(fresh, old);
        let rows = key_rows(&pool, "opencode-blake");
        assert_eq!(rows.len(), 1);
        assert_eq!(rows[0].user_id, Some(bot), "the binding carried over");
        assert!(validate_api_key(&pool, &manager, &old).is_err());
        assert!(validate_api_key(&pool, &manager, &fresh).is_ok());
    }

    #[test]
    fn rotation_of_a_missing_name_is_not_found_and_writes_nothing() {
        let pool = test_db();
        let manager = create_key_manager().unwrap();
        let err = rotate_api_key(&pool, &manager, "never-existed").expect_err("nothing to rotate");
        assert!(matches!(err, crate::error::LificError::NotFound(_)));
        assert!(key_rows(&pool, "never-existed").is_empty());
    }

    /// Credential creation and account lockdown, actually racing.
    ///
    /// Everything else about this interaction is asserted sequentially, which
    /// can only show that each order produces the right outcome. What is
    /// claimed on top of that is *linearizability*: that no interleaving
    /// exists, because both sides run as one SQLite `BEGIN IMMEDIATE`
    /// transaction and SQLite admits one writer at a time. That claim needs
    /// two real connections to a real file, and this is where it is tested.
    ///
    /// Setup: two `DbPool`s opened separately on one tempfile database, which
    /// is the same separation two processes have (the CLI resetting a password
    /// while the server is running). Sequencing is by channel; there are no
    /// sleeps and no timing assumptions anywhere.
    ///
    /// Exclusion is observed rather than assumed. While one pool holds its
    /// transaction open, a third raw connection with `busy_timeout = 0` tries
    /// `BEGIN IMMEDIATE` and must be refused *now* rather than made to wait.
    /// That is SQLite telling us directly that the writer is held.
    mod lockdown_race {
        use super::*;
        use std::sync::mpsc;

        /// A human with a fresh session, the shape a REST credential mint
        /// authorizes against.
        fn seed(pool: &db::DbPool) -> (i64, String) {
            let conn = pool.write().unwrap();
            let user = crate::db::queries::users::create_user(
                &conn,
                &crate::db::models::CreateUser {
                    username: "blake".into(),
                    email: "blake@test.local".into(),
                    password: "testpassword1".into(),
                    display_name: None,
                    is_admin: true,
                    is_bot: false,
                },
            )
            .unwrap();
            let session = crate::db::queries::users::create_session(&conn, user.id, None).unwrap();
            (user.id, session.token)
        }

        /// Whether the database's single write slot is currently taken.
        ///
        /// Opens its own connection and disables the busy handler, so
        /// `BEGIN IMMEDIATE` resolves immediately either way: it takes the
        /// lock (nobody is writing) or returns SQLITE_BUSY (somebody is). No
        /// waiting, so no flakiness.
        fn writer_is_held(path: &std::path::Path) -> bool {
            let probe = rusqlite::Connection::open(path).expect("probe connection");
            probe
                .busy_timeout(std::time::Duration::ZERO)
                .expect("disable the busy handler");
            match probe.execute_batch("BEGIN IMMEDIATE") {
                Ok(()) => {
                    probe.execute_batch("ROLLBACK").expect("release the probe");
                    false
                }
                // Only contention counts. Any other failure means the probe
                // itself is broken (a missing file, a corrupt database), and
                // reading that as "somebody is writing" would let this whole
                // test pass for the wrong reason.
                Err(rusqlite::Error::SqliteFailure(error, _))
                    if matches!(
                        error.code,
                        rusqlite::ErrorCode::DatabaseBusy | rusqlite::ErrorCode::DatabaseLocked
                    ) =>
                {
                    true
                }
                Err(other) => panic!("probe failed for a reason other than contention: {other:?}"),
            }
        }

        fn active_keys(pool: &db::DbPool, name: &str) -> i64 {
            pool.read()
                .unwrap()
                .query_row(
                    "SELECT COUNT(*) FROM api_keys WHERE name = ?1 AND revoked = 0",
                    params![name],
                    |r| r.get(0),
                )
                .unwrap()
        }

        /// Mint first: the lockdown cannot begin until the mint commits, and
        /// then it revokes the key the mint made. No interleaving leaves a
        /// live key behind a locked-down account.
        #[test]
        fn a_lockdown_cannot_slip_inside_a_credential_mint() {
            let dir = tempfile::tempdir().expect("scratch dir");
            let path = dir.path().join("lific.db");
            let minting = db::open(&path).expect("minting pool");
            let recovering = db::open(&path).expect("recovering pool");
            let (user_id, session) = seed(&minting);
            let manager = create_key_manager().unwrap();
            let prepared = PreparedApiKey::generate(&manager).unwrap();
            assert!(
                !writer_is_held(&path),
                "control: with nobody writing, the probe must be able to take the lock"
            );

            let (inserted_tx, inserted_rx) = mpsc::channel::<()>();
            let (probe_tx, probe_rx) = mpsc::channel::<bool>();

            let (minting, session) = (&minting, &session);
            let (key, held_during_mint) = std::thread::scope(|scope| {
                let mint = scope.spawn(move || {
                    let held = std::cell::Cell::new(false);
                    // Exactly the transaction `POST /api/auth/keys` runs.
                    let key = minting.transaction(|tx| {
                        revalidate_recent_session(tx, session, user_id)?;
                        let plaintext = prepared.insert(tx, "racing", None, Some(user_id))?;
                        inserted_tx.send(()).expect("announce the insert");
                        // Still inside the transaction: hold it open until the
                        // other side has been told it cannot get in.
                        held.set(probe_rx.recv().expect("probe result"));
                        Ok(plaintext)
                    });
                    (key.expect("the mint commits"), held.get())
                });

                inserted_rx.recv().expect("the mint reached its insert");
                probe_tx
                    .send(writer_is_held(&path))
                    .expect("hand the probe result back");
                mint.join().expect("mint thread")
            });

            assert!(
                held_during_mint,
                "the write slot must be taken for the whole mint transaction"
            );
            assert_eq!(active_keys(minting, "racing"), 1, "the mint committed");

            // Only now, on the other pool, can the recovery run.
            recovering
                .transaction(|tx| crate::db::queries::users::lock_down_account(tx, user_id))
                .expect("the lockdown commits once the writer is free");

            assert!(
                validate_api_key(&recovering, &manager, &key).is_err(),
                "a key minted immediately before a lockdown is inside its blast radius"
            );
            assert_eq!(active_keys(&recovering, "racing"), 0);
        }

        /// Lockdown first: the mint cannot begin until the lockdown commits,
        /// and then its in-transaction revalidation finds no session and
        /// refuses. No interleaving lets a key through on a dead session.
        #[test]
        fn a_credential_mint_cannot_slip_inside_a_lockdown() {
            let dir = tempfile::tempdir().expect("scratch dir");
            let path = dir.path().join("lific.db");
            let minting = db::open(&path).expect("minting pool");
            let recovering = db::open(&path).expect("recovering pool");
            let (user_id, session) = seed(&minting);
            let manager = create_key_manager().unwrap();
            let prepared = PreparedApiKey::generate(&manager).unwrap();
            assert!(
                !writer_is_held(&path),
                "control: with nobody writing, the probe must be able to take the lock"
            );

            let (locked_tx, locked_rx) = mpsc::channel::<()>();
            let (probe_tx, probe_rx) = mpsc::channel::<bool>();

            let recovering_ref = &recovering;
            let held_during_lockdown = std::thread::scope(|scope| {
                let recovery = scope.spawn(move || {
                    let held = std::cell::Cell::new(false);
                    recovering_ref
                        .transaction(|tx| {
                            crate::db::queries::users::lock_down_account(tx, user_id)?;
                            locked_tx.send(()).expect("announce the lockdown");
                            held.set(probe_rx.recv().expect("probe result"));
                            Ok(())
                        })
                        .expect("the lockdown commits");
                    held.get()
                });

                locked_rx.recv().expect("the lockdown reached its writes");
                probe_tx
                    .send(writer_is_held(&path))
                    .expect("hand the probe result back");
                recovery.join().expect("recovery thread")
            });

            assert!(
                held_during_lockdown,
                "the write slot must be taken for the whole lockdown transaction"
            );

            // The mint now runs against the committed post-lockdown state.
            let outcome = minting.transaction(|tx| {
                revalidate_recent_session(tx, &session, user_id)?;
                prepared.insert(tx, "racing", None, Some(user_id))
            });
            assert!(
                matches!(outcome, Err(crate::error::LificError::Forbidden(_))),
                "revalidation must refuse a session the lockdown deleted: {outcome:?}"
            );
            assert_eq!(
                active_keys(&minting, "racing"),
                0,
                "the refused mint wrote nothing"
            );
        }
    }

    #[test]
    fn verify_key_succeeds() {
        let pool = test_db();
        let manager = create_key_manager().unwrap();
        let key = create_api_key(&pool, &manager, "test-key", None).unwrap();

        // Load the hash and verify
        let keys = list_api_keys(&pool).unwrap();
        assert_eq!(keys.len(), 1);

        let secure_key = SecureString::from(key);
        let conn = pool.read().unwrap();
        let hash: String = conn
            .query_row(
                "SELECT key_hash FROM api_keys WHERE name = 'test-key'",
                [],
                |row| row.get(0),
            )
            .unwrap();

        let status = manager.verify(&secure_key, &hash).unwrap();
        assert!(matches!(status, KeyStatus::Valid));
    }

    #[test]
    fn wrong_key_fails() {
        let pool = test_db();
        let manager = create_key_manager().unwrap();
        create_api_key(&pool, &manager, "test-key", None).unwrap();

        let conn = pool.read().unwrap();
        let hash: String = conn
            .query_row(
                "SELECT key_hash FROM api_keys WHERE name = 'test-key'",
                [],
                |row| row.get(0),
            )
            .unwrap();

        let wrong_key = SecureString::from(
            "lific_sk-live-AAAAAAAAAAAAAAAAAAAAAAAAAAAA.0000000000000000".to_string(),
        );
        let status = manager.verify(&wrong_key, &hash);
        // Either returns Invalid or an error (checksum mismatch) -- both mean rejection
        if let Ok(KeyStatus::Valid) = status {
            panic!("wrong key should not validate");
        }
    }

    #[test]
    fn revoke_key_works() {
        let pool = test_db();
        let manager = create_key_manager().unwrap();
        create_api_key(&pool, &manager, "revoke-me", None).unwrap();

        revoke_api_key(&pool, "revoke-me").unwrap();

        let keys = list_api_keys(&pool).unwrap();
        assert!(keys[0].revoked);
    }

    #[test]
    fn rotate_key_replaces_old() {
        let pool = test_db();
        let manager = create_key_manager().unwrap();
        let old_key = create_api_key(&pool, &manager, "rotate-me", None).unwrap();
        let new_key = rotate_api_key(&pool, &manager, "rotate-me").unwrap();

        assert_ne!(old_key, new_key);
        assert!(new_key.starts_with("lific_sk-live-"));

        // Old key deleted, only new key remains
        let keys = list_api_keys(&pool).unwrap();
        assert_eq!(keys.len(), 1);
        assert!(!keys[0].revoked);
    }

    // LIF-391: a key that belongs to a user is bound by the insert that
    // creates it. There is no window where the row exists unbound, which is
    // what used to let a crash mid-creation strand a key that resolves as
    // the operator.
    #[test]
    fn created_key_is_bound_by_the_insert() {
        let pool = test_db();
        let manager = create_key_manager().unwrap();
        let user_id = {
            let conn = pool.write().unwrap();
            conn.execute(
                "INSERT INTO users (username, email, password_hash, display_name, is_admin, is_bot)
                 VALUES ('owner', 'owner@test.local', 'x', 'Owner', 0, 0)",
                [],
            )
            .unwrap();
            conn.last_insert_rowid()
        };
        create_api_key(&pool, &manager, "owned", Some(user_id)).unwrap();
        create_api_key_with_expiry(&pool, &manager, "dated", Some("2030-06-01"), Some(user_id))
            .unwrap();

        let conn = pool.read().unwrap();
        for name in ["owned", "dated"] {
            let bound: Option<i64> = conn
                .query_row(
                    "SELECT user_id FROM api_keys WHERE name = ?1",
                    params![name],
                    |row| row.get(0),
                )
                .unwrap();
            assert_eq!(bound, Some(user_id), "key '{name}' must be created bound");
        }

        // No binding asked for, none written.
        create_api_key(&pool, &manager, "anonymous", None).unwrap();
        let bound: Option<i64> = conn
            .query_row(
                "SELECT user_id FROM api_keys WHERE name = 'anonymous'",
                [],
                |row| row.get(0),
            )
            .unwrap();
        assert_eq!(bound, None);
    }

    // LIF-391: `lific connect` re-minting a bot's key rotates an existing
    // name; the new key must land on the bot even when the old row was
    // unbound, since the rotate path no longer patches the binding after.
    #[test]
    fn rotate_bound_overrides_the_previous_binding() {
        let pool = test_db();
        let manager = create_key_manager().unwrap();
        create_api_key(&pool, &manager, "claude-code-owner", None).unwrap();
        let bot_id = {
            let conn = pool.write().unwrap();
            conn.execute(
                "INSERT INTO users (username, email, password_hash, display_name, is_admin, is_bot)
                 VALUES ('claude-code', 'bot@test.local', 'x', 'Claude Code', 0, 1)",
                [],
            )
            .unwrap();
            conn.last_insert_rowid()
        };

        rotate_api_key_bound(&pool, &manager, "claude-code-owner", Some(bot_id)).unwrap();

        let conn = pool.read().unwrap();
        let bound: Option<i64> = conn
            .query_row(
                "SELECT user_id FROM api_keys WHERE name = 'claude-code-owner' AND revoked = 0",
                [],
                |row| row.get(0),
            )
            .unwrap();
        assert_eq!(bound, Some(bot_id));
    }

    // LIF-132: rotation must carry the user binding over to the new key.
    // Previously the old row was deleted (user_id and all) and the new key
    // was created unbound, silently de-attributing bot/user keys.
    #[test]
    fn rotate_key_preserves_user_binding() {
        let pool = test_db();
        let manager = create_key_manager().unwrap();
        // A key bound to a user at creation.
        let user_id = {
            let conn = pool.write().unwrap();
            conn.execute(
                "INSERT INTO users (username, email, password_hash, display_name, is_admin, is_bot)
                 VALUES ('bot', 'bot@test.local', 'x', 'Bot', 0, 1)",
                [],
            )
            .unwrap();
            conn.last_insert_rowid()
        };
        create_api_key(&pool, &manager, "bot-key", Some(user_id)).unwrap();

        rotate_api_key(&pool, &manager, "bot-key").unwrap();

        let conn = pool.read().unwrap();
        let bound: Option<i64> = conn
            .query_row(
                "SELECT user_id FROM api_keys WHERE name = 'bot-key' AND revoked = 0",
                [],
                |row| row.get(0),
            )
            .unwrap();
        assert_eq!(
            bound,
            Some(user_id),
            "rotated key must keep its user binding"
        );
    }

    // LIF-132: rotating an unbound key still works and stays unbound.
    #[test]
    fn rotate_unbound_key_stays_unbound() {
        let pool = test_db();
        let manager = create_key_manager().unwrap();
        create_api_key(&pool, &manager, "plain", None).unwrap();
        rotate_api_key(&pool, &manager, "plain").unwrap();

        let conn = pool.read().unwrap();
        let bound: Option<i64> = conn
            .query_row(
                "SELECT user_id FROM api_keys WHERE name = 'plain' AND revoked = 0",
                [],
                |row| row.get(0),
            )
            .unwrap();
        assert_eq!(bound, None);
    }

    #[test]
    fn duplicate_name_rejected() {
        let pool = test_db();
        let manager = create_key_manager().unwrap();
        create_api_key(&pool, &manager, "unique", None).unwrap();
        let result = create_api_key(&pool, &manager, "unique", None);
        assert!(result.is_err());
    }

    #[test]
    fn has_any_keys_works() {
        let pool = test_db();
        assert!(!has_any_keys(&pool));

        let manager = create_key_manager().unwrap();
        create_api_key(&pool, &manager, "first", None).unwrap();
        assert!(has_any_keys(&pool));
    }

    // LIFIC-9: the initial-key decision is shared by `init` and `start`.
    #[test]
    fn should_mint_initial_key_empty_bootstrap_mints() {
        let pool = test_db();
        // No humans, no keys: the genuinely empty bootstrap.
        assert!(should_mint_initial_key(&pool));
    }

    #[test]
    fn should_mint_initial_key_false_once_a_human_operator_exists() {
        let pool = test_db();
        let conn = pool.write().unwrap();
        crate::db::queries::users::create_passwordless_admin(&conn, "Blake").unwrap();
        drop(conn);
        // A human exists even with zero keys: passwordless mode, no mint.
        assert!(!should_mint_initial_key(&pool));
    }

    #[test]
    fn should_mint_initial_key_false_when_any_key_exists() {
        let pool = test_db();
        let manager = create_key_manager().unwrap();
        create_api_key(&pool, &manager, "first", None).unwrap();
        assert!(!should_mint_initial_key(&pool));
    }

    #[test]
    fn create_key_stores_key_id() {
        let pool = test_db();
        let manager = create_key_manager().unwrap();
        let key = create_api_key(&pool, &manager, "id-test", None).unwrap();

        let conn = pool.read().unwrap();
        let stored_key_id: Option<String> = conn
            .query_row(
                "SELECT key_id FROM api_keys WHERE name = 'id-test'",
                [],
                |row| row.get(0),
            )
            .unwrap();

        // key_id should be stored and be a 32-char hex string
        let key_id = stored_key_id.expect("key_id should be stored");
        assert_eq!(key_id.len(), 32);
        assert!(key_id.chars().all(|c| c.is_ascii_hexdigit()));

        // Extracting key_id from the plaintext should match
        let secure_key = SecureString::from(key);
        let extracted_id = manager.extract_key_id(&secure_key);
        assert_eq!(extracted_id, key_id);
    }

    #[test]
    fn key_id_lookup_finds_correct_key() {
        let pool = test_db();
        let manager = create_key_manager().unwrap();

        // Create multiple keys
        let key1 = create_api_key(&pool, &manager, "key-1", None).unwrap();
        let _key2 = create_api_key(&pool, &manager, "key-2", None).unwrap();

        // Extract key_id from key1 and look it up
        let secure_key = SecureString::from(key1);
        let key_id = manager.extract_key_id(&secure_key);

        let conn = pool.read().unwrap();
        let found_name: String = conn
            .query_row(
                "SELECT name FROM api_keys WHERE key_id = ?1 AND revoked = 0",
                params![key_id],
                |row| row.get(0),
            )
            .unwrap();
        assert_eq!(found_name, "key-1");
    }

    #[test]
    fn legacy_key_without_key_id_still_verifiable() {
        let pool = test_db();
        let manager = create_key_manager().unwrap();
        let key = create_api_key(&pool, &manager, "legacy", None).unwrap();

        // Simulate a pre-migration key by clearing key_id
        let conn = pool.write().unwrap();
        conn.execute(
            "UPDATE api_keys SET key_id = NULL WHERE name = 'legacy'",
            [],
        )
        .unwrap();
        drop(conn);

        // Verify still works by scanning NULL key_id rows
        let secure_key = SecureString::from(key);
        let conn = pool.read().unwrap();
        let hash: String = conn
            .query_row(
                "SELECT key_hash FROM api_keys WHERE name = 'legacy'",
                [],
                |row| row.get(0),
            )
            .unwrap();

        let status = manager.verify(&secure_key, &hash).unwrap();
        assert!(matches!(status, KeyStatus::Valid));
    }

    // ── LIF-204: OAuth-token user_id -> resolved AuthUser (REST middleware) ──
    //
    // `require_api_key` already resolves an OAuth token's bound user_id into a
    // full `AuthUser` (LIF-79) and inserts it as `Extension<Option<AuthUser>>`.
    // These tests exercise that resolution end-to-end through the actual
    // middleware (rather than just the lower-level `oauth::oauth_token_user_id`
    // helper, already covered in oauth.rs) to prove the request path shared by
    // every REST handler and the /mcp route.

    fn test_auth_state(pool: &db::DbPool) -> AuthState {
        AuthState {
            db: pool.clone(),
            manager: create_key_manager().unwrap(),
            public_url: "https://example.com".into(),
            required: true,
        }
    }

    /// Minimal router: `require_api_key` in front of a handler that echoes
    /// back whatever `Extension<Option<AuthUser>>` the middleware resolved.
    /// Lets tests assert on the resolved identity without a full REST route.
    fn echo_app(auth_state: AuthState) -> Router {
        async fn echo(
            Extension(auth_user): Extension<Option<crate::db::models::AuthUser>>,
        ) -> String {
            match auth_user {
                Some(u) => format!("user:{}:{}:{}", u.id, u.username, u.is_admin),
                None => "none".to_string(),
            }
        }
        Router::new()
            .route("/echo", get(echo))
            .layer(middleware::from_fn_with_state(auth_state, require_api_key))
    }

    /// Insert an `oauth_tokens` row directly, bound to `user_id` (or
    /// unbound if `None`), bypassing the full authorize/token-exchange dance
    /// (already covered end-to-end in oauth.rs). Returns the raw bearer token.
    fn insert_oauth_token(pool: &db::DbPool, suffix: &str, user_id: Option<i64>) -> String {
        let token = format!("lific_at_test-{suffix}");
        let hash = sha256_hex(token.as_bytes());
        let expires = (chrono::Utc::now() + chrono::Duration::hours(1)).to_rfc3339();
        let client_id = format!("client-{suffix}");
        let conn = pool.write().unwrap();
        conn.execute(
            "INSERT INTO oauth_clients (client_id, client_name, redirect_uris) VALUES (?1, 'Test', '[\"http://localhost\"]')",
            params![client_id],
        )
        .unwrap();
        conn.execute(
            "INSERT INTO oauth_tokens (access_token, client_id, expires_at, scope, user_id) VALUES (?1, ?2, ?3, 'mcp', ?4)",
            params![hash, client_id, expires, user_id],
        )
        .unwrap();
        token
    }

    #[tokio::test]
    async fn oauth_token_rest_request_resolves_to_correct_auth_user() {
        let pool = test_db();
        let user_id = {
            let conn = pool.write().unwrap();
            crate::db::queries::users::create_user(
                &conn,
                &crate::db::models::CreateUser {
                    username: "tokenuser".into(),
                    email: "tokenuser@test.com".into(),
                    password: "testpassword1".into(),
                    display_name: Some("Token User".into()),
                    is_admin: false,
                    is_bot: false,
                },
            )
            .unwrap()
            .id
        };
        let token = insert_oauth_token(&pool, "resolves", Some(user_id));

        let resp = echo_app(test_auth_state(&pool))
            .oneshot(
                Request::builder()
                    .uri("/echo")
                    .header("authorization", format!("Bearer {token}"))
                    .body(Body::empty())
                    .unwrap(),
            )
            .await
            .unwrap();
        assert_eq!(resp.status(), StatusCode::OK);
        let bytes = resp.into_body().collect().await.unwrap().to_bytes();
        assert_eq!(
            bytes.as_ref(),
            format!("user:{user_id}:tokenuser:false").as_bytes(),
            "OAuth token must resolve to the bound user, not None"
        );
    }

    #[tokio::test]
    async fn oauth_token_bound_to_bot_resolves_to_the_bot_identity() {
        // LIFIC-13: at OAuth approval Lific mints a per-tool bot and binds the
        // issued token to it. The middleware must resolve that token to the bot
        // (which authz then raises to the bot's owner for permissions).
        let pool = test_db();
        let bot_id = {
            let conn = pool.write().unwrap();
            crate::db::queries::users::create_user(
                &conn,
                &crate::db::models::CreateUser {
                    username: "claude-code-blake".into(),
                    email: "claude-code-blake@bot.local".into(),
                    password: "testpassword1".into(),
                    display_name: Some("Claude Code".into()),
                    is_admin: false,
                    is_bot: true,
                },
            )
            .unwrap()
            .id
        };
        let token = insert_oauth_token(&pool, "bot", Some(bot_id));

        let resp = identity_echo_app(test_auth_state(&pool))
            .oneshot(
                Request::builder()
                    .uri("/echo")
                    .header("authorization", format!("Bearer {token}"))
                    .body(Body::empty())
                    .unwrap(),
            )
            .await
            .unwrap();
        assert_eq!(resp.status(), StatusCode::OK);
        let bytes = resp.into_body().collect().await.unwrap().to_bytes();
        let body = String::from_utf8(bytes.as_ref().to_vec()).unwrap();
        assert!(
            body.contains(&format!("id:{bot_id}:claude-code-blake")),
            "OAuth token must resolve to the per-tool bot, got: {body}"
        );
    }

    #[tokio::test]
    async fn legacy_api_key_without_user_resolves_to_none_via_middleware() {
        let pool = test_db();
        let manager = create_key_manager().unwrap();
        let key = create_api_key(&pool, &manager, "legacy-plain", None).unwrap();

        let resp = echo_app(test_auth_state(&pool))
            .oneshot(
                Request::builder()
                    .uri("/echo")
                    .header("authorization", format!("Bearer {key}"))
                    .body(Body::empty())
                    .unwrap(),
            )
            .await
            .unwrap();
        assert_eq!(resp.status(), StatusCode::OK);
        let bytes = resp.into_body().collect().await.unwrap().to_bytes();
        assert_eq!(
            bytes.as_ref(),
            b"none",
            "a legacy key with no bound user must stay unresolved (default-deny)"
        );
    }

    // ── Owner deactivation reaches the bots (LIF-214 follow-up) ──
    //
    // A bot is a separate `users` row holding its own API key and OAuth
    // token, and `authz::effective_user` resolves it to its owner before any
    // permission check. Deactivating the owner therefore has to stop the
    // bot's credentials too, or the switched-off account keeps acting through
    // its agents. Enforcement lives on the read path, so these also assert
    // that reactivating the owner brings the bots straight back with the same
    // credentials.

    /// Owner (human, non-admin so the last-admin guard stays out of the way)
    /// plus one bot they own. Returns `(pool, owner_id, bot_id)`.
    fn owner_and_bot(pool: &db::DbPool) -> (i64, i64) {
        let conn = pool.write().unwrap();
        // An admin has to exist so the owner is never the last one.
        crate::db::queries::users::create_user(
            &conn,
            &crate::db::models::CreateUser {
                username: "instance-admin".into(),
                email: "instance-admin@test.com".into(),
                password: "testpassword1".into(),
                display_name: None,
                is_admin: true,
                is_bot: false,
            },
        )
        .unwrap();
        let owner = crate::db::queries::users::create_user(
            &conn,
            &crate::db::models::CreateUser {
                username: "botowner".into(),
                email: "botowner@test.com".into(),
                password: "testpassword1".into(),
                display_name: Some("Bot Owner".into()),
                is_admin: false,
                is_bot: false,
            },
        )
        .unwrap();
        let bot =
            crate::db::queries::users::ensure_bot(&conn, owner.id, "opencode", "OpenCode").unwrap();
        (owner.id, bot.id)
    }

    fn set_owner_active(pool: &db::DbPool, owner_id: i64, active: bool) {
        let conn = pool.write().unwrap();
        crate::db::queries::users::set_active(&conn, owner_id, active).unwrap();
    }

    async fn echo_with_bearer(pool: &db::DbPool, token: &str) -> (StatusCode, String) {
        let resp = echo_app(test_auth_state(pool))
            .oneshot(
                Request::builder()
                    .uri("/echo")
                    .header("authorization", format!("Bearer {token}"))
                    .body(Body::empty())
                    .unwrap(),
            )
            .await
            .unwrap();
        let status = resp.status();
        let bytes = resp.into_body().collect().await.unwrap().to_bytes();
        (status, String::from_utf8(bytes.to_vec()).unwrap())
    }

    #[tokio::test]
    async fn bot_api_key_stops_working_while_its_owner_is_deactivated() {
        let pool = test_db();
        let (owner_id, bot_id) = owner_and_bot(&pool);
        let manager = create_key_manager().unwrap();
        let key = create_api_key(&pool, &manager, "opencode-key", Some(bot_id)).unwrap();

        let (status, body) = echo_with_bearer(&pool, &key).await;
        assert_eq!(status, StatusCode::OK);
        assert_eq!(body, format!("user:{bot_id}:opencode-botowner:false"));

        set_owner_active(&pool, owner_id, false);
        let (status, body) = echo_with_bearer(&pool, &key).await;
        assert_eq!(
            status,
            StatusCode::UNAUTHORIZED,
            "a bot must not outlive its owner's access: {body}"
        );
        assert_ne!(
            body, "none",
            "and must never degrade to the unbound-key operator fallback"
        );

        set_owner_active(&pool, owner_id, true);
        let (status, body) = echo_with_bearer(&pool, &key).await;
        assert_eq!(
            status,
            StatusCode::OK,
            "reactivating the owner restores the bot without re-minting: {body}"
        );
        assert_eq!(body, format!("user:{bot_id}:opencode-botowner:false"));
    }

    #[tokio::test]
    async fn bot_oauth_token_stops_working_while_its_owner_is_deactivated() {
        let pool = test_db();
        let (owner_id, bot_id) = owner_and_bot(&pool);
        let token = insert_oauth_token(&pool, "ownerdeact", Some(bot_id));

        let (status, body) = echo_with_bearer(&pool, &token).await;
        assert_eq!(status, StatusCode::OK);
        assert_eq!(body, format!("user:{bot_id}:opencode-botowner:false"));

        set_owner_active(&pool, owner_id, false);
        let (status, body) = echo_with_bearer(&pool, &token).await;
        assert_eq!(
            status,
            StatusCode::UNAUTHORIZED,
            "the bot's OAuth token dies with its owner's access: {body}"
        );

        set_owner_active(&pool, owner_id, true);
        let (status, body) = echo_with_bearer(&pool, &token).await;
        assert_eq!(status, StatusCode::OK, "restored on reactivation: {body}");
        assert_eq!(body, format!("user:{bot_id}:opencode-botowner:false"));
    }

    /// The read-path check is not bot-specific: a key bound to a deactivated
    /// human is refused even if the revocation `set_active` performs never
    /// happened (simulated here by flipping the flag directly).
    #[tokio::test]
    async fn api_key_bound_to_a_deactivated_human_is_refused_even_unrevoked() {
        let pool = test_db();
        let (owner_id, _bot_id) = owner_and_bot(&pool);
        let manager = create_key_manager().unwrap();
        let key = create_api_key(&pool, &manager, "human-key", Some(owner_id)).unwrap();

        {
            let conn = pool.write().unwrap();
            conn.execute(
                "UPDATE users SET is_active = 0 WHERE id = ?1",
                params![owner_id],
            )
            .unwrap();
        }

        let (status, body) = echo_with_bearer(&pool, &key).await;
        assert_eq!(status, StatusCode::UNAUTHORIZED);
        assert_ne!(body, "none", "never falls back to the operator");
    }

    /// An ownerless bot (`owner_id IS NULL`) inherits nothing, so nothing
    /// gates it. `effective_user` evaluates it as itself; so does this.
    #[tokio::test]
    async fn ownerless_bot_key_is_unaffected_by_the_owner_check() {
        let pool = test_db();
        let bot_id = {
            let conn = pool.write().unwrap();
            crate::db::queries::users::create_user(
                &conn,
                &crate::db::models::CreateUser {
                    username: "orphan-bot".into(),
                    email: "orphan-bot@bot.local".into(),
                    password: "testpassword1".into(),
                    display_name: Some("Orphan".into()),
                    is_admin: false,
                    is_bot: true,
                },
            )
            .unwrap()
            .id
        };
        let manager = create_key_manager().unwrap();
        let key = create_api_key(&pool, &manager, "orphan-key", Some(bot_id)).unwrap();

        let (status, body) = echo_with_bearer(&pool, &key).await;
        assert_eq!(status, StatusCode::OK);
        assert_eq!(body, format!("user:{bot_id}:orphan-bot:false"));
    }

    // ── LIF-294: [auth] required = false ─────────────────────────

    /// Router whose handler reports whether the request runs with the
    /// operator bypass: with authz enforcement ON, `visible_project_ids`
    /// for a `None` user returns unrestricted (None) only inside an
    /// operator context.
    fn operator_probe_app(auth_state: AuthState, pool: db::DbPool) -> Router {
        Router::new()
            .route(
                "/probe",
                get(
                    move |Extension(identity): Extension<
                        Option<crate::resolve_caller::ResolvedIdentity>,
                    >| {
                        let pool = pool.clone();
                        async move {
                            match crate::authz::visible_project_ids(&pool, &identity).unwrap() {
                                None => "unrestricted".to_string(),
                                Some(ids) => format!("restricted:{}", ids.len()),
                            }
                        }
                    },
                ),
            )
            .layer(middleware::from_fn_with_state(auth_state, require_api_key))
    }

    #[tokio::test]
    async fn auth_not_required_credentialless_request_passes_as_operator() {
        let pool = test_db();
        seed_admin(&pool, "admin"); // resolve_caller needs a first_admin to resolve to
        enable_enforcement(&pool);
        let mut state = test_auth_state(&pool);
        state.required = false;

        let resp = operator_probe_app(state, pool.clone())
            .oneshot(
                Request::builder()
                    .uri("/probe")
                    .body(Body::empty())
                    .unwrap(),
            )
            .await
            .unwrap();
        assert_eq!(resp.status(), StatusCode::OK);
        let bytes = resp.into_body().collect().await.unwrap().to_bytes();
        assert_eq!(
            bytes.as_ref(),
            b"unrestricted",
            "with [auth] required=false, an anonymous request must carry the operator bypass"
        );
    }

    #[tokio::test]
    async fn auth_required_default_credentialless_request_still_401s() {
        let pool = test_db();
        let resp =
            echo_app(test_auth_state(&pool)) // required: true
                .oneshot(Request::builder().uri("/echo").body(Body::empty()).unwrap())
                .await
                .unwrap();
        assert_eq!(resp.status(), StatusCode::UNAUTHORIZED);
    }

    // THE critical negative: optional auth must never mask a bad credential.
    // A client that DOES send a token is asking to be authenticated; if that
    // token is garbage the request fails loudly instead of silently running
    // with anonymous operator powers.
    #[tokio::test]
    async fn auth_not_required_presented_invalid_tokens_still_401() {
        let pool = test_db();
        let mut state = test_auth_state(&pool);
        state.required = false;

        for bad in [
            "lific_sk-garbage",         // malformed API key
            "lific_sess_expiredorfake", // unknown session
            "lific_at_neverissued",     // unknown OAuth token
        ] {
            let resp = echo_app(state.clone())
                .oneshot(
                    Request::builder()
                        .uri("/echo")
                        .header("authorization", format!("Bearer {bad}"))
                        .body(Body::empty())
                        .unwrap(),
                )
                .await
                .unwrap();
            assert_eq!(
                resp.status(),
                StatusCode::UNAUTHORIZED,
                "presented-but-invalid credential '{bad}' must 401 even with auth optional"
            );
        }
    }

    // A real credential presented while auth is optional authenticates
    // normally — identity resolution is unchanged.
    #[tokio::test]
    async fn auth_not_required_valid_session_still_resolves_user() {
        let pool = test_db();
        let (token, user_id) = {
            let conn = pool.write().unwrap();
            let user = crate::db::queries::users::create_user(
                &conn,
                &crate::db::models::CreateUser {
                    username: "optionaluser".into(),
                    email: "optional@test.com".into(),
                    password: "testpassword1".into(),
                    display_name: None,
                    is_admin: false,
                    is_bot: false,
                },
            )
            .unwrap();
            let token = crate::db::queries::users::create_session(&conn, user.id, None)
                .unwrap()
                .token;
            (token, user.id)
        };
        let mut state = test_auth_state(&pool);
        state.required = false;

        let resp = echo_app(state)
            .oneshot(
                Request::builder()
                    .uri("/echo")
                    .header("authorization", format!("Bearer {token}"))
                    .body(Body::empty())
                    .unwrap(),
            )
            .await
            .unwrap();
        assert_eq!(resp.status(), StatusCode::OK);
        let bytes = resp.into_body().collect().await.unwrap().to_bytes();
        assert_eq!(
            bytes.as_ref(),
            format!("user:{user_id}:optionaluser:false").as_bytes()
        );
    }

    #[tokio::test]
    async fn oauth_token_for_deleted_user_is_rejected() {
        let pool = test_db();
        let user_id = {
            let conn = pool.write().unwrap();
            let id = crate::db::queries::users::create_user(
                &conn,
                &crate::db::models::CreateUser {
                    username: "ghost".into(),
                    email: "ghost@test.com".into(),
                    password: "testpassword1".into(),
                    display_name: None,
                    is_admin: false,
                    is_bot: false,
                },
            )
            .unwrap()
            .id;
            // Simulate the user having since been deleted; oauth_tokens.user_id
            // has no FK constraint so this dangling reference is possible.
            conn.execute("DELETE FROM users WHERE id = ?1", params![id])
                .unwrap();
            id
        };
        let token = insert_oauth_token(&pool, "ghost", Some(user_id));

        let resp = echo_app(test_auth_state(&pool))
            .oneshot(
                Request::builder()
                    .uri("/echo")
                    .header("authorization", format!("Bearer {token}"))
                    .body(Body::empty())
                    .unwrap(),
            )
            .await
            .unwrap();
        // PR #23 review: a token bound to a user that no longer exists is a
        // dead credential, not an anonymous one. Anonymous would flow into
        // the first-admin fallback — deleting a bot must never escalate its
        // leftover token to operator.
        assert_eq!(resp.status(), StatusCode::UNAUTHORIZED);
    }

    // ── LIF-131: api_keys.expires_at must be enforced at auth time ──────────
    //
    // The column existed (migration 003) and `lific key list` showed it, but
    // the auth path never checked it, so an expired key authenticated forever.
    // These drive the real `require_api_key` middleware: a 401 means the key
    // was refused, a 200 means it authenticated (body "none" = no bound user).

    /// Overwrite a key's expires_at directly (bypassing the CLI/date parsing)
    /// so enforcement can be exercised deterministically.
    fn set_key_expiry(pool: &db::DbPool, name: &str, expires_at: &str) {
        let conn = pool.write().unwrap();
        conn.execute(
            "UPDATE api_keys SET expires_at = ?1 WHERE name = ?2",
            params![expires_at, name],
        )
        .unwrap();
    }

    async fn auth_status(pool: &db::DbPool, key: &str) -> StatusCode {
        echo_app(test_auth_state(pool))
            .oneshot(
                Request::builder()
                    .uri("/echo")
                    .header("authorization", format!("Bearer {key}"))
                    .body(Body::empty())
                    .unwrap(),
            )
            .await
            .unwrap()
            .status()
    }

    #[tokio::test]
    async fn expired_key_id_lookup_is_rejected() {
        let pool = test_db();
        let manager = create_key_manager().unwrap();
        let key = create_api_key(&pool, &manager, "expired", None).unwrap();
        // Expire it well in the past.
        set_key_expiry(&pool, "expired", "2000-01-01T00:00:00Z");

        assert_eq!(
            auth_status(&pool, &key).await,
            StatusCode::UNAUTHORIZED,
            "an expired key must not authenticate (key_id lookup path)"
        );
    }

    #[tokio::test]
    async fn unexpired_key_authenticates() {
        let pool = test_db();
        let manager = create_key_manager().unwrap();
        let key = create_api_key(&pool, &manager, "future", None).unwrap();
        // Far-future expiry: still valid.
        set_key_expiry(&pool, "future", "2999-12-31T23:59:59Z");

        assert_eq!(
            auth_status(&pool, &key).await,
            StatusCode::OK,
            "a key with a future expiry must still authenticate"
        );
    }

    #[tokio::test]
    async fn null_expiry_authenticates() {
        let pool = test_db();
        let manager = create_key_manager().unwrap();
        // Default create leaves expires_at NULL — the never-expires case.
        let key = create_api_key(&pool, &manager, "forever", None).unwrap();

        assert_eq!(
            auth_status(&pool, &key).await,
            StatusCode::OK,
            "a NULL expires_at means the key never expires (unchanged behavior)"
        );
    }

    #[tokio::test]
    async fn expired_legacy_key_without_key_id_is_rejected() {
        let pool = test_db();
        let manager = create_key_manager().unwrap();
        let key = create_api_key(&pool, &manager, "legacy-expired", None).unwrap();
        // Simulate a pre-migration key (NULL key_id) that has also expired,
        // exercising the fallback scan path.
        {
            let conn = pool.write().unwrap();
            conn.execute(
                "UPDATE api_keys SET key_id = NULL, expires_at = '2000-01-01T00:00:00Z' \
                 WHERE name = 'legacy-expired'",
                [],
            )
            .unwrap();
        }

        assert_eq!(
            auth_status(&pool, &key).await,
            StatusCode::UNAUTHORIZED,
            "an expired legacy key must not authenticate (NULL key_id scan path)"
        );
    }

    #[test]
    fn create_api_key_with_expiry_writes_column() {
        let pool = test_db();
        let manager = create_key_manager().unwrap();
        create_api_key_with_expiry(&pool, &manager, "dated", Some("2030-06-01"), None).unwrap();

        let conn = pool.read().unwrap();
        let stored: Option<String> = conn
            .query_row(
                "SELECT expires_at FROM api_keys WHERE name = 'dated'",
                [],
                |row| row.get(0),
            )
            .unwrap();
        assert_eq!(stored.as_deref(), Some("2030-06-01"));
    }

    // ── LIF-261 / LIFIC-7: operator-key trust rule, end-to-end through the middleware ──
    //
    // resolve_caller maps any credential that authenticates but resolves no
    // user (unbound API key, legacy unbound OAuth token, "auth off" request)
    // to the first admin — so an unbound key passes the gate via
    // `identity.user.is_admin`. These drive a real route that runs
    // `authz::require_role(.., Viewer)` in enforced mode behind the real
    // `require_api_key`: a 200 means the gate passed, a 403 means it denied.

    fn enable_enforcement(pool: &db::DbPool) {
        let conn = pool.write().unwrap();
        crate::db::queries::settings::update(
            &conn,
            crate::db::queries::settings::InstanceSettingsPatch {
                authz_enforced: Some(true),
                ..Default::default()
            },
        )
        .unwrap();
    }

    fn seed_project_id(pool: &db::DbPool, ident: &str) -> i64 {
        let conn = pool.write().unwrap();
        crate::db::queries::create_project(
            &conn,
            &crate::db::models::CreateProject {
                name: format!("Project {ident}"),
                identifier: ident.into(),
                description: String::new(),
                emoji: None,
                lead_user_id: None,
            },
        )
        .unwrap()
        .id
    }

    /// A route that Viewer-gates a fixed project via `authz::require_role`
    /// behind the real `require_api_key`. 200 = allowed, 403 = Forbidden.
    fn gate_app(auth_state: AuthState, pool: db::DbPool, project_id: i64) -> Router {
        async fn gate(
            State((pool, project_id)): State<(db::DbPool, i64)>,
            Extension(identity): Extension<Option<crate::resolve_caller::ResolvedIdentity>>,
        ) -> Result<String, crate::error::LificError> {
            crate::authz::require_role(
                &pool,
                &identity,
                project_id,
                crate::db::models::Role::Viewer,
            )?;
            Ok("allowed".into())
        }
        Router::new()
            .route("/gate", get(gate))
            .with_state((pool, project_id))
            .layer(middleware::from_fn_with_state(auth_state, require_api_key))
    }

    async fn gate_status(app: Router, key: &str) -> StatusCode {
        app.oneshot(
            Request::builder()
                .uri("/gate")
                .header("authorization", format!("Bearer {key}"))
                .body(Body::empty())
                .unwrap(),
        )
        .await
        .unwrap()
        .status()
    }

    #[tokio::test]
    async fn enforced_operator_unbound_key_passes_viewer_gate_via_middleware() {
        let pool = test_db();
        seed_admin(&pool, "admin"); // resolve_caller needs a first_admin to resolve to
        let manager = create_key_manager().unwrap();
        let key = create_api_key(&pool, &manager, "operator", None).unwrap(); // unbound
        let project = seed_project_id(&pool, "OPM");
        enable_enforcement(&pool);

        let app = gate_app(test_auth_state(&pool), pool.clone(), project);
        assert_eq!(
            gate_status(app, &key).await,
            StatusCode::OK,
            "an unbound (operator) API key must pass the Viewer gate in enforced mode"
        );
    }

    // THE test: a legacy pre-binding OAuth token also resolves to None, but it
    // is NOT an operator credential — it must stay Forbidden even in the exact
    // same enforced-mode Viewer gate the operator key just passed.
    #[tokio::test]
    async fn enforced_legacy_unbound_oauth_token_is_forbidden_via_middleware() {
        let pool = test_db();
        let project = seed_project_id(&pool, "OAM");
        enable_enforcement(&pool);
        // Unbound OAuth token (user_id = None) — the LIF-204 legacy case.
        // No admin is seeded, so resolve_caller returns None and the enforced
        // gate default-denies. (With an admin present, resolve_caller would
        // resolve it to first_admin — the operator bypass is "the first admin
        // is trusted," not credential-type-specific.)
        let token = insert_oauth_token(&pool, "legacy-unbound", None);

        let app = gate_app(test_auth_state(&pool), pool.clone(), project);
        assert_eq!(
            gate_status(app, &token).await,
            StatusCode::FORBIDDEN,
            "with no admin to resolve to, a credential-less request stays default-denied"
        );
    }

    // A key bound to a real (non-member) user is NOT an operator: even though
    // it isn't None, it must be denied in enforced mode (no membership row),
    // proving the operator bypass keys off the unbound binding, not the key
    // type in general.
    #[tokio::test]
    async fn enforced_user_bound_key_nonmember_is_forbidden_via_middleware() {
        let pool = test_db();
        let manager = create_key_manager().unwrap();
        let key = {
            // A key bound to a fresh non-admin user at creation.
            let uid = {
                let conn = pool.write().unwrap();
                crate::db::queries::users::create_user(
                    &conn,
                    &crate::db::models::CreateUser {
                        username: "bounduser".into(),
                        email: "bound@test.local".into(),
                        password: "testpassword1".into(),
                        display_name: None,
                        is_admin: false,
                        is_bot: false,
                    },
                )
                .unwrap()
                .id
            };
            create_api_key(&pool, &manager, "bound", Some(uid)).unwrap()
        };
        let project = seed_project_id(&pool, "BNM");
        enable_enforcement(&pool);

        let app = gate_app(test_auth_state(&pool), pool.clone(), project);
        assert_eq!(
            gate_status(app, &key).await,
            StatusCode::FORBIDDEN,
            "a user-bound key for a non-member must be denied — it is not an operator credential"
        );
    }

    // ── LIF-267: attachment-download matcher + session-cookie parsing ───────

    #[test]
    fn is_attachment_download_matches_numeric_id_get() {
        assert!(is_attachment_download(&Method::GET, "/api/attachments/5"));
        assert!(is_attachment_download(
            &Method::GET,
            "/api/attachments/12345"
        ));
    }

    #[test]
    fn is_attachment_download_tolerates_trailing_slash() {
        assert!(is_attachment_download(&Method::GET, "/api/attachments/7/"));
    }

    #[test]
    fn is_attachment_download_excludes_list_route() {
        // The list route (no id) must stay header-only.
        assert!(!is_attachment_download(&Method::GET, "/api/attachments"));
        assert!(!is_attachment_download(&Method::GET, "/api/attachments/"));
    }

    #[test]
    fn is_attachment_download_excludes_non_numeric_and_deeper_paths() {
        assert!(!is_attachment_download(
            &Method::GET,
            "/api/attachments/abc"
        ));
        assert!(!is_attachment_download(
            &Method::GET,
            "/api/attachments/5/extra"
        ));
        assert!(!is_attachment_download(&Method::GET, "/api/attachments/5x"));
    }

    /// LIF-418: a thumbnail is loaded by an `<img>` exactly like the full
    /// image, so it needs the same cookie fallback. The other two derived
    /// routes are XHR-only and must not get it.
    #[test]
    fn is_attachment_download_includes_the_thumbnail_variant() {
        assert!(is_attachment_download(
            &Method::GET,
            "/api/attachments/5/thumbnail"
        ));
        assert!(is_attachment_download(
            &Method::GET,
            "/api/attachments/5/thumbnail/"
        ));
        assert!(!is_attachment_download(
            &Method::GET,
            "/api/attachments/5/links"
        ));
        assert!(!is_attachment_download(
            &Method::GET,
            "/api/attachments/5/preview"
        ));
        assert!(!is_attachment_download(
            &Method::GET,
            "/api/attachments/abc/thumbnail"
        ));
        assert!(!is_attachment_download(
            &Method::POST,
            "/api/attachments/5/thumbnail"
        ));
    }

    #[test]
    fn is_attachment_download_excludes_non_get_methods() {
        assert!(!is_attachment_download(
            &Method::DELETE,
            "/api/attachments/5"
        ));
        assert!(!is_attachment_download(&Method::POST, "/api/attachments/5"));
    }

    #[test]
    fn session_cookie_token_extracts_only_session_prefix() {
        let mut headers = HeaderMap::new();
        headers.insert(
            "cookie",
            "foo=bar; lific_token=lific_sess_abc123; baz=qux"
                .parse()
                .unwrap(),
        );
        assert_eq!(
            session_cookie_token(&headers).as_deref(),
            Some("lific_sess_abc123")
        );
    }

    #[test]
    fn session_cookie_token_rejects_non_session_values() {
        // An API key or OAuth token in the cookie is never accepted.
        for value in ["lific_sk-live-xxx", "lific_at_xxx", "garbage"] {
            let mut headers = HeaderMap::new();
            headers.insert("cookie", format!("lific_token={value}").parse().unwrap());
            assert_eq!(
                session_cookie_token(&headers),
                None,
                "non-session cookie value must be rejected: {value}"
            );
        }
    }

    #[test]
    fn session_cookie_token_none_when_absent() {
        let headers = HeaderMap::new();
        assert_eq!(session_cookie_token(&headers), None);
        let mut headers = HeaderMap::new();
        headers.insert("cookie", "other=1; another=2".parse().unwrap());
        assert_eq!(session_cookie_token(&headers), None);
    }

    // ── LIFIC-8: middleware inserts ResolvedIdentity alongside Option<AuthUser> ──
    //
    // `require_api_key` now also inserts `Extension<ResolvedIdentity>` (when a
    // user can be resolved) at every success branch. These drive the real
    // middleware through a handler that echoes the identity back, asserting
    // the resolved user AND the transport for each credential type — including
    // the first-admin passwordless fallback for the credential-less and
    // unbound-credential paths.

    fn seed_admin(pool: &db::DbPool, username: &str) -> i64 {
        let conn = pool.write().unwrap();
        let u = crate::db::queries::users::create_user(
            &conn,
            &crate::db::models::CreateUser {
                username: username.into(),
                email: format!("{username}@local.test"),
                password: "adminpass123".into(),
                display_name: Some(format!("Admin {username}")),
                is_admin: true,
                is_bot: false,
            },
        )
        .unwrap();
        u.id
    }

    /// Echo handler that reports the `ResolvedIdentity` the middleware
    /// resolved, mirroring `echo_app` but for the new identity type.
    fn identity_echo_app(auth_state: AuthState) -> Router {
        async fn echo(
            Extension(identity): Extension<Option<crate::resolve_caller::ResolvedIdentity>>,
        ) -> String {
            match identity {
                Some(id) => format!(
                    "id:{}:{}:{}:{}",
                    id.user.id,
                    id.user.username,
                    id.user.is_admin,
                    id.transport.as_str()
                ),
                None => "none".to_string(),
            }
        }
        Router::new()
            .route("/echo", get(echo))
            .layer(middleware::from_fn_with_state(auth_state, require_api_key))
    }

    async fn identity_body(app: Router, auth: Option<&str>) -> String {
        let mut req = Request::builder().uri("/echo");
        if let Some(token) = auth {
            req = req.header("authorization", format!("Bearer {token}"));
        }
        let resp = app.oneshot(req.body(Body::empty()).unwrap()).await.unwrap();
        assert_eq!(resp.status(), StatusCode::OK);
        let bytes = resp.into_body().collect().await.unwrap().to_bytes();
        String::from_utf8(bytes.as_ref().to_vec()).unwrap()
    }

    #[tokio::test]
    async fn resolved_identity_session_token_is_the_user_on_web_transport() {
        let pool = test_db();
        let (token, user_id) = {
            let conn = pool.write().unwrap();
            let u = crate::db::queries::users::create_user(
                &conn,
                &crate::db::models::CreateUser {
                    username: "sessuser".into(),
                    email: "sessuser@test.com".into(),
                    password: "testpassword1".into(),
                    display_name: None,
                    is_admin: false,
                    is_bot: false,
                },
            )
            .unwrap();
            let token = crate::db::queries::users::create_session(&conn, u.id, None)
                .unwrap()
                .token;
            (token, u.id)
        };

        let body = identity_body(identity_echo_app(test_auth_state(&pool)), Some(&token)).await;
        assert_eq!(
            body,
            format!("id:{user_id}:sessuser:false:web"),
            "session token must resolve to its user on the web transport"
        );
    }

    #[tokio::test]
    async fn resolved_identity_bound_api_key_resolves_to_bound_user() {
        let pool = test_db();
        let manager = create_key_manager().unwrap();
        let user_id = {
            let conn = pool.write().unwrap();
            crate::db::queries::users::create_user(
                &conn,
                &crate::db::models::CreateUser {
                    username: "keyuser".into(),
                    email: "keyuser@test.com".into(),
                    password: "testpassword1".into(),
                    display_name: None,
                    is_admin: false,
                    is_bot: false,
                },
            )
            .unwrap()
            .id
        };
        let key = create_api_key(&pool, &manager, "bound", Some(user_id)).unwrap();

        let body = identity_body(identity_echo_app(test_auth_state(&pool)), Some(&key)).await;
        assert_eq!(
            body,
            format!("id:{user_id}:keyuser:false:api"),
            "a user-bound API key must resolve to that user on the api transport"
        );
    }

    #[tokio::test]
    async fn resolved_identity_bound_oauth_token_resolves_to_bound_user() {
        let pool = test_db();
        let user_id = {
            let conn = pool.write().unwrap();
            crate::db::queries::users::create_user(
                &conn,
                &crate::db::models::CreateUser {
                    username: "oauthuser".into(),
                    email: "oauthuser@test.com".into(),
                    password: "testpassword1".into(),
                    display_name: None,
                    is_admin: false,
                    is_bot: false,
                },
            )
            .unwrap()
            .id
        };
        let token = insert_oauth_token(&pool, "bound-oauth", Some(user_id));

        let body = identity_body(identity_echo_app(test_auth_state(&pool)), Some(&token)).await;
        assert_eq!(
            body,
            format!("id:{user_id}:oauthuser:false:api"),
            "a user-bound OAuth token must resolve to that user on the api transport"
        );
    }

    // The passwordless fallback: a legacy unbound OAuth token carries no user,
    // so resolve_caller falls back to the first admin. The legacy
    // Option<AuthUser> stays None (proven by the existing middleware tests);
    // only the new ResolvedIdentity sees the fallback user.
    #[tokio::test]
    async fn resolved_identity_legacy_unbound_oauth_falls_back_to_first_admin() {
        let pool = test_db();
        let admin_id = seed_admin(&pool, "admin");
        let token = insert_oauth_token(&pool, "legacy-unbound-id", None);

        let body = identity_body(identity_echo_app(test_auth_state(&pool)), Some(&token)).await;
        assert_eq!(
            body,
            format!("id:{admin_id}:admin:true:api"),
            "a legacy unbound OAuth token must resolve to the first admin via the fallback"
        );
    }

    // An operator-trusted unbound API key likewise resolves to the first admin
    // in the new identity (its admin-ness comes from first_admin, not a
    // separate operator flag).
    #[tokio::test]
    async fn resolved_identity_unbound_api_key_falls_back_to_first_admin() {
        let pool = test_db();
        let admin_id = seed_admin(&pool, "admin");
        let manager = create_key_manager().unwrap();
        let key = create_api_key(&pool, &manager, "operator", None).unwrap(); // unbound

        let body = identity_body(identity_echo_app(test_auth_state(&pool)), Some(&key)).await;
        assert_eq!(
            body,
            format!("id:{admin_id}:admin:true:api"),
            "an unbound (operator) API key must resolve to the first admin"
        );
    }

    // Auth-off: a credential-less request is the passwordless case par
    // excellence — resolve_caller supplies the first admin so identity is
    // always known even with no credential presented.
    #[tokio::test]
    async fn resolved_identity_auth_off_credentialless_falls_back_to_first_admin() {
        let pool = test_db();
        let admin_id = seed_admin(&pool, "admin");
        let mut state = test_auth_state(&pool);
        state.required = false;

        let body = identity_body(identity_echo_app(state), None).await;
        assert_eq!(
            body,
            format!("id:{admin_id}:admin:true:api"),
            "auth-off credential-less request must resolve to the first admin"
        );
    }

    // The degenerate case: no credential AND no admin exists → no
    // ResolvedIdentity is inserted. The legacy Option<AuthUser> path is
    // unchanged (the request still passes as operator-equivalent).
    #[tokio::test]
    async fn resolved_identity_auth_off_zero_users_inserts_no_identity() {
        let pool = test_db(); // no users at all
        let mut state = test_auth_state(&pool);
        state.required = false;

        let body = identity_body(identity_echo_app(state), None).await;
        assert_eq!(body, "none", "zero-user bootstrap inserts no identity");
    }

    // ── LIFIC-18: stdio token resolution (auth::resolve_stdio_token) ───────

    #[test]
    fn resolve_api_key_user_bound_key_returns_user() {
        let pool = test_db();
        let manager = create_key_manager().unwrap();
        let uid = {
            let conn = pool.write().unwrap();
            crate::db::queries::users::create_user(
                &conn,
                &crate::db::models::CreateUser {
                    username: "agent-user".into(),
                    email: "agent-user@local.test".into(),
                    password: "testpassword1".into(),
                    display_name: None,
                    is_admin: false,
                    is_bot: true,
                },
            )
            .unwrap()
            .id
        };
        let key = create_api_key(&pool, &manager, "opencode-agent", Some(uid)).unwrap();
        let resolved = resolve_api_key_user(&pool, &manager, &key).unwrap();
        let user = resolved.expect("bound key resolves to a user");
        assert_eq!(user.id, uid);
        assert_eq!(user.username, "agent-user");
    }

    #[test]
    fn resolve_api_key_user_unbound_key_is_ok_none() {
        let pool = test_db();
        let manager = create_key_manager().unwrap();
        let key = create_api_key(&pool, &manager, "unbound", None).unwrap();
        let resolved = resolve_api_key_user(&pool, &manager, &key).unwrap();
        assert!(
            resolved.is_none(),
            "a valid-but-unbound key must be Ok(None) — operator fallback"
        );
    }

    #[test]
    fn resolve_api_key_user_invalid_key_is_err() {
        let pool = test_db();
        let manager = create_key_manager().unwrap();
        let err = resolve_api_key_user(&pool, &manager, "lific_sk-live-NOTAREALKEY")
            .expect_err("a bogus key must be an error");
        assert!(!err.is_empty());
    }

    #[test]
    fn resolve_stdio_token_without_env_is_ok_none() {
        // No LIFIC_TOKEN in the environment → Ok(None): the operator fallback.
        let _guard = lock_lific_token_env_blocking();
        let pool = test_db();
        let manager = create_key_manager().unwrap();
        // SAFETY: guarded by the crate-wide LIFIC_TOKEN lock.
        unsafe { std::env::remove_var("LIFIC_TOKEN") };
        let resolved = resolve_stdio_token(&pool, &manager).unwrap();
        assert!(resolved.is_none(), "absent token must be Ok(None)");
    }

    #[test]
    fn resolve_stdio_token_valid_env_resolves_bound_user() {
        let _guard = lock_lific_token_env_blocking();
        let pool = test_db();
        let manager = create_key_manager().unwrap();
        let uid = {
            let conn = pool.write().unwrap();
            crate::db::queries::users::create_user(
                &conn,
                &crate::db::models::CreateUser {
                    username: "stdio-agent".into(),
                    email: "stdio-agent@local.test".into(),
                    password: "testpassword1".into(),
                    display_name: None,
                    is_admin: false,
                    is_bot: true,
                },
            )
            .unwrap()
            .id
        };
        let key = create_api_key(&pool, &manager, "codex-agent", Some(uid)).unwrap();
        // SAFETY: guarded by the crate-wide LIFIC_TOKEN lock; restored every path below.
        unsafe { std::env::set_var("LIFIC_TOKEN", &key) };
        let resolved = resolve_stdio_token(&pool, &manager).unwrap();
        unsafe { std::env::remove_var("LIFIC_TOKEN") };
        assert_eq!(resolved.unwrap().id, uid);
    }

    // ── LIF-403: OAuth tokens are barred from credential management ────────
    //
    // An OAuth token stays a first-class REST credential (parity with what it
    // can do through MCP), but it may not touch the routes that mint, list or
    // revoke credentials, nor account/user administration. Enforcement lives
    // in this middleware, so these drive the REAL `api::router` behind the
    // REAL `require_api_key` — the escalation being closed (POST
    // /api/auth/keys minting a permanent key from an expiring token) is only
    // meaningful against the actual route.

    /// `api::router` with the extensions `server.rs` supplies, behind the
    /// real auth middleware.
    fn real_api_app(pool: &db::DbPool) -> Router {
        let state = test_auth_state(pool);
        let manager = std::sync::Arc::new(state.manager.clone());
        crate::api::router(pool.clone(), &[])
            .layer(Extension(manager))
            .layer(Extension(crate::realtime::RealtimeHub::new()))
            .layer(Extension(crate::config::AuthConfig {
                allow_signup: true,
                required: true,
                secure_cookies: false,
            }))
            .layer(middleware::from_fn_with_state(state, require_api_key))
    }

    async fn send(
        app: Router,
        method: &str,
        uri: &str,
        body: Option<serde_json::Value>,
        token: &str,
    ) -> (StatusCode, String) {
        let mut builder = Request::builder()
            .method(method)
            .uri(uri)
            .header("authorization", format!("Bearer {token}"));
        let body = match body {
            Some(json) => {
                builder = builder.header("content-type", "application/json");
                Body::from(serde_json::to_vec(&json).unwrap())
            }
            None => Body::empty(),
        };
        let resp = app.oneshot(builder.body(body).unwrap()).await.unwrap();
        let status = resp.status();
        let bytes = resp.into_body().collect().await.unwrap().to_bytes();
        (status, String::from_utf8_lossy(&bytes).into_owned())
    }

    fn seed_user(pool: &db::DbPool, username: &str) -> i64 {
        let conn = pool.write().unwrap();
        crate::db::queries::users::create_user(
            &conn,
            &crate::db::models::CreateUser {
                username: username.into(),
                email: format!("{username}@test.local"),
                password: "testpassword1".into(),
                display_name: None,
                is_admin: false,
                is_bot: false,
            },
        )
        .unwrap()
        .id
    }

    // THE test: the LIF-403 escalation. An expiring, revocable OAuth token
    // must not be able to trade itself for a permanent API key.
    #[tokio::test]
    async fn oauth_token_cannot_mint_an_api_key() {
        let pool = test_db();
        let uid = seed_user(&pool, "toolowner");
        let token = insert_oauth_token(&pool, "mint", Some(uid));

        let (status, body) = send(
            real_api_app(&pool),
            "POST",
            "/api/auth/keys",
            Some(serde_json::json!({ "name": "stolen" })),
            &token,
        )
        .await;

        assert_eq!(
            status,
            StatusCode::FORBIDDEN,
            "an OAuth token must not reach the key-mint route: {body}"
        );
        assert!(
            body.contains("API key management"),
            "the 403 must say why: {body}"
        );
        assert!(
            list_api_keys(&pool).unwrap().is_empty(),
            "no key may have been created"
        );
    }

    // The rest of the credential-management and account-administration
    // surface, through the real router.
    #[tokio::test]
    async fn oauth_token_is_refused_on_every_credential_management_route() {
        let pool = test_db();
        let uid = seed_user(&pool, "toolowner2");
        let token = insert_oauth_token(&pool, "surface", Some(uid));

        let cases: [(&str, &str, Option<serde_json::Value>); 9] = [
            ("GET", "/api/auth/keys", None),
            ("DELETE", "/api/auth/keys/1", None),
            ("GET", "/api/auth/bots", None),
            ("POST", "/api/auth/bots", Some(serde_json::json!({}))),
            ("POST", "/api/auth/bots/1/disconnect", None),
            ("DELETE", "/api/auth/bots/1", None),
            ("POST", "/api/auth/me/password", Some(serde_json::json!({}))),
            ("DELETE", "/api/auth/me/sessions", None),
            ("POST", "/api/users/1/promote", None),
        ];

        for (method, uri, body) in cases {
            let (status, resp) = send(real_api_app(&pool), method, uri, body, &token).await;
            assert_eq!(
                status,
                StatusCode::FORBIDDEN,
                "{method} {uri} must be closed to an OAuth token: {resp}"
            );
        }
    }

    // The other half of the design decision: OAuth tokens keep working on
    // ordinary REST routes. A regression here would be a functional break for
    // every connector that reads through REST.
    #[tokio::test]
    async fn oauth_token_still_works_on_ordinary_rest_routes() {
        let pool = test_db();
        let uid = seed_user(&pool, "reader");
        let token = insert_oauth_token(&pool, "reads", Some(uid));

        for uri in ["/api/projects", "/api/issues", "/api/users", "/api/auth/me"] {
            let (status, body) = send(real_api_app(&pool), "GET", uri, None, &token).await;
            assert_eq!(
                status,
                StatusCode::OK,
                "GET {uri} must stay open to an OAuth token: {body}"
            );
        }
    }

    // An API key authenticates ordinary REST routes but may no longer mint a
    // second credential. Previously it could, which made one leaked key a key
    // factory: the attacker minted a spare, and the account lockdown the
    // victim then ran revoked a credential the attacker had already replaced.
    // Minting now belongs to a browser session the human authenticated
    // recently, and nothing else.
    #[tokio::test]
    async fn api_key_credential_may_no_longer_mint_a_key() {
        let pool = test_db();
        let uid = seed_user(&pool, "keyholder");
        let manager = create_key_manager().unwrap();
        let key = create_api_key(&pool, &manager, "existing", Some(uid)).unwrap();

        let (status, body) = send(real_api_app(&pool), "GET", "/api/auth/me", None, &key).await;
        assert_eq!(
            status,
            StatusCode::OK,
            "the key still authenticates: {body}"
        );

        let (status, body) = send(
            real_api_app(&pool),
            "POST",
            "/api/auth/keys",
            Some(serde_json::json!({ "name": "fresh" })),
            &key,
        )
        .await;
        assert_eq!(status, StatusCode::FORBIDDEN, "{body}");
        assert!(!body.contains("lific_sk"), "no key was returned: {body}");
    }

    #[tokio::test]
    async fn session_credential_can_still_mint_a_key() {
        let pool = test_db();
        let uid = seed_user(&pool, "browseruser");
        let session = {
            let conn = pool.write().unwrap();
            crate::db::queries::users::create_session(&conn, uid, None)
                .unwrap()
                .token
        };

        let (status, body) = send(
            real_api_app(&pool),
            "POST",
            "/api/auth/keys",
            Some(serde_json::json!({ "name": "from-browser" })),
            &session,
        )
        .await;
        assert_eq!(status, StatusCode::OK, "session path unaffected: {body}");
        assert!(body.contains("lific_sk"), "a key was returned: {body}");
    }

    // /mcp is untouched: the deny-list is a REST-route list, and the MCP
    // endpoint is what OAuth tokens exist for.
    #[tokio::test]
    async fn mcp_endpoint_is_unaffected_by_the_oauth_deny_list() {
        let pool = test_db();
        let uid = seed_user(&pool, "mcpbot");
        let token = insert_oauth_token(&pool, "mcp", Some(uid));

        let app = Router::new()
            .route("/mcp", axum::routing::post(|| async { "mcp-ok" }))
            .layer(middleware::from_fn_with_state(
                test_auth_state(&pool),
                require_api_key,
            ));

        let (status, body) = send(app, "POST", "/mcp", None, &token).await;
        assert_eq!(status, StatusCode::OK, "/mcp must still accept the token");
        assert_eq!(body, "mcp-ok");
    }

    // The audit transport (`ActorCtx`) and the resolved identity's transport
    // must agree for an OAuth token, on both doors. They are computed in two
    // places — the `ActorCtx` literal in the OAuth branch, and
    // `insert_identity_extensions`, which applies the same `/mcp` conditional
    // to the `default` it is handed — so this pins them together.
    #[tokio::test]
    async fn oauth_transport_matches_between_actor_and_resolved_identity() {
        let pool = test_db();
        let uid = seed_user(&pool, "transportuser");
        let token = insert_oauth_token(&pool, "transport", Some(uid));

        async fn probe(
            Extension(identity): Extension<Option<crate::resolve_caller::ResolvedIdentity>>,
        ) -> String {
            let actor = crate::actor::current();
            format!(
                "{}|{}",
                actor.transport.as_str(),
                identity.map_or_else(|| "none".into(), |i| i.transport.as_str().to_string())
            )
        }

        for (uri, expected) in [("/mcp", "mcp|mcp"), ("/echo", "api|api")] {
            let app = Router::new()
                .route("/mcp", axum::routing::post(probe))
                .route("/echo", axum::routing::post(probe))
                .layer(middleware::from_fn_with_state(
                    test_auth_state(&pool),
                    require_api_key,
                ));
            let (status, body) = send(app, "POST", uri, None, &token).await;
            assert_eq!(status, StatusCode::OK);
            assert_eq!(
                body, expected,
                "{uri}: audit transport and resolved-identity transport must agree"
            );
        }
    }

    // ── The deny-list itself ───────────────────────────────────────────────

    #[test]
    fn oauth_blocked_reason_covers_the_credential_surface() {
        for (method, path) in [
            (Method::GET, "/api/auth/keys"),
            (Method::POST, "/api/auth/keys"),
            (Method::POST, "/api/auth/keys/"),
            (Method::DELETE, "/api/auth/keys/42"),
            (Method::GET, "/api/auth/bots"),
            (Method::POST, "/api/auth/bots"),
            (Method::POST, "/api/auth/bots/7/disconnect"),
            (Method::DELETE, "/api/auth/bots/7"),
            (Method::POST, "/api/auth/me/password"),
            (Method::DELETE, "/api/auth/me/sessions"),
            (Method::POST, "/api/auth/me/refresh"),
            (Method::PATCH, "/api/auth/me"),
            (Method::POST, "/api/users"),
            (Method::POST, "/api/users/3/promote"),
            (Method::POST, "/api/users/3/demote"),
            (Method::POST, "/api/users/3/deactivate"),
            (Method::POST, "/api/users/3/reactivate"),
            (Method::POST, "/oauth/token"),
            (Method::POST, "/oauth/register"),
        ] {
            assert!(
                oauth_blocked_reason(&method, path).is_some(),
                "{method} {path} must be closed to OAuth tokens"
            );
        }
    }

    #[test]
    fn oauth_blocked_reason_leaves_ordinary_routes_open() {
        for (method, path) in [
            (Method::GET, "/api/auth/me"),
            (Method::GET, "/api/users"),
            (Method::GET, "/api/projects"),
            (Method::POST, "/api/projects"),
            (Method::POST, "/api/issues"),
            (Method::PUT, "/api/issues/5"),
            (Method::GET, "/api/search"),
            (Method::POST, "/mcp"),
            (Method::GET, "/api/health"),
            // Near-misses that must not be caught by the prefix rules.
            (Method::GET, "/api/auth/keysomething"),
            (Method::GET, "/api/usersomething"),
        ] {
            assert_eq!(
                oauth_blocked_reason(&method, path),
                None,
                "{method} {path} must stay open to OAuth tokens"
            );
        }
    }
}