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
// SPDX-License-Identifier: MIT OR Apache-2.0
// Copyright (C) 2026 Matthew Jackson
//! Consent records, consent withdrawal, and RFC 9470 step-up authentication.
//!
//! Two things a real deployment needs that nothing else in this crate provided.
//!
//! # 1. A consent is a UNIT, and a unit can be withdrawn
//!
//! Before this module the only durable grouping this server had was the refresh chain's
//! `family_id` (see [`crate::token::RefreshTokenRecord::family_id`]), which exists so that RFC 9700
//! section 4.14.2 reuse detection can revoke "the tokens issued for that authorization grant".
//! That grouping is too NARROW to answer the question a user asks. A user does not ask "end the
//! chain that started on Tuesday"; they ask "this application should no longer act for me", and one
//! application acting for one user accumulates many families over time, because every fresh trip
//! through the authorization endpoint mints another one.
//!
//! So a [`ConsentRecord`] is the broader unit, keyed by (client, subject), and
//! [`crate::store::Storage::revoke_consent`] is [`crate::store::Storage::revoke_token_family`] at
//! that broader granularity: the same "remove every record reachable from this unit" primitive,
//! asked of a bigger unit, in ONE storage operation so a host's database can do the whole cascade
//! in one transaction. A withdrawal that left tokens alive would be the whole feature failing
//! silently, and silently is the worst way for it to fail, because the user has been told they
//! stopped something they did not. `tests/consent.rs` attacks exactly that case.
//!
//! # 2. This library cannot authenticate anybody, and says so
//!
//! RFC 9470 is about the AUTHENTICATION behind an authorization: a resource server decides the
//! request it just received needs a stronger or a fresher login than the token reflects, and says
//! so with an `insufficient_user_authentication` challenge (section 3). The client then repeats its
//! authorization request carrying `acr_values` and `max_age` (section 4, the parameters OpenID
//! Connect Core section 3.1.2.1 defines), and the authorization server is expected to act on them
//! (section 5) and to report what it did as `acr` and `auth_time` (section 6).
//!
//! SECTION 6 HAS TWO SUBSECTIONS AND THIS CRATE ANSWERS BOTH, because a token reaches a resource
//! server two ways: 6.1 is the RFC 9068 JWT access token, read offline by a server that never
//! introspects, and 6.2 is RFC 7662 introspection, which is all an OPAQUE token has. Through 0.9.1
//! only 6.2 was answered, which left the step-up invisible to exactly the deployment that verifies
//! signatures locally.
//!
//! This crate has no login page, no session store, no password, no second factor, and no way to
//! challenge a user, and it will not grow any of them: that is the same boundary the crate docs
//! draw around the HTTP listener and persistence. So the division of labour is blunt, and worth
//! stating in full rather than leaving to be discovered:
//!
//! - the HOST authenticates the user, by whatever means, and REPORTS the result as an
//! [`Authentication`]: when it happened, and which authentication context class it satisfied. The
//! library takes that report at face value, because it has nothing to check it against. A host
//! that stamps `auth_time` with the current instant on every request has disabled `max_age` for
//! itself and no code here can tell.
//! - the LIBRARY records that report on the consent, on the authorization code, and on the tokens
//! the code mints, and it ENFORCES `max_age` and `acr_values` against it
//! ([`AuthenticationRequirement::satisfied_by`]). Enforcement is the half that must not be left
//! to the host: a `max_age` a host is trusted to check for itself is a `max_age` that gets
//! checked in whichever code path somebody remembered.
//! - the library CANNOT re-authenticate anyone in response to a failure. It answers
//! [`crate::error::ErrorCode::InsufficientUserAuthentication`] and the host decides whether that
//! becomes a fresh login prompt or a refusal.
//!
//! # Allocation
//!
//! [`Authentication`] hangs off its three records as an `Option<Box<Authentication>>`: one null
//! pointer for the common case of a host that reports nothing, and one small allocation for a host
//! that does. Its `acr`, and the consent record's identifier and subject, are `Box<str>` rather
//! than `String` for the reason [`crate::token::IssuedToken::jkt`] gives: they are written once and
//! never appended to, so a `String`'s growable capacity would be 8 dead bytes on every record a
//! store holds. Everything here is feature gated, so a build without `consent` carries neither the
//! pointers nor the code that reads them.
use ;
use ;
use crateClientId;
use crate;
use crateScopeSet;
/// The largest number of RFC 8707 resource indicators one [`ConsentRecord`] will accumulate.
///
/// # Why this one matters more than the per-request caps
///
/// [`crate::server::MAX_RESOURCE_INDICATORS`] bounds what a single request may ask for, and that
/// cost dies with the request. This bounds what a (client, subject) relationship accumulates over
/// its whole life. The list is only ever widened by [`ConsentRecord::extend`], it is never pruned,
/// and [`ConsentRecord::covers`] walks it linearly on every authorization request that consults the
/// record. Without a bound, a client naming one fresh indicator per request buys a record that
/// grows forever and a check that gets slower forever, and the deployment never gets that back.
///
/// # Why 32
///
/// It is twice the per-request cap, which is the smallest number that is not simply the per-request
/// cap in disguise: a relationship legitimately widens over time, so a user who approves one set of
/// resources today and a different set next month should not hit the ceiling on the second visit.
/// Beyond that, a single (client, subject) pair spanning more than thirty-two distinct resource
/// servers is a client acting for the user across an estate large enough that per-resource consent
/// has stopped meaning anything to the person granting it, which is a product problem this number
/// makes visible rather than a limit it creates.
pub const MAX_CONSENT_RESOURCES: usize = 32;
/// The largest number of authentication context classes one `acr_values` parameter may name
/// (RFC 9470 section 4, OpenID Connect Core section 3.1.2.1).
///
/// # Why a cap at all
///
/// `acr_values` is ONE parameter carrying a space-delimited list, and it arrives unauthenticated,
/// before any user interaction, at `GET /authorize` and at the RFC 9126 push. Parsing it stores one
/// `Box<str>` per non-empty segment, which is one heap allocation per segment, so without a bound
/// the segment count is whatever the request line or the body allowed. The cheapest input is
/// `"a a a ..."` at two bytes a token: 64 KiB of body, which is
/// `crate::http::MAX_BODY_BYTES`, is about 32,768 allocations from a single parameter, and the
/// GET form is worse because a URL is not bounded by that constant at all.
///
/// That is the exact shape `crate::http::MAX_FORM_PARAMETERS` exists to refuse, and it slipped
/// past because it is one parameter rather than many: that constant's own arithmetic puts 64 KiB of
/// `&a=b` pairs at about 2,300 parameters, so this one parameter bought an order of magnitude more
/// work than the case the parameter cap was introduced for.
///
/// # Why 16
///
/// `acr_values` is an ORDERED PREFERENCE list, not a set to enumerate: OpenID Connect Core section
/// 3.1.2.1 has the AS satisfy the first class it can, so the entries past the first few are already
/// alternatives nobody expects to be reached. No published profile lists more than a handful. It is
/// also the number [`crate::server::MAX_RESOURCE_INDICATORS`] and
/// `rar::MAX_AUTHORIZATION_DETAILS_ELEMENTS` use for the other repeatable things one request can
/// carry, so a reader does not have to hold a third number.
///
/// # It refuses, it does not truncate
///
/// Truncating would answer a resource server's step-up challenge with a class the user never
/// satisfied, or drop the one class the client could actually meet, and tell nobody. That is the
/// same failure [`crate::server::MAX_RESOURCE_INDICATORS`] refuses for RFC 8707 and
/// [`crate::rar`] refuses for unknown members. `invalid_request` is the honest answer.
pub const MAX_ACR_VALUES: usize = 16;
/// What the HOST says about how, and when, it authenticated the resource owner.
///
/// This is a REPORT, not a proof. See the module docs: this crate cannot authenticate anyone and
/// has no way to check this against anything, so it records it and holds requests to it.
/// A persisted record that one resource owner granted one client a set of permissions.
///
/// Keyed by (`client_id`, `subject`): one live consent per pair, widened in place when the user
/// approves something further or re-authenticates. That key is what makes withdrawal answerable,
/// and it is deliberately COARSER than the refresh chain's `family_id`, because a user withdrawing
/// consent means "not this application, not for me, not any more" rather than "not this chain".
/// The RFC 9396 `authorization_details` an authorization request is ASKING FOR, in a shape that
/// exists in every feature configuration.
///
/// A WRAPPER rather than the details themselves, and the reason is structural, the same one
/// `crate::server`'s `GrantedDetails` records about `issue`: an argument can carry a `cfg`, but the
/// ARGUMENT AT A CALL SITE cannot, so a gated parameter on [`ConsentRecord::covers`] would make
/// every host's approval resolver — the one place this crate asks a host to write a comparison —
/// duplicate that call under a `cfg` of its own. A host that writes one call site cannot get it
/// wrong in the configuration it does not build.
///
/// Without `rar` this type has no fields, so it is zero sized, [`RequestedDetails::none`] compiles
/// to nothing, and a deployment that never enabled RFC 9396 pays for none of it.
///
/// `#[non_exhaustive]` because its field set varies with a feature, which is the rule
/// `tests/host_api_shape.rs` gates for every public type in this crate. Both fields are private
/// already, so the attribute costs a host nothing: the two constructors below are the only way to
/// make one in any build.
/// What the client asked for about the USER's authentication, from an authorization request.
///
/// RFC 9470 section 4 carries exactly two parameters, both defined by OpenID Connect Core section
/// 3.1.2.1, and this crate implements only those two. Reading two of OpenID Connect's parameters
/// does not make this OpenID Connect: there is no `id_token`, no UserInfo endpoint and no claims
/// model here, and all three are off this crate's list on purpose.
/// Why a host's reported authentication did not satisfy an [`AuthenticationRequirement`].
///
/// Fieldless on purpose, and not only for the size of it. The `error_description` these produce goes
/// back to the CLIENT through the authorization response redirect (RFC 6749 section 4.1.2.1), and
/// the client is not entitled to learn when the user last logged in or which `acr` they hold; "not
/// fresh enough" is the whole of what it needs in order to decide to ask again.
/// The other error-shaped types in this crate ([`crate::dpop::DpopFailure`],
/// [`crate::client_assertion::AssertionFailure`], [`crate::mtls::MtlsRegistrationError`]) are all
/// `std::error::Error`, and a host that puts one of them behind `?` or in a `Box<dyn Error>` has to
/// be able to do the same with this one. `Display` above is the whole implementation.
/// Build the RFC 9470 section 3 `WWW-Authenticate` challenge a RESOURCE SERVER sends when the token
/// it received is valid but the authentication behind it is not enough.
///
/// This crate is an authorization server, not a resource server, so this is a HELPER for the host's
/// own resource servers rather than something this server ever sends: the same posture as
/// [`crate::resource_metadata`], where the type is defined here and publishing it is the resource's
/// job. It is here because the challenge and the authorization request that answers it have to agree
/// on the spelling of two parameters, and one module owning both is how they stay in agreement.
///
/// `scheme` is the RFC 6750 section 3 (or RFC 9449 section 7.1) authentication scheme the resource
/// server challenges with, normally `Bearer` or `DPoP`. Section 3 puts `error`, `error_description`,
/// `acr_values` and `max_age` in the challenge; `acr_values` is omitted when empty rather than sent
/// blank, because an empty list reads as "no class is acceptable".
///
/// # What this does to the values, and why it is not only escaping
///
/// The values are emitted inside quoted strings, so a `"` or a `\` in an `acr` value would forge the
/// parameter that follows it. Both are ESCAPED, per the `quoted-string` rule of RFC 9110 section
/// 5.6.4, rather than the value being rejected: this crate does not own the host's `acr` vocabulary
/// and has no business refusing a value it merely has to transmit.
///
/// Escaping is not enough on its own, and reading section 5.6.4 as though it were is the defect the
/// 0.9.1 audit found here. `qdtext` is `HTAB / SP / %x21 / %x23-5B / %x5D-7E / obs-text`, and
/// `quoted-pair` is `"\" ( HTAB / SP / VCHAR / obs-text )`: NEITHER production admits a control
/// character, so a CR, an LF or a DEL cannot be escaped into a legal `quoted-string` at all. It can
/// only be removed, and until this was fixed it was passed through verbatim, which is a header
/// break emitted out of a value this doc called escaped. Reachability is not theoretical: the
/// parameter type is exactly [`AuthenticationRequirement::acr_values`], which
/// [`AuthenticationRequirement::from_pairs`] fills from the client's own query parameter, and the
/// whole point of this helper is that those classes come back out in a header.
///
/// So anything outside `HTAB`, `SP`, `%x21..=%x7E` and `%x80..=%xFF` is DROPPED. Dropping rather
/// than refusing keeps the signature total, and the two are the same answer in practice: a value
/// with a control character in it is not a class name any host defined, so there is nothing to
/// preserve. Every byte a `quoted-string` does admit survives, non-ASCII included.
///
/// `scheme` is written outside any quoted string, where no escaping exists at all, so it is filtered
/// to the RFC 9110 section 5.6.2 `tchar` set for the same reason. `Bearer` and `DPoP` pass through
/// untouched; anything that would forge the rest of the header does not survive to do it.
///
/// One allocation for every value this crate can produce: the buffer is sized up front from the
/// parts that go into it, and filtering only ever shortens what goes in. A value that is nothing but
/// quotes and backslashes escapes to twice its length and would grow the buffer once; that is a
/// `String`'s ordinary behaviour and not a bound anyone relies on.
/// Append `value` as the inside of an RFC 9110 section 5.6.4 `quoted-string`: `"` and `\` escaped,
/// and everything the grammar cannot carry dropped.
///
/// The two rules are not alternatives, and treating them as one was the bug. `quoted-string` is
/// `DQUOTE *( qdtext / quoted-pair ) DQUOTE` with
///
/// ```text
/// qdtext = HTAB / SP / %x21 / %x23-5B / %x5D-7E / obs-text
/// quoted-pair = "\" ( HTAB / SP / VCHAR / obs-text )
/// obs-text = %x80-FF
/// ```
///
/// so `"` (%x22) and `\` (%x5C) are the two characters that are legal only as the second half of a
/// `quoted-pair`, which is what the escape below produces. A control character is in NEITHER
/// production: it is not `qdtext`, and it is not `VCHAR`, so a backslash in front of it produces an
/// illegal `quoted-pair` rather than a legal escape. There is no spelling of CR, LF or DEL inside a
/// `quoted-string`, so the only conformant thing to do with one is to not emit it. See
/// [`step_up_challenge`] for why the value can contain one in the first place.
///
/// [`char::is_control`] is not the test used here: it is true for the Unicode `Cc` category, which
/// includes `%x80..=%x9F`, and those are `obs-text` and therefore legal. The ranges are written out
/// instead, and every scalar value above `%x7F` is kept.
/// RFC 9110 section 5.6.2 `tchar`, the character set an auth scheme (section 11.1 `auth-scheme`,
/// which is a `token`) is made of.
///
/// Used to filter [`step_up_challenge`]'s `scheme`, which is written outside every quoted string in
/// the challenge and so has no escape available to it at all: one space in it and everything after
/// it reads as the auth parameters of a different scheme, one `"` and the quoting is off by one for
/// the rest of the header.