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
// SPDX-License-Identifier: MIT OR Apache-2.0
// Copyright (C) 2026 Matthew Jackson
//! Registered OAuth clients, mirrored from RFC 6749 section 2 with the OAuth 2.1 public /
//! confidential split.
use fmt;
use ;
use ;
use crateGrantType;
// Aliased to the name this module used when it carried its own copy of the encoder, so the call
// sites below (and `src/tests/client.rs`, which reaches the private helper) read unchanged. There
// is one FUNCTION; `crate::hex` owns it, and `tests/hex_single_definition.rs` keeps it at one.
use crateencode as hex_lower;
use crateScopeSet;
/// A client identifier (RFC 6749 section 2.2): opaque to this crate, unique per registration.
;
/// A STORED VERIFIER for a client secret: enough to check a presented secret, never enough to
/// present one.
///
/// This is what [`ClientAuth::ConfidentialSecretHash`] holds, and it is the shape a host should
/// persist. RFC 6749 section 2.3.1 says the client secret is a password; a password at rest
/// belongs in a one-way form, so that a dump of the client table is not a set of working
/// credentials.
///
/// Two kinds of scheme:
///
/// - [`SecretHash::SHA256_HEX`], built by [`SecretHash::sha256`] and verified by this crate with
/// no host code and no new dependency (`sha2` is already here for RFC 7636 PKCE). Plain SHA-256
/// is the RIGHT primitive for this particular job and the wrong one for a user password: a
/// client secret is high-entropy and host-generated, so there is no dictionary to run against
/// it, and the offline-guessing threat that makes a slow KDF necessary for human-chosen
/// passwords does not exist here. The comparison is constant time regardless, for the reason
/// given on [`ClientAuth::verify_with`].
/// - Anything else, built by [`SecretHash::custom`] and verified by a host-supplied
/// [`SecretVerifier`]. A host whose policy names argon2id, scrypt or bcrypt, or whose
/// verification happens in an HSM, keeps that dependency in its own tree where it belongs. A
/// custom scheme with NO verifier installed never authenticates: failing closed is the only
/// safe reading of "the server cannot check this credential".
/// Hand-written for the same reason as [`ClientAuth`]'s: a stored verifier is not a credential a
/// client can present, but it IS the input to an offline attack, so it must not turn up in a
/// host's logs through `{:?}`. The SCHEME stays visible, because that is the field an operator
/// needs when auditing which registrations still use a weak or retired one.
/// The host's client-secret verifier, for [`SecretHash`] schemes this crate does not implement.
///
/// Installed on the server (`AuthorizationServer::with_secret_verifier`) and consulted by
/// [`ClientAuth::verify_with`]. It is an ADDITION, never an override: a registration in the
/// built-in scheme is always verified by this crate, so a permissive or buggy host verifier cannot
/// weaken one.
///
/// Implementations MUST compare in constant time with respect to the presented secret: a
/// comparison that returns early on the first differing byte leaks, through its own timing, how
/// much of a guess was right, which turns an offline search into an online one an attacker can
/// run a byte at a time. Every serious password-hashing crate already does this.
///
/// # MUST NOT PANIC
///
/// [`SecretVerifier::verify`] MUST answer `false` for every input it cannot make sense of: a
/// stored encoding it does not recognise, a truncated hash, a parameter block with a length it did
/// not expect, a presented secret that is empty or enormous. `false` is the fail-closed answer and
/// it is always available; this trait has no error channel precisely because there is nothing a
/// verifier could report that is not "this does not verify".
///
/// This crate catches no unwind anywhere on a request path, so a panic here is not turned into
/// `invalid_client`. It unwinds out of `AuthorizationServer::authenticate_client` and out of the
/// token request the host is driving. NAMING THE CONSEQUENCE: this seam is reachable by a caller
/// with NO valid credential at all. [`SecretVerifier::dummy_hash`] is consulted on the
/// UNKNOWN-CLIENT path, deliberately, so an unauthenticated request with an invented `client_id`
/// runs this verifier over host-controlled bytes. A verifier that panics on a malformed encoding
/// is therefore a remotely reachable panic on the token endpoint, and on a host that treats a
/// panicking task as fatal it is a remotely reachable process abort.
///
/// # `verify` runs on the CALLER'S EXECUTOR THREAD, and it is not async
///
/// This method is synchronous and is called inline inside an `async fn`, so the KDF runs on
/// whichever executor thread is polling the token request. Nothing here yields, and a host cannot
/// interpose `spawn_blocking` from outside: there is no async variant of this seam in 0.9.
///
/// The cost is not hypothetical, and this trait's own docs price it: argon2id at ordinary
/// parameters is roughly 200 ms (see [`SecretVerifier::dummy_hash`]). On a CURRENT-THREAD runtime
/// that is 200 ms during which the reactor polls nothing else, per token request, and the
/// unknown-client path pays it too. What a host should do about it:
///
/// - budget for it as request LATENCY, not as background work,
/// - run the server on a multi-threaded runtime, so one stalled worker is not the whole reactor,
/// - keep the [`crate::events::RateLimiter`] installed. It runs BEFORE this seam, so a host can
/// bound how many of these an unauthenticated caller can start,
/// - or hand off internally: a verifier may keep its own blocking pool and block on the result,
/// which moves the KDF off the executor thread at the cost of a hop.
///
/// An ASYNC variant of this method would remove the need for all four, and it is not in 0.9: it
/// would be a breaking change to a trait hosts already implement, so it belongs to 1.0.
/// How the client authenticates to the token endpoint (RFC 6749 section 2.3).
///
/// `Debug` is hand-written rather than derived (see below) so that `ConfidentialSecret`'s secret
/// never appears in a debug format. `Client` derives `Debug` and holds a `ClientAuth`, so this
/// also keeps `{:?}` on a whole `Client` safe, without needing a hand-written `Debug` there too.
/// `#[non_exhaustive]`: `client-assertion` and `mtls` each add a variant, independently, so this
/// enum has four possible variant sets. A host matches this to render "how does this client
/// authenticate" in an admin UI, or to decide what its own registration endpoint will accept, and
/// neither of those should stop compiling because an unrelated crate in the graph wanted mutual
/// TLS. Registering a client is unaffected: naming a variant is still just naming it.
/// Hand-written so `ConfidentialSecret { secret }` never prints the secret. An AS library that
/// logs nothing itself should still not make `tracing::debug!(?client)` on a host's part into a
/// plaintext credential leak; deriving `Debug` here would do exactly that. Every non-secret
/// variant and field stays visible so the type is still useful to debug-print.
/// Constant-time equality, by comparing SHA-256 digests over a fixed 32 bytes rather than the raw
/// inputs.
///
/// Hashing first is what makes this constant time, on two axes that a raw byte-by-byte compare
/// cannot deliver at once:
///
/// 1. Value: the accumulator below visits all 32 digest bytes regardless of where (or whether) the
/// inputs first differ, so no early difference shows up as an early exit.
/// 2. Length: SHA-256 always produces exactly 32 bytes no matter how long `a` or `b` are, so the
/// loop always runs exactly 32 iterations. Comparing the raw inputs directly, even with a
/// "run for max(a.len(), b.len())" loop, makes wall time grow with the presented secret's
/// length once it exceeds the registered one, which lets a network attacker binary-search the
/// registered secret's length by timing the token endpoint. Hashing first removes the input
/// length from the loop bound entirely.
///
/// This also happens to make the function actually correct: two digests are equal only when the
/// two inputs were equal (SHA-256 collision resistance), so there is no longer a length-encoding
/// edge case where sufficiently padded unequal inputs compare equal.
///
/// This is NOT password hashing, and SHA-256 is not being used as a KDF here. `secret` is a
/// high-entropy, host-generated and host-managed credential, not a human-chosen password, so
/// there is no offline-guessing threat this needs to be slow against. SHA-256's only job in this
/// function is to be a fixed-width length equaliser ahead of a constant-time compare; nobody
/// should read this as a template for verifying user passwords.
/// A registered client: identity, authentication, and what it is allowed to do.
/// What a dynamically registered client carries beyond an ordinary registration: the RFC 7592
/// section 2 management credential, and the RFC 7591 section 3.2.1 members that are not
/// recoverable from the rest of the [`Client`].
///
/// The registration access token is held as a one-way [`SecretHash`], never as itself. It is a
/// bearer credential that reads, rewrites and DELETES a registration, so it is at least as
/// sensitive as the client secret next to it, and it is stored the same way for the same reason:
/// a dump of the client table must not be a set of working credentials.