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
#![allow(clippy::result_large_err)]
use std::collections::BTreeMap;
use std::sync::Arc;
use std::time::{Duration, Instant};
use jsonwebtoken::decode;
use jsonwebtoken::decode_header;
use jsonwebtoken::errors::ErrorKind as JwtErrorKind;
use jsonwebtoken::jwk::{Jwk, JwkSet, KeyAlgorithm, KeyOperations, PublicKeyUse};
use jsonwebtoken::{Algorithm, DecodingKey, Validation};
use reqwest::Client;
use serde_json::Value;
use crate::cache::DocumentFetcher;
use crate::circuit_breaker::CircuitBreaker;
use crate::client::AuthplaneClient;
use crate::constants::{jwt_claims, oauth_params};
use crate::metadata::AuthorizationServerMetadata;
use crate::oauth::introspect_token;
use crate::prm::ProtectedResourceMetadata;
use crate::transport::{build_basic_auth_header, is_http_success, ssrf_safe_get};
use crate::{AuthplaneError, FetchSettings, build_prm, build_prm_url};
use crate::{DpopRequestContext, DpopVerificationOptions};
use crate::{VerifiedClaims, VerifierError};
const DEFAULT_ALLOWED_ALGORITHMS: &[Algorithm] = &[Algorithm::RS256, Algorithm::ES256];
const JWKS_REFRESH_MIN_INTERVAL: Duration = Duration::from_secs(30);
/// Credentials for the RFC 7662 introspection round-trip that backs
/// revocation checking.
///
/// They must belong to a **confidential** client that is either the
/// issuing client or a runtime-client of the Resource named in the token's
/// `aud`. Since authserver 0.1.2 any other caller — including a public
/// (secret-less) client — gets `{"active": false}` for every token, and the
/// verifier would reject all traffic as revoked. Empty `client_id` or
/// `client_secret` is therefore refused when the resource is constructed.
/// Link the resource server's client with
/// `authserver admin resource runtime-client add --client-id <rs-client-id> --slug <resource-slug>`.
#[derive(Debug, Clone)]
pub struct RevocationConfig {
pub client_id: String,
pub client_secret: String,
pub fail_open: bool,
}
#[derive(Debug, Clone)]
pub struct ResourceOptions {
/// Private so the type-system enforces the validation in
/// [`Self::with_allowed_algorithms`]: only `RS256` and `ES256` may
/// be installed. HMAC (`HS256`/`HS384`/`HS512`), `none`, and every
/// other asymmetric variant the underlying JWT crate exposes
/// (`RS384`/`RS512`/`PS*`/`ES384`/`EdDSA`) are rejected at
/// construction. Read via [`Self::allowed_algorithms`].
pub(crate) allowed_algorithms: Vec<Algorithm>,
pub clock_skew_seconds: u64,
/// Maximum age for inbound DPoP proofs (seconds). Separate from
/// `clock_skew_seconds` because DPoP proof TTL and access-token
/// clock skew are independent time domains.
pub dpop_proof_max_age_seconds: u64,
pub revocation: Option<RevocationConfig>,
/// Per-resource inbound DPoP configuration (RFC 9449 §7.1 + RFC 9728 §2).
///
/// * `None` (default) — Mode 3: resource has NOT opted into DPoP. The
/// verifier rejects any inbound DPoP signal (`cnf.jkt` on the access
/// token or a DPoP proof header) with
/// [`VerifierError::DpopNotSupported`]; PRM omits the `dpop_*`
/// discovery fields entirely.
/// * `Some(InboundDPoPOptions::default())` — Mode 2: bearer-only
/// tokens accepted, DPoP-bound tokens validated end-to-end. PRM
/// advertises DPoP capability with
/// `dpop_bound_access_tokens_required: false`.
/// * `Some(InboundDPoPOptions::required())` — Mode 1: bearer-only
/// tokens rejected with `VerifierError::DpopBindingMismatch`. PRM
/// advertises `dpop_bound_access_tokens_required: true`.
pub inbound_dpop: Option<crate::InboundDPoPOptions>,
/// Override for the URL emitted as the RFC 9728 §5.1 `resource_metadata`
/// parameter of every `WWW-Authenticate` challenge. `None` (default)
/// derives it from the resource identifier per RFC 9728 §3.1
/// ([`AuthplaneResource::prm_document_url`]). Set it when the document
/// is served elsewhere — e.g. the AS-hosted
/// `/.well-known/oauth-protected-resource/{ref}`. Private so the
/// absolute-URL check in [`Self::with_resource_metadata_url`] cannot be
/// bypassed; read via [`Self::resource_metadata_url`].
pub(crate) resource_metadata_url: Option<String>,
}
impl Default for ResourceOptions {
fn default() -> Self {
Self {
allowed_algorithms: DEFAULT_ALLOWED_ALGORITHMS.to_vec(),
clock_skew_seconds: 30,
dpop_proof_max_age_seconds: 300,
revocation: None,
inbound_dpop: None,
resource_metadata_url: None,
}
}
}
impl ResourceOptions {
/// Builder shortcut for opting the resource into inbound DPoP. Equivalent
/// to assigning `Some(opts)` to [`Self::inbound_dpop`] via struct-update
/// syntax — exists to avoid the four-line boilerplate at call sites:
///
/// ```ignore
/// ResourceOptions {
/// inbound_dpop: Some(InboundDPoPOptions::default()),
/// ..ResourceOptions::default()
/// }
/// ```
///
/// becomes:
///
/// ```ignore
/// ResourceOptions::default().with_inbound_dpop(InboundDPoPOptions::default())
/// ```
pub fn with_inbound_dpop(mut self, opts: crate::InboundDPoPOptions) -> Self {
self.inbound_dpop = Some(opts);
self
}
/// Restrict the accepted access-token algorithms to a non-empty
/// subset of [`DEFAULT_ALLOWED_ALGORITHMS`] (currently `RS256` and
/// `ES256`). Returns an error on an empty list or on any algorithm
/// outside that allowlist, at construction rather than at the first
/// verification.
///
/// An allowlist (rather than an HMAC blocklist) is required: the
/// jsonwebtoken crate also exposes `RS384`, `RS512`, `PS*`, `ES384`,
/// and `EdDSA`. None of these are part of the supported
/// access-token algorithm contract; silently accepting them here
/// would let a caller advertise an alg in their PRM / JWKS that
/// peers can't validate, and broaden the algorithm-confusion
/// surface beyond that contract.
///
/// Acts as the public construction path; the field is `pub(crate)`
/// so the only way to install a custom set from outside the crate
/// is through this validator.
pub fn with_allowed_algorithms(
mut self,
algorithms: Vec<Algorithm>,
) -> Result<Self, ResourceOptionsError> {
if algorithms.is_empty() {
return Err(ResourceOptionsError::EmptyAlgorithmList);
}
for alg in &algorithms {
if !is_supported_access_token_algorithm(*alg) {
return Err(ResourceOptionsError::UnsupportedAlgorithm(*alg));
}
}
self.allowed_algorithms = algorithms;
Ok(self)
}
/// Borrow the configured access-token algorithm allow-list.
pub fn allowed_algorithms(&self) -> &[Algorithm] {
&self.allowed_algorithms
}
/// Publish a custom Protected Resource Metadata URL in the
/// `resource_metadata` challenge parameter (RFC 9728 §5.1) instead of
/// the one derived from the resource identifier.
///
/// Rejects anything that is not an absolute URL with a host, at
/// construction rather than on the first `401`: a client cannot fetch
/// a relative reference out of a header, and RFC 9728 §3.3 gives it no
/// recovery path when the document does not resolve.
///
/// Whitespace, control characters, `"` and `\` are rejected on the raw
/// string, for the reason [`crate::prm`] spells out on the resource
/// identifier: the WHATWG parser trims leading and trailing C0/space and
/// removes tab and newline anywhere before parsing, so a value carrying
/// them parses cleanly while being stored and advertised intact. A
/// trailing newline — the shape a file-sourced env var or `$(cat …)`
/// produces — would then make `HeaderValue::from_str` fail and drop
/// `WWW-Authenticate` from every `401`, which is the header this setter
/// exists to populate.
pub fn with_resource_metadata_url(
mut self,
url: impl Into<String>,
) -> Result<Self, ResourceOptionsError> {
let url = url.into();
let is_absolute = url::Url::parse(&url).is_ok_and(|parsed| parsed.has_host());
if !is_absolute {
return Err(ResourceOptionsError::InvalidResourceMetadataUrl(url));
}
if url
.chars()
.any(|c| c.is_ascii_whitespace() || c.is_ascii_control() || c == '"' || c == '\\')
{
return Err(ResourceOptionsError::InvalidResourceMetadataUrl(url));
}
self.resource_metadata_url = Some(url);
Ok(self)
}
/// The configured `resource_metadata` override, if any.
pub fn resource_metadata_url(&self) -> Option<&str> {
self.resource_metadata_url.as_deref()
}
}
/// Configuration error surfaced by the validating `ResourceOptions`
/// setters.
#[derive(Debug, thiserror::Error)]
#[non_exhaustive]
pub enum ResourceOptionsError {
#[error("allowed_algorithms must be non-empty; omit the setter to accept the default set")]
EmptyAlgorithmList,
#[error(
"resource_metadata_url must be an absolute URL with a host and must not contain whitespace, control characters, '\"' or '\\', got {0:?}; omit the setter to derive it from the resource identifier (RFC 9728 section 3.1)"
)]
InvalidResourceMetadataUrl(String),
#[error(
"access-token algorithm {0:?} is not allowed; only `RS256` and `ES256` are accepted. `none`, HMAC (`HS256`/`HS384`/`HS512`), and the additional asymmetric variants the underlying JWT crate exposes (`RS384`/`RS512`/`PS*`/`ES384`/`EdDSA`) are rejected at construction."
)]
UnsupportedAlgorithm(Algorithm),
}
#[derive(Debug, Clone)]
pub struct AuthplaneResource {
issuer: String,
resource: String,
/// URL emitted as the RFC 9728 §5.1 `resource_metadata` challenge
/// parameter. Resolved once at construction — the operator's override
/// or the RFC 9728 §3.1 derivation from `resource` — so the 401 path
/// never re-derives it.
resource_metadata_url: String,
scopes: Vec<String>,
metadata: AuthorizationServerMetadata,
fetch_settings: FetchSettings,
options: ResourceOptions,
http: Client,
jwks_state: Arc<std::sync::Mutex<JwksState>>,
/// Optional shared JWKS cache (provided by `AuthplaneClient`).
/// When `Some`, key lookups bypass the in-process `JwksState` and go
/// through the shared cache, picking up background refresh + stale
/// fallback + coordinated fetch semantics.
jwks_cache: Option<Arc<crate::cache::JwksCache>>,
/// Optional live AS-metadata binding (provided by `AuthplaneClient`).
/// RFC 8414 §2 publishes `jwks_uri` in the metadata document, so the
/// URI the shared `JwksCache` fetches from has to be re-read on the
/// configured metadata refresh interval; this binding is what does
/// it, driven by the verification traffic below. `None` for the
/// `from_prefetched_metadata` / test constructors, whose caller owns
/// discovery.
metadata_binding: Option<Arc<crate::metadata_binding::MetadataBinding>>,
/// Optional shared circuit breaker (provided by `AuthplaneClient`).
/// When `Some`, the introspection round-trip on the verify hot path
/// participates in the same breaker that gates `AuthplaneClient::introspect`
/// — an AS-introspect outage trips the breaker after the threshold
/// instead of paying a round-trip per `verify_with_context` call.
/// `None` keeps the legacy (un-guarded) path for the
/// `from_prefetched_metadata` / test constructors.
circuit_breaker: Option<Arc<CircuitBreaker>>,
}
#[derive(Debug, Clone)]
struct JwksState {
keys: Vec<Jwk>,
loaded_at: Instant,
}
impl AuthplaneResource {
pub async fn create(
issuer: &str,
resource: &str,
scopes: &[String],
fetch_settings: FetchSettings,
) -> Result<Self, VerifierError> {
Self::create_with_options(
issuer,
resource,
scopes,
fetch_settings,
ResourceOptions::default(),
)
.await
}
pub async fn create_with_options(
issuer: &str,
resource: &str,
scopes: &[String],
fetch_settings: FetchSettings,
options: ResourceOptions,
) -> Result<Self, VerifierError> {
// Redundant with the `from_parts` gate for the guarantee, but it
// reports a malformed identifier before AS discovery: a typo'd
// resource costs no network round trip, and against an
// unreachable AS the operator is told the identifier is
// malformed rather than that the AS is down.
validate_resource_for_construction(resource)?;
let client = AuthplaneClient::create(issuer, fetch_settings.clone())
.await
.map_err(|error| VerifierError::MetadataUnavailable {
message: error.to_string(),
})?;
client
.resource_with_options(resource, scopes, options)
.await
}
#[allow(clippy::too_many_arguments)]
pub(crate) async fn from_parts(
issuer: String,
resource: String,
scopes: Vec<String>,
metadata: AuthorizationServerMetadata,
fetch_settings: FetchSettings,
options: ResourceOptions,
http: Client,
jwks_cache: Option<Arc<crate::cache::JwksCache>>,
metadata_binding: Option<Arc<crate::metadata_binding::MetadataBinding>>,
circuit_breaker: Option<Arc<CircuitBreaker>>,
) -> Result<Self, VerifierError> {
validate_resource_for_construction(&resource)?;
validate_revocation_for_construction(&options)?;
let resource_metadata_url = resolve_resource_metadata_url(&resource, &options)?;
let initial_keys: Vec<Jwk> = if jwks_cache.is_some() {
// Shared cache will own JWKS fetching; we only keep an empty
// in-memory state for backwards-compat path read-throughs.
Vec::new()
} else {
fetch_jwks(&metadata.jwks_uri, &fetch_settings)
.await
.map_err(|message| VerifierError::JwksUnavailable { message })?
};
Ok(Self {
issuer,
resource,
resource_metadata_url,
scopes,
metadata,
fetch_settings,
options,
http,
jwks_state: Arc::new(std::sync::Mutex::new(JwksState {
keys: initial_keys,
loaded_at: Instant::now(),
})),
jwks_cache,
metadata_binding,
circuit_breaker,
})
}
/// Construct an `AuthplaneResource` from pre-fetched metadata and
/// JWKS, bypassing the discovery calls that
/// [`AuthplaneResource::create`] performs.
///
/// Primarily intended for conformance test harnesses and callers
/// that manage JWKS caching externally (e.g. shared in-process
/// cache, preloaded fixture). Normal applications should use
/// [`AuthplaneResource::create`] or [`AuthplaneResource::create_with_options`]
/// so discovery and JWKS fetching happen through the standard
/// hardening path.
#[doc(hidden)]
pub fn from_prefetched_metadata(
issuer: &str,
resource: &str,
scopes: &[String],
metadata: AuthorizationServerMetadata,
fetch_settings: FetchSettings,
options: ResourceOptions,
jwks: JwkSet,
) -> Result<Self, VerifierError> {
validate_resource_for_construction(resource)?;
validate_revocation_for_construction(&options)?;
let resource_metadata_url = resolve_resource_metadata_url(resource, &options)?;
let http = crate::transport::build_http_client(&fetch_settings).map_err(|error| {
VerifierError::MetadataUnavailable {
message: error.to_string(),
}
})?;
Ok(Self {
issuer: issuer.to_string(),
resource: resource.to_string(),
resource_metadata_url,
scopes: scopes.to_vec(),
metadata,
fetch_settings,
options,
http,
jwks_state: Arc::new(std::sync::Mutex::new(JwksState {
keys: jwks.keys,
loaded_at: Instant::now(),
})),
jwks_cache: None,
metadata_binding: None,
circuit_breaker: None,
})
}
#[cfg(test)]
pub(crate) fn from_metadata_and_jwks(
issuer: &str,
resource: &str,
scopes: &[String],
metadata: AuthorizationServerMetadata,
fetch_settings: FetchSettings,
options: ResourceOptions,
jwks: JwkSet,
) -> Self {
Self::from_prefetched_metadata(
issuer,
resource,
scopes,
metadata,
fetch_settings,
options,
jwks,
)
.expect("valid fetch settings")
}
/// Test-only constructor variant that wires a `CircuitBreaker` into the
/// resource so tests can drive the short-circuit branches of
/// [`Self::verify`]'s revocation path (open + fail-open accepts the
/// token; open + fail-closed surfaces `MetadataUnavailable`). Mirrors
/// the production path where `AuthplaneClient::resource_with_options`
/// passes its own `Arc<CircuitBreaker>`.
#[cfg(test)]
#[allow(clippy::too_many_arguments)]
pub(crate) fn from_metadata_jwks_and_breaker(
issuer: &str,
resource: &str,
scopes: &[String],
metadata: AuthorizationServerMetadata,
fetch_settings: FetchSettings,
options: ResourceOptions,
jwks: JwkSet,
circuit_breaker: Arc<CircuitBreaker>,
) -> Self {
let http =
crate::transport::build_http_client(&fetch_settings).expect("valid fetch settings");
let resource_metadata_url =
resolve_resource_metadata_url(resource, &options).expect("valid resource");
Self {
issuer: issuer.to_string(),
resource: resource.to_string(),
resource_metadata_url,
scopes: scopes.to_vec(),
metadata,
fetch_settings,
options,
http,
jwks_state: Arc::new(std::sync::Mutex::new(JwksState {
keys: jwks.keys,
loaded_at: Instant::now(),
})),
jwks_cache: None,
metadata_binding: None,
circuit_breaker: Some(circuit_breaker),
}
}
pub fn resource(&self) -> &str {
&self.resource
}
pub fn issuer(&self) -> &str {
&self.issuer
}
pub fn scopes(&self) -> &[String] {
&self.scopes
}
pub fn prm_response(&self) -> ProtectedResourceMetadata {
// RFC 9728 §2 + RFC 9449 §7.1 — resources advertise DPoP support
// here so OAuth-discovery clients can mint matching tokens. When
// `inbound_dpop` is not configured (Mode 3), the document omits
// `dpop_*` entirely; when configured, it lists the accepted proof
// algorithms and sets `dpop_bound_access_tokens_required` from
// the `required` flag (Mode 1 vs Mode 2).
let (dpop_algs, dpop_required) = match self.options.inbound_dpop.as_ref() {
Some(inbound) => {
// `filter_map` drops any algorithm `prm_alg_label` can't name
// (today only the HMAC variants, which the upstream allowlist
// filters already reject — see `prm_alg_label`'s doc). Failing
// closed by omission keeps the PRM document well-formed and
// serves a degraded discovery answer rather than panicking on
// a per-request hot path; a `debug_assert!` inside
// `prm_alg_label` still surfaces the regression in
// dev / test builds.
let algs: Vec<String> = inbound
.resolved_allowed_proof_algorithms()
.iter()
.filter_map(prm_alg_label)
.collect();
(Some(algs), inbound.is_required())
}
None => (None, false),
};
build_prm(
&self.issuer,
&self.resource,
&self.scopes,
dpop_algs.as_deref(),
dpop_required,
)
}
/// RFC 9728 §3.1 — absolute URL of this resource's metadata document,
/// derived from the resource identifier. This is where a server that
/// hosts its own document should serve it.
pub fn prm_document_url(&self) -> Result<String, AuthplaneError> {
build_prm_url(&self.resource)
}
/// The URL advertised as the RFC 9728 §5.1 `resource_metadata`
/// parameter of every `WWW-Authenticate` challenge this resource
/// emits: the [`ResourceOptions::with_resource_metadata_url`] override
/// when set, otherwise [`Self::prm_document_url`].
pub fn resource_metadata_url(&self) -> &str {
&self.resource_metadata_url
}
/// Build the `WWW-Authenticate` challenge for a verifier failure on
/// this resource: [`www_authenticate()`](crate::www_authenticate()) plus the RFC 9728 §5.1
/// `resource_metadata` parameter carrying
/// [`Self::resource_metadata_url`]. Adapters should prefer this over
/// the free function so every `401` tells the client where to discover
/// the authorization server. `realm` is optional; pass `""` to omit it.
pub fn www_authenticate(&self, error: &VerifierError, realm: &str) -> String {
crate::www_authenticate::www_authenticate_with_resource_metadata(
error,
realm,
&self.resource_metadata_url,
)
}
/// Signature and claims only — no DPoP mode dispatch.
///
/// The shared core of [`verify`](Self::verify) and
/// [`verify_with_context`](Self::verify_with_context). Deliberately
/// unaware of `cnf`: the binding guard belongs to the bearer-only
/// entrypoint and the three-mode dispatch to the context one, and putting
/// either here would break the other. `verify` used to *be* this function,
/// which is how a DPoP-bound token could be accepted as a plain bearer
/// token by anything calling it.
async fn verify_claims_only(&self, token: &str) -> Result<VerifiedClaims, VerifierError> {
if token.trim().is_empty() {
return Err(VerifierError::TokenMissing);
}
let header = decode_header(token).map_err(|error| VerifierError::InvalidSignature {
message: error.to_string(),
})?;
let kid = header.kid.ok_or_else(|| VerifierError::InvalidClaims {
message: "token header missing 'kid' field".to_string(),
})?;
let alg = header.alg;
let typ = header.typ.ok_or_else(|| VerifierError::InvalidClaims {
message: "token header missing 'typ' field".to_string(),
})?;
// RFC 9068 §2.1: access tokens MUST use typ "at+jwt". We enforce this
// strictly — tokens with "JWT" or missing typ are rejected, which
// prevents type-confusion attacks where a generic JWT is accepted as
// an access token.
if typ != "at+jwt" {
return Err(VerifierError::InvalidClaims {
message: format!("token type must be 'at+jwt', got {typ:?}"),
});
}
if !self.options.allowed_algorithms.contains(&alg) || is_dangerous_algorithm(alg) {
return Err(VerifierError::InvalidClaims {
message: format!("token algorithm {alg:?} is not allowed"),
});
}
let key = self.lookup_key(&kid, alg).await?;
let decoding_key =
DecodingKey::from_jwk(&key).map_err(|error| VerifierError::JwksUnavailable {
message: error.to_string(),
})?;
let mut validation = Validation::new(alg);
validation.leeway = self.options.clock_skew_seconds;
validation.validate_nbf = true;
validation.set_required_spec_claims(&[jwt_claims::EXP, jwt_claims::ISS, jwt_claims::AUD]);
validation.set_issuer(&[self.issuer.as_str()]);
validation.set_audience(&[self.resource.as_str()]);
let verified = decode::<Value>(token, &decoding_key, &validation)
.map_err(|error| map_jwt_error(error.kind(), &kid))?;
let payload = verified.claims;
let claims = build_verified_claims(&payload, &kid, self.options.clock_skew_seconds)?;
if let Some(revocation) = &self.options.revocation {
// CRITICAL: gate the introspection round-trip through the shared
// CircuitBreaker when one is wired. `AuthplaneClient::introspect`
// already does this via `run_guarded`, but the resource path —
// which validates every inbound token in production — used to
// call introspect_token directly. An AS-introspect outage paid
// a full round-trip per verify, and the breaker never opened
// because failures were not recorded against it. With the
// breaker plumbed in, an outage trips the breaker after the
// configured threshold and subsequent verify() calls
// short-circuit on AuthplaneError::CircuitOpen instead.
if let Some(breaker) = &self.circuit_breaker
&& !breaker.allow()
{
if revocation.fail_open {
// Circuit open + fail-open: accept the token without
// round-tripping. Same lenient policy the legacy
// path applied to network errors.
return Ok(claims);
}
return Err(VerifierError::MetadataUnavailable {
message: "authorization server circuit breaker is open".to_string(),
});
}
let auth_header =
build_basic_auth_header(&revocation.client_id, &revocation.client_secret);
let introspection_result = introspect_token(
&self.http,
self.metadata.introspection_endpoint().map_err(|error| {
VerifierError::MetadataUnavailable {
message: error.to_string(),
}
})?,
token,
&auth_header,
&self.fetch_settings,
None,
)
.await;
match introspection_result {
Ok(response) => {
if let Some(breaker) = &self.circuit_breaker {
breaker.record_success();
}
if !response.active {
return Err(VerifierError::TokenRevoked);
}
}
Err(error) => {
// Only count this against the breaker if it would also
// count on the `AuthplaneClient` introspection path —
// otherwise a benign OAuth response from the AS (e.g.
// `invalid_client` from rotated credentials) would trip
// the breaker here while leaving the client path
// healthy, then degrade every inbound token check for
// the cooldown window (fail_open=true silently accepts
// possibly-revoked tokens; fail_open=false rejects all
// traffic). The shared predicate lives in
// `circuit_policy` so the two consumers cannot drift.
if let Some(breaker) = &self.circuit_breaker
&& crate::circuit_policy::should_count_failure(&error)
{
breaker.record_failure();
}
if !revocation.fail_open {
return Err(VerifierError::MetadataUnavailable {
message: error.to_string(),
});
}
}
}
}
Ok(claims)
}
/// Verify a **bearer** access token (RFC 6750 §2.1).
///
/// Rejects a DPoP-bound token rather than accepting it as a bearer one.
/// A `cnf` claim means the authorization server issued this token
/// sender-constrained, and the binding is the whole reason a stolen token
/// is useless to a thief. This entrypoint has no request context, so it
/// has no proof to check against — accepting the token anyway would
/// silently discard the constraint, which is a downgrade, not a
/// limitation.
///
/// * Token **without** `cnf`: verified and returned.
/// * Token **with** `cnf`, resource **not** configured for inbound DPoP
/// (Mode 3): [`VerifierError::DpopNotSupported`] — the same answer
/// [`verify_with_context`](Self::verify_with_context) gives.
/// * Token **with** `cnf`, resource **is** configured for inbound DPoP
/// (Modes 1 and 2): [`VerifierError::DpopBindingMismatch`] — a proof is
/// required and this entrypoint cannot supply one. Use
/// [`verify_with_context`](Self::verify_with_context).
///
/// Mode dispatch comes first, so a malformed `cnf` cannot select a weaker
/// path than a well-formed one. It is *not* separated out beyond that:
/// `verify_with_context` checks for `cnf.jkt` before the proof-missing
/// check and answers [`VerifierError::InvalidClaims`] for a `cnf` without
/// one, which this entrypoint does not reproduce — there is no proof to
/// bind either way here, so the malformed case folds into the same
/// rejection. Both reject; the class differs, and the catalog case that
/// pins `invalid_claims` for that shape
/// (`rfc9449-dpop-bound-token-must-contain-cnf-jkt`) is specified against
/// the DPoP entrypoint with a proof present, not against this one.
pub async fn verify(&self, token: &str) -> Result<VerifiedClaims, VerifierError> {
let claims = self.verify_claims_only(token).await?;
// RFC 7800 §3.1 — a `cnf` of any object shape makes the token bound.
if claims
.raw
.get(jwt_claims::CNF)
.and_then(Value::as_object)
.is_some()
{
return Err(if self.options.inbound_dpop.is_none() {
VerifierError::DpopNotSupported
} else {
// Not `DpopProofMissing`: that variant means a request context
// was supplied and carried no proof. This entrypoint takes no
// context at all, which is a different thing to tell a caller
// matching on the error.
VerifierError::DpopBindingMismatch {
message: "access token is DPoP-bound (`cnf.jkt` present) but no DPoP \
request context was provided; use verify_with_context"
.to_string(),
}
});
}
Ok(claims)
}
/// RFC 9449 §7 — unified verify entrypoint.
///
/// Accepts both bearer and DPoP-bound access tokens behind a single
/// API that takes a request-level [`DpopRequestContext`] and uses
/// the token's `cnf.jkt` claim to decide whether sender-constraint
/// validation must run, so a caller need not decide up front which
/// entrypoint a token requires.
///
/// Behavior matrix:
///
/// * Token **without** `cnf`/`cnf.jkt`: ignored request context;
/// verification succeeds as a bearer token.
/// * Token **with** `cnf` but no `cnf.jkt`: rejected with
/// [`VerifierError::InvalidClaims`] — structurally malformed
/// confirmation claim.
/// * Token **with** `cnf.jkt` and `context.proof == None`: rejected
/// with [`VerifierError::DpopProofMissing`].
/// * Token **with** `cnf.jkt` and `context.proof = Some(_)`: proof
/// is validated (method, URL, nonce, `ath`, and `cnf.jkt` ↔ `jwk`
/// thumbprint binding). Mismatch surfaces as
/// [`VerifierError::InvalidClaims`].
pub async fn verify_with_context(
&self,
token: &str,
context: &DpopRequestContext,
) -> Result<VerifiedClaims, VerifierError> {
let claims = self.verify_claims_only(token).await?;
let cnf_object = claims.raw.get(jwt_claims::CNF).and_then(Value::as_object);
// RFC 7800 §3.1 — a `cnf` of any object shape makes the token bound
// for mode-dispatch purposes. The jkt-presence check happens AFTER
// mode dispatch so a malformed cnf-without-jkt surfaces as
// InvalidClaims regardless of Mode 1/2.
let token_is_bound = cnf_object.is_some();
// Constructors normalize blank proofs to `None`, so presence is
// exactly `is_some()` — mode dispatch and proof verification can
// never disagree over a `Some("")` proof.
let proof_present = context.proof.is_some();
// RFC 9449 §6 / RFC 9728 §2 — three-mode inbound DPoP.
//
// Mode 3 (inbound_dpop = None): resource has NOT opted into DPoP.
// Any DPoP signal on the request — bound token OR proof header —
// is rejected upfront. Silent downgrade to bearer would drop sender-
// binding; ad-hoc defaults applied here would be invisible in PRM.
let Some(inbound) = self.options.inbound_dpop.as_ref() else {
if token_is_bound || proof_present {
return Err(VerifierError::DpopNotSupported);
}
return Ok(claims);
};
// Modes 1 & 2 — resource supports DPoP.
if !token_is_bound {
// Mode 1 — `required = true` rejects bearer-only tokens.
if inbound.is_required() {
return Err(VerifierError::DpopBindingMismatch {
message:
"Resource requires DPoP-bound access tokens but the presented token has no `cnf.jkt`"
.to_string(),
});
}
// Mode 2 with a stray proof attached to a bearer-only token —
// the proof's `ath` has nothing to bind to, so it's malformed.
if proof_present {
return Err(VerifierError::DpopBindingMismatch {
message:
"DPoP proof presented but the access token is not DPoP-bound (`cnf.jkt` missing); \
the proof has nothing to bind to"
.to_string(),
});
}
// Bearer token, Mode 2 — accepted. The request context is
// informational; any (absent) proof is ignored.
return Ok(claims);
}
// Token is DPoP-bound — `cnf` is present but must carry a non-empty `jkt`.
let cnf = cnf_object.expect("token_is_bound implies cnf_object is Some");
let expected_jkt = cnf
.get(jwt_claims::JKT)
.and_then(Value::as_str)
.filter(|value| !value.is_empty())
.ok_or_else(|| VerifierError::InvalidClaims {
message:
"DPoP-bound access token carries 'cnf' but is missing required 'cnf.jkt' claim"
.to_string(),
})?
.to_string();
let proof = context
.proof
.as_deref()
.ok_or(VerifierError::DpopProofMissing)?;
// Resolve verification parameters: per-resource inbound_dpop fields
// override the resource-level defaults when set explicitly.
let allowed_algs = inbound.resolved_allowed_proof_algorithms();
let clock_skew = inbound.resolved_clock_skew_seconds(self.options.clock_skew_seconds);
let max_age =
inbound.resolved_max_proof_age_seconds(self.options.dpop_proof_max_age_seconds);
let dpop_options = DpopVerificationOptions {
expected_access_token: Some(token),
expected_nonce: context.nonce.as_deref(),
allowed_algorithms: &allowed_algs,
clock_skew_seconds: clock_skew,
max_age_seconds: max_age,
};
// Replay-store resolution: configured per-resource via `inbound_dpop`
// (RFC 9449 §11.1). `InboundDPoPOptions::default()` auto-allocates
// an `InMemoryDpopReplayStore` so this branch is unconditional —
// any DPoP-bound request that reaches `verify_with_context` runs
// through the with-replay path. Multi-process deployments install
// a shared store via `InboundDPoPOptions::with_replay_store`.
// Per-request replay-store override was removed deliberately: two
// requests racing with different store instances would deduplicate
// independently, defeating the JTI single-use guarantee. The
// resource-level configuration is the single source of truth.
// Use the jkt-first variant: comparing cnf.jkt BEFORE the jti commit
// closes the slot-poisoning window. An attacker who knows a
// legitimate jti could otherwise submit a proof bearing that jti
// with the wrong jkt; the jti gets registered in the replay store
// before the (post-verify) jkt check fires, and the legitimate
// proof carrying the same jti is then rejected as a replay.
let verified = crate::dpop::verify_dpop_proof_with_jkt_and_replay(
proof,
context.method.as_str(),
context.url.as_str(),
dpop_options,
&expected_jkt,
inbound.replay_store().as_ref(),
)
.await?;
let mut claims = claims;
claims.dpop_proof = Some(verified);
Ok(claims)
}
fn find_key(&self, kid: &str, alg: Algorithm) -> Option<Jwk> {
let state = self.jwks_state.lock().expect("jwks mutex poisoned");
state
.keys
.iter()
.find(|jwk| jwk_matches(jwk, kid, alg))
.cloned()
}
/// Resolve a JWK by `kid`/`alg`, preferring the shared [`JwksCache`]
/// when one was provided by [`AuthplaneClient`]. Falls back to the
/// in-process `JwksState` (with the legacy refresh-on-miss policy)
/// for callers that built the resource via `from_prefetched_metadata`.
async fn lookup_key(&self, kid: &str, alg: Algorithm) -> Result<Jwk, VerifierError> {
// RFC 8414 §2 keeps `jwks_uri` in the metadata document, so key
// resolution has to start from a document that is still current.
// Following a rotation therefore costs nothing beyond the
// verification traffic already flowing: this re-reads metadata
// only once the configured refresh interval has elapsed, and
// rebinds the shared JWKS cache when the URI changed. Runs before
// the lookup so a rotation that landed during the interval is
// already bound by the time keys are read.
if let Some(binding) = self.metadata_binding.as_ref() {
binding.refresh_if_due().await;
}
if let Some(cache) = self.jwks_cache.as_ref() {
let alg_label = alg_jose_label(alg);
let mut found = cache
.get_key_by_kid(kid, alg_label)
.await
.map_err(|error| VerifierError::JwksUnavailable {
message: error.to_string(),
})?;
// A `kid` the bound key set does not contain is the request that
// proves the binding is stale, and the metadata document naming
// where keys live is no more current than the key set it
// produced. Re-read it here rather than waiting for the interval
// gate above, which is a no-op until the interval is up: an AS
// that rotates `jwks_uri` and retires the old key set at the same
// moment would otherwise fail every verification until then. The
// re-read is floored inside the binding, so an arbitrary `kid`
// cannot turn the pre-authentication path into one discovery
// fetch per request. Retried only when `jwks_uri` actually moved
// — the rebind expires the keys cached from the withdrawn URL,
// so that is the only case where a second lookup can answer
// differently.
if found.is_none()
&& let Some(binding) = self.metadata_binding.as_ref()
&& binding.refresh_on_kid_miss().await
{
found = cache
.get_key_by_kid(kid, alg_label)
.await
.map_err(|error| VerifierError::JwksUnavailable {
message: error.to_string(),
})?;
}
let value = found.ok_or_else(|| VerifierError::InvalidSignature {
message: format!("token kid {kid:?} not found in JWKS after refresh"),
})?;
let jwk: Jwk =
serde_json::from_value(value).map_err(|error| VerifierError::JwksUnavailable {
message: error.to_string(),
})?;
return Ok(jwk);
}
// Legacy in-process state path.
if let Some(found) = self.find_key(kid, alg) {
return Ok(found);
}
if self.should_refresh_jwks() {
self.refresh_jwks().await?;
}
self.find_key(kid, alg)
.ok_or_else(|| VerifierError::InvalidSignature {
message: format!("token kid {kid:?} not found in JWKS after refresh"),
})
}
async fn refresh_jwks(&self) -> Result<(), VerifierError> {
let keys = fetch_jwks(&self.metadata.jwks_uri, &self.fetch_settings)
.await
.map_err(|message| VerifierError::JwksUnavailable { message })?;
let mut state = self.jwks_state.lock().expect("jwks mutex poisoned");
state.keys = keys;
state.loaded_at = Instant::now();
Ok(())
}
fn should_refresh_jwks(&self) -> bool {
let state = self.jwks_state.lock().expect("jwks mutex poisoned");
state.keys.is_empty() || state.loaded_at.elapsed() >= JWKS_REFRESH_MIN_INTERVAL
}
}
/// Construction-time gate on the configured resource identifier: it must
/// be an absolute URL with a scheme and a host, free of fragment,
/// whitespace/control characters, and userinfo (RFC 8707 §2 + RFC 9728
/// §3 — see `prm::validate_resource_identifier` for the full grounding).
///
/// Runs in every constructor funnel (`from_parts` behind
/// `create`/`create_with_options`/`AuthplaneClient::resource*`, plus
/// `from_prefetched_metadata`) so a malformed identifier fails when the
/// resource is built — not later, when the 401-challenge path first
/// tries to derive the PRM document URL from it. The underlying
/// rejection carries the module's `invalid_resource` error code; it
/// surfaces here as `MetadataUnavailable`, the same variant these
/// constructors already use for other construction-time failures, which
/// deliberately maps to no `WWW-Authenticate` challenge code.
fn validate_resource_for_construction(resource: &str) -> Result<(), VerifierError> {
crate::prm::validate_resource_identifier(resource).map_err(|error| {
VerifierError::MetadataUnavailable {
message: error.to_string(),
}
})
}
/// Construction-time gate on [`RevocationConfig`]: introspection needs
/// confidential client credentials. Since authserver 0.1.2 an
/// unauthenticated or public-client introspection call gets
/// `{"active": false}` for every token, so a resource built with empty
/// credentials would not be "less strict" — it would reject all traffic as
/// revoked, and silently. Fail here, where the cause is attributable, with
/// the same variant the other construction-time checks use.
fn validate_revocation_for_construction(options: &ResourceOptions) -> Result<(), VerifierError> {
let Some(revocation) = &options.revocation else {
return Ok(());
};
if revocation.client_id.trim().is_empty() || revocation.client_secret.trim().is_empty() {
return Err(VerifierError::MetadataUnavailable {
message: "RevocationConfig requires a confidential client: client_id and \
client_secret must be non-empty. authserver >= 0.1.2 answers \
active=false to unauthenticated introspection, so every token would \
be rejected as revoked"
.to_string(),
});
}
Ok(())
}
/// The URL for the `resource_metadata` challenge parameter: the operator's
/// override when set, otherwise the RFC 9728 §3.1 derivation. The
/// derivation can only fail on an identifier
/// `validate_resource_for_construction` already rejected, so the error arm
/// is defensive.
fn resolve_resource_metadata_url(
resource: &str,
options: &ResourceOptions,
) -> Result<String, VerifierError> {
match options.resource_metadata_url() {
Some(url) => Ok(url.to_string()),
None => build_prm_url(resource).map_err(|error| VerifierError::MetadataUnavailable {
message: error.to_string(),
}),
}
}
async fn fetch_jwks(jwks_uri: &str, fetch_settings: &FetchSettings) -> Result<Vec<Jwk>, String> {
// CRITICAL: always go through ssrf_safe_get so DNS pinning + IP-allowlist
// checks run between the lexical URL validation and the TCP connect. The
// previous direct http.get() path only ran validate_fetch_url (lexical),
// leaving a DNS-rebinding window where the resolved IP could be swapped
// to a cloud-metadata address between validation and connect. The
// ssrf_safe_get path resolves the host, validates every returned IP
// against the allow-list, and connects to the pinned IP with the
// original Host header preserved.
let response = ssrf_safe_get(
jwks_uri,
fetch_settings,
DocumentFetcher::DEFAULT_JWKS_MAX_BYTES,
)
.await
.map_err(|error| error.to_string())?;
let status = response.status_code;
if !is_http_success(status) {
// `ssrf_safe_get` parses the body as JSON and falls back to
// `Value::Null` on parse failure (see `transport::ssrf_safe_request`),
// so a non-JSON error page would render as the literal string
// `null` here. Surface a clearer hint instead of the misleading
// payload — for the proper fix, transport would have to carry the
// raw bytes alongside the parsed `Value`.
let body_hint = if response.body.is_null() {
"<non-JSON response body>".to_string()
} else {
response.body.to_string()
};
return Err(format!(
"jwks fetch failed with status {status}: {body_hint}"
));
}
let jwks: JwkSet = serde_json::from_value(response.body).map_err(|error| error.to_string())?;
Ok(jwks.keys)
}
/// Map a `jsonwebtoken::Algorithm` to its RFC 7518 / IANA JOSE-Algorithms
/// short name. Returns `None` for the HMAC variants (`HS*`), which are
/// rejected upstream by [`is_dangerous_algorithm`] and by
/// [`InboundDPoPOptions::with_allowed_proof_algorithms`] /
/// [`ResourceOptions::with_allowed_algorithms`]. Used by both the PRM
/// document (`dpop_signing_alg_values_supported`, RFC 9449 §7.1 + RFC
/// 9728 §2) and the JWKS cache lookup path that fans out by alg.
fn alg_jose_label(alg: Algorithm) -> Option<&'static str> {
match alg {
Algorithm::RS256 => Some("RS256"),
Algorithm::RS384 => Some("RS384"),
Algorithm::RS512 => Some("RS512"),
Algorithm::PS256 => Some("PS256"),
Algorithm::PS384 => Some("PS384"),
Algorithm::PS512 => Some("PS512"),
Algorithm::ES256 => Some("ES256"),
Algorithm::ES384 => Some("ES384"),
Algorithm::EdDSA => Some("EdDSA"),
// HMAC: never reaches a PRM document or a JWKS lookup we'd serve.
Algorithm::HS256 | Algorithm::HS384 | Algorithm::HS512 => None,
}
}
/// PRM variant of [`alg_jose_label`]. Returns `Some(label)` for any algorithm
/// the SDK is willing to advertise in `dpop_signing_alg_values_supported`,
/// and `None` for the HMAC variants — those should never reach this function
/// (the upstream allowlist filters `is_dangerous_algorithm`,
/// `InboundDPoPOptions::with_allowed_proof_algorithms`,
/// `ResourceOptions::with_allowed_algorithms`, and the private
/// `SUPPORTED_DPOP_ALGORITHMS` constant all reject HMAC), but `None` keeps
/// the PRM-emission path total: if a regression ever bypasses every filter,
/// the unsupported entry is silently dropped from the advertised list rather
/// than panicking in the per-request `prm_response()` hot path.
///
/// A `debug_assert!` still surfaces the regression in dev / test builds so
/// CI flags any future bypass loudly, without taking down a Tokio task in
/// production.
fn prm_alg_label(alg: &Algorithm) -> Option<String> {
let label = alg_jose_label(*alg).map(str::to_string);
debug_assert!(
label.is_some(),
"prm_alg_label received unsupported algorithm {alg:?}; \
InboundDPoPOptions::with_allowed_proof_algorithms / \
SUPPORTED_DPOP_ALGORITHMS should have rejected it",
);
label
}
fn jwk_matches(jwk: &Jwk, kid: &str, alg: Algorithm) -> bool {
if jwk.common.key_id.as_deref() != Some(kid) {
return false;
}
if let Some(use_hint) = &jwk.common.public_key_use
&& use_hint != &PublicKeyUse::Signature
{
return false;
}
if let Some(key_ops) = &jwk.common.key_operations
&& !key_ops
.iter()
.any(|operation| operation == &KeyOperations::Verify)
{
return false;
}
if let Some(key_alg) = jwk.common.key_algorithm
&& key_algorithm_to_jwt(key_alg) != Some(alg)
{
return false;
}
true
}
fn key_algorithm_to_jwt(value: KeyAlgorithm) -> Option<Algorithm> {
match value {
KeyAlgorithm::RS256 => Some(Algorithm::RS256),
KeyAlgorithm::RS384 => Some(Algorithm::RS384),
KeyAlgorithm::RS512 => Some(Algorithm::RS512),
KeyAlgorithm::PS256 => Some(Algorithm::PS256),
KeyAlgorithm::PS384 => Some(Algorithm::PS384),
KeyAlgorithm::PS512 => Some(Algorithm::PS512),
KeyAlgorithm::ES256 => Some(Algorithm::ES256),
KeyAlgorithm::ES384 => Some(Algorithm::ES384),
_ => None,
}
}
fn is_dangerous_algorithm(alg: Algorithm) -> bool {
matches!(alg, Algorithm::HS256 | Algorithm::HS384 | Algorithm::HS512)
}
/// Only `RS256` and `ES256` are part of the access-token contract.
/// Used by
/// [`ResourceOptions::with_allowed_algorithms`] at construction so we
/// reject (not just HMAC, but) every other variant the underlying
/// `jsonwebtoken` crate exposes.
fn is_supported_access_token_algorithm(alg: Algorithm) -> bool {
matches!(alg, Algorithm::RS256 | Algorithm::ES256)
}
fn build_verified_claims(
payload: &Value,
kid: &str,
clock_skew_seconds: u64,
) -> Result<VerifiedClaims, VerifierError> {
let object = payload
.as_object()
.ok_or_else(|| VerifierError::InvalidClaims {
message: "token payload must be a JSON object".to_string(),
})?;
let issuer = required_string(payload, jwt_claims::ISS)?;
let subject = required_string(payload, jwt_claims::SUB)?;
let client_id = required_string(payload, jwt_claims::CLIENT_ID)?;
let jti = required_string(payload, jwt_claims::JTI)?;
let expires_at = required_i64(payload, jwt_claims::EXP)?;
let issued_at = required_i64(payload, jwt_claims::IAT)?;
let now = unix_now();
if issued_at > now + clock_skew_seconds as i64 {
return Err(VerifierError::InvalidClaims {
message: format!(
"token 'iat' claim is in the future (iat={issued_at}, now={now}, leeway={}s)",
clock_skew_seconds
),
});
}
let audience = parse_audience(payload.get(jwt_claims::AUD)).ok_or_else(|| {
VerifierError::InvalidClaims {
message: "token missing required 'aud' claim".to_string(),
}
})?;
let not_before = payload
.get(jwt_claims::NBF)
.and_then(Value::as_i64)
.unwrap_or_default();
let scopes = payload
.get(oauth_params::SCOPE)
.and_then(Value::as_str)
.map(|value| {
value
.split_whitespace()
.filter(|scope| !scope.is_empty())
.map(ToString::to_string)
.collect::<Vec<_>>()
})
.unwrap_or_default();
let agent_id = payload
.get(jwt_claims::AGENT_ID)
.and_then(Value::as_str)
.unwrap_or_default()
.to_string();
let agent_chain = crate::json_util::string_array(payload, jwt_claims::AGENT_CHAIN);
let raw = object
.iter()
.map(|(key, value)| (key.clone(), value.clone()))
.collect::<BTreeMap<_, _>>();
Ok(VerifiedClaims {
sub: subject,
client_id,
scopes,
issuer,
audience,
expires_at,
issued_at,
jti,
kid: kid.to_string(),
agent_id,
agent_chain,
not_before,
raw,
dpop_proof: None,
})
}
fn parse_audience(aud: Option<&Value>) -> Option<Vec<String>> {
match aud {
Some(Value::String(value)) if !value.is_empty() => Some(vec![value.to_string()]),
Some(Value::Array(values)) => {
let normalized = values
.iter()
.filter_map(Value::as_str)
.filter(|value| !value.is_empty())
.map(ToString::to_string)
.collect::<Vec<_>>();
if normalized.is_empty() {
None
} else {
Some(normalized)
}
}
_ => None,
}
}
fn required_string(payload: &Value, field: &str) -> Result<String, VerifierError> {
payload
.get(field)
.and_then(Value::as_str)
.filter(|value| !value.is_empty())
.map(ToString::to_string)
.ok_or_else(|| VerifierError::InvalidClaims {
message: format!("token missing required '{field}' claim"),
})
}
fn required_i64(payload: &Value, field: &str) -> Result<i64, VerifierError> {
payload
.get(field)
.and_then(Value::as_i64)
.ok_or_else(|| VerifierError::InvalidClaims {
message: format!("token missing required '{field}' claim"),
})
}
use crate::time_utils::unix_now_secs_i64 as unix_now;
fn map_jwt_error(kind: &JwtErrorKind, kid: &str) -> VerifierError {
match kind {
JwtErrorKind::ExpiredSignature => VerifierError::TokenExpired,
JwtErrorKind::InvalidAudience
| JwtErrorKind::InvalidIssuer
| JwtErrorKind::ImmatureSignature
| JwtErrorKind::MissingRequiredClaim(_)
| JwtErrorKind::InvalidAlgorithm => VerifierError::InvalidClaims {
message: format!("{kind:?}"),
},
JwtErrorKind::InvalidSignature => VerifierError::InvalidSignature {
message: format!("signature verification failed for kid {kid:?}"),
},
_ => VerifierError::InvalidSignature {
message: format!("{kind:?}"),
},
}
}
#[cfg(test)]
mod tests {
use jsonwebtoken::jwk::JwkSet;
use jsonwebtoken::{Algorithm, EncodingKey, Header, encode};
use serde_json::json;
use super::{
AuthplaneResource, ResourceOptions, ResourceOptionsError, RevocationConfig,
build_verified_claims, jwk_matches, parse_audience,
};
use crate::metadata::AuthorizationServerMetadata;
use crate::{DpopProofOptions, DpopRequestContext, create_dpop_proof, jwk_thumbprint_sha256};
use crate::{FetchSettings, VerifierError};
const TEST_JWKS: &str = r#"{
"keys": [
{
"kty": "RSA",
"kid": "test-kid",
"alg": "RS256",
"use": "sig",
"n": "pza1Jk6AXrea2P-TlgPStQO4PJ8H4mCz3qaW-PqscKygy31-_T-XNpYlH948O-hS3eN0bKLLKJetWx8bSWxBlMMW4DlV-vv32kO-phwPGE0BbQ2rMfZXfEKwKbcU_hTQv3_yfo6eugv3g_9bZR16MaNOWL0fWTmmcYoD7j8mODWoTgwGnHoriRE9wLgHOkXSJ-lnV4gR3Wa0HdI1Th91kve4mMC4DxxpzZ37xh5d0wyExHSb9bssowS70hts0JD-TX46MSpgVoCcZfBefyJ9JKoVgxVZ2aYGsdR8pwVRSRYUf2CYDvKyUZ8HfoWBv4JwBO0AVqT5Eb5F-X375fULQQ",
"e": "AQAB"
}
]
}"#;
const TEST_PRIVATE_PEM: &str = include_str!("../tests/fixtures/test-private.pem");
fn auth_header() -> Header {
Header {
alg: Algorithm::RS256,
kid: Some("test-kid".to_string()),
typ: Some("at+jwt".to_string()),
..Header::new(Algorithm::RS256)
}
}
fn signed_token_with_claims(claims: serde_json::Value) -> String {
encode(
&auth_header(),
&claims,
&EncodingKey::from_rsa_pem(TEST_PRIVATE_PEM.as_bytes()).expect("private key"),
)
.expect("token")
}
fn resource_with_test_jwks() -> AuthplaneResource {
// Existing DPoP tests assume Mode 2 (DPoP supported, bearer also
// accepted). Opt in here so the introduction of Mode 3 (the new
// default when inbound_dpop is None) doesn't sweep through every
// existing assertion. New tests below opt out (or in to Mode 1)
// explicitly via the corresponding `resource_with_*` helper.
resource_with_test_jwks_and_options(
ResourceOptions::default().with_inbound_dpop(crate::InboundDPoPOptions::default()),
)
}
fn resource_with_test_jwks_and_options(options: ResourceOptions) -> AuthplaneResource {
let metadata = AuthorizationServerMetadata {
issuer: "https://auth.example.com".to_string(),
jwks_uri: "https://auth.example.com/.well-known/jwks.json".to_string(),
token_endpoint: Some("https://auth.example.com/oauth/token".to_string()),
introspection_endpoint: Some("https://auth.example.com/oauth/introspect".to_string()),
revocation_endpoint: Some("https://auth.example.com/oauth/revoke".to_string()),
};
let jwks: JwkSet = serde_json::from_str(TEST_JWKS).expect("valid jwks");
AuthplaneResource::from_metadata_and_jwks(
"https://auth.example.com",
"https://api.example.com/mcp",
&["tools/read".to_string()],
metadata,
FetchSettings::from_dev_mode(true),
options,
jwks,
)
}
#[test]
fn build_verified_claims_rejects_future_iat() {
let payload = json!({
"iss": "https://auth.example.com",
"sub": "user-1",
"client_id": "client-1",
"aud": "https://api.example.com",
"exp": 4102444800i64,
"iat": 4102444800i64,
"jti": "token-1"
});
let error = build_verified_claims(
&payload,
"test-kid",
ResourceOptions::default().clock_skew_seconds,
)
.expect_err("iat must fail");
assert!(matches!(error, VerifierError::InvalidClaims { .. }));
}
#[test]
fn jwk_selection_honors_kid_use_and_alg() {
let jwks: JwkSet = serde_json::from_str(TEST_JWKS).expect("valid jwks");
assert!(jwk_matches(
&jwks.keys[0],
"test-kid",
jsonwebtoken::Algorithm::RS256
));
assert!(!jwk_matches(
&jwks.keys[0],
"wrong-kid",
jsonwebtoken::Algorithm::RS256
));
assert!(!jwk_matches(
&jwks.keys[0],
"test-kid",
jsonwebtoken::Algorithm::ES256
));
}
#[test]
fn resource_prm_uses_resource_and_issuer() {
let metadata = AuthorizationServerMetadata {
issuer: "https://auth.example.com".to_string(),
jwks_uri: "https://auth.example.com/.well-known/jwks.json".to_string(),
token_endpoint: Some("https://auth.example.com/oauth/token".to_string()),
introspection_endpoint: Some("https://auth.example.com/oauth/introspect".to_string()),
revocation_endpoint: Some("https://auth.example.com/oauth/revoke".to_string()),
};
let jwks: JwkSet = serde_json::from_str(TEST_JWKS).expect("valid jwks");
let resource = AuthplaneResource::from_metadata_and_jwks(
"https://auth.example.com",
"https://api.example.com/mcp",
&["tools/add".to_string()],
metadata,
FetchSettings::from_dev_mode(true),
ResourceOptions::default(),
jwks,
);
let prm = resource.prm_response();
assert_eq!(prm.resource, "https://api.example.com/mcp");
assert_eq!(
prm.authorization_servers,
vec!["https://auth.example.com".to_string()]
);
assert_eq!(
resource.prm_document_url().expect("valid prm document url"),
"https://api.example.com/.well-known/oauth-protected-resource/mcp"
);
}
/// Drive `from_prefetched_metadata` (the sync constructor sharing the
/// resource-identifier gate with `from_parts`) with an arbitrary
/// resource string.
fn try_construct_resource(resource: &str) -> Result<AuthplaneResource, VerifierError> {
let metadata = AuthorizationServerMetadata {
issuer: "https://auth.example.com".to_string(),
jwks_uri: "https://auth.example.com/.well-known/jwks.json".to_string(),
token_endpoint: Some("https://auth.example.com/oauth/token".to_string()),
introspection_endpoint: Some("https://auth.example.com/oauth/introspect".to_string()),
revocation_endpoint: Some("https://auth.example.com/oauth/revoke".to_string()),
};
let jwks: JwkSet = serde_json::from_str(TEST_JWKS).expect("valid jwks");
AuthplaneResource::from_prefetched_metadata(
"https://auth.example.com",
resource,
&["tools/read".to_string()],
metadata,
FetchSettings::from_dev_mode(true),
ResourceOptions::default(),
jwks,
)
}
fn assert_construction_rejects_resource(resource: &str, expected_message: &str) {
let error = try_construct_resource(resource)
.map(|_| ())
.expect_err("construction must reject the resource identifier");
let VerifierError::MetadataUnavailable { message } = error else {
panic!("expected MetadataUnavailable, got {error:?}");
};
assert!(
message.contains(expected_message),
"unexpected message: {message}"
);
}
const ABSOLUTE_URL_MESSAGE: &str =
"resource identifier must be an absolute URL with a scheme and a host";
#[test]
fn construction_rejects_relative_resource_identifier() {
// RFC 8707 §2 requires an absolute URI; the failure must surface
// when the resource is built, not later when the 401-challenge
// path first derives the PRM document URL.
assert_construction_rejects_resource("/mcp", ABSOLUTE_URL_MESSAGE);
}
#[test]
fn construction_rejects_scheme_relative_resource_identifier() {
// Carries an authority but no scheme — must reject on its own,
// independent of the plain relative form.
assert_construction_rejects_resource("//api.example.com/mcp", ABSOLUTE_URL_MESSAGE);
}
#[test]
fn construction_rejects_opaque_resource_identifier() {
// Has no host to anchor the RFC 9728 §3 well-known insertion.
assert_construction_rejects_resource("urn:example:api", ABSOLUTE_URL_MESSAGE);
}
#[test]
fn construction_rejects_fragment_bearing_resource_identifier() {
// RFC 8707 §2 forbids a fragment outright; the raw-string check
// must catch it because parsing splits the fragment off.
assert_construction_rejects_resource(
"https://api.example.com/mcp#v2",
"resource identifier must not include a fragment component",
);
}
#[test]
fn construction_rejects_userinfo_bearing_resource_identifier() {
// RFC 9110 §4.2.4 — a credential in the identifier would be
// served verbatim in the PRM `resource` member.
assert_construction_rejects_resource(
"https://svc:pw@api.example.com/mcp",
"resource identifier must not include userinfo",
);
}
/// Drive `from_parts` — the production constructor funnel behind
/// `create`/`create_with_options`/`AuthplaneClient::resource*` —
/// directly: its resource-identifier gate runs before `fetch_jwks`,
/// so with `jwks_cache: None` a bad resource must return `Err`
/// without touching the network. The gate's message (rather than a
/// JWKS fetch failure) proves the rejection came from validation.
#[tokio::test]
async fn from_parts_rejects_bad_resource_before_any_network_call() {
let metadata = AuthorizationServerMetadata {
issuer: "https://auth.example.com".to_string(),
jwks_uri: "https://auth.example.com/.well-known/jwks.json".to_string(),
token_endpoint: Some("https://auth.example.com/oauth/token".to_string()),
introspection_endpoint: Some("https://auth.example.com/oauth/introspect".to_string()),
revocation_endpoint: Some("https://auth.example.com/oauth/revoke".to_string()),
};
let fetch_settings = FetchSettings::from_dev_mode(true);
let http =
crate::transport::build_http_client(&fetch_settings).expect("valid fetch settings");
let error = AuthplaneResource::from_parts(
"https://auth.example.com".to_string(),
"/mcp".to_string(),
vec!["tools/read".to_string()],
metadata,
fetch_settings,
ResourceOptions::default(),
http,
None,
None,
None,
)
.await
.map(|_| ())
.expect_err("from_parts must reject the resource identifier");
let VerifierError::MetadataUnavailable { message } = error else {
panic!("expected MetadataUnavailable, got {error:?}");
};
assert!(
message.contains(ABSOLUTE_URL_MESSAGE),
"unexpected message: {message}"
);
}
#[test]
fn construction_accepts_http_localhost_resource() {
// Scheme and host required, scheme not narrowed: plain-http local
// development hosts stay constructible end-to-end.
let resource = try_construct_resource("http://localhost:8080/mcp")
.expect("http localhost resource must construct");
assert_eq!(
resource.prm_document_url().expect("valid prm document url"),
"http://localhost:8080/.well-known/oauth-protected-resource/mcp"
);
}
#[test]
fn revocation_config_can_be_constructed() {
let config = RevocationConfig {
client_id: "client-1".to_string(),
client_secret: "secret-1".to_string(),
fail_open: true,
};
assert!(config.fail_open);
}
#[test]
fn parse_audience_string_single_entry() {
let aud = serde_json::json!("https://api.example.com");
let parsed = parse_audience(Some(&aud)).expect("string aud must parse");
assert_eq!(parsed, vec!["https://api.example.com".to_string()]);
}
#[test]
fn parse_audience_string_empty_is_none() {
let aud = serde_json::json!("");
assert!(parse_audience(Some(&aud)).is_none());
}
#[test]
fn parse_audience_array_multiple_entries() {
let aud = serde_json::json!(["https://one.example", "https://two.example"]);
let parsed = parse_audience(Some(&aud)).expect("array aud must parse");
assert_eq!(
parsed,
vec![
"https://one.example".to_string(),
"https://two.example".to_string(),
]
);
}
#[test]
fn parse_audience_array_filters_empty_entries() {
let aud = serde_json::json!(["", "https://real.example", ""]);
let parsed = parse_audience(Some(&aud)).expect("array aud must parse");
assert_eq!(parsed, vec!["https://real.example".to_string()]);
}
#[test]
fn parse_audience_array_of_only_empty_strings_is_none() {
let aud = serde_json::json!(["", ""]);
assert!(parse_audience(Some(&aud)).is_none());
}
#[test]
fn parse_audience_array_of_non_strings_is_none() {
// Per RFC 7519 `aud` must be string-valued; numeric-valued entries
// must be ignored, not coerced.
let aud = serde_json::json!([42, true]);
assert!(parse_audience(Some(&aud)).is_none());
}
#[test]
fn parse_audience_object_is_none() {
let aud = serde_json::json!({"not": "a valid aud"});
assert!(parse_audience(Some(&aud)).is_none());
}
#[test]
fn parse_audience_missing_is_none() {
assert!(parse_audience(None).is_none());
}
#[test]
fn build_verified_claims_accepts_array_audience_with_target_resource() {
// RFC 8707 §2 — resource servers MUST accept tokens whose `aud` is
// a list that contains the configured resource; the decoded
// `audience` vector must preserve every array entry.
let payload = json!({
"iss": "https://auth.example.com",
"sub": "user-1",
"client_id": "client-1",
"aud": ["https://api.example.com", "https://other.example.com"],
"exp": 4102444800i64,
"iat": 1700000000i64,
"jti": "token-1"
});
let claims = build_verified_claims(
&payload,
"test-kid",
ResourceOptions::default().clock_skew_seconds,
)
.expect("array aud must decode");
assert_eq!(
claims.audience,
vec![
"https://api.example.com".to_string(),
"https://other.example.com".to_string(),
]
);
}
#[test]
fn build_verified_claims_string_audience_becomes_single_element_vec() {
let payload = json!({
"iss": "https://auth.example.com",
"sub": "user-1",
"client_id": "client-1",
"aud": "https://api.example.com",
"exp": 4102444800i64,
"iat": 1700000000i64,
"jti": "token-1"
});
let claims = build_verified_claims(
&payload,
"test-kid",
ResourceOptions::default().clock_skew_seconds,
)
.expect("string aud must decode");
assert_eq!(claims.audience, vec!["https://api.example.com".to_string()]);
}
#[test]
fn build_verified_claims_rejects_empty_audience() {
let payload = json!({
"iss": "https://auth.example.com",
"sub": "user-1",
"client_id": "client-1",
"aud": "",
"exp": 4102444800i64,
"iat": 1700000000i64,
"jti": "token-1"
});
let error = build_verified_claims(
&payload,
"test-kid",
ResourceOptions::default().clock_skew_seconds,
)
.expect_err("empty aud must fail");
assert!(matches!(error, VerifierError::InvalidClaims { .. }));
}
#[test]
fn build_verified_claims_rejects_missing_jti() {
let payload = json!({
"iss": "https://auth.example.com",
"sub": "user-1",
"client_id": "client-1",
"aud": "https://api.example.com",
"exp": 4102444800i64,
"iat": 1700000000i64
});
let error = build_verified_claims(
&payload,
"test-kid",
ResourceOptions::default().clock_skew_seconds,
)
.expect_err("missing jti must fail");
assert!(matches!(error, VerifierError::InvalidClaims { .. }));
}
#[test]
fn build_verified_claims_promotes_agent_id_and_chain() {
let payload = json!({
"iss": "https://auth.example.com",
"sub": "user-1",
"client_id": "client-1",
"aud": "https://api.example.com",
"exp": 4102444800i64,
"iat": 1700000000i64,
"jti": "token-1",
"agent_id": "agent-007",
"agent_chain": ["orchestrator", "agent-007"]
});
let claims = build_verified_claims(
&payload,
"test-kid",
ResourceOptions::default().clock_skew_seconds,
)
.expect("agent claims must decode");
assert_eq!(claims.agent_id, "agent-007");
assert_eq!(
claims.agent_chain,
vec!["orchestrator".to_string(), "agent-007".to_string()]
);
}
#[test]
fn build_verified_claims_defaults_agent_id_to_empty_when_absent() {
let payload = json!({
"iss": "https://auth.example.com",
"sub": "user-1",
"client_id": "client-1",
"aud": "https://api.example.com",
"exp": 4102444800i64,
"iat": 1700000000i64,
"jti": "token-1"
});
let claims = build_verified_claims(
&payload,
"test-kid",
ResourceOptions::default().clock_skew_seconds,
)
.expect("bare token must decode");
assert_eq!(claims.agent_id, "");
assert!(claims.agent_chain.is_empty());
assert_eq!(claims.not_before, 0);
}
#[test]
fn revocation_config_fail_closed_is_the_default() {
// The `RevocationConfig` struct requires `fail_open` to be
// explicit, but we document that the secure-by-default posture
// is `fail_open = false`.
let config = RevocationConfig {
client_id: "client-1".to_string(),
client_secret: "secret-1".to_string(),
fail_open: false,
};
assert!(!config.fail_open, "default posture must be fail-closed");
}
#[tokio::test]
async fn verify_with_context_accepts_bearer_token_with_request_context_and_no_proof() {
// Catalog:
// `rfc9449-bearer-token-with-request-context-and-no-proof-must-still-verify-as-bearer`.
// Bearer access token (no `cnf`) + request context supplied but
// `proof = None` MUST still verify as bearer.
let resource = resource_with_test_jwks();
let token = signed_token_with_claims(json!({
"iss": "https://auth.example.com",
"sub": "user-1",
"client_id": "client-1",
"aud": "https://api.example.com/mcp",
"exp": 4102444800i64,
"iat": 1700000000i64,
"jti": "token-1"
}));
let context = DpopRequestContext::new("GET", "https://api.example.com/mcp", None, None);
let claims = resource
.verify_with_context(&token, &context)
.await
.expect("bearer + request context must verify");
assert_eq!(claims.client_id, "client-1");
assert!(!claims.raw.contains_key("cnf"));
}
/// The gap this trio closes: `verify` used to be the shared core, so a
/// DPoP-bound token handed to the bearer-only entrypoint was verified and
/// returned with its `cnf` never looked at — the sender constraint
/// silently discarded. Nothing pinned that behaviour, which is why it
/// survived. The `authplane-fastmcp` adapter is bearer-only by necessity
/// (the upstream framework exposes no inbound HTTP context) and calls
/// exactly this path.
#[tokio::test]
async fn verify_rejects_a_dpop_bound_token_in_mode_3() {
// Mode 3 — resource has not opted into inbound DPoP. Same answer
// `verify_with_context` gives for the same input.
let resource = resource_with_test_jwks_and_options(ResourceOptions::default());
let token = signed_token_with_claims(json!({
"iss": "https://auth.example.com",
"sub": "user-1",
"client_id": "client-1",
"aud": "https://api.example.com/mcp",
"exp": 4102444800i64,
"iat": 1700000000i64,
"jti": "token-1",
"cnf": { "jkt": "some-thumbprint" }
}));
let err = resource
.verify(&token)
.await
.expect_err("a sender-constrained token must not pass as a bearer token");
assert!(
matches!(err, VerifierError::DpopNotSupported),
"expected DpopNotSupported, got {err:?}"
);
}
#[tokio::test]
async fn verify_rejects_a_dpop_bound_token_when_the_resource_supports_dpop() {
// Modes 1 and 2 — the resource does support DPoP, so the token is
// fine; what is missing is the proof, which this entrypoint cannot
// supply. `resource_with_test_jwks` is Mode 2.
let resource = resource_with_test_jwks();
let token = signed_token_with_claims(json!({
"iss": "https://auth.example.com",
"sub": "user-1",
"client_id": "client-1",
"aud": "https://api.example.com/mcp",
"exp": 4102444800i64,
"iat": 1700000000i64,
"jti": "token-1",
"cnf": { "jkt": "some-thumbprint" }
}));
let err = resource
.verify(&token)
.await
.expect_err("a bound token with no proof must not pass");
// `DpopBindingMismatch`, not `DpopProofMissing`: this entrypoint takes
// no request context, so "a context was supplied and held no proof" is
// not what happened. `verify_with_context` owns that class.
assert!(
matches!(err, VerifierError::DpopBindingMismatch { .. }),
"expected DpopBindingMismatch, got {err:?}"
);
}
#[tokio::test]
async fn verify_still_accepts_a_plain_bearer_token() {
// The regression guard for the two above: the guard must reject only
// bound tokens, not every token.
let resource = resource_with_test_jwks_and_options(ResourceOptions::default());
let token = signed_token_with_claims(json!({
"iss": "https://auth.example.com",
"sub": "user-1",
"client_id": "client-1",
"aud": "https://api.example.com/mcp",
"exp": 4102444800i64,
"iat": 1700000000i64,
"jti": "token-1"
}));
let claims = resource
.verify(&token)
.await
.expect("a bearer token must still verify");
assert_eq!(claims.sub, "user-1");
}
#[tokio::test]
async fn verify_with_context_rejects_dpop_bound_token_when_proof_is_missing() {
// Catalog:
// `rfc9449-dpop-bound-token-with-request-context-and-no-proof-must-be-rejected-via-main-verify-path`.
// DPoP-bound token (`cnf.jkt` present) + request context WITHOUT
// a proof MUST reject with `DpopProofMissing` ( error_category
// "dpop_proof_missing" in the catalog).
let resource = resource_with_test_jwks();
let token = signed_token_with_claims(json!({
"iss": "https://auth.example.com",
"sub": "user-1",
"client_id": "client-1",
"aud": "https://api.example.com/mcp",
"exp": 4102444800i64,
"iat": 1700000000i64,
"jti": "token-1",
"cnf": { "jkt": "some-thumbprint" }
}));
let context = DpopRequestContext::new("GET", "https://api.example.com/mcp", None, None);
let err = resource
.verify_with_context(&token, &context)
.await
.expect_err("dpop-bound token + no proof must fail");
assert!(
matches!(err, VerifierError::DpopProofMissing),
"expected DpopProofMissing, got {err:?}"
);
}
#[tokio::test]
async fn verify_with_context_accepts_matching_dpop_bound_token_and_proof() {
let resource = resource_with_test_jwks();
let public_jwk = json!({
"kty": "RSA",
"kid": "test-kid",
"use": "sig",
"alg": "RS256",
"n": "pza1Jk6AXrea2P-TlgPStQO4PJ8H4mCz3qaW-PqscKygy31-_T-XNpYlH948O-hS3eN0bKLLKJetWx8bSWxBlMMW4DlV-vv32kO-phwPGE0BbQ2rMfZXfEKwKbcU_hTQv3_yfo6eugv3g_9bZR16MaNOWL0fWTmmcYoD7j8mODWoTgwGnHoriRE9wLgHOkXSJ-lnV4gR3Wa0HdI1Th91kve4mMC4DxxpzZ37xh5d0wyExHSb9bssowS70hts0JD-TX46MSpgVoCcZfBefyJ9JKoVgxVZ2aYGsdR8pwVRSRYUf2CYDvKyUZ8HfoWBv4JwBO0AVqT5Eb5F-X375fULQQ",
"e": "AQAB"
});
let jkt = jwk_thumbprint_sha256(&public_jwk).expect("jkt");
let token = signed_token_with_claims(json!({
"iss": "https://auth.example.com",
"sub": "user-1",
"client_id": "client-1",
"aud": "https://api.example.com/mcp",
"exp": 4102444800i64,
"iat": 1700000000i64,
"jti": "token-1",
"cnf": { "jkt": jkt }
}));
let proof = create_dpop_proof(
"POST",
"https://api.example.com/mcp",
Some(&token),
&DpopProofOptions {
private_key_pem: TEST_PRIVATE_PEM.to_string(),
public_jwk,
algorithm: Algorithm::RS256,
key_id: Some("test-kid".to_string()),
nonce: Some("nonce-1".to_string()),
proof_ttl_seconds: None,
},
)
.expect("proof");
let context = DpopRequestContext::new(
"POST",
"https://api.example.com/mcp",
Some(proof.as_str()),
Some("nonce-1"),
);
let claims = resource
.verify_with_context(&token, &context)
.await
.expect("matching dpop binding must verify");
assert_eq!(claims.client_id, "client-1");
}
#[tokio::test]
async fn verify_with_context_rejects_cnf_with_missing_jkt() {
// A token with `cnf` present but no `cnf.jkt` is structurally malformed
// and must be rejected — we cannot fall through to bearer acceptance
// because the AS signalled a binding.
let resource = resource_with_test_jwks();
let token = signed_token_with_claims(json!({
"iss": "https://auth.example.com",
"sub": "user-1",
"client_id": "client-1",
"aud": "https://api.example.com/mcp",
"exp": 4102444800i64,
"iat": 1700000000i64,
"jti": "token-1",
"cnf": { "x5t#S256": "unrelated" }
}));
let context = DpopRequestContext::new("GET", "https://api.example.com/mcp", None, None);
let err = resource
.verify_with_context(&token, &context)
.await
.expect_err("cnf without jkt must be rejected");
match err {
VerifierError::InvalidClaims { message } => assert!(
message.to_ascii_lowercase().contains("cnf.jkt"),
"error must call out cnf.jkt, got {message}"
),
other => panic!("expected InvalidClaims, got {other:?}"),
}
}
#[tokio::test]
async fn verify_with_context_rejects_cnf_jkt_mismatch_as_invalid_claims() {
let resource = resource_with_test_jwks();
let token = signed_token_with_claims(json!({
"iss": "https://auth.example.com",
"sub": "user-1",
"client_id": "client-1",
"aud": "https://api.example.com/mcp",
"exp": 4102444800i64,
"iat": 1700000000i64,
"jti": "token-1",
"cnf": { "jkt": "wrong-thumbprint" }
}));
let proof = create_dpop_proof(
"POST",
"https://api.example.com/mcp",
Some(&token),
&DpopProofOptions {
private_key_pem: TEST_PRIVATE_PEM.to_string(),
public_jwk: json!({
"kty": "RSA",
"kid": "test-kid",
"use": "sig",
"alg": "RS256",
"n": "pza1Jk6AXrea2P-TlgPStQO4PJ8H4mCz3qaW-PqscKygy31-_T-XNpYlH948O-hS3eN0bKLLKJetWx8bSWxBlMMW4DlV-vv32kO-phwPGE0BbQ2rMfZXfEKwKbcU_hTQv3_yfo6eugv3g_9bZR16MaNOWL0fWTmmcYoD7j8mODWoTgwGnHoriRE9wLgHOkXSJ-lnV4gR3Wa0HdI1Th91kve4mMC4DxxpzZ37xh5d0wyExHSb9bssowS70hts0JD-TX46MSpgVoCcZfBefyJ9JKoVgxVZ2aYGsdR8pwVRSRYUf2CYDvKyUZ8HfoWBv4JwBO0AVqT5Eb5F-X375fULQQ",
"e": "AQAB"
}),
algorithm: Algorithm::RS256,
key_id: Some("test-kid".to_string()),
nonce: None,
proof_ttl_seconds: None,
},
)
.expect("proof");
let context = DpopRequestContext::new(
"POST",
"https://api.example.com/mcp",
Some(proof.as_str()),
None,
);
let err = resource
.verify_with_context(&token, &context)
.await
.expect_err("mismatched cnf.jkt must be rejected");
assert!(
err.to_string().contains("cnf.jkt mismatch"),
"expected cnf.jkt mismatch error, got {err:?}"
);
}
const PRM_URL: &str = "https://api.example.com/.well-known/oauth-protected-resource/mcp";
fn prefetched_resource(options: ResourceOptions) -> Result<AuthplaneResource, VerifierError> {
let metadata = AuthorizationServerMetadata {
issuer: "https://auth.example.com".to_string(),
jwks_uri: "https://auth.example.com/.well-known/jwks.json".to_string(),
token_endpoint: None,
introspection_endpoint: Some("https://auth.example.com/oauth/introspect".to_string()),
revocation_endpoint: None,
};
let jwks: JwkSet = serde_json::from_str(TEST_JWKS).expect("valid jwks");
AuthplaneResource::from_prefetched_metadata(
"https://auth.example.com",
"https://api.example.com/mcp",
&["tools/read".to_string()],
metadata,
FetchSettings::from_dev_mode(true),
options,
jwks,
)
}
/// RFC 9728 §5.1 — with no override the challenge advertises the
/// §3.1 derivation, the same URL `prm_document_url` returns.
#[test]
fn resource_metadata_url_defaults_to_the_derived_prm_document_url() {
let resource = resource_with_test_jwks();
assert_eq!(resource.resource_metadata_url(), PRM_URL);
assert_eq!(
resource.prm_document_url().expect("derivable"),
resource.resource_metadata_url()
);
let header = resource.www_authenticate(&VerifierError::TokenExpired, "api");
assert_eq!(
header,
format!(
"Bearer realm=\"api\", error=\"invalid_token\", \
error_description=\"token has expired\", resource_metadata=\"{PRM_URL}\""
)
);
}
#[test]
fn resource_metadata_url_override_is_advertised_instead_of_the_derivation() {
let custom = "https://auth.example.com/.well-known/oauth-protected-resource/calc";
let options = ResourceOptions::default()
.with_resource_metadata_url(custom)
.expect("absolute URL");
assert_eq!(options.resource_metadata_url(), Some(custom));
let resource = resource_with_test_jwks_and_options(options);
assert_eq!(resource.resource_metadata_url(), custom);
// The derivation is untouched: it still names where this server
// would host its own document.
assert_eq!(resource.prm_document_url().expect("derivable"), PRM_URL);
let header = resource.www_authenticate(&VerifierError::TokenMissing, "");
assert!(header.ends_with(&format!("resource_metadata=\"{custom}\"")));
}
#[test]
fn with_resource_metadata_url_rejects_non_absolute_urls() {
for bad in ["/.well-known/oauth-protected-resource/mcp", "not a url", ""] {
let error = ResourceOptions::default()
.with_resource_metadata_url(bad)
.err()
.unwrap_or_else(|| panic!("{bad:?} must be rejected"));
assert!(
matches!(&error, ResourceOptionsError::InvalidResourceMetadataUrl(got) if got == bad),
"unexpected error for {bad:?}: {error}"
);
}
}
/// The WHATWG parser trims leading and trailing C0/space and removes tab
/// and newline anywhere before parsing, so every one of these parses with
/// a host. The value is stored and advertised as typed, so a CR or LF
/// makes `HeaderValue::from_str` fail and drops `WWW-Authenticate` from
/// every `401` — silently, since the adapters have no error path there.
#[test]
fn with_resource_metadata_url_rejects_octets_the_header_cannot_carry() {
for bad in [
"https://auth.example.com/prm\n",
"https://auth.example.com/prm\r\n",
" https://auth.example.com/prm",
"https://auth.example.com/prm\tx",
"https://auth.example.com/prm doc",
"https://auth.example.com/prm\u{7f}",
"https://auth.example.com/prm\"x",
"https://auth.example.com/prm\\x",
] {
let error = ResourceOptions::default()
.with_resource_metadata_url(bad)
.err()
.unwrap_or_else(|| panic!("{bad:?} must be rejected"));
assert!(
matches!(&error, ResourceOptionsError::InvalidResourceMetadataUrl(got) if got == bad),
"unexpected error for {bad:?}: {error}"
);
}
}
/// authserver >= 0.1.2 answers `active: false` to unauthenticated
/// introspection, so a resource built with empty credentials would
/// reject every token as revoked. Refuse it at construction.
#[test]
fn construction_rejects_revocation_config_with_empty_credentials() {
for (client_id, client_secret) in [("", "secret"), ("rs-client", ""), ("rs-client", " ")] {
let options = ResourceOptions {
revocation: Some(RevocationConfig {
client_id: client_id.to_string(),
client_secret: client_secret.to_string(),
fail_open: false,
}),
..ResourceOptions::default()
};
let error = prefetched_resource(options).expect_err("empty credentials must fail");
let VerifierError::MetadataUnavailable { message } = &error else {
panic!("expected MetadataUnavailable, got {error:?}");
};
assert!(message.contains("confidential client"), "{message}");
assert!(message.contains("authserver >= 0.1.2"), "{message}");
}
}
#[test]
fn construction_accepts_revocation_config_with_credentials() {
let options = ResourceOptions {
revocation: Some(RevocationConfig {
client_id: "rs-client".to_string(),
client_secret: "rs-secret".to_string(),
fail_open: false,
}),
..ResourceOptions::default()
};
prefetched_resource(options).expect("credentials present");
}
#[test]
fn with_allowed_algorithms_accepts_asymmetric_subset() {
let opts = ResourceOptions::default()
.with_allowed_algorithms(vec![Algorithm::ES256])
.expect("ES256 is asymmetric, must be accepted");
assert_eq!(opts.allowed_algorithms(), &[Algorithm::ES256]);
}
#[test]
fn with_allowed_algorithms_rejects_empty_list() {
let err = ResourceOptions::default()
.with_allowed_algorithms(Vec::new())
.expect_err("empty list must be rejected");
assert!(matches!(
err,
super::ResourceOptionsError::EmptyAlgorithmList
));
}
#[test]
fn with_allowed_algorithms_rejects_hmac() {
// RFC 7518 §3.2 HS256: a JWKS rotation that ships a symmetric key
// as a public JWK would let an attacker forge tokens (algorithm
// confusion). Construction-time rejection beats verify-time
// rejection because it makes the misconfiguration impossible to
// ship.
for hmac in [Algorithm::HS256, Algorithm::HS384, Algorithm::HS512] {
let err = ResourceOptions::default()
.with_allowed_algorithms(vec![hmac])
.expect_err("HMAC must be rejected at construction");
assert!(
matches!(err, super::ResourceOptionsError::UnsupportedAlgorithm(alg) if alg == hmac),
"expected UnsupportedAlgorithm({hmac:?}), got {err:?}"
);
}
}
#[test]
fn with_allowed_algorithms_rejects_mixed_list_when_any_hmac() {
// A single HMAC entry in an otherwise-asymmetric list MUST poison
// the whole call — silently dropping it would silently relax the
// user's stated intent.
let err = ResourceOptions::default()
.with_allowed_algorithms(vec![Algorithm::RS256, Algorithm::HS256])
.expect_err("mixed list with HMAC must be rejected");
assert!(matches!(
err,
super::ResourceOptionsError::UnsupportedAlgorithm(Algorithm::HS256)
));
}
#[test]
fn with_allowed_algorithms_rejects_non_contract_asymmetric_variants() {
// The access-token contract allowlists exactly {RS256, ES256};
// the additional asymmetric variants `jsonwebtoken` exposes are
// not part of it. Accepting them here would let a caller advertise
// an alg in their PRM / JWKS that peers can't validate and
// broaden the algorithm-confusion surface beyond that contract.
// Each non-contract variant must fail at construction with
// `UnsupportedAlgorithm`.
for alg in [
Algorithm::RS384,
Algorithm::RS512,
Algorithm::PS256,
Algorithm::PS384,
Algorithm::PS512,
Algorithm::ES384,
Algorithm::EdDSA,
] {
let err = ResourceOptions::default()
.with_allowed_algorithms(vec![alg])
.expect_err("non-contract asymmetric alg must be rejected");
assert!(
matches!(err, super::ResourceOptionsError::UnsupportedAlgorithm(rejected) if rejected == alg),
"expected UnsupportedAlgorithm({alg:?}), got {err:?}"
);
}
}
#[test]
fn with_allowed_algorithms_rejects_mixed_list_when_any_non_contract() {
// Same poisoning semantics as the HMAC case, but for the
// non-contract asymmetric variants. A list of `[RS256, RS384]` is
// not the user expressing "accept the union" — it's the user
// expressing intent we can't honour, so we surface the offending
// entry instead of silently dropping it.
let err = ResourceOptions::default()
.with_allowed_algorithms(vec![Algorithm::RS256, Algorithm::RS384])
.expect_err("mixed list with non-contract alg must be rejected");
assert!(matches!(
err,
super::ResourceOptionsError::UnsupportedAlgorithm(Algorithm::RS384)
));
}
#[tokio::test]
async fn verify_with_context_rejects_replayed_proof() {
// Regression coverage: `InboundDPoPOptions::default()` used to leave
// `replay_store = None`, which silently fell through to the
// no-replay path. Auto-allocation now wires an in-memory store by
// default — confirm that a second verify with the same proof + jti
// hits `DpopReplayDetected`, proving the with-replay branch is the
// one being exercised.
let resource = resource_with_test_jwks();
let public_jwk = json!({
"kty": "RSA",
"kid": "test-kid",
"use": "sig",
"alg": "RS256",
"n": "pza1Jk6AXrea2P-TlgPStQO4PJ8H4mCz3qaW-PqscKygy31-_T-XNpYlH948O-hS3eN0bKLLKJetWx8bSWxBlMMW4DlV-vv32kO-phwPGE0BbQ2rMfZXfEKwKbcU_hTQv3_yfo6eugv3g_9bZR16MaNOWL0fWTmmcYoD7j8mODWoTgwGnHoriRE9wLgHOkXSJ-lnV4gR3Wa0HdI1Th91kve4mMC4DxxpzZ37xh5d0wyExHSb9bssowS70hts0JD-TX46MSpgVoCcZfBefyJ9JKoVgxVZ2aYGsdR8pwVRSRYUf2CYDvKyUZ8HfoWBv4JwBO0AVqT5Eb5F-X375fULQQ",
"e": "AQAB"
});
let jkt = jwk_thumbprint_sha256(&public_jwk).expect("jkt");
let token = signed_token_with_claims(json!({
"iss": "https://auth.example.com",
"sub": "user-1",
"client_id": "client-1",
"aud": "https://api.example.com/mcp",
"exp": 4102444800i64,
"iat": 1700000000i64,
"jti": "token-replay-1",
"cnf": { "jkt": jkt }
}));
let proof = create_dpop_proof(
"POST",
"https://api.example.com/mcp",
Some(&token),
&DpopProofOptions {
private_key_pem: TEST_PRIVATE_PEM.to_string(),
public_jwk,
algorithm: Algorithm::RS256,
key_id: Some("test-kid".to_string()),
nonce: Some("nonce-replay".to_string()),
proof_ttl_seconds: None,
},
)
.expect("proof");
let context = DpopRequestContext::new(
"POST",
"https://api.example.com/mcp",
Some(proof.as_str()),
Some("nonce-replay"),
);
resource
.verify_with_context(&token, &context)
.await
.expect("first verify must succeed");
let err = resource
.verify_with_context(&token, &context)
.await
.expect_err("replayed proof must be rejected");
assert!(
matches!(err, VerifierError::DpopReplayDetected),
"expected DpopReplayDetected on replay, got {err:?}"
);
}
fn prm_resource_with_options(options: ResourceOptions) -> AuthplaneResource {
let metadata = AuthorizationServerMetadata {
issuer: "https://auth.example.com".to_string(),
jwks_uri: "https://auth.example.com/.well-known/jwks.json".to_string(),
token_endpoint: Some("https://auth.example.com/oauth/token".to_string()),
introspection_endpoint: Some("https://auth.example.com/oauth/introspect".to_string()),
revocation_endpoint: Some("https://auth.example.com/oauth/revoke".to_string()),
};
let jwks: JwkSet = serde_json::from_str(TEST_JWKS).expect("valid jwks");
AuthplaneResource::from_metadata_and_jwks(
"https://auth.example.com",
"https://api.example.com/mcp",
&["tools/read".to_string()],
metadata,
FetchSettings::from_dev_mode(true),
options,
jwks,
)
}
#[test]
fn prm_response_mode_3_omits_dpop_fields() {
// Mode 3: ResourceOptions::default() has inbound_dpop = None,
// so the PRM document MUST omit both dpop_* fields entirely
// (RFC 9728 §2 — absence signals "no DPoP capability").
let resource = prm_resource_with_options(ResourceOptions::default());
let prm = resource.prm_response();
assert!(
prm.dpop_signing_alg_values_supported.is_none(),
"Mode 3 PRM must not advertise DPoP algs"
);
assert!(
prm.dpop_bound_access_tokens_required.is_none(),
"Mode 3 PRM must not advertise dpop_bound_access_tokens_required"
);
}
#[test]
fn prm_response_mode_2_advertises_dpop_optional() {
// Mode 2: inbound_dpop = InboundDPoPOptions::default() means
// DPoP-bound tokens are accepted but not required. PRM advertises
// the supported proof algs AND `dpop_bound_access_tokens_required: false`.
let resource = prm_resource_with_options(
ResourceOptions::default().with_inbound_dpop(crate::InboundDPoPOptions::default()),
);
let prm = resource.prm_response();
assert_eq!(
prm.dpop_signing_alg_values_supported,
Some(vec!["ES256".to_string(), "RS256".to_string()]),
"Mode 2 PRM must list the default proof algs in the documented canonical order [ES256, RS256]"
);
assert_eq!(prm.dpop_bound_access_tokens_required, Some(false));
}
#[test]
fn prm_response_mode_1_advertises_dpop_required() {
// Mode 1: inbound_dpop = InboundDPoPOptions::required() — bearer-
// only tokens are rejected, PRM tells well-behaved clients to
// retry with a DPoP-bound token.
let resource = prm_resource_with_options(
ResourceOptions::default().with_inbound_dpop(crate::InboundDPoPOptions::required()),
);
let prm = resource.prm_response();
assert_eq!(
prm.dpop_signing_alg_values_supported,
Some(vec!["ES256".to_string(), "RS256".to_string()]),
);
assert_eq!(prm.dpop_bound_access_tokens_required, Some(true));
}
#[test]
fn prm_response_respects_custom_proof_alg_subset() {
// When the resource narrows the accepted proof algs (e.g. to
// ES256 only), the PRM document must reflect that exact subset
// — clients picking RS256 would just get a verify-time rejection.
let inbound = crate::InboundDPoPOptions::default()
.with_allowed_proof_algorithms(vec![Algorithm::ES256])
.expect("ES256 subset is valid");
let resource =
prm_resource_with_options(ResourceOptions::default().with_inbound_dpop(inbound));
let prm = resource.prm_response();
assert_eq!(
prm.dpop_signing_alg_values_supported,
Some(vec!["ES256".to_string()]),
);
assert_eq!(prm.dpop_bound_access_tokens_required, Some(false));
}
#[test]
fn dpop_proof_max_age_independent_of_clock_skew() {
// Raising clock_skew_seconds must NOT extend the DPoP proof
// acceptance window beyond dpop_proof_max_age_seconds.
let default = ResourceOptions::default();
assert_eq!(default.clock_skew_seconds, 30);
assert_eq!(default.dpop_proof_max_age_seconds, 300);
let custom = ResourceOptions {
clock_skew_seconds: 600, // high clock skew
dpop_proof_max_age_seconds: 120, // tight DPoP proof window
..ResourceOptions::default()
};
// The DPoP proof TTL must remain 120, not get inflated to 600.
assert_eq!(custom.dpop_proof_max_age_seconds, 120);
assert_eq!(custom.clock_skew_seconds, 600);
// Before the fix, max_age_seconds was computed as
// `clock_skew_seconds.max(300)` = 600, effectively overriding
// any intended DPoP proof window. Now it's a separate field.
}
// -------------------------------------------------------------------
// Circuit-breaker short-circuit behaviour on the revocation path
// -------------------------------------------------------------------
//
// These tests cover the new branches introduced when introspection was
// routed through the shared `CircuitBreaker`: when the breaker is OPEN
// we never make the HTTP round-trip; whether the token is accepted or
// rejected depends on `RevocationConfig::fail_open`. The breaker-OPEN
// path is the one that doesn't hit `self.http` at all, so we can test
// it without an HTTP mock — the closed-and-call-introspect path needs
// a wiremock-style harness and is left for the integration suite.
use std::sync::Arc;
use crate::CircuitBreaker;
fn revocation_resource_with_breaker(
fail_open: bool,
breaker: Arc<CircuitBreaker>,
) -> AuthplaneResource {
let metadata = AuthorizationServerMetadata {
issuer: "https://auth.example.com".to_string(),
jwks_uri: "https://auth.example.com/.well-known/jwks.json".to_string(),
token_endpoint: Some("https://auth.example.com/oauth/token".to_string()),
introspection_endpoint: Some("https://auth.example.com/oauth/introspect".to_string()),
revocation_endpoint: Some("https://auth.example.com/oauth/revoke".to_string()),
};
let jwks: JwkSet = serde_json::from_str(TEST_JWKS).expect("valid jwks");
let options = ResourceOptions {
revocation: Some(RevocationConfig {
client_id: "client-1".to_string(),
client_secret: "secret-1".to_string(),
fail_open,
}),
..ResourceOptions::default()
};
AuthplaneResource::from_metadata_jwks_and_breaker(
"https://auth.example.com",
"https://api.example.com/mcp",
&["tools/read".to_string()],
metadata,
FetchSettings::from_dev_mode(true),
options,
jwks,
breaker,
)
}
fn token_for_revocation_branch() -> String {
signed_token_with_claims(json!({
"iss": "https://auth.example.com",
"sub": "user-1",
"client_id": "client-1",
"aud": "https://api.example.com/mcp",
"exp": 4_102_444_800i64,
"iat": 1_700_000_000i64,
"jti": "token-1"
}))
}
fn opened_breaker() -> Arc<CircuitBreaker> {
let breaker = CircuitBreaker::with_config(1, 60.0);
breaker.record_failure();
// Sanity-check: the breaker is OPEN before we hand it to the resource,
// so `breaker.allow()` will return false on the first verify call and
// the short-circuit branch fires.
assert!(
!breaker.allow(),
"breaker must be open before the test runs"
);
Arc::new(breaker)
}
#[tokio::test]
async fn verify_with_open_breaker_and_fail_open_accepts_token_without_introspection() {
// Branch: revocation configured + breaker OPEN + fail_open=true.
// Expected: `verify` returns Ok without calling the introspection
// endpoint. (If it did, the call would hit the unreachable AS host
// and either time out or return a transport error.)
let resource = revocation_resource_with_breaker(true, opened_breaker());
let token = token_for_revocation_branch();
let claims = resource
.verify(&token)
.await
.expect("open breaker + fail-open must accept the token");
assert_eq!(claims.sub, "user-1");
}
#[tokio::test]
async fn verify_with_open_breaker_and_fail_closed_surfaces_metadata_unavailable() {
// Branch: revocation configured + breaker OPEN + fail_open=false.
// Expected: `verify` returns MetadataUnavailable with a message
// naming the circuit breaker, never touching the AS.
let resource = revocation_resource_with_breaker(false, opened_breaker());
let token = token_for_revocation_branch();
let error = resource
.verify(&token)
.await
.expect_err("open breaker + fail-closed must reject");
match error {
VerifierError::MetadataUnavailable { message } => {
assert!(
message.contains("circuit breaker"),
"expected the message to name the breaker, got: {message}"
);
}
other => panic!("expected MetadataUnavailable, got {other:?}"),
}
}
// -------------------------------------------------------------------
// Failure-accounting tests — cover the introspection-call path (not
// just the breaker-already-open short-circuit). The reviewer flagged
// that the prior set only tested OPEN-branch behaviour, so a
// regression where `record_failure()` is called on a benign OAuth
// error (e.g. `invalid_client`) would not be caught. The shared
// `circuit_policy::should_count_failure` predicate is what gates the
// counting; these tests exercise both halves of that gate from the
// resource-side introspection path.
// -------------------------------------------------------------------
fn revocation_resource_pointing_at(
introspection_url: &str,
fail_open: bool,
breaker: Arc<CircuitBreaker>,
) -> AuthplaneResource {
let metadata = AuthorizationServerMetadata {
issuer: "https://auth.example.com".to_string(),
jwks_uri: "https://auth.example.com/.well-known/jwks.json".to_string(),
token_endpoint: Some("https://auth.example.com/oauth/token".to_string()),
introspection_endpoint: Some(introspection_url.to_string()),
revocation_endpoint: Some("https://auth.example.com/oauth/revoke".to_string()),
};
let jwks: JwkSet = serde_json::from_str(TEST_JWKS).expect("valid jwks");
let options = ResourceOptions {
revocation: Some(RevocationConfig {
client_id: "client-1".to_string(),
client_secret: "secret-1".to_string(),
fail_open,
}),
..ResourceOptions::default()
};
AuthplaneResource::from_metadata_jwks_and_breaker(
"https://auth.example.com",
"https://api.example.com/mcp",
&["tools/read".to_string()],
metadata,
FetchSettings::from_dev_mode(true),
options,
jwks,
breaker,
)
}
#[tokio::test]
async fn introspection_invalid_client_does_not_trip_breaker() {
// The introspection endpoint returns the OAuth error that lives
// in `circuit_policy::OAUTH_ERRORS_NO_CIRCUIT`. With fail_open=true
// the verify call still succeeds; the breaker MUST remain Closed
// even after enough failed introspection round-trips to cross the
// configured threshold. This is the asymmetry the audit was
// worried about — `AuthplaneClient::run_guarded` ignores
// `invalid_client` for breaker accounting and the resource path
// must do the same, otherwise misconfigured introspection creds
// silently disable revocation checks for the cooldown window.
let mut server = mockito::Server::new_async().await;
let introspection_url = format!("{}/oauth/introspect", server.url());
let _mock = server
.mock("POST", "/oauth/introspect")
.with_status(401)
.with_header("content-type", "application/json")
.with_body(r#"{"error":"invalid_client","error_description":"bad creds"}"#)
.expect_at_least(3)
.create_async()
.await;
let breaker = Arc::new(CircuitBreaker::with_config(2, 60.0));
let resource = revocation_resource_pointing_at(&introspection_url, true, breaker.clone());
let token = token_for_revocation_branch();
// 3 calls > threshold of 2 — if the resource counted these against
// the breaker, it would already be Open by the third iteration.
for _ in 0..3 {
resource
.verify(&token)
.await
.expect("fail_open swallows the introspection error");
}
assert!(
breaker.allow(),
"breaker must still allow traffic after benign OAuth errors"
);
}
#[tokio::test]
async fn introspection_server_error_does_trip_breaker() {
// Counter-test: `server_error` is NOT in OAUTH_ERRORS_NO_CIRCUIT,
// so it MUST count against the breaker. Without this assertion the
// first test could pass trivially by a `should_count_failure` that
// returned `false` for every input.
let mut server = mockito::Server::new_async().await;
let introspection_url = format!("{}/oauth/introspect", server.url());
let _mock = server
.mock("POST", "/oauth/introspect")
.with_status(500)
.with_header("content-type", "application/json")
.with_body(r#"{"error":"server_error","error_description":"boom"}"#)
.expect_at_least(2)
.create_async()
.await;
let breaker = Arc::new(CircuitBreaker::with_config(2, 60.0));
let resource = revocation_resource_pointing_at(&introspection_url, true, breaker.clone());
let token = token_for_revocation_branch();
for _ in 0..2 {
resource
.verify(&token)
.await
.expect("fail_open swallows the introspection error");
}
assert!(
!breaker.allow(),
"breaker must be Open after threshold genuine AS failures"
);
}
// ------------------------------------------------------------------
// jwks_uri rotation discovered by a `kid` miss
// ------------------------------------------------------------------
/// The shipped RSA test key, republished under an arbitrary `kid`.
///
/// Only the `kid` distinguishes the two JWKS documents in the rotation
/// test below, which is enough: the pre-rotation document never carries
/// the post-rotation `kid`, so the lookup can only succeed by fetching
/// the document published at the rotated `jwks_uri`.
fn jwks_document_with_kid(kid: &str) -> serde_json::Value {
let mut document: serde_json::Value =
serde_json::from_str(TEST_JWKS).expect("valid jwks fixture");
document["keys"][0]["kid"] = json!(kid);
document
}
/// A second RSA key pair with material distinct from `TEST_PRIVATE_PEM`,
/// for rotations that republish under the **same** `kid`.
const TEST_PRIVATE_PEM_2: &str = include_str!("../tests/fixtures/test-private-2.pem");
const TEST_MODULUS_2: &str = "xRkG21gLFZsQn9F9D-UAevGbhdQFw1htbfWdtviNQTO3jjyk3vg273zrbNcs_-KrhtTRcLlJQWgtXuSakzdw472PvGsDph8k1v_XDj_jZztXXj6K9-G_ntAFigT55CdRXw7DzjoJKbDMgIyaVASAlvOi7Vdz39iWn5BlGU4GhUyjgKJHoUee5jCWWN0c2A-N0RclR77JVbptEi8DUZ5P2UjoW1n26pkyP4Pmy2zjBlAj6S7jm7BayeiMvaaTRy_esqjzRxh8D62BbUtFpZ-Og60HXayUqjJBOnR4Pt5d515e9BiBVqWLe7hkG0TPA6fweOxN96VL50Bf24mUPPvx9Q";
/// The second key's JWKS, published under an arbitrary `kid`.
fn jwks_document_with_kid_and_second_key(kid: &str) -> serde_json::Value {
let mut document: serde_json::Value =
serde_json::from_str(TEST_JWKS).expect("valid jwks fixture");
document["keys"][0]["kid"] = json!(kid);
document["keys"][0]["n"] = json!(TEST_MODULUS_2);
document
}
fn signed_token_with_kid_and_key(
kid: &str,
private_pem: &str,
issuer: &str,
audience: &str,
jti: &str,
) -> String {
let now = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.expect("system clock after the epoch")
.as_secs() as i64;
let claims = json!({
"iss": issuer,
"sub": "user-1",
"client_id": "client-1",
"aud": audience,
"jti": jti,
"iat": now,
"exp": now + 300,
"scope": "tools/read"
});
let header = Header {
alg: Algorithm::RS256,
kid: Some(kid.to_string()),
typ: Some("at+jwt".to_string()),
..Header::new(Algorithm::RS256)
};
encode(
&header,
&claims,
&EncodingKey::from_rsa_pem(private_pem.as_bytes()).expect("private key"),
)
.expect("token")
}
fn signed_token_with_kid(kid: &str, issuer: &str, audience: &str, jti: &str) -> String {
signed_token_with_kid_and_key(kid, TEST_PRIVATE_PEM, issuer, audience, jti)
}
#[tokio::test]
async fn kid_miss_re_reads_metadata_without_waiting_for_the_refresh_interval() {
// The interval gate is set far out of reach (one hour) and the JWKS
// TTL with it, so nothing in this test can re-read metadata on a
// timer. The only thing that can follow the rotation is the `kid`
// miss itself — the one request that proves the current binding is
// stale. Without a re-read on that branch the rotated key is
// unreachable for the whole interval, which is a hard verification
// failure for every request in it.
use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering};
let mut server = mockito::Server::new_async().await;
let issuer = server.url();
let audience = "https://api.example.com/mcp";
let rotated = Arc::new(AtomicBool::new(false));
let metadata_hits = Arc::new(AtomicUsize::new(0));
let v1_hits = Arc::new(AtomicUsize::new(0));
let v2_hits = Arc::new(AtomicUsize::new(0));
let metadata_issuer = issuer.clone();
let metadata_rotated = rotated.clone();
let metadata_counter = metadata_hits.clone();
let _metadata_mock = server
.mock("GET", "/.well-known/oauth-authorization-server")
.with_status(200)
.with_header("content-type", "application/json")
.with_body_from_request(move |_request| {
metadata_counter.fetch_add(1, Ordering::SeqCst);
let jwks_uri = if metadata_rotated.load(Ordering::SeqCst) {
format!("{metadata_issuer}/jwks-v2.json")
} else {
format!("{metadata_issuer}/jwks-v1.json")
};
json!({ "issuer": metadata_issuer, "jwks_uri": jwks_uri })
.to_string()
.into_bytes()
})
.create_async()
.await;
// The withdrawn document keeps serving the retired key, so a
// verifier that never rebinds still gets a well-formed JWKS back and
// fails only on the lookup. Nothing but the rebind can make the
// rotated token verify.
let v1_counter = v1_hits.clone();
let _jwks_v1_mock = server
.mock("GET", "/jwks-v1.json")
.with_status(200)
.with_header("content-type", "application/json")
.with_body_from_request(move |_request| {
v1_counter.fetch_add(1, Ordering::SeqCst);
jwks_document_with_kid("jwks-v1-key")
.to_string()
.into_bytes()
})
.create_async()
.await;
let v2_counter = v2_hits.clone();
let _jwks_v2_mock = server
.mock("GET", "/jwks-v2.json")
.with_status(200)
.with_header("content-type", "application/json")
.with_body_from_request(move |_request| {
v2_counter.fetch_add(1, Ordering::SeqCst);
jwks_document_with_kid("jwks-v2-key")
.to_string()
.into_bytes()
})
.create_async()
.await;
let client = crate::AuthplaneClient::builder(&issuer)
.with_fetch_settings(FetchSettings::from_dev_mode(true))
.with_metadata_refresh_seconds(3600)
.with_jwks_refresh_seconds(3600)
.build()
.await
.expect("client discovers the pre-rotation metadata");
let resource = client
.resource(audience, &["tools/read".to_string()])
.await
.expect("resource built through the public API");
resource
.verify(&signed_token_with_kid(
"jwks-v1-key",
&issuer,
audience,
"jti-retired",
))
.await
.expect("token signed by the jwks-v1 key must verify before rotation");
let metadata_hits_before = metadata_hits.load(Ordering::SeqCst);
// The AS rotates. No timer fires, no caller asks for a refresh.
rotated.store(true, Ordering::SeqCst);
let claims = resource
.verify(&signed_token_with_kid(
"jwks-v2-key",
&issuer,
audience,
"jti-rotated",
))
.await
.expect("the kid miss must re-read metadata and follow the rotation");
assert_eq!(claims.kid, "jwks-v2-key");
assert!(
metadata_hits.load(Ordering::SeqCst) > metadata_hits_before,
"the kid miss must re-read metadata, not wait for the interval",
);
assert_eq!(
v2_hits.load(Ordering::SeqCst),
1,
"keys must be fetched from the rotated jwks_uri",
);
assert!(
v1_hits.load(Ordering::SeqCst) >= 1,
"the pre-rotation document must have been the one serving keys",
);
client.aclose().await;
}
#[tokio::test]
async fn kid_miss_re_read_is_floored_so_an_unknown_kid_cannot_amplify_fetches() {
// Counter-test to the one above. The re-read bypasses the refresh
// interval by design, and the caller that reaches it has not
// authenticated anything — `verify` has only decoded the header. A
// well-formed header carrying an arbitrary `kid` must therefore not
// cost the AS one discovery fetch per request.
use std::sync::atomic::{AtomicUsize, Ordering};
let mut server = mockito::Server::new_async().await;
let issuer = server.url();
let audience = "https://api.example.com/mcp";
let metadata_hits = Arc::new(AtomicUsize::new(0));
let metadata_issuer = issuer.clone();
let metadata_counter = metadata_hits.clone();
let _metadata_mock = server
.mock("GET", "/.well-known/oauth-authorization-server")
.with_status(200)
.with_header("content-type", "application/json")
.with_body_from_request(move |_request| {
metadata_counter.fetch_add(1, Ordering::SeqCst);
json!({
"issuer": metadata_issuer,
"jwks_uri": format!("{metadata_issuer}/jwks-v1.json")
})
.to_string()
.into_bytes()
})
.create_async()
.await;
let _jwks_mock = server
.mock("GET", "/jwks-v1.json")
.with_status(200)
.with_header("content-type", "application/json")
.with_body(jwks_document_with_kid("jwks-v1-key").to_string())
.create_async()
.await;
let client = crate::AuthplaneClient::builder(&issuer)
.with_fetch_settings(FetchSettings::from_dev_mode(true))
.with_metadata_refresh_seconds(3600)
.with_jwks_refresh_seconds(3600)
.build()
.await
.expect("client discovers metadata");
let resource = client
.resource(audience, &["tools/read".to_string()])
.await
.expect("resource built through the public API");
let boot_hits = metadata_hits.load(Ordering::SeqCst);
for index in 0..8 {
let token =
signed_token_with_kid("unknown-kid", &issuer, audience, &format!("jti-{index}"));
resource
.verify(&token)
.await
.expect_err("an unknown kid must not verify");
}
assert_eq!(
metadata_hits.load(Ordering::SeqCst) - boot_hits,
1,
"the forced re-read floor must admit one discovery fetch, not one per request",
);
client.aclose().await;
}
#[tokio::test]
async fn interval_re_read_follows_a_rotation_that_republishes_the_same_kid() {
// The rotated document republishes the SAME `kid` with different key
// material, so `get_key_by_kid` always finds the kid in whatever
// document is bound and the miss branch can never fire. The only
// mechanism that can make the rotated token verify is the
// interval-driven re-read at the top of `lookup_key` — delete the
// `refresh_if_due()` call and this test fails. It equally pins the
// one job the rebind's cache expiry genuinely has: without it the
// still-warm pre-rotation document keeps answering the shared kid
// with the retired material for the whole JWKS TTL.
use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering};
let mut server = mockito::Server::new_async().await;
let issuer = server.url();
let audience = "https://api.example.com/mcp";
let shared_kid = "shared-kid";
let rotated = Arc::new(AtomicBool::new(false));
let metadata_hits = Arc::new(AtomicUsize::new(0));
let v1_hits = Arc::new(AtomicUsize::new(0));
let v2_hits = Arc::new(AtomicUsize::new(0));
let metadata_issuer = issuer.clone();
let metadata_rotated = rotated.clone();
let metadata_counter = metadata_hits.clone();
let _metadata_mock = server
.mock("GET", "/.well-known/oauth-authorization-server")
.with_status(200)
.with_header("content-type", "application/json")
.with_body_from_request(move |_request| {
metadata_counter.fetch_add(1, Ordering::SeqCst);
let jwks_uri = if metadata_rotated.load(Ordering::SeqCst) {
format!("{metadata_issuer}/jwks-v2.json")
} else {
format!("{metadata_issuer}/jwks-v1.json")
};
json!({ "issuer": metadata_issuer, "jwks_uri": jwks_uri })
.to_string()
.into_bytes()
})
.create_async()
.await;
let v1_counter = v1_hits.clone();
let _jwks_v1_mock = server
.mock("GET", "/jwks-v1.json")
.with_status(200)
.with_header("content-type", "application/json")
.with_body_from_request(move |_request| {
v1_counter.fetch_add(1, Ordering::SeqCst);
jwks_document_with_kid("shared-kid")
.to_string()
.into_bytes()
})
.create_async()
.await;
let v2_counter = v2_hits.clone();
let _jwks_v2_mock = server
.mock("GET", "/jwks-v2.json")
.with_status(200)
.with_header("content-type", "application/json")
.with_body_from_request(move |_request| {
v2_counter.fetch_add(1, Ordering::SeqCst);
jwks_document_with_kid_and_second_key("shared-kid")
.to_string()
.into_bytes()
})
.create_async()
.await;
// A one-second refresh interval so the gate comes due inside the
// test; the JWKS TTL stays long so only the rebind's expiry — never
// the document's own age — can force keys to be re-fetched.
let client = crate::AuthplaneClient::builder(&issuer)
.with_fetch_settings(FetchSettings::from_dev_mode(true))
.with_metadata_refresh_seconds(1)
.with_jwks_refresh_seconds(3600)
.build()
.await
.expect("client discovers the pre-rotation metadata");
let resource = client
.resource(audience, &["tools/read".to_string()])
.await
.expect("resource built through the public API");
resource
.verify(&signed_token_with_kid_and_key(
shared_kid,
TEST_PRIVATE_PEM,
&issuer,
audience,
"jti-original",
))
.await
.expect("token signed by the original key must verify before rotation");
// The AS rotates: same kid, new material, new jwks_uri.
rotated.store(true, Ordering::SeqCst);
let v1_hits_at_rotation = v1_hits.load(Ordering::SeqCst);
// Let the refresh interval elapse so the next verification's gate
// is unambiguously due — this test must go through the interval
// path, not win a race against it.
tokio::time::sleep(std::time::Duration::from_millis(1200)).await;
let claims = resource
.verify(&signed_token_with_kid_and_key(
shared_kid,
TEST_PRIVATE_PEM_2,
&issuer,
audience,
"jti-rotated",
))
.await
.expect("the due re-read must rebind and fetch the same kid's new material");
assert_eq!(claims.kid, shared_kid);
assert!(
v2_hits.load(Ordering::SeqCst) >= 1,
"keys must have been re-fetched from the rotated jwks_uri",
);
assert_eq!(
v1_hits.load(Ordering::SeqCst),
v1_hits_at_rotation,
"no further fetch of the withdrawn document once the rebind happened",
);
client.aclose().await;
}
}