authplane-sdk 0.1.0

Authplane Rust SDK core primitives.
Documentation
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
use reqwest::Client;
use serde::{Deserialize, Serialize};
use serde_json::Value;

use crate::constants::{
    auth_schemes, http_headers, http_methods, introspection_fields, jwt_claims, media_types,
};
use crate::errors::{
    AuthError, AuthplaneError, map_oauth_error, protocol_error, transport_error, validation_error,
};
use crate::fetch_settings::FetchSettings;
use crate::transport::validate_fetch_url;

pub const GRANT_TYPE_TOKEN_EXCHANGE: &str = "urn:ietf:params:oauth:grant-type:token-exchange";
pub const TOKEN_TYPE_ACCESS_TOKEN: &str = "urn:ietf:params:oauth:token-type:access_token";

#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct TokenExchangeOptions {
    pub subject_token: String,
    pub subject_token_type: String,
    pub actor_token: String,
    pub actor_token_type: String,
    pub scope: String,
    pub resources: Vec<String>,
    pub audiences: Vec<String>,
}

impl TokenExchangeOptions {
    pub fn normalized(&self) -> Self {
        let mut normalized = self.clone();
        normalized.resources.retain(|value| !value.is_empty());
        normalized.audiences.retain(|value| !value.is_empty());
        normalized
    }
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(from = "TokenResponseWire")]
#[non_exhaustive]
pub struct TokenResponse {
    // Field-level `#[serde(default)]` is intentionally omitted: deserialization
    // is routed through `TokenResponseWire` by `#[serde(from)]` above, and the
    // wire type carries its own per-field defaults. Re-declaring `default` here
    // would be inert (dead for deserialize, ignored for serialize).
    pub access_token: String,
    pub token_type: String,
    /// AS-supplied lifetime hint in seconds (RFC 6749 ยง5.1).
    ///
    /// * `None` โ€” the AS omitted the field. The cache layer applies its
    ///   configured `default_ttl`.
    /// * `Some(0)` โ€” the AS asked for immediate expiry (RFC 6749 ยง5.1
    ///   permits this for one-shot flows). The cache refuses to store.
    /// * `Some(n)` with `n > 0` โ€” use `n` seconds, then apply the buffer.
    ///
    /// On the wire, `None` is omitted entirely (the field is absent from
    /// the JSON), matching what the AS would have sent โ€” so serializing a
    /// cached `TokenResponse` round-trips through the same parse path
    /// that produced it.
    ///
    /// ```
    /// # use authplane_sdk::oauth::TokenResponse;
    /// # fn dispatch(resp: TokenResponse) {
    /// match resp.expires_in {
    ///     None => { /* AS gave no hint โ€” apply default TTL */ }
    ///     Some(0) => { /* explicit immediate expiry โ€” do not cache */ }
    ///     Some(_n) => { /* use n seconds */ }
    /// }
    /// # }
    /// ```
    #[serde(skip_serializing_if = "Option::is_none")]
    pub expires_in: Option<i64>,
    pub scope: String,
    pub refresh_token: String,
    pub issued_token_type: String,
    /// Raw `cnf` (confirmation) object from the token response when
    /// present. RFC 9449 ยง6.1 places `cnf.jkt` here for DPoP-bound
    /// tokens; this field preserves any extension members
    /// (`x5t#S256`, future additions) verbatim. Only `Some` when the
    /// AS sent a JSON object; non-object `cnf` values are dropped.
    pub cnf: Option<Value>,
    /// Convenience accessor for the DPoP key thumbprint at
    /// `cnf.jkt` (RFC 9449 ยง6.1). Empty string when the token is
    /// not DPoP-bound. Always derived from `cnf.jkt` on deserialize
    /// via `#[serde(from = "TokenResponseWire")]`, matching the
    /// imperative `parse_token_response_inner` path through
    /// `extract_cnf_and_jkt` โ€” a top-level `cnf_jkt` on the wire is
    /// not honoured, so a poisoned blob that disagrees with its own
    /// `cnf` object cannot mint a mismatched thumbprint.
    pub cnf_jkt: String,
}

/// Private wire shape for [`TokenResponse`]: derived `Deserialize`. The
/// public type's `#[serde(from)]` runs the `cnf.jkt` โ†’ `cnf_jkt`
/// derivation in `From<TokenResponseWire>` so constructed and
/// deserialized values agree on the binding. A top-level `cnf_jkt` on
/// the wire is intentionally absent from this struct: it is ignored by
/// serde's default unknown-field policy so the thumbprint is always
/// derived from `cnf` and the two constructors stay symmetric.
#[derive(Deserialize)]
struct TokenResponseWire {
    access_token: String,
    token_type: String,
    #[serde(default, deserialize_with = "deserialize_optional_expires_in")]
    expires_in: Option<i64>,
    scope: String,
    #[serde(default)]
    refresh_token: String,
    #[serde(default)]
    issued_token_type: String,
    #[serde(default)]
    cnf: Option<Value>,
}

impl From<TokenResponseWire> for TokenResponse {
    fn from(wire: TokenResponseWire) -> Self {
        let (cnf, cnf_jkt) = normalize_cnf(wire.cnf);
        Self {
            access_token: wire.access_token,
            token_type: wire.token_type,
            expires_in: wire.expires_in,
            scope: wire.scope,
            refresh_token: wire.refresh_token,
            issued_token_type: wire.issued_token_type,
            cnf,
            cnf_jkt,
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(from = "IntrospectionResponseWire")]
#[non_exhaustive]
pub struct IntrospectionResponse {
    // Field-level `#[serde(default)]` is intentionally omitted here for the
    // same reason as on [`TokenResponse`]: deserialization is routed through
    // `IntrospectionResponseWire` by `#[serde(from)]` above.
    pub active: bool,
    pub scope: String,
    pub client_id: String,
    pub sub: String,
    pub token_type: String,
    pub iss: String,
    pub aud: Option<Value>,
    pub exp: Option<i64>,
    pub iat: Option<i64>,
    pub jti: String,
    pub agent_id: String,
    pub agent_chain: Vec<String>,
    /// Raw `cnf` (confirmation) object from the RFC 7662 introspection
    /// response. RFC 9449 ยง6.2 places the DPoP key thumbprint at
    /// `cnf.jkt`; this field preserves the full object so callers can
    /// read extension members (`x5t#S256`, future additions). Only
    /// `Some` when the AS sent a JSON object; non-object `cnf` values
    /// are dropped to keep the typed shape honest.
    pub cnf: Option<Value>,
    /// Convenience accessor for the DPoP key thumbprint at
    /// `cnf.jkt` (RFC 9449 ยง6.2 / RFC 7662). Empty string when
    /// absent. Always derived from `cnf.jkt` on deserialize via
    /// `#[serde(from = "IntrospectionResponseWire")]` โ€” see the
    /// matching note on [`TokenResponse::cnf_jkt`].
    pub cnf_jkt: String,
}

/// Private wire shape for [`IntrospectionResponse`]. A top-level
/// `cnf_jkt` on the wire is intentionally absent: it is ignored by
/// serde's default unknown-field policy so the thumbprint is always
/// derived from `cnf` and the imperative `introspect_token` path
/// (via `extract_cnf_and_jkt`) agrees byte-for-byte with the
/// `From<IntrospectionResponseWire>` impl.
#[derive(Deserialize)]
struct IntrospectionResponseWire {
    active: bool,
    #[serde(default)]
    scope: String,
    #[serde(default)]
    client_id: String,
    #[serde(default)]
    sub: String,
    #[serde(default)]
    token_type: String,
    #[serde(default)]
    iss: String,
    #[serde(default)]
    aud: Option<Value>,
    #[serde(default)]
    exp: Option<i64>,
    #[serde(default)]
    iat: Option<i64>,
    #[serde(default)]
    jti: String,
    #[serde(default)]
    agent_id: String,
    #[serde(default)]
    agent_chain: Vec<String>,
    #[serde(default)]
    cnf: Option<Value>,
}

impl From<IntrospectionResponseWire> for IntrospectionResponse {
    fn from(wire: IntrospectionResponseWire) -> Self {
        let (cnf, cnf_jkt) = normalize_cnf(wire.cnf);
        Self {
            active: wire.active,
            scope: wire.scope,
            client_id: wire.client_id,
            sub: wire.sub,
            token_type: wire.token_type,
            iss: wire.iss,
            aud: wire.aud,
            exp: wire.exp,
            iat: wire.iat,
            jti: wire.jti,
            agent_id: wire.agent_id,
            agent_chain: wire.agent_chain,
            cnf,
            cnf_jkt,
        }
    }
}

pub fn parse_token_exchange_error(status_code: Option<u16>, body: &str) -> AuthplaneError {
    let parsed: Result<Value, _> = serde_json::from_str(body);
    match parsed {
        Ok(payload) => map_oauth_error(status_code, &payload),
        Err(_) => AuthplaneError::Auth(AuthError {
            message: "OAuth request failed".to_string(),
            code: "invalid_response".to_string(),
            status_code,
        }),
    }
}

/// Parse an OAuth 2.0 token response.
///
/// When `expect_dpop` is `true` (a DPoP proof was sent with the request),
/// the response `token_type` **must** be `"DPoP"` per RFC 9449 ยง5. If the
/// AS returns `"Bearer"` instead, this function returns a `ProtocolError`
/// so the caller knows the proof was silently ignored.
pub fn parse_token_response(
    data: &Value,
    allow_issued_token_type: bool,
) -> Result<TokenResponse, AuthplaneError> {
    parse_token_response_inner(data, allow_issued_token_type, false)
}

/// Same as [`parse_token_response`] with DPoP-type enforcement.
pub fn parse_token_response_dpop(
    data: &Value,
    allow_issued_token_type: bool,
) -> Result<TokenResponse, AuthplaneError> {
    parse_token_response_inner(data, allow_issued_token_type, true)
}

fn parse_token_response_inner(
    data: &Value,
    allow_issued_token_type: bool,
    expect_dpop: bool,
) -> Result<TokenResponse, AuthplaneError> {
    let access_token = required_string(data, "access_token")?;
    let token_type = required_string(data, "token_type")?;
    if !token_type.eq_ignore_ascii_case(auth_schemes::BEARER)
        && !token_type.eq_ignore_ascii_case(auth_schemes::DPOP)
    {
        return Err(protocol_error(&format!(
            "unsupported token_type {token_type:?}; only {} and {} are supported",
            auth_schemes::BEARER,
            auth_schemes::DPOP
        )));
    }
    if expect_dpop && !token_type.eq_ignore_ascii_case(auth_schemes::DPOP) {
        return Err(protocol_error(&format!(
            "DPoP proof was sent but token_type is {token_type:?}, not {:?}; \
             the authorization server may have ignored the DPoP proof (RFC 9449 ยง5)",
            auth_schemes::DPOP
        )));
    }

    let issued_token_type = optional_string(data, "issued_token_type");
    if allow_issued_token_type {
        if issued_token_type.is_empty() {
            return Err(protocol_error(
                "token exchange response missing required 'issued_token_type' (RFC 8693 ยง2.2.1)",
            ));
        }
        if issued_token_type != TOKEN_TYPE_ACCESS_TOKEN {
            return Err(protocol_error(&format!(
                "unsupported issued_token_type {issued_token_type:?}; only access_token is supported"
            )));
        }
    }

    let (cnf, cnf_jkt) = extract_cnf_and_jkt(data);

    Ok(TokenResponse {
        access_token,
        token_type,
        expires_in: optional_non_negative_i64(data, "expires_in")?,
        scope: optional_string(data, "scope"),
        refresh_token: optional_string(data, "refresh_token"),
        issued_token_type,
        cnf,
        cnf_jkt,
    })
}

pub async fn client_credentials_grant(
    http: &Client,
    token_endpoint: &str,
    auth_header: &str,
    fetch_settings: &FetchSettings,
    scopes: &[String],
    resources: &[String],
    dpop_provider: Option<&crate::dpop_provider::DpopProvider>,
) -> Result<TokenResponse, AuthplaneError> {
    let form = build_client_credentials_form(scopes, resources);

    let expect_dpop = dpop_provider.is_some();
    let (status, payload) = form_post_with_dpop(
        http,
        token_endpoint,
        "token endpoint",
        &form,
        auth_header,
        fetch_settings,
        dpop_provider,
        // No access token to bind via `ath` โ€” this call IS the one
        // that mints it. AS endpoints don't require `ath` per
        // RFC 9449 ยง4.2 (ath is for resource-server requests).
        None,
    )
    .await?;

    if crate::transport::is_http_success(status) {
        return if expect_dpop {
            parse_token_response_dpop(&payload, false)
        } else {
            parse_token_response(&payload, false)
        };
    }

    Err(map_oauth_error(Some(status), &payload))
}

pub async fn exchange_token(
    http: &Client,
    token_endpoint: &str,
    options: &TokenExchangeOptions,
    auth_header: &str,
    fetch_settings: &FetchSettings,
    dpop_provider: Option<&crate::dpop_provider::DpopProvider>,
) -> Result<TokenResponse, AuthplaneError> {
    if options.subject_token.is_empty() {
        return Err(validation_error("subject_token is required"));
    }

    let normalized = options.normalized();
    let form = build_token_exchange_form(&normalized);

    let expect_dpop = dpop_provider.is_some();
    let (status, payload) = form_post_with_dpop(
        http,
        token_endpoint,
        "token endpoint",
        &form,
        auth_header,
        fetch_settings,
        dpop_provider,
        None,
    )
    .await?;

    if crate::transport::is_http_success(status) {
        return if expect_dpop {
            parse_token_response_dpop(&payload, true)
        } else {
            parse_token_response(&payload, true)
        };
    }

    Err(map_oauth_error(Some(status), &payload))
}

/// RFC 7662 ยง2.1 introspection call.
///
/// `auth_header` is sent verbatim; an empty string sends no credentials
/// (RFC 7662 leaves the authentication method to the deployment). Against
/// authserver >= 0.1.2 that path is not useful: only the issuing client or
/// a runtime-client of the Resource named in `aud` gets a real answer, and
/// an unauthenticated or public-client caller receives `{"active": false}`
/// for every token. Resource servers should introspect with confidential
/// credentials via [`crate::RevocationConfig`].
pub async fn introspect_token(
    http: &Client,
    introspection_endpoint: &str,
    token: &str,
    auth_header: &str,
    fetch_settings: &FetchSettings,
    dpop_provider: Option<&crate::dpop_provider::DpopProvider>,
) -> Result<IntrospectionResponse, AuthplaneError> {
    let form = [
        (
            crate::constants::oauth_params::TOKEN.to_string(),
            token.to_string(),
        ),
        (
            crate::constants::oauth_params::TOKEN_TYPE_HINT.to_string(),
            crate::constants::oauth_errors::TOKEN_TYPE_HINT_ACCESS_TOKEN.to_string(),
        ),
    ];
    let (status, payload) = form_post_with_dpop(
        http,
        introspection_endpoint,
        "introspection endpoint",
        &form,
        auth_header,
        fetch_settings,
        dpop_provider,
        None,
    )
    .await?;

    if crate::transport::is_http_success(status) {
        let (cnf, cnf_jkt) = extract_cnf_and_jkt(&payload);
        return Ok(IntrospectionResponse {
            active: payload
                .get(introspection_fields::ACTIVE)
                .and_then(Value::as_bool)
                .unwrap_or(false),
            scope: optional_string(&payload, introspection_fields::SCOPE),
            client_id: optional_string(&payload, jwt_claims::CLIENT_ID),
            sub: optional_string(&payload, jwt_claims::SUB),
            token_type: optional_string(&payload, introspection_fields::TOKEN_TYPE),
            iss: optional_string(&payload, jwt_claims::ISS),
            aud: payload.get(jwt_claims::AUD).cloned(),
            exp: payload.get(jwt_claims::EXP).and_then(Value::as_i64),
            iat: payload.get(jwt_claims::IAT).and_then(Value::as_i64),
            jti: optional_string(&payload, jwt_claims::JTI),
            agent_id: optional_string(&payload, jwt_claims::AGENT_ID),
            agent_chain: crate::json_util::string_array(&payload, jwt_claims::AGENT_CHAIN),
            cnf,
            cnf_jkt,
        });
    }

    Err(map_oauth_error(Some(status), &payload))
}

pub async fn revoke_token(
    http: &Client,
    revocation_endpoint: &str,
    token: &str,
    auth_header: &str,
    fetch_settings: &FetchSettings,
    dpop_provider: Option<&crate::dpop_provider::DpopProvider>,
) -> Result<(), AuthplaneError> {
    let form = [
        (
            crate::constants::oauth_params::TOKEN.to_string(),
            token.to_string(),
        ),
        (
            crate::constants::oauth_params::TOKEN_TYPE_HINT.to_string(),
            crate::constants::oauth_errors::TOKEN_TYPE_HINT_ACCESS_TOKEN.to_string(),
        ),
    ];
    let (status, payload) = form_post_with_dpop(
        http,
        revocation_endpoint,
        "revocation endpoint",
        &form,
        auth_header,
        fetch_settings,
        dpop_provider,
        None,
    )
    .await?;

    if crate::transport::is_http_success(status) {
        return Ok(());
    }

    Err(map_oauth_error(Some(status), &payload))
}

fn required_string(data: &Value, key: &str) -> Result<String, AuthplaneError> {
    let value = optional_string(data, key);
    if value.is_empty() {
        return Err(protocol_error(&format!(
            "token response missing required field {key:?}"
        )));
    }
    Ok(value)
}

fn optional_string(data: &Value, key: &str) -> String {
    data.get(key)
        .and_then(Value::as_str)
        .unwrap_or_default()
        .trim()
        .to_string()
}

/// Extract the raw `cnf` confirmation object and its `jkt` thumbprint from
/// an OAuth response payload. Non-object `cnf` values are dropped so the
/// typed shape stays honest; absent `cnf.jkt` collapses to an empty string,
/// matching the `#[serde(default)]` contract on the field itself.
///
/// Convenience over [`normalize_cnf`] for the imperative `parse_*` paths
/// that already hold the full payload as a `Value`.
fn extract_cnf_and_jkt(payload: &Value) -> (Option<Value>, String) {
    normalize_cnf(payload.get("cnf").cloned())
}

/// Shared `(Option<Value>, String)` derivation for an already-extracted
/// `cnf` field. Used by both [`extract_cnf_and_jkt`] (which pulls `cnf`
/// off a full payload) and the `From<*Wire>` impls (which receive `cnf`
/// directly from serde). Non-object values are dropped; missing `jkt`
/// becomes the empty string.
fn normalize_cnf(cnf: Option<Value>) -> (Option<Value>, String) {
    let cnf = cnf.filter(Value::is_object);
    let cnf_jkt = cnf
        .as_ref()
        .and_then(Value::as_object)
        .and_then(|map| map.get("jkt"))
        .and_then(Value::as_str)
        .unwrap_or_default()
        .to_string();
    (cnf, cnf_jkt)
}

/// Parse a non-negative integer field that may legitimately be absent.
///
/// Returns `None` when the field is missing or JSON `null` (the AS did not
/// supply the hint), `Some(N)` for a present value including an explicit
/// `Some(0)` (RFC 6749 ยง5.1 permits `expires_in: 0` for one-shot flows).
/// Negative values and non-integer / non-string types are rejected.
/// Errors the shared parse helper can emit. Stringly-typed so each caller
/// wraps the message into its own error type without leaking
/// `AuthplaneError` into the `serde` path.
enum ParseOptionalI64Error {
    NotAnInteger { got: Option<String> },
    Negative,
}

/// Parses a JSON value as an optional non-negative `i64`, accepting both
/// JSON numbers and stringly-encoded numerics. Shared by the
/// `parse_token_response` path and the `serde(deserialize_with = ...)`
/// shim so both entry points apply the same validation.
///
/// * Returns `Ok(None)` when the value is `Value::Null` (or absent).
/// * Returns `Ok(Some(n))` for `n โ‰ฅ 0`.
/// * Returns `Err` for any non-integer / non-string shape and for
///   negative values.
fn parse_optional_non_negative_i64(value: &Value) -> Result<Option<i64>, ParseOptionalI64Error> {
    if value.is_null() {
        return Ok(None);
    }
    let parsed = if let Some(number) = value.as_i64() {
        number
    } else if let Some(text) = value.as_str() {
        text.parse::<i64>()
            .map_err(|_| ParseOptionalI64Error::NotAnInteger {
                got: Some(text.to_string()),
            })?
    } else {
        return Err(ParseOptionalI64Error::NotAnInteger { got: None });
    };
    if parsed < 0 {
        return Err(ParseOptionalI64Error::Negative);
    }
    Ok(Some(parsed))
}

/// `parse_token_response`-side adapter โ€” wraps
/// [`parse_optional_non_negative_i64`] errors as protocol errors with
/// the offending field name.
fn optional_non_negative_i64(data: &Value, key: &str) -> Result<Option<i64>, AuthplaneError> {
    let value = data.get(key).unwrap_or(&Value::Null);
    parse_optional_non_negative_i64(value).map_err(|err| match err {
        ParseOptionalI64Error::NotAnInteger { got: Some(text) } => protocol_error(&format!(
            "token response field {key:?} must be an integer, got {text:?}"
        )),
        ParseOptionalI64Error::NotAnInteger { got: None } => {
            protocol_error(&format!("token response field {key:?} must be an integer"))
        }
        ParseOptionalI64Error::Negative => protocol_error(&format!(
            "token response field {key:?} must be non-negative"
        )),
    })
}

/// `serde(deserialize_with = ...)` shim โ€” wraps
/// [`parse_optional_non_negative_i64`] errors as serde errors so callers
/// who deserialize a `TokenResponse` directly (e.g. from a cached JSON
/// payload) get the same validation as `parse_token_response` from a
/// fresh AS response.
fn deserialize_optional_expires_in<'de, D>(deserializer: D) -> Result<Option<i64>, D::Error>
where
    D: serde::Deserializer<'de>,
{
    use serde::de::Error;
    let raw: Option<Value> = Option::deserialize(deserializer)?;
    let value = raw.unwrap_or(Value::Null);
    parse_optional_non_negative_i64(&value).map_err(|err| match err {
        ParseOptionalI64Error::NotAnInteger { got: Some(text) } => {
            D::Error::custom(format!("expires_in must be an integer, got {text:?}"))
        }
        ParseOptionalI64Error::NotAnInteger { got: None } => {
            D::Error::custom("expires_in must be an integer")
        }
        ParseOptionalI64Error::Negative => D::Error::custom("expires_in must be non-negative"),
    })
}

/// Send a form POST with optional DPoP proof and automatic nonce retry.
///
/// If a `DpopProvider` is supplied, the function:
/// 1. Builds a DPoP proof for the endpoint (using the provider's
///    current per-origin nonce, if any),
/// 2. Sends the request,
/// 3. Extracts `DPoP-Nonce` from the response headers and stores it
///    on the provider,
/// 4. If the AS returned `use_dpop_nonce` with a fresh nonce on the
///    error response (RFC 9449 ยง6.1), retries the request once with
///    a proof rebuilt using the new nonce. A single retry is enough:
///    once the provider has the AS nonce, subsequent calls reuse it
///    via `current_nonce`.
///
/// Returns `(status, payload)`. The single retry is intentionally
/// not exposed as a knob; servers that send `use_dpop_nonce`
/// repeatedly are out-of-spec and we surface the error to the
/// caller rather than loop.
#[allow(clippy::too_many_arguments)] // each arg is load-bearing for the ยง6.1 retry contract
pub(crate) async fn form_post_with_dpop(
    http: &Client,
    url: &str,
    url_label: &str,
    form: &[(String, String)],
    auth_header: &str,
    fetch_settings: &FetchSettings,
    dpop_provider: Option<&crate::dpop_provider::DpopProvider>,
    dpop_access_token: Option<&str>,
) -> Result<(u16, Value), AuthplaneError> {
    validate_fetch_url(url, fetch_settings, url_label)?;

    let initial_dpop = match dpop_provider {
        Some(provider) => Some(provider.build_proof(http_methods::POST, url, dpop_access_token)?),
        None => None,
    };

    let (status, payload, nonce) =
        do_form_post(http, url, form, auth_header, initial_dpop.as_deref()).await?;

    // Store DPoP-Nonce from response.
    if let Some(provider) = dpop_provider {
        if !nonce.is_empty() {
            let _ = provider.note_nonce(url, &nonce);
        }

        // Retry on use_dpop_nonce error (RFC 9449 ยง6.1).
        let error_code = payload
            .get("error")
            .and_then(Value::as_str)
            .unwrap_or_default();
        if error_code == crate::constants::oauth_errors::USE_DPOP_NONCE && !nonce.is_empty() {
            let retry_proof = provider.build_proof(http_methods::POST, url, dpop_access_token)?;
            let (status2, payload2, nonce2) =
                do_form_post(http, url, form, auth_header, Some(&retry_proof)).await?;
            // Also refresh the stored nonce from the retry response so the
            // next outbound call starts with the freshest server-issued value.
            // RFC 9449 ยง6.1 permits the AS to rotate nonces on every response;
            // absorbing the retry's `DPoP-Nonce` here avoids a second
            // guaranteed-stale request when the AS rotates aggressively.
            if !nonce2.is_empty() {
                let _ = provider.note_nonce(url, &nonce2);
            }
            return Ok((status2, payload2));
        }
    }

    Ok((status, payload))
}

/// Low-level form POST. Returns (status, body, dpop_nonce_header).
async fn do_form_post(
    http: &Client,
    url: &str,
    form: &[(String, String)],
    auth_header: &str,
    dpop_proof: Option<&str>,
) -> Result<(u16, Value, String), AuthplaneError> {
    let mut request = http
        .post(url)
        .header(http_headers::AUTHORIZATION, auth_header)
        .header(http_headers::ACCEPT, media_types::APPLICATION_JSON);
    if let Some(proof) = dpop_proof {
        request = request.header(http_headers::DPOP, proof);
    }
    let response = request
        .form(form)
        .send()
        .await
        .map_err(|error| transport_error(&error.to_string()))?;
    let status = response.status().as_u16();
    let nonce = response
        .headers()
        .get(http_headers::DPOP_NONCE)
        .and_then(|v| v.to_str().ok())
        .unwrap_or_default()
        .to_string();
    // RFC 7009 ยง2.2 revocation responses are typically 200 with no body, and a
    // server can send an empty success body on any of these form-POST endpoints.
    // Treat an empty body as `{}` rather than erroring on `serde_json::from_slice`
    // so a spec-conformant revoke doesn't surface as `Err(transport_error)`.
    let bytes = response
        .bytes()
        .await
        .map_err(|error| transport_error(&error.to_string()))?;
    let payload: Value = if bytes.is_empty() {
        Value::Object(serde_json::Map::new())
    } else {
        serde_json::from_slice(&bytes).map_err(|error| transport_error(&error.to_string()))?
    };
    Ok((status, payload, nonce))
}

#[doc(hidden)]
pub fn build_client_credentials_form(
    scopes: &[String],
    resources: &[String],
) -> Vec<(String, String)> {
    use crate::constants::oauth_params::*;
    let mut form = vec![(
        GRANT_TYPE.to_string(),
        GRANT_TYPE_CLIENT_CREDENTIALS.to_string(),
    )];
    if !scopes.is_empty() {
        form.push((SCOPE.to_string(), scopes.join(" ")));
    }
    for resource in resources.iter().filter(|value| !value.is_empty()) {
        form.push((RESOURCE.to_string(), resource.clone()));
    }
    form
}

#[doc(hidden)]
pub fn build_token_exchange_form(options: &TokenExchangeOptions) -> Vec<(String, String)> {
    use crate::constants::oauth_params::*;
    let mut form = vec![
        (
            GRANT_TYPE.to_string(),
            GRANT_TYPE_TOKEN_EXCHANGE.to_string(),
        ),
        (SUBJECT_TOKEN.to_string(), options.subject_token.clone()),
        (
            SUBJECT_TOKEN_TYPE.to_string(),
            if options.subject_token_type.is_empty() {
                TOKEN_TYPE_ACCESS_TOKEN.to_string()
            } else {
                options.subject_token_type.clone()
            },
        ),
    ];

    if !options.actor_token.is_empty() {
        form.push((ACTOR_TOKEN.to_string(), options.actor_token.clone()));
        form.push((
            ACTOR_TOKEN_TYPE.to_string(),
            if options.actor_token_type.is_empty() {
                TOKEN_TYPE_ACCESS_TOKEN.to_string()
            } else {
                options.actor_token_type.clone()
            },
        ));
    }
    if !options.scope.is_empty() {
        form.push((SCOPE.to_string(), options.scope.clone()));
    }
    for resource in &options.resources {
        form.push((RESOURCE.to_string(), resource.clone()));
    }
    for audience in &options.audiences {
        form.push((AUDIENCE.to_string(), audience.clone()));
    }
    form
}

#[cfg(test)]
mod tests {
    use serde_json::json;

    use crate::{AuthplaneError, parse_token_exchange_error};

    use super::{
        GRANT_TYPE_TOKEN_EXCHANGE, IntrospectionResponse, TOKEN_TYPE_ACCESS_TOKEN,
        TokenExchangeOptions, TokenResponse, build_client_credentials_form,
        build_token_exchange_form, parse_token_response,
    };

    #[test]
    fn maps_consent_required_to_typed_error() {
        let body = r#"{
            "error":"consent_required",
            "error_description":"Consent needed",
            "service_id":"drive",
            "cause":"approval_needed",
            "consent_url":"https://consent.example.com/start"
        }"#;

        let error = parse_token_exchange_error(Some(400), body);
        let AuthplaneError::ConsentRequired(consent) = error else {
            panic!("expected consent required");
        };

        assert_eq!(consent.code, "consent_required");
        assert_eq!(consent.service_id, "drive");
        assert_eq!(consent.cause_detail, "approval_needed");
        assert_eq!(
            consent.consent_url.as_deref(),
            Some("https://consent.example.com/start")
        );
    }

    #[test]
    fn maps_interaction_required_to_typed_error() {
        let body = r#"{
            "error":"interaction_required",
            "error_description":"User interaction required",
            "service":"calendar"
        }"#;

        let error = parse_token_exchange_error(Some(400), body);
        let AuthplaneError::ConsentRequired(consent) = error else {
            panic!("expected consent required");
        };

        assert_eq!(consent.code, "interaction_required");
        assert_eq!(consent.service_id, "calendar");
        assert_eq!(consent.cause_detail, "User interaction required");
        assert_eq!(consent.consent_url, None);
    }

    #[test]
    fn maps_invalid_json_to_invalid_response_auth_error() {
        let error = parse_token_exchange_error(Some(500), "{invalid-json");
        let AuthplaneError::Auth(auth_error) = error else {
            panic!("expected auth error");
        };

        assert_eq!(auth_error.code, "invalid_response");
        assert_eq!(auth_error.message, "OAuth request failed");
        assert_eq!(auth_error.status_code, Some(500));
    }

    #[test]
    fn maps_non_consent_error_to_auth_error() {
        let body = r#"{
            "error":"invalid_target",
            "error_description":"target missing"
        }"#;
        let error = parse_token_exchange_error(Some(400), body);
        let AuthplaneError::Auth(auth_error) = error else {
            panic!("expected auth error");
        };

        assert_eq!(auth_error.code, "invalid_target");
        assert_eq!(auth_error.message, "target missing");
        assert!(auth_error.is_invalid_target());
        assert!(!auth_error.is_access_denied());
        assert!(!crate::should_open_circuit_for_oauth_error(
            &auth_error.code
        ));
    }

    /// authserver 0.2.0 answers a cross-client exchange whose client is
    /// not allowlisted on the target Resource with `access_denied` and
    /// HTTP 403. It is a plain `AuthError` โ€” not `ConsentRequired`, since
    /// no user interaction can clear it โ€” and must not trip the breaker.
    #[test]
    fn maps_access_denied_to_auth_error_outside_the_breaker() {
        let body = r#"{
            "error":"access_denied",
            "error_description":"client is not allowed to exchange for this resource"
        }"#;
        let error = parse_token_exchange_error(Some(403), body);
        let AuthplaneError::Auth(auth_error) = error else {
            panic!("expected auth error");
        };

        assert_eq!(auth_error.code, "access_denied");
        assert_eq!(auth_error.status_code, Some(403));
        assert!(auth_error.is_access_denied());
        assert!(!auth_error.is_invalid_target());
        assert!(!crate::should_open_circuit_for_oauth_error(
            &auth_error.code
        ));
    }

    #[test]
    fn token_exchange_options_filter_empty_resource_and_audience_values() {
        let options = TokenExchangeOptions {
            subject_token: "subject".to_string(),
            resources: vec!["".to_string(), "https://api.example.com".to_string()],
            audiences: vec!["".to_string(), "api://billing".to_string()],
            ..TokenExchangeOptions::default()
        }
        .normalized();

        assert_eq!(
            options.resources,
            vec!["https://api.example.com".to_string()]
        );
        assert_eq!(options.audiences, vec!["api://billing".to_string()]);
    }

    #[test]
    fn client_credentials_form_includes_scope_and_resource() {
        let form = build_client_credentials_form(
            &["tools/read".to_string()],
            &["https://api.example.com".to_string()],
        );
        assert!(form.contains(&("grant_type".to_string(), "client_credentials".to_string())));
        assert!(form.contains(&("scope".to_string(), "tools/read".to_string())));
        assert!(form.contains(&(
            "resource".to_string(),
            "https://api.example.com".to_string()
        )));
    }

    #[test]
    fn token_exchange_form_applies_defaults_and_repeated_values() {
        let form = build_token_exchange_form(&TokenExchangeOptions {
            subject_token: "subject-token".to_string(),
            resources: vec![
                "https://api-one.example.com".to_string(),
                "https://api-two.example.com".to_string(),
            ],
            audiences: vec!["api://inventory".to_string()],
            ..TokenExchangeOptions::default()
        });

        assert!(form.contains(&(
            "grant_type".to_string(),
            GRANT_TYPE_TOKEN_EXCHANGE.to_string()
        )));
        assert!(form.contains(&(
            "subject_token_type".to_string(),
            TOKEN_TYPE_ACCESS_TOKEN.to_string()
        )));
        assert!(form.contains(&(
            "resource".to_string(),
            "https://api-one.example.com".to_string()
        )));
        assert!(form.contains(&(
            "resource".to_string(),
            "https://api-two.example.com".to_string()
        )));
        assert!(form.contains(&("audience".to_string(), "api://inventory".to_string())));
    }

    #[test]
    fn parse_token_response_requires_issued_token_type_for_exchange() {
        let payload = json!({
            "access_token": "new-token",
            "token_type": "Bearer"
        });

        let error = parse_token_response(&payload, true).expect_err("missing issued token type");
        let AuthplaneError::Auth(auth_error) = error else {
            panic!("expected auth error");
        };
        assert_eq!(auth_error.code, "protocol_error");
    }

    #[test]
    fn parse_token_response_preserves_issued_token_type() {
        let payload = json!({
            "access_token": "new-token",
            "token_type": "Bearer",
            "issued_token_type": TOKEN_TYPE_ACCESS_TOKEN
        });

        let response = parse_token_response(&payload, true).expect("valid token response");
        assert_eq!(response.issued_token_type, TOKEN_TYPE_ACCESS_TOKEN);
    }

    #[test]
    fn parse_token_response_rejects_unsupported_token_type() {
        let payload = json!({
            "access_token": "new-token",
            "token_type": "mac"
        });
        let error = parse_token_response(&payload, false).expect_err("must reject token type");
        let AuthplaneError::Auth(auth_error) = error else {
            panic!("expected auth error");
        };
        assert_eq!(auth_error.code, "protocol_error");
        assert!(auth_error.message.contains("unsupported token_type"));
    }

    #[test]
    fn parse_token_response_rejects_unsupported_issued_token_type() {
        let payload = json!({
            "access_token": "new-token",
            "token_type": "Bearer",
            "issued_token_type": "urn:ietf:params:oauth:token-type:refresh_token"
        });
        let error =
            parse_token_response(&payload, true).expect_err("must reject issued token type");
        let AuthplaneError::Auth(auth_error) = error else {
            panic!("expected auth error");
        };
        assert_eq!(auth_error.code, "protocol_error");
        assert!(auth_error.message.contains("unsupported issued_token_type"));
    }

    #[test]
    fn parse_token_response_rejects_negative_expires_in() {
        let payload = json!({
            "access_token": "new-token",
            "token_type": "Bearer",
            "expires_in": -1
        });
        let error =
            parse_token_response(&payload, false).expect_err("must reject negative expires_in");
        let AuthplaneError::Auth(auth_error) = error else {
            panic!("expected auth error");
        };
        assert_eq!(auth_error.code, "protocol_error");
        assert!(auth_error.message.contains("must be non-negative"));
    }

    #[test]
    fn parse_token_response_rejects_non_integer_expires_in() {
        let payload = json!({
            "access_token": "new-token",
            "token_type": "Bearer",
            "expires_in": "abc"
        });
        let error =
            parse_token_response(&payload, false).expect_err("must reject non-integer expires_in");
        let AuthplaneError::Auth(auth_error) = error else {
            panic!("expected auth error");
        };
        assert_eq!(auth_error.code, "protocol_error");
        assert!(auth_error.message.contains("must be an integer"));
    }

    #[test]
    fn parse_token_response_missing_expires_in_becomes_none() {
        // A missing `expires_in` is a *signal*: the AS did not commit to a
        // lifetime hint. The cache layer uses this to apply its configured
        // `default_ttl`. Previously this field was defaulted to `0`, which
        // collided with the RFC 6749 ยง5.1 explicit-zero shape and silently
        // extended a deliberately-immediate-expiry token to one hour.
        let payload = json!({
            "access_token": "new-token",
            "token_type": "Bearer"
        });
        let response = parse_token_response(&payload, false).expect("valid response");
        assert_eq!(response.expires_in, None);
    }

    #[test]
    fn parse_token_response_explicit_zero_expires_in_becomes_some_zero() {
        // RFC 6749 ยง5.1 permits `expires_in: 0` for one-shot flows.
        // Distinguished from a missing hint via `Some(0)` vs `None`.
        let payload = json!({
            "access_token": "new-token",
            "token_type": "Bearer",
            "expires_in": 0
        });
        let response = parse_token_response(&payload, false).expect("valid response");
        assert_eq!(response.expires_in, Some(0));
    }

    #[test]
    fn client_credentials_form_emits_one_resource_per_value() {
        // RFC 8707 ยง2 โ€” a client that needs multiple resources MUST emit
        // one `resource=` parameter per value, not a joined single string.
        let form = build_client_credentials_form(
            &["tools/read".to_string()],
            &[
                "https://api-one.example.com".to_string(),
                "https://api-two.example.com".to_string(),
            ],
        );
        let resource_entries: Vec<_> = form.iter().filter(|(k, _)| k == "resource").collect();
        assert_eq!(resource_entries.len(), 2);
        assert!(
            resource_entries
                .iter()
                .any(|(_, v)| v == "https://api-one.example.com")
        );
        assert!(
            resource_entries
                .iter()
                .any(|(_, v)| v == "https://api-two.example.com")
        );
    }

    #[test]
    fn client_credentials_form_space_joins_multiple_scopes() {
        // RFC 6749 ยง3.3 โ€” multiple scope values MUST be space-delimited
        // (URL-encoding is handled by the HTTP client).
        let form = build_client_credentials_form(
            &["tools/read".to_string(), "tools/write".to_string()],
            &[],
        );
        let scope = form
            .iter()
            .find(|(k, _)| k == "scope")
            .map(|(_, v)| v.clone())
            .expect("scope must be present");
        assert_eq!(scope, "tools/read tools/write");
    }

    #[test]
    fn client_credentials_form_omits_empty_scope() {
        let form = build_client_credentials_form(&[], &[]);
        assert!(
            !form.iter().any(|(k, _)| k == "scope"),
            "scope must be omitted when empty"
        );
    }

    #[test]
    fn token_exchange_form_uses_token_exchange_grant_type() {
        let options = TokenExchangeOptions {
            subject_token: "subject-1".to_string(),
            ..TokenExchangeOptions::default()
        };
        let form = build_token_exchange_form(&options);
        assert!(form.contains(&(
            "grant_type".to_string(),
            GRANT_TYPE_TOKEN_EXCHANGE.to_string()
        )));
    }

    #[test]
    fn token_exchange_form_defaults_subject_token_type_when_missing() {
        // RFC 8693 ยง2.1 โ€” when subject_token_type is not set by the
        // caller, the SDK MUST default to access-token.
        let options = TokenExchangeOptions {
            subject_token: "subject-1".to_string(),
            ..TokenExchangeOptions::default()
        };
        let form = build_token_exchange_form(&options);
        let subject_type = form
            .iter()
            .find(|(k, _)| k == "subject_token_type")
            .map(|(_, v)| v.clone())
            .expect("subject_token_type must be present");
        assert_eq!(subject_type, TOKEN_TYPE_ACCESS_TOKEN);
    }

    #[test]
    fn token_exchange_form_defaults_actor_token_type_only_when_actor_token_present() {
        // Actor token type default only applies when actor_token is set โ€”
        // otherwise we must not emit a spurious actor_token_type.
        let with_actor = TokenExchangeOptions {
            subject_token: "subject-1".to_string(),
            actor_token: "actor-1".to_string(),
            ..TokenExchangeOptions::default()
        };
        let form = build_token_exchange_form(&with_actor);
        assert!(
            form.iter()
                .any(|(k, v)| k == "actor_token_type" && v == TOKEN_TYPE_ACCESS_TOKEN)
        );

        let without_actor = TokenExchangeOptions {
            subject_token: "subject-1".to_string(),
            ..TokenExchangeOptions::default()
        };
        let form = build_token_exchange_form(&without_actor);
        assert!(
            !form.iter().any(|(k, _)| k == "actor_token_type"),
            "actor_token_type must be omitted when actor_token is missing"
        );
    }

    #[test]
    fn token_exchange_form_normalizes_empty_resource_and_audience() {
        // Empty-string entries in resources/audiences must not reach the wire.
        let options = TokenExchangeOptions {
            subject_token: "subject-1".to_string(),
            resources: vec!["".to_string(), "https://api.example.com".to_string()],
            audiences: vec!["api://billing".to_string(), "".to_string()],
            ..TokenExchangeOptions::default()
        }
        .normalized();
        let form = build_token_exchange_form(&options);
        let resources: Vec<_> = form.iter().filter(|(k, _)| k == "resource").collect();
        let audiences: Vec<_> = form.iter().filter(|(k, _)| k == "audience").collect();
        assert_eq!(resources.len(), 1);
        assert_eq!(audiences.len(), 1);
    }

    #[test]
    fn parse_token_exchange_error_without_status_code_still_maps_to_auth_error() {
        let error = parse_token_exchange_error(None, "not json at all");
        let AuthplaneError::Auth(auth_error) = error else {
            panic!("expected auth error");
        };
        assert_eq!(auth_error.code, "invalid_response");
        assert_eq!(auth_error.status_code, None);
    }

    #[test]
    fn parse_token_exchange_error_401_maps_to_authentication_failure() {
        let body = r#"{"error":"invalid_client","error_description":"bad creds"}"#;
        let error = parse_token_exchange_error(Some(401), body);
        let AuthplaneError::Auth(auth_error) = error else {
            panic!("expected auth error");
        };
        assert_eq!(auth_error.code, "invalid_client");
        assert_eq!(auth_error.status_code, Some(401));
    }

    #[test]
    fn introspection_response_deserialize_derives_cnf_jkt_from_cnf_object() {
        // RFC 9449 ยง6.2 places the DPoP thumbprint at `cnf.jkt`.
        // Direct `serde_json::from_value` must surface it as
        // `cnf_jkt` so the typed shape matches what `introspect_token`
        // builds on the construction path. Previous behaviour left
        // `cnf_jkt` empty on direct deserialize โ€” a footgun for any
        // cache/proxy that round-tripped the struct through JSON.
        let payload = json!({
            "active": true,
            "token_type": "DPoP",
            "cnf": {"jkt": "abc"},
        });
        let response: IntrospectionResponse = serde_json::from_value(payload).unwrap();
        assert!(response.active);
        assert_eq!(response.cnf_jkt, "abc");
        assert_eq!(
            response.cnf.and_then(|v| v.get("jkt").cloned()),
            Some(json!("abc"))
        );
    }

    #[test]
    fn introspection_response_deserializes_with_absent_cnf_defaults_to_empty() {
        let payload = json!({"active": true, "token_type": "Bearer"});
        let response: IntrospectionResponse = serde_json::from_value(payload).unwrap();
        assert!(response.active);
        assert_eq!(response.cnf, None);
        assert_eq!(response.cnf_jkt, "");
    }

    #[test]
    fn introspection_response_round_trips_cnf_binding_through_serde() {
        // to_value โ†’ from_value must preserve both `cnf` and
        // `cnf_jkt`. The two were asymmetric before the
        // `#[serde(from = "IntrospectionResponseWire")]` fix: a
        // caller building the struct via constructor saw `cnf_jkt`
        // populated, but the same struct after a JSON round-trip
        // through a cache layer had `cnf_jkt = ""` while `cnf` was
        // preserved โ€” two shapes for the same wire payload.
        let payload = json!({
            "active": true,
            "token_type": "DPoP",
            "cnf": {"jkt": "thumbprint-abc"},
        });
        let first: IntrospectionResponse = serde_json::from_value(payload).unwrap();
        let serialized = serde_json::to_value(&first).unwrap();
        let second: IntrospectionResponse = serde_json::from_value(serialized).unwrap();
        assert_eq!(first, second);
        assert_eq!(second.cnf_jkt, "thumbprint-abc");
    }

    #[test]
    fn introspection_response_deserialize_drops_non_object_cnf() {
        // Defensive parity with `extract_cnf_and_jkt`: a malformed AS
        // sending `cnf` as a non-object scalar should not pollute the
        // typed shape; we drop the `cnf` and leave `cnf_jkt` empty.
        let payload = json!({"active": true, "cnf": "not-an-object"});
        let response: IntrospectionResponse = serde_json::from_value(payload).unwrap();
        assert_eq!(response.cnf, None);
        assert_eq!(response.cnf_jkt, "");
    }

    #[test]
    fn token_response_deserialize_derives_cnf_jkt_from_cnf_object() {
        // Same symmetric-deserialize fix as the introspection peer
        // above, applied to RFC 9449 ยง6.1 token responses.
        let payload = json!({
            "access_token": "at",
            "token_type": "DPoP",
            "expires_in": 3600,
            "scope": "tools/echo",
            "cnf": {"jkt": "thumbprint-token"},
        });
        let response: TokenResponse = serde_json::from_value(payload).unwrap();
        assert_eq!(response.cnf_jkt, "thumbprint-token");
        assert_eq!(
            response.cnf.and_then(|v| v.get("jkt").cloned()),
            Some(json!("thumbprint-token"))
        );
    }

    #[test]
    fn token_response_round_trips_cnf_binding_through_serde() {
        let payload = json!({
            "access_token": "at",
            "token_type": "DPoP",
            "expires_in": 3600,
            "scope": "tools/echo",
            "cnf": {"jkt": "thumbprint-token"},
        });
        let first: TokenResponse = serde_json::from_value(payload).unwrap();
        let serialized = serde_json::to_value(&first).unwrap();
        let second: TokenResponse = serde_json::from_value(serialized).unwrap();
        assert_eq!(first, second);
        assert_eq!(second.cnf_jkt, "thumbprint-token");
    }

    #[test]
    fn token_response_deserialize_ignores_wire_cnf_jkt_when_it_disagrees_with_cnf() {
        // A poisoned cache/persistence blob that carries both a `cnf.jkt`
        // and a mismatched top-level `cnf_jkt` must resolve to the
        // `cnf.jkt` thumbprint โ€” the imperative `parse_token_response_inner`
        // path through `extract_cnf_and_jkt` always derives from `cnf`,
        // and the serde path is required to agree.
        let payload = json!({
            "access_token": "at",
            "token_type": "DPoP",
            "expires_in": 3600,
            "scope": "tools/echo",
            "cnf": {"jkt": "from-cnf"},
            "cnf_jkt": "poisoned-top-level",
        });
        let response: TokenResponse = serde_json::from_value(payload).unwrap();
        assert_eq!(response.cnf_jkt, "from-cnf");
    }

    #[test]
    fn introspection_response_deserialize_ignores_wire_cnf_jkt_when_it_disagrees_with_cnf() {
        let payload = json!({
            "active": true,
            "token_type": "DPoP",
            "cnf": {"jkt": "from-cnf"},
            "cnf_jkt": "poisoned-top-level",
        });
        let response: IntrospectionResponse = serde_json::from_value(payload).unwrap();
        assert_eq!(response.cnf_jkt, "from-cnf");
    }
}