skybase 0.1.11

The Open-Source Backend & Developer Platform for the AT Protocol (Firebase for ATProto, powered by skyauth)
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
//! Sovereign PDS write client executing DPoP-signed mutations against an ATProto PDS.

use reqwest::{Client, Method};
use serde::Serialize;
use std::sync::Arc;
use url::Url;

use skyauth::client::AtprotoOAuthClient;
use skyauth::dpop::{extract_dpop_nonce, normalize_htu, DPoPNonceCache};
use skyauth::session::OAuthSession;

use crate::error::{Result, SkybaseError};
use crate::repo::types::{
    validate_rkey, CreateRecordRequest, CreateRecordResult, DeleteRecordRequest, PutRecordRequest,
    PutRecordResult, RecordView, XrpcErrorResponse,
};

/// Maximum bounded body size for error inspection (64 KB).
const MAX_ERROR_BODY_BYTES: usize = 65_536;

/// Maximum bounded body size for successful responses (1 MB).
const MAX_SUCCESS_BODY_BYTES: usize = 1_048_576;

/// Sovereign PDS repository write client.
///
/// Manages RFC 9449 DPoP authentication, cryptographic proof generation,
/// and transparent single-retry challenge recovery for ATProto record mutations.
#[derive(Clone)]
pub struct PdsRepoClient {
    session: Arc<OAuthSession>,
    oauth_client: Option<Arc<AtprotoOAuthClient>>,
    http_client: Client,
    nonce_cache: DPoPNonceCache,
    endpoint_override: Option<String>,
}

impl PdsRepoClient {
    /// Fallible constructor creating a `PdsRepoClient` with strict redirect policies.
    ///
    /// # Errors
    /// Returns [`SkybaseError::Network`] if the underlying HTTP client cannot be built.
    pub fn try_new(session: Arc<OAuthSession>, client: Arc<AtprotoOAuthClient>) -> Result<Self> {
        let nonce_cache = client.nonce_cache().clone();
        let http_client = Client::builder()
            .redirect(reqwest::redirect::Policy::none())
            .build()
            .map_err(SkybaseError::Network)?;

        Ok(Self {
            session,
            oauth_client: Some(client),
            http_client,
            nonce_cache,
            endpoint_override: None,
        })
    }

    /// Fallible constructor directly from an [`OAuthSession`] with a fresh [`DPoPNonceCache`].
    ///
    /// # Errors
    /// Returns [`SkybaseError::Network`] if the underlying HTTP client cannot be built.
    pub fn try_from_session(session: Arc<OAuthSession>) -> Result<Self> {
        let http_client = Client::builder()
            .redirect(reqwest::redirect::Policy::none())
            .build()
            .map_err(SkybaseError::Network)?;

        Ok(Self {
            session,
            oauth_client: None,
            http_client,
            nonce_cache: DPoPNonceCache::new(),
            endpoint_override: None,
        })
    }

    /// Creates a new `PdsRepoClient` wrapping an authenticated [`OAuthSession`] and [`AtprotoOAuthClient`].
    ///
    /// Borrows and shares the [`DPoPNonceCache`] from the OAuth client.
    ///
    /// # Errors
    /// Returns [`SkybaseError::Network`] if HTTP client creation fails.
    pub fn new(session: Arc<OAuthSession>, client: Arc<AtprotoOAuthClient>) -> Result<Self> {
        Self::try_new(session, client)
    }

    /// Creates a `PdsRepoClient` directly from an [`OAuthSession`] with a fresh [`DPoPNonceCache`].
    ///
    /// # Errors
    /// Returns [`SkybaseError::Network`] if HTTP client creation fails.
    pub fn from_session(session: Arc<OAuthSession>) -> Result<Self> {
        Self::try_from_session(session)
    }

    /// Convenience constructor creating an internal [`OAuthSession`] for manual credentials or testing.
    ///
    /// # Errors
    /// Returns [`SkybaseError::Auth`] if session initialization fails, or [`SkybaseError::Network`]
    /// if HTTP client creation fails.
    pub fn from_credentials(
        pds_endpoint: impl Into<String>,
        repo_did: impl Into<String>,
        access_token: impl Into<String>,
    ) -> Result<Self> {
        let endpoint = pds_endpoint.into();
        let did = repo_did.into();
        let token = access_token.into();

        let session = OAuthSession::new(
            did,
            token,
            None,
            "DPoP",
            None,
            None,
            skyauth::dpop::DPoPKey::generate(),
            Some(endpoint),
            None,
            None,
        )
        .map_err(SkybaseError::Auth)?;

        Self::try_from_session(Arc::new(session))
    }

    /// Overrides the PDS endpoint destination URL.
    #[must_use]
    pub fn with_endpoint(mut self, endpoint: impl Into<String>) -> Self {
        self.endpoint_override = Some(endpoint.into());
        self
    }

    /// Configures a custom [`reqwest::Client`].
    #[must_use]
    pub fn with_http_client(mut self, client: Client) -> Self {
        self.http_client = client;
        self
    }

