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
// SPDX-License-Identifier: MIT OR Apache-2.0
// Copyright (C) 2026 Matthew Jackson
//! The host seams this library cannot fill for itself: an AUDIT EVENT channel and a RATE LIMITING
//! decision point, plus the one slot ([`Hooks`]) the server carries for both of them and for the
//! client secret verifier ([`crate::client::SecretVerifier`]).
//!
//! # Why these live here and not in the host's own code
//!
//! Both answer questions only the library can ask and only the host can answer.
//!
//! - OBSERVATION. This crate revokes a whole token family when it detects authorization code
//! replay (RFC 9700 section 4.1.1) or refresh token reuse (OAuth 2.1 draft section 6.1, RFC 9700
//! section 4.14.2). Those are the crate's most serious security behaviours and their entire
//! value is that somebody NOTICES: a revocation that appears in no log is an incident nobody
//! investigates. Nothing outside the library can see either event, because the evidence (a
//! consumed code presented twice, a spent refresh record presented again) exists only inside the
//! grant machinery.
//! - THROTTLING. RFC 8628 section 5.1 makes the device user code's entropy adequate only IN
//! COMBINATION WITH rate limiting of code entry. The library knows an attempt happened and
//! whether it failed; it does NOT know the caller, the IP, the session or the user, because it
//! never sees a request. So the library asks and reports, and the host counts and decides.
//!
//! That reasoning explains why this is a SEAM. It never justified shipping the seam EMPTY, and
//! as of 0.9.0 the crate does not: [`crate::rate_limit::FixedWindowRateLimiter`] is a counter a
//! host installs in one line, with defaults derived from the section 5.1 arithmetic. It is a
//! floor rather than a ceiling (it is per process, so on a multi-node deployment the effective
//! limit is multiplied by the node count); its module docs say plainly what it cannot do.
//!
//! # Zero cost until enabled
//!
//! The crate doc promises a host that never turns something on pays nothing for it, and this
//! module is built to keep that promise structurally rather than by intention:
//!
//! - [`Hooks`] is ONE pointer wide. The seams behind it are four in a default build (an
//! [`EventSink`], a [`RateLimiter`], a [`crate::client::SecretVerifier`] and a
//! [`crate::registration::RegistrationPolicy`]) and six with `jar` and `jwt` (a
//! `RequestObjectKeys` and an `Es256Verifier`). Held as separate `Option<Box<dyn _>>` fields they
//! would be 16 bytes each on every [`crate::server::AuthorizationServer`] value, so 64 bytes paid
//! by every host and 96 by one enabling both features; instead they live inside a boxed struct
//! that is not allocated at all until something is installed.
//!
//! The registration policy is worth naming rather than counting, because it is the seam whose
//! ABSENCE is the security behaviour: with none installed, every RFC 7591 registration is REFUSED
//! (see [`Hooks::registration_policy`]), which is the opposite default to the rate limiter's.
//! - [`Hooks::emit`] takes a CLOSURE, not an [`Event`]. With no sink installed the closure is
//! never called, so the event is never built: no allocation, no formatting, no vtable dispatch,
//! just one null check on a pointer that is already in cache. `tests/events.rs` measures exactly
//! that with a counting allocator and with a closure that panics if it is ever run.
//! - Events are delivered SYNCHRONOUSLY, on the calling task. This crate has no background task by
//! design (see the crate docs) and does not gain one here; a host that wants buffering owns a
//! channel and writes three lines of [`EventSink`].
//!
//! # What events may carry, and what they may never carry
//!
//! Security finding C13 hand-wrote `Debug` on every type in this crate that holds a credential, so
//! that a host's `tracing::debug!(?request)` could not become a plaintext credential leak. An event
//! channel is a second way out of the process for the same values, and it goes to the same logs,
//! so it is held to the same rule: NO access token, NO refresh token, NO authorization code, NO
//! device code, NO user code, NO client secret, NO PKCE verifier. `tests/events.rs` scans the
//! [`Event`] declaration and fails if a field named after one of those appears.
//!
//! What events DO carry is what an incident response needs to act:
//!
//! - `client_id`, which RFC 6749 section 2.2 states is not a secret.
//! - the `subject`, where there is one. This is the host's own user identifier, which the host
//! already put into this crate; it is not a credential (holding it authenticates nobody), though
//! a host in a privacy-regulated setting may want to treat it as personal data in its logs.
//! - the `family_id` of a refresh chain. This one is worth justifying, because it is the only
//! opaque server-minted string in the whole set. It is SAFE to log because it is not a
//! credential in any sense the protocol recognises: it is accepted at no endpoint, it appears in
//! no request and no response, it is never given to a client, and possessing it lets nobody
//! obtain, refresh or introspect a token. Its only power is to NAME a set of records in the
//! host's own store, which is precisely the correlation an operator needs to answer "what else
//! did this compromised grant issue" and to call [`crate::store::Storage::revoke_token_family`]
//! by hand. The alternative, logging the tokens themselves, is the leak this rule exists to
//! prevent; the alternative of logging nothing makes the revocation untraceable.
use crateSecretVerifier;
use crateAssertionFailure;
use crateDpopFailure;
use crateErrorCode;
use crateGrantType;
use crateRegistrationPolicy;
use crateScopeSet;
use crateTokenTypeHint;
/// Why a client failed to authenticate (RFC 6749 section 5.2 `invalid_client`).
///
/// The WIRE collapses all of these into one `invalid_client`, deliberately, so an attacker cannot
/// probe which client ids exist. The AUDIT channel separates them just as deliberately: the host
/// is not the attacker, and "a thousand unknown client ids" and "a thousand wrong secrets for one
/// real client" are different incidents with different responses.
///
/// Shared by BOTH planes: it is the reason carried by
/// [`Event::ClientAuthenticationFailed`] (the token plane) and by
/// [`Event::ClientRegistrationAuthenticationFailed`] (the RFC 7592 management plane). One
/// vocabulary rather than two, because a host counting credential guesses wants to count the same
/// shapes wherever they happen; WHICH plane an attempt arrived on is the event variant, not this
/// enum, so a sink can separate them without having to learn a second set of names.
/// The OPERATOR's sentence, never the client's. Everything here is what the wire deliberately
/// refuses to distinguish (see the type's docs), so these strings must not reach a response body.
/// It is the `Err` payload of `crate::mtls::authenticate_via_mtls`, so a host handling that with
/// `?` or collecting it into a `Box<dyn Error>` needs this, exactly as `DpopFailure` and
/// `AssertionFailure` do for theirs. (Plain text rather than intra-doc links: those two types are
/// behind features this one is not, so a link would dangle in a default build.)
/// Something the authorization server did, or refused to do, worth recording.
///
/// Every field borrows: an event costs no allocation to build, which is what lets a host with a
/// sink installed pay only for what its sink chooses to keep. See the module docs for the rule on
/// what may and may not appear here.
///
/// `#[non_exhaustive]`: later releases will add events, and adding one must not be a breaking change
/// for a host that matched on this. The RFC 7591 and 7592 registration events below arrived exactly
/// that way and are here now, as did [`Event::DpopProofRefused`], which this paragraph named as
/// the next candidate until 0.9.1 added it.
/// Where events go. The host implements this; the library never logs anything itself.
///
/// `on_event` is called SYNCHRONOUSLY, inside the request the host is already driving, and it takes
/// `&self` so the sink is shared. Two consequences a host should design for: a slow sink slows the
/// token endpoint, and a panicking sink panics the request. A host doing anything expensive should
/// push onto a channel here and do the work elsewhere; this crate will not own that thread.
/// Something a caller is attempting that a host may want to throttle.
///
/// Deliberately carries no credential: not the user code being tried (RFC 8628 section 6.1 makes
/// it the credential a human types) and not the secret. It also cannot carry an IP or a session,
/// because this library never sees a request; a host correlates using its own request context,
/// which it still holds at the moment it calls into the server.
/// What a [`RateLimiter`] decided.
/// How an attempt turned out, reported back so a limiter can count FAILURES rather than traffic.
/// The host's throttle. THIS LIBRARY DOES NOT RATE LIMIT ANYTHING, and cannot: it never sees a
/// request, so it has no caller, no IP, no session and no user to count against.
///
/// RFC 8628 section 5.1 is explicit that the device user code's entropy is sufficient only in
/// combination with rate limiting of user code entry, so for any deployment offering the device
/// grant this is not optional in practice, only optional in the type system.
///
/// A HOST DOES NOT HAVE TO WRITE ONE. [`crate::rate_limit::FixedWindowRateLimiter`] is an
/// implementation this crate ships, in memory, with no new dependency and with defaults derived
/// from the section 5.1 arithmetic. Implement this trait yourself when you have something the
/// library does not: a request IP, a session, a user, or a store shared across nodes.
///
/// # MUST NOT PANIC, and should not block
///
/// Both methods MUST answer for every input. There is no error channel and none is needed:
/// [`RateLimitDecision::Allow`] and [`RateLimitDecision::Deny`] are both always available, and a
/// limiter that cannot reach its shared counter should decide which of the two its deployment
/// prefers rather than unwinding. This crate catches no unwind anywhere on a request path.
///
/// NAMING THE CONSEQUENCE, once per method, because the two sit at different points of a request:
///
/// - [`RateLimiter::check`] runs BEFORE any credential is evaluated, which is the whole point of
/// it, and that puts it on the paths an UNAUTHENTICATED caller reaches: the authorization
/// request, RFC 8628 section 5.1 user-code entry, and client authentication at the token
/// endpoint. A panic here is remotely reachable by anyone who can open a socket, and it takes
/// the request down before the throttle that would have limited how often they could try it.
/// - [`RateLimiter::record`] runs AFTER the request has done its work, including after the store
/// writes it drove. A panic here unwinds a request whose records are already written and whose
/// response never reaches the client: the credential was spent, the client was told nothing, and
/// the only place the two could have been reconciled was the response that was lost.
///
/// Blocking is the same argument one notch quieter. `check` is called inline on the caller's
/// executor thread, so a limiter that waits on a network round trip to a shared counter adds that
/// wait to every request, and on a current-thread runtime it adds it to every OTHER request too.
/// The installed seams. Boxed as a unit (see [`Hooks`]) so that installing none of them allocates
/// anything at all.
/// The server's slot for the host seams: exactly one pointer wide, and null until the host
/// installs something.
///
/// This shape is the design decision the module docs argue for. Holding the seams as separate
/// `Option<Box<dyn _>>` fields directly on [`crate::server::AuthorizationServer`] would add 16
/// bytes each, 64 in a default build and 96 with `jar` and `jwt`, to every server value in every
/// deployment, including every deployment that installs nothing, and
/// `tests/allocation.rs` holds that type to a size budget precisely so a convenience like that
/// cannot be paid for silently.
;