    /// Configures a custom [`DPoPNonceCache`].
    #[must_use]
    pub fn with_nonce_cache(mut self, nonce_cache: DPoPNonceCache) -> Self {
        self.nonce_cache = nonce_cache;
        self
    }

    /// Returns a reference to the bound [`OAuthSession`].
    #[must_use]
    pub fn session(&self) -> &Arc<OAuthSession> {
        &self.session
    }

    /// Returns the subject DID of the repository owner.
    #[must_use]
    pub fn did(&self) -> &str {
        self.session.sub()
    }

    /// Resolves the active PDS endpoint URL string.
    ///
    /// # Errors
    /// Returns [`SkybaseError::Repo`] if neither an override nor session PDS endpoint is present.
    pub fn pds_endpoint(&self) -> Result<&str> {
        if let Some(ref ep) = self.endpoint_override {
            return Ok(ep.as_str());
        }
        self.session.pds_endpoint().ok_or_else(|| {
            SkybaseError::Repo("No PDS endpoint found in OAuthSession or client".into())
        })
    }

    /// Returns a reference to the bound [`AtprotoOAuthClient`], if present.
    #[must_use]
    pub fn oauth_client(&self) -> Option<&Arc<AtprotoOAuthClient>> {
        self.oauth_client.as_ref()
    }

    /// Returns a reference to the internal [`DPoPNonceCache`].
    #[must_use]
    pub fn nonce_cache(&self) -> &DPoPNonceCache {
        &self.nonce_cache
    }

    /// Returns a reference to the underlying [`reqwest::Client`].
    #[must_use]
    pub fn http_client(&self) -> &Client {
        &self.http_client
    }

    /// Creates a record in the sovereign PDS repository via `com.atproto.repo.createRecord`.
    ///
    /// # Arguments
    /// - `collection`: The collection NSID (e.g. `"app.bsky.feed.post"`).
    /// - `rkey`: Optional record key. If `None`, the PDS assigns a server-generated TID.
    /// - `record`: The serializable record payload.
    /// - `validate`: Whether the PDS should validate the record against its Lexicon schema.
    ///
    /// # Returns
    /// A [`CreateRecordResult`] containing the canonical `uri` and `cid`.
    ///
    /// # Errors
    /// Returns [`SkybaseError::Repo`] if validation fails or server rejects the request,
    /// [`SkybaseError::Network`] on transport errors, or [`SkybaseError::Serialization`] on JSON errors.
    pub async fn create_record<T: Serialize>(
        &self,
        collection: &str,
        rkey: Option<&str>,
        record: &T,
        validate: bool,
    ) -> Result<CreateRecordResult> {
        if let Some(rk) = rkey {
            validate_rkey(rk)?;
        }

        let did = self.did();
        let payload = CreateRecordRequest {
            repo: did,
            collection,
            rkey,
            validate,
            record,
            swap_commit: None,
        };

        let body_bytes = serde_json::to_vec(&payload).map_err(SkybaseError::Serialization)?;
        let url = self.build_xrpc_url("com.atproto.repo.createRecord")?;

        let resp = self
            .send_with_nonce_retry(Method::POST, &url, Some(body_bytes))
            .await?;

        let resp_bytes = read_bounded_bytes(resp, MAX_SUCCESS_BODY_BYTES).await?;

        let res_json: serde_json::Value =
            serde_json::from_slice(&resp_bytes).map_err(SkybaseError::Serialization)?;

        let uri = res_json
            .get("uri")
            .and_then(|v| v.as_str())
            .filter(|s| !s.trim().is_empty())
            .ok_or_else(|| {
                SkybaseError::Repo("Missing or empty 'uri' in createRecord response".into())
            })?
            .to_string();

        let cid = res_json
            .get("cid")
            .and_then(|v| v.as_str())
            .filter(|s| !s.trim().is_empty())
            .ok_or_else(|| {
                SkybaseError::Repo("Missing or empty 'cid' in createRecord response".into())
            })?
            .to_string();

        Ok(CreateRecordResult { uri, cid })
    }

    /// Deletes a record from the sovereign PDS repository via `com.atproto.repo.deleteRecord`.
    ///
    /// # Arguments
    /// - `collection`: The collection NSID (e.g. `"app.bsky.feed.post"`).
    /// - `rkey`: The record key to delete.
    ///
    /// # Errors
    /// Returns [`SkybaseError::Repo`] if `rkey` is invalid or server rejects deletion,
    /// [`SkybaseError::Network`] on transport failure.
    pub async fn delete_record(&self, collection: &str, rkey: &str) -> Result<()> {
        validate_rkey(rkey)?;

        let did = self.did();
        let payload = DeleteRecordRequest {
            repo: did,
            collection,
            rkey,
            swap_record: None,
            swap_commit: None,
        };

        let body_bytes = serde_json::to_vec(&payload).map_err(SkybaseError::Serialization)?;
        let url = self.build_xrpc_url("com.atproto.repo.deleteRecord")?;

        let _resp = self
            .send_with_nonce_retry(Method::POST, &url, Some(body_bytes))
            .await?;

        Ok(())
    }

    /// Writes or updates a record in the sovereign PDS repository via `com.atproto.repo.putRecord`.
    ///
    /// # Arguments
    /// - `collection`: The collection NSID (e.g. `"social.skybouncer.config"`).
    /// - `rkey`: The record key (e.g. `"self"`).
    /// - `record`: The serializable record payload.
    /// - `validate`: Whether the PDS should validate the record against its Lexicon schema.
    ///
    /// # Returns
    /// A [`PutRecordResult`] containing the canonical `uri` and `cid`.
    ///
    /// # Errors
    /// Returns [`SkybaseError::Repo`] if validation fails or server rejects the request,
    /// [`SkybaseError::Network`] on transport errors, or [`SkybaseError::Serialization`] on JSON errors.
    pub async fn put_record<T: Serialize>(
        &self,
        collection: &str,
        rkey: &str,
        record: &T,
        validate: bool,
    ) -> Result<PutRecordResult> {
        validate_rkey(rkey)?;

        let did = self.did();
        let payload = PutRecordRequest {
            repo: did,
            collection,
            rkey,
            validate,
            record,
            swap_record: None,
            swap_commit: None,
        };

        let body_bytes = serde_json::to_vec(&payload).map_err(SkybaseError::Serialization)?;
        let url = self.build_xrpc_url("com.atproto.repo.putRecord")?;

        let resp = self
            .send_with_nonce_retry(Method::POST, &url, Some(body_bytes))
            .await?;

        let resp_bytes = read_bounded_bytes(resp, MAX_SUCCESS_BODY_BYTES).await?;

        let res_json: serde_json::Value =
            serde_json::from_slice(&resp_bytes).map_err(SkybaseError::Serialization)?;

        let uri = res_json
            .get("uri")
            .and_then(|v| v.as_str())
            .filter(|s| !s.trim().is_empty())
            .ok_or_else(|| {
                SkybaseError::Repo("Missing or empty 'uri' in putRecord response".into())
            })?
            .to_string();

        let cid = res_json
            .get("cid")
            .and_then(|v| v.as_str())
            .filter(|s| !s.trim().is_empty())
            .ok_or_else(|| {
                SkybaseError::Repo("Missing or empty 'cid' in putRecord response".into())
            })?
            .to_string();

        Ok(PutRecordResult { uri, cid })
    }

    /// Fetches a record from a repository via `com.atproto.repo.getRecord`.
    ///
    /// # Arguments
    /// - `repo`: The repository DID or handle.
    /// - `collection`: The collection NSID.
    /// - `rkey`: The record key.
    ///
    /// # Returns
    /// A [`RecordView`] containing the canonical `uri`, `cid`, and JSON `value`.
    ///
    /// # Errors
    /// Returns [`SkybaseError::Repo`] if the record is not found or request fails,
    /// [`SkybaseError::Network`] on transport errors, or [`SkybaseError::Serialization`] on JSON errors.
    pub async fn get_record(&self, repo: &str, collection: &str, rkey: &str) -> Result<RecordView> {
        validate_rkey(rkey)?;

        let url_str = self.build_xrpc_url("com.atproto.repo.getRecord")?;
        let mut parsed_url = Url::parse(&url_str)
            .map_err(|e| SkybaseError::Repo(format!("Invalid URL '{url_str}': {e}")))?;
        parsed_url
            .query_pairs_mut()
            .append_pair("repo", repo)
            .append_pair("collection", collection)
            .append_pair("rkey", rkey);

        let resp = self
            .send_with_nonce_retry(Method::GET, parsed_url.as_str(), None)
            .await?;

        let resp_bytes = read_bounded_bytes(resp, MAX_SUCCESS_BODY_BYTES).await?;

        let record_view: RecordView =
            serde_json::from_slice(&resp_bytes).map_err(SkybaseError::Serialization)?;

        Ok(record_view)
    }

    /// Uploads a binary blob to the sovereign PDS repository via `com.atproto.repo.uploadBlob`.
    ///
    /// # Arguments
    /// - `mime_type`: The MIME content type of the blob (e.g. `"image/png"`).
    /// - `bytes`: Raw binary payload of the blob.
    ///
    /// # Returns
    /// The JSON `blob` descriptor returned by the PDS (`{ "$type": "blob", "ref": ..., "mimeType": ..., "size": ... }`).
    ///
    /// # Errors
    /// Returns [`SkybaseError::Repo`] if the response omits the `blob` object,
    /// [`SkybaseError::Network`] on transport failure, or [`SkybaseError::Serialization`] on JSON errors.
    pub async fn upload_blob(&self, mime_type: &str, bytes: Vec<u8>) -> Result<serde_json::Value> {
        let url = self.build_xrpc_url("com.atproto.repo.uploadBlob")?;
        let resp = self
            .send_with_nonce_retry_content_type(Method::POST, &url, mime_type, Some(bytes))
            .await?;

        let resp_bytes = read_bounded_bytes(resp, MAX_SUCCESS_BODY_BYTES).await?;
        let res_json: serde_json::Value =
            serde_json::from_slice(&resp_bytes).map_err(SkybaseError::Serialization)?;

        res_json
            .get("blob")
            .cloned()
            .ok_or_else(|| SkybaseError::Repo("Missing 'blob' in uploadBlob response".into()))
    }

    /// Constructs the full target XRPC URL for a given NSID method.
    fn build_xrpc_url(&self, nsid: &str) -> Result<String> {
        let endpoint = self.pds_endpoint()?;
        let trimmed_endpoint = endpoint.trim_end_matches('/');
        let mut url = Url::parse(trimmed_endpoint).map_err(|e| {
            SkybaseError::Repo(format!(
                "Invalid PDS endpoint URL '{trimmed_endpoint}': {e}"
            ))
        })?;

        let base_path = url.path().trim_end_matches('/');
        let full_path = if base_path.is_empty() {
            format!("/xrpc/{nsid}")
        } else {
            format!("{base_path}/xrpc/{nsid}")
        };
        url.set_path(&full_path);

        Ok(url.to_string())
    }

    /// Executes an HTTP request against a PDS endpoint with DPoP signing and transparent
    /// single-retry challenge recovery.
    ///
    /// # Safety & Invariants
    /// - Max 1 retry: Exactly two attempts maximum (`attempts <= 2`).
    /// - Nonce caching: Cached nonces are refreshed on any response carrying `DPoP-Nonce`.
    /// - Lock safety: No locks are held across `.await` points.
    /// - Bounded reads: Error bodies are bounded to 64 KB.
    async fn send_with_nonce_retry(
        &self,
        method: Method,
        url_str: &str,
        body_bytes: Option<Vec<u8>>,
    ) -> Result<reqwest::Response> {
        self.send_with_nonce_retry_content_type(method, url_str, "application/json", body_bytes)
            .await
    }

    async fn send_with_nonce_retry_content_type(
        &self,
        method: Method,
        url_str: &str,
        content_type: &str,
        body_bytes: Option<Vec<u8>>,
    ) -> Result<reqwest::Response> {
        if self.session.is_expired() {
            let now = std::time::SystemTime::now()
                .duration_since(std::time::UNIX_EPOCH)
                .map_or(0, |d| d.as_secs());
            let exp = self
                .session
                .expires_at()
                .and_then(|t| t.duration_since(std::time::UNIX_EPOCH).ok())
                .map_or(now, |d| d.as_secs());
            return Err(SkybaseError::Auth(
                skyauth::error::TokenError::Expired { exp, now }.into(),
            ));
        }

        let parsed_url = Url::parse(url_str)
            .map_err(|e| SkybaseError::Repo(format!("Invalid request URL '{url_str}': {e}")))?;
        let server_origin = parsed_url.origin().ascii_serialization();
        let htu = normalize_htu(url_str).map_err(SkybaseError::from)?;

        let mut attempts: u32 = 0;

        loop {
            attempts = attempts.saturating_add(1);

            // 1. Retrieve current cached nonce for origin
            let current_nonce = self.nonce_cache.get_nonce(&server_origin);

            // 2. Generate signed DPoP proof bound to session's access token hash (ath)
            let dpop_proof = self
                .session
                .create_dpop_proof(method.as_str(), &htu, current_nonce.as_deref())
                .map_err(SkybaseError::from)?;

            // 3. Build HTTP request
            let mut req = self
                .http_client
                .request(method.clone(), parsed_url.clone())
                .header("Authorization", self.session.dpop_auth_header())
                .header("DPoP", dpop_proof)
                .header("Content-Type", content_type)
                .header("Accept", "application/json");

            if let Some(ref bytes) = body_bytes {
                req = req.body(bytes.clone());
            }

            // 4. Dispatch request
            let resp = req.send().await.map_err(SkybaseError::Network)?;

            // 5. Opportunistically record fresh nonce from response headers (if present)
            let new_nonce = resp
                .headers()
                .get("DPoP-Nonce")
                .or_else(|| resp.headers().get("dpop-nonce"))
                .and_then(|h| h.to_str().ok())
                .and_then(|val| extract_dpop_nonce(Some(val)));

            if let Some(ref nonce_val) = new_nonce {
                self.nonce_cache
                    .set_nonce(&server_origin, nonce_val.clone());
            }

            let status = resp.status();

            // 6. Fast-path: Successful response (2xx)
            if status.is_success() {
                return Ok(resp);
            }

            // 7. Nonce Challenge Detection (HTTP 401 or HTTP 400)
            if status == reqwest::StatusCode::UNAUTHORIZED
                || status == reqwest::StatusCode::BAD_REQUEST
            {
                let headers = resp.headers().clone();
                let is_challenge_header = is_rs_dpop_nonce_challenge(&headers);

                // If header doesn't explicitly confirm, buffer body to check JSON error field
                let (is_challenge, err_bytes) = if is_challenge_header {
                    (true, Vec::new())
                } else {
                    let err_bytes = read_error_bytes_bounded(resp, MAX_ERROR_BODY_BYTES).await;
                    let json_val: Option<serde_json::Value> =
                        serde_json::from_slice(&err_bytes).ok();
                    let is_error_field = is_use_dpop_nonce_error(json_val.as_ref());
                    (is_error_field, err_bytes)
                };

                if is_challenge {
                    if attempts >= 2 {
                        return Err(SkybaseError::Repo(
                            "DPoP nonce challenge retry limit exceeded (max 1 retry)".into(),
                        ));
                    }

                    // Verify the challenge response carried a fresh nonce for the retry
                    if new_nonce.is_none() {
                        return Err(SkybaseError::Repo(
                            "PDS issued use_dpop_nonce challenge but omitted DPoP-Nonce header"
                                .into(),
                        ));
                    }

                    tracing::debug!(
                        origin = %server_origin,
                        attempt = attempts,
                        "Received DPoP nonce challenge; retrying with fresh nonce"
                    );
                    continue;
                }

                // Not a DPoP nonce challenge
                let err_msg = format_xrpc_error(status, &err_bytes);
                return Err(SkybaseError::Repo(err_msg));
            }

            // 8. Other HTTP failure statuses (403, 404, 500, 502, etc.)
            let err_bytes = read_error_bytes_bounded(resp, MAX_ERROR_BODY_BYTES).await;
            let err_msg = format_xrpc_error(status, &err_bytes);
            return Err(SkybaseError::Repo(err_msg));
        }
    }
}

/// Checks whether a Resource Server response signals an explicit `use_dpop_nonce`
/// challenge via `WWW-Authenticate: DPoP error="use_dpop_nonce"` (RFC 9449 § 8.4).
fn is_rs_dpop_nonce_challenge(headers: &reqwest::header::HeaderMap) -> bool {
    headers
        .get_all(reqwest::header::WWW_AUTHENTICATE)
        .iter()
        .filter_map(|v| v.to_str().ok())
        .any(|challenge| {
            let challenge = challenge.trim();
            let Some(space) = challenge.find(' ') else {
                return false;
            };
            let (scheme, rest) = challenge.split_at(space);
            if !scheme.eq_ignore_ascii_case("DPoP") {
                return false;
            }
            let rest = rest.trim_start();
            rest.split(',').any(|param| {
                let param = param.trim();
                let Some((key, value)) = param.split_once('=') else {
                    return false;
                };
                if !key.trim().eq_ignore_ascii_case("error") {
                    return false;
                }
                let value = value.trim().trim_matches('"');
                value.eq_ignore_ascii_case("use_dpop_nonce")
            })
        })
}

/// Checks whether a parsed JSON error body contains `"error": "use_dpop_nonce"`.
fn is_use_dpop_nonce_error(json: Option<&serde_json::Value>) -> bool {
    json.and_then(|j| j.get("error"))
        .and_then(|e| e.as_str())
        .is_some_and(|err| err.eq_ignore_ascii_case("use_dpop_nonce"))
}

/// Reads an HTTP response body incrementally chunk-by-chunk up to `max_bytes`.
///
/// # Errors
/// Returns [`SkybaseError::Repo`] if the payload exceeds `max_bytes`, or
/// [`SkybaseError::Network`] on transport failure.
async fn read_bounded_bytes(mut resp: reqwest::Response, max_bytes: usize) -> Result<Vec<u8>> {
    let mut buffer = Vec::new();
    while let Some(chunk) = resp.chunk().await.map_err(SkybaseError::Network)? {
        if buffer.len().saturating_add(chunk.len()) > max_bytes {
            return Err(SkybaseError::Repo(format!(
                "HTTP response body exceeded maximum limit of {max_bytes} bytes"
            )));
        }
        buffer.extend_from_slice(&chunk);
    }
    Ok(buffer)
}

/// Reads an HTTP error response body incrementally chunk-by-chunk up to `max_bytes` without failing on truncation.
async fn read_error_bytes_bounded(mut resp: reqwest::Response, max_bytes: usize) -> Vec<u8> {
    let mut buffer = Vec::new();
    while let Ok(Some(chunk)) = resp.chunk().await {
        if buffer.len().saturating_add(chunk.len()) > max_bytes {
            let remaining = max_bytes.saturating_sub(buffer.len());
            buffer.extend_from_slice(&chunk[..remaining]);
            break;
        }
        buffer.extend_from_slice(&chunk);
    }
    buffer
}

/// Formats an XRPC or HTTP error payload into a readable error message.
fn format_xrpc_error(status: reqwest::StatusCode, bytes: &[u8]) -> String {
    if let Ok(xrpc_err) = serde_json::from_slice::<XrpcErrorResponse>(bytes) {
        let code = xrpc_err.error.unwrap_or_else(|| status.to_string());
        let msg = xrpc_err
            .message
            .unwrap_or_else(|| "Unknown error".to_string());
        format!("PDS request failed with status {status}: {code} - {msg}")
    } else {
        let text = String::from_utf8_lossy(bytes);
        let trimmed = text.trim();
        if trimmed.is_empty() {
            format!("HTTP request failed with status: {status}")
        } else {
            format!("HTTP request failed with status {status}: {trimmed}")
        }
    }
}

#[cfg(test)]
#[allow(clippy::unwrap_used, clippy::expect_used, clippy::panic, missing_docs)]
mod tests {
    use super::*;
    use reqwest::header::{HeaderMap, HeaderValue, WWW_AUTHENTICATE};
    use serde_json::json;

    #[test]
    fn test_client_from_credentials() {
        let client = PdsRepoClient::from_credentials(
            "https://pds.example.com",
            "did:plc:alice",
            "access_token_123",
        )
        .expect("client creation from credentials should succeed");

        assert_eq!(client.did(), "did:plc:alice");
        assert_eq!(
            client.pds_endpoint().expect("endpoint should be present"),
            "https://pds.example.com"
        );
        assert_eq!(client.session().dpop_auth_header(), "DPoP access_token_123");
    }

    #[test]
    fn test_client_missing_endpoint_fails_closed() {
        let session = OAuthSession::new(
            "did:plc:alice",
            "token",
            None,
            "DPoP",
            None,
            Some(3600),
            skyauth::dpop::DPoPKey::generate(),
            None, // No endpoint
            None,
            None,
        )
        .expect("session creation failed");

        let client = PdsRepoClient::from_session(Arc::new(session)).expect("client creation");
        assert!(client.pds_endpoint().is_err());
    }

    #[test]
    fn test_client_endpoint_override() {
        let client =
            PdsRepoClient::from_credentials("https://pds.example.com", "did:plc:alice", "token")
                .expect("client creation failed")
                .with_endpoint("https://custom-pds.example.com");

        assert_eq!(
            client.pds_endpoint().expect("overridden endpoint"),
            "https://custom-pds.example.com"
        );
    }

    #[test]
    fn test_is_rs_dpop_nonce_challenge_detection() {
        let mut headers = HeaderMap::new();
        headers.insert(
            WWW_AUTHENTICATE,
            HeaderValue::from_static(
                "DPoP error=\"use_dpop_nonce\", error_description=\"Nonce expired\"",
            ),
        );
        assert!(is_rs_dpop_nonce_challenge(&headers));

        let mut headers_other = HeaderMap::new();
        headers_other.insert(
            WWW_AUTHENTICATE,
            HeaderValue::from_static("Bearer error=\"invalid_token\""),
        );
        assert!(!is_rs_dpop_nonce_challenge(&headers_other));

        let headers_empty = HeaderMap::new();
        assert!(!is_rs_dpop_nonce_challenge(&headers_empty));
    }

    #[test]
    fn test_is_use_dpop_nonce_error_detection() {
        let json_nonce = json!({
            "error": "use_dpop_nonce",
            "message": "DPoP proof requires nonce"
        });
        assert!(is_use_dpop_nonce_error(Some(&json_nonce)));

        let json_invalid = json!({
            "error": "InvalidRequest",
            "message": "Missing field"
        });
        assert!(!is_use_dpop_nonce_error(Some(&json_invalid)));

        assert!(!is_use_dpop_nonce_error(None));
    }

    #[tokio::test]
    async fn test_client_create_record_network_roundtrip() {
        use wiremock::matchers::{header_exists, method, path};
        use wiremock::{Mock, MockServer, ResponseTemplate};

        let server = MockServer::start().await;

        Mock::given(method("POST"))
            .and(path("/xrpc/com.atproto.repo.createRecord"))
            .and(header_exists("authorization"))
            .and(header_exists("dpop"))
            .respond_with(ResponseTemplate::new(200).set_body_json(json!({
                "uri": "at://did:plc:test_user/app.bsky.feed.post/post_1",
                "cid": "bafyrei_test_cid_1"
            })))
            .mount(&server)
            .await;

        let client =
            PdsRepoClient::from_credentials(server.uri(), "did:plc:test_user", "test_access_token")
                .expect("client creation failed");

        let res = client
            .create_record(
                "app.bsky.feed.post",
                Some("post_1"),
                &json!({ "text": "Hello, world!" }),
                true,
            )
            .await
            .expect("create_record should succeed");

        assert_eq!(res.uri, "at://did:plc:test_user/app.bsky.feed.post/post_1");
        assert_eq!(res.cid, "bafyrei_test_cid_1");
    }

    #[tokio::test]
    async fn test_client_delete_record_network_roundtrip() {
        use wiremock::matchers::{header_exists, method, path};
        use wiremock::{Mock, MockServer, ResponseTemplate};

        let server = MockServer::start().await;

        Mock::given(method("POST"))
            .and(path("/xrpc/com.atproto.repo.deleteRecord"))
            .and(header_exists("authorization"))
            .and(header_exists("dpop"))
            .respond_with(ResponseTemplate::new(200).set_body_json(json!({})))
            .mount(&server)
            .await;

        let client =
            PdsRepoClient::from_credentials(server.uri(), "did:plc:test_user", "test_access_token")
                .expect("client creation failed");

        let res = client
            .delete_record("app.bsky.feed.post", "post_to_delete")
            .await;
        assert!(res.is_ok(), "delete_record should succeed");
    }

    #[tokio::test]
    async fn test_client_dpop_nonce_retry_roundtrip() {
        use wiremock::matchers::{method, path};
        use wiremock::{Mock, MockServer, ResponseTemplate};

        let server = MockServer::start().await;

        // Mount 1-time 401 challenge
        Mock::given(method("POST"))
            .and(path("/xrpc/com.atproto.repo.createRecord"))
            .respond_with(
                ResponseTemplate::new(401)
                    .insert_header("DPoP-Nonce", "fresh_nonce_123")
                    .set_body_json(json!({
                        "error": "use_dpop_nonce",
                        "message": "DPoP proof requires nonce"
                    })),
            )
            .up_to_n_times(1)
            .mount(&server)
            .await;

        // Default 200 response
        Mock::given(method("POST"))
            .and(path("/xrpc/com.atproto.repo.createRecord"))
            .respond_with(ResponseTemplate::new(200).set_body_json(json!({
                "uri": "at://did:plc:alice/app.bsky.feed.post/retried_post",
                "cid": "bafyrei_retried_cid"
            })))
            .mount(&server)
            .await;

        let client = PdsRepoClient::from_credentials(server.uri(), "did:plc:alice", "token_xyz")
            .expect("client creation failed");

        let res = client
            .create_record(
                "app.bsky.feed.post",
                Some("retried_post"),
                &json!({ "text": "Retried successfully" }),
                true,
            )
            .await
            .expect("should transparently succeed on nonce retry");

        assert_eq!(
            res.uri,
            "at://did:plc:alice/app.bsky.feed.post/retried_post"
        );
        assert_eq!(res.cid, "bafyrei_retried_cid");

        // Verify that the fresh nonce is now cached
        let origin = url::Url::parse(&server.uri())
            .unwrap()
            .origin()
            .ascii_serialization();
        assert_eq!(
            client.nonce_cache().get_nonce(&origin).as_deref(),
            Some("fresh_nonce_123")
        );
    }

    #[tokio::test]
    async fn test_client_dpop_nonce_retry_limit_exceeded() {
        use wiremock::matchers::{method, path};
        use wiremock::{Mock, MockServer, ResponseTemplate};

        let server = MockServer::start().await;

        // Always challenge with 401 use_dpop_nonce
        Mock::given(method("POST"))
            .and(path("/xrpc/com.atproto.repo.createRecord"))
            .respond_with(
                ResponseTemplate::new(401)
                    .insert_header("DPoP-Nonce", "infinite_nonce")
                    .set_body_json(json!({
                        "error": "use_dpop_nonce",
                        "message": "Always challenging"
                    })),
            )
            .mount(&server)
            .await;

        let client = PdsRepoClient::from_credentials(server.uri(), "did:plc:alice", "token_xyz")
            .expect("client creation failed");

        let res = client
            .create_record(
                "app.bsky.feed.post",
                Some("p1"),
                &json!({ "text": "Infinite loop test" }),
                true,
            )
            .await;

        assert!(res.is_err());
        let err_str = res.unwrap_err().to_string();
        assert!(
            err_str.contains("retry limit exceeded"),
            "Error must indicate retry limit exceeded, got: {err_str}"
        );
    }

    #[tokio::test]
    async fn test_client_dpop_nonce_missing_header_fails() {
        use wiremock::matchers::{method, path};
        use wiremock::{Mock, MockServer, ResponseTemplate};

        let server = MockServer::start().await;

        // Challenge with use_dpop_nonce but omit DPoP-Nonce header
        Mock::given(method("POST"))
            .and(path("/xrpc/com.atproto.repo.createRecord"))
            .respond_with(ResponseTemplate::new(401).set_body_json(json!({
                "error": "use_dpop_nonce",
                "message": "Requires nonce but forgot header"
            })))
            .mount(&server)
            .await;

        let client = PdsRepoClient::from_credentials(server.uri(), "did:plc:alice", "token_xyz")
            .expect("client creation failed");

        let res = client
            .create_record(
                "app.bsky.feed.post",
                Some("p1"),
                &json!({ "text": "Missing header test" }),
                true,
            )
            .await;

        assert!(res.is_err());
        let err_str = res.unwrap_err().to_string();
        assert!(
            err_str.contains("omitted DPoP-Nonce header"),
            "Error must indicate missing nonce header, got: {err_str}"
        );
    }

    #[tokio::test]
    async fn test_client_non_challenge_401_fails_immediately() {
        use wiremock::matchers::{method, path};
        use wiremock::{Mock, MockServer, ResponseTemplate};

        let server = MockServer::start().await;

        Mock::given(method("POST"))
            .and(path("/xrpc/com.atproto.repo.createRecord"))
            .respond_with(ResponseTemplate::new(401).set_body_json(json!({
                "error": "invalid_token",
                "message": "Token expired or revoked"
            })))
            .mount(&server)
            .await;

        let client = PdsRepoClient::from_credentials(server.uri(), "did:plc:alice", "token_xyz")
            .expect("client creation failed");

        let res = client
            .create_record(
                "app.bsky.feed.post",
                Some("p1"),
                &json!({ "text": "Non-challenge test" }),
                true,
            )
            .await;

        assert!(res.is_err());
        let err_str = res.unwrap_err().to_string();
        assert!(
            err_str.contains("invalid_token"),
            "Error must contain invalid_token, got: {err_str}"
        );
    }

    #[tokio::test]
    async fn test_client_xrpc_error_response_formatting() {
        use wiremock::matchers::{method, path};
        use wiremock::{Mock, MockServer, ResponseTemplate};

        let server = MockServer::start().await;

        Mock::given(method("POST"))
            .and(path("/xrpc/com.atproto.repo.createRecord"))
            .respond_with(ResponseTemplate::new(400).set_body_json(json!({
                "error": "InvalidRequest",
                "message": "Schema validation failed: missing field 'text'"
            })))
            .mount(&server)
            .await;

        let client = PdsRepoClient::from_credentials(server.uri(), "did:plc:alice", "token_xyz")
            .expect("client creation failed");

        let res = client
            .create_record("app.bsky.feed.post", Some("p1"), &json!({}), true)
            .await;

        assert!(res.is_err());
        let err_str = res.unwrap_err().to_string();
        assert!(
            err_str.contains("InvalidRequest - Schema validation failed"),
            "Error must format XRPC error code and message, got: {err_str}"
        );
    }

    #[tokio::test]
    async fn test_client_invalid_rkey_preflight_rejection() {
        let client = PdsRepoClient::from_credentials(
            "https://pds.example.com",
            "did:plc:alice",
            "token_xyz",
        )
        .expect("client creation failed");

        let create_res = client
            .create_record(
                "app.bsky.feed.post",
                Some("invalid..key"),
                &json!({ "text": "bad rkey" }),
                true,
            )
            .await;
        assert!(create_res.is_err());

        let delete_res = client
            .delete_record("app.bsky.feed.post", "invalid/slash")
            .await;
        assert!(delete_res.is_err());
    }

    #[tokio::test]
    async fn test_client_create_record_rejects_empty_uri_or_cid() {
        use wiremock::matchers::{method, path};
        use wiremock::{Mock, MockServer, ResponseTemplate};

        let server = MockServer::start().await;

        Mock::given(method("POST"))
            .and(path("/xrpc/com.atproto.repo.createRecord"))
            .respond_with(ResponseTemplate::new(200).set_body_json(json!({
                "uri": "   ",
                "cid": "bafyrei_valid_cid"
            })))
            .mount(&server)
            .await;

        let client = PdsRepoClient::from_credentials(server.uri(), "did:plc:alice", "token_xyz")
            .expect("client creation failed");

        let res = client
            .create_record("app.bsky.feed.post", None, &json!({ "text": "hi" }), true)
            .await;
        assert!(res.is_err());
        assert!(res
            .unwrap_err()
            .to_string()
            .contains("Missing or empty 'uri'"));
    }

    #[tokio::test]
    async fn test_client_create_record_rejects_oversized_response_body() {
        use wiremock::matchers::{method, path};
        use wiremock::{Mock, MockServer, ResponseTemplate};

        let server = MockServer::start().await;

        // Construct a response body > 1MB (MAX_SUCCESS_BODY_BYTES)
        let huge_text = "x".repeat(MAX_SUCCESS_BODY_BYTES + 1024);
        let huge_body = format!(
            r#"{{"uri":"at://did:plc:alice/app.bsky.feed.post/1","cid":"bafy","extra":"{huge_text}"}}"#
        );

        Mock::given(method("POST"))
            .and(path("/xrpc/com.atproto.repo.createRecord"))
            .respond_with(ResponseTemplate::new(200).set_body_raw(huge_body, "application/json"))
            .mount(&server)
            .await;

        let client = PdsRepoClient::from_credentials(server.uri(), "did:plc:alice", "token_xyz")
            .expect("client creation failed");

        let res = client
            .create_record("app.bsky.feed.post", None, &json!({ "text": "hi" }), true)
            .await;
        assert!(res.is_err());
        assert!(res
            .unwrap_err()
            .to_string()
            .contains("exceeded maximum limit"));
    }

    #[test]
    fn test_client_try_constructors() {
        let session = Arc::new(
            OAuthSession::new(
                "did:plc:alice",
                "token",
                None,
                "DPoP",
                None,
                Some(3600),
                skyauth::dpop::DPoPKey::generate(),
                Some("https://pds.example.com".into()),
                None,
                None,
            )
            .expect("session creation failed"),
        );

        let client = PdsRepoClient::try_from_session(session)
            .expect("try_from_session should succeed with valid parameters");
        assert_eq!(client.did(), "did:plc:alice");
    }
}