kryphocron 0.1.0

Privacy-first ATProto substrate primitives: type architecture, audit vocabulary, inter-service auth, and encryption hook surfaces
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
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.

//! §4.3 / §4.4 capability subject types.
//!
//! Subject types per capability class:
//!
//! - User-class: [`ResourceId`] (typically; `ManageAudience`
//!   takes `(ResourceId, AudienceListId)`).
//! - Endpoint (channel-class): [`ChannelBinding`].
//! - SubstrateScope (substrate-class): [`ScopeSelector`].
//! - ModerationCapability: `(ResourceId, ModerationCaseId)`.

use core::marker::PhantomData;
use std::time::SystemTime;

use thiserror::Error;

use crate::identity::SessionId;
use crate::proto::{AtUri, Did, Nsid, Rkey};
use crate::sealed;

// ============================================================
// HasResourceLocation — sealed trait for subjects carrying an
// (owner DID, lexicon NSID) pair (§4.3 / §4.4).
// ============================================================

/// Subjects whose representation includes a resource location
/// (owner [`Did`] + lexicon [`Nsid`]). Implemented by user-class
/// subjects and the inner [`ResourceId`]-bearing moderation
/// subject; not implemented for channel- / substrate-class
/// subjects (which carry no NSID at all).
///
/// Sealed: only crate-ship subject types implement it. The trait
/// powers two §4.3 bind-time concerns:
///
/// 1. **Stage 0 — lexicon-deprecation gate.** The bind path
///    extracts the NSID via [`Self::resource_nsid`] and consults
///    [`crate::KRYPHOCRON_LEXICON_REGISTRY`] (§5.6).
/// 2. **Audit-event construction.** Bind emits
///    [`crate::audit::UserAuditEvent::CapabilityBound`] /
///    [`crate::audit::ModerationAuditEvent::ModeratorInspected`]
///    et al. with `subject_repr` /
///    `target_repr: TargetRepresentation::structural_only(StructuralRepresentation::Resource { did, nsid })`,
///    which needs both pieces.
///
/// **Sealed** via a crate-private supertrait — external types
/// cannot impl this trait.
pub trait HasResourceLocation: sealed::Sealed {
    /// Borrow the owner DID of the resource this subject names.
    fn resource_did(&self) -> &Did;
    /// Borrow the lexicon NSID of the resource this subject
    /// names.
    fn resource_nsid(&self) -> &Nsid;
}

/// Parsed, canonicalized record reference (§4.4).
///
/// Fields are private; construction validates per-component
/// canonicalization. URI-normalization attacks are foreclosed
/// by the type's invariants — equality checks compare canonical
/// forms, not raw input strings.
///
/// ```compile_fail
/// // Outside the crate, struct-literal construction fails: the
/// // `_private` PhantomData<sealed::Token> field cannot be
/// // named because `sealed::Token` is not pub-visible.
/// use kryphocron::ResourceId;
/// let _r = ResourceId {
///     did: kryphocron::Did::new("did:plc:x").unwrap(),
///     nsid: kryphocron::Nsid::new("a.b.c").unwrap(),
///     rkey: kryphocron::Rkey::new("r").unwrap(),
///     _private: std::marker::PhantomData,
/// };
/// ```
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct ResourceId {
    did: Did,
    nsid: Nsid,
    rkey: Rkey,
    _private: PhantomData<sealed::Token>,
}

impl ResourceId {
    /// Construct a [`ResourceId`] from canonicalized parts.
    ///
    /// v0.1 ships the constructor shape; a future release layers
    /// in the proto-blue canonicalization step that rejects
    /// DIDs / NSIDs / rkeys outside the lexicon-validated grammar.
    #[must_use]
    pub fn new(did: Did, nsid: Nsid, rkey: Rkey) -> Self {
        ResourceId {
            did,
            nsid,
            rkey,
            _private: PhantomData,
        }
    }

    /// Borrow the DID.
    #[must_use]
    pub fn did(&self) -> &Did {
        &self.did
    }

    /// Borrow the NSID.
    #[must_use]
    pub fn nsid(&self) -> &Nsid {
        &self.nsid
    }

    /// Borrow the record key.
    #[must_use]
    pub fn rkey(&self) -> &Rkey {
        &self.rkey
    }
}

impl sealed::Sealed for ResourceId {}
impl HasResourceLocation for ResourceId {
    fn resource_did(&self) -> &Did {
        &self.did
    }
    fn resource_nsid(&self) -> &Nsid {
        &self.nsid
    }
}

/// Audience-list reference (§4.3 ManageAudience subject side).
///
/// v0.1 stores an [`AtUri`]; a future lexicon pass may constrain
/// the URI shape further.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct AudienceListId(AtUri);

impl AudienceListId {
    /// Construct an [`AudienceListId`].
    #[must_use]
    pub fn new(uri: AtUri) -> Self {
        AudienceListId(uri)
    }

    /// Borrow the underlying URI.
    #[must_use]
    pub fn uri(&self) -> &AtUri {
        &self.0
    }
}

/// Channel-class subject: peer + session (§4.3 ChannelBinding).
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct ChannelBinding {
    /// The peer service identity.
    pub peer: crate::identity::ServiceIdentity,
    /// Session identifier issued at handshake.
    pub session_id: SessionId,
}

/// 8-byte shard identifier.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct ShardId([u8; 8]);

impl ShardId {
    /// Construct a [`ShardId`].
    #[must_use]
    pub const fn from_bytes(bytes: [u8; 8]) -> Self {
        ShardId(bytes)
    }

    /// Borrow the underlying bytes.
    #[must_use]
    pub const fn as_bytes(&self) -> &[u8; 8] {
        &self.0
    }
}

/// Range over [`ShardId`]s (§4.3 substrate-class subjects).
///
/// Rejects empty or inverted ranges at construction — no empty-
/// range no-op can be encoded.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct ShardRange {
    start: ShardId,
    end_exclusive: ShardId,
}

impl ShardRange {
    /// Construct a [`ShardRange`] with `start < end_exclusive`.
    pub fn new(start: ShardId, end_exclusive: ShardId) -> Result<Self, ScopeError> {
        if start >= end_exclusive {
            return Err(ScopeError::EmptyOrInvertedRange);
        }
        Ok(ShardRange { start, end_exclusive })
    }

    /// Borrow the inclusive start.
    #[must_use]
    pub fn start(&self) -> ShardId {
        self.start
    }

    /// Borrow the exclusive end.
    #[must_use]
    pub fn end_exclusive(&self) -> ShardId {
        self.end_exclusive
    }
}

/// `RecordState` filter for substrate-class garbage-collect
/// scopes (§4.3).
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum RecordStateFilter {
    /// All non-live records.
    AllNonLive,
    /// Tombstoned records only.
    TombstonedOnly,
    /// Taken-down records only.
    TakenDownOnly,
    /// Sealed records only.
    SealedOnly,
}

/// Time window for substrate-class garbage-collect scopes
/// (§4.3).
///
/// Empty or inverted windows rejected at construction.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct TimeWindow {
    start: SystemTime,
    end_exclusive: SystemTime,
}

impl TimeWindow {
    /// Construct a [`TimeWindow`] with `start < end_exclusive`.
    pub fn new(start: SystemTime, end_exclusive: SystemTime) -> Result<Self, ScopeError> {
        if start >= end_exclusive {
            return Err(ScopeError::EmptyOrInvertedRange);
        }
        Ok(TimeWindow { start, end_exclusive })
    }

    /// Borrow the inclusive start.
    #[must_use]
    pub fn start(&self) -> SystemTime {
        self.start
    }

    /// Borrow the exclusive end.
    #[must_use]
    pub fn end_exclusive(&self) -> SystemTime {
        self.end_exclusive
    }
}

/// Substrate-class subject selector (§4.3).
#[non_exhaustive]
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ScopeSelector {
    /// Shard scan over a half-open shard-id range.
    Shard(ShardRange),
    /// Garbage-collection scope over a record-state filter and a
    /// time window.
    GarbageCollect {
        /// Which record states to garbage-collect.
        state_filter: RecordStateFilter,
        /// Time window over which the scope applies.
        window: TimeWindow,
    },
    /// Replicate scope: a peer + a shard range to replicate.
    Replicate {
        /// Peer service identity.
        peer: crate::identity::ServiceIdentity,
        /// Shard range to replicate.
        shard: ShardRange,
    },
}

/// Subject errors at substrate-class scope construction (§4.3).
#[derive(Debug, Clone, PartialEq, Eq, Error)]
#[non_exhaustive]
pub enum ScopeError {
    /// Range was empty or inverted.
    #[error("empty or inverted range")]
    EmptyOrInvertedRange,
}

/// 16-byte moderation case identifier (§4.3).
///
/// Constructed via [`ModerationCaseId::from_bytes`] from a
/// caller-supplied 16-byte value (typically generated upstream
/// from the OS CSPRNG via `getrandom` — the v0.1 surface keeps
/// case-id generation operator-controlled rather than baking a
/// random-source choice into the crate).
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct ModerationCaseId([u8; 16]);

impl ModerationCaseId {
    /// Construct a [`ModerationCaseId`] from raw bytes.
    #[must_use]
    pub const fn from_bytes(bytes: [u8; 16]) -> Self {
        ModerationCaseId(bytes)
    }

    /// Borrow the underlying bytes.
    #[must_use]
    pub const fn as_bytes(&self) -> &[u8; 16] {
        &self.0
    }
}

/// Composite user-class subject for `ManageAudience` (§4.3).
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct ManageAudienceSubject {
    /// The resource whose audience is being managed.
    pub resource: ResourceId,
    /// Audience-list reference.
    pub audience_list: AudienceListId,
}

impl sealed::Sealed for ManageAudienceSubject {}
impl HasResourceLocation for ManageAudienceSubject {
    fn resource_did(&self) -> &Did {
        self.resource.did()
    }
    fn resource_nsid(&self) -> &Nsid {
        self.resource.nsid()
    }
}

/// Composite moderation-class subject (§4.3).
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct ModerationSubject {
    /// The resource (or principal) targeted by the moderation
    /// action.
    pub resource: ResourceId,
    /// Moderation case id.
    pub case: ModerationCaseId,
}

impl sealed::Sealed for ModerationSubject {}
impl HasResourceLocation for ModerationSubject {
    fn resource_did(&self) -> &Did {
        self.resource.did()
    }
    fn resource_nsid(&self) -> &Nsid {
        self.resource.nsid()
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn shard_range_rejects_empty() {
        let s = ShardId::from_bytes([0; 8]);
        assert!(matches!(
            ShardRange::new(s, s),
            Err(ScopeError::EmptyOrInvertedRange)
        ));
    }

    #[test]
    fn shard_range_rejects_inverted() {
        let lo = ShardId::from_bytes([0; 8]);
        let hi = ShardId::from_bytes([0xFF; 8]);
        assert!(ShardRange::new(lo, hi).is_ok());
        assert!(matches!(
            ShardRange::new(hi, lo),
            Err(ScopeError::EmptyOrInvertedRange)
        ));
    }

    /// §4.3 / §4.4: subjects with a resource location expose
    /// `did + nsid` via the sealed `HasResourceLocation` trait.
    /// ResourceId returns its own fields; ManageAudienceSubject
    /// and ModerationSubject forward to their inner ResourceId.
    #[test]
    fn has_resource_location_returns_did_and_nsid_for_all_three_impls() {
        let did = Did::new("did:plc:phase7dtest").unwrap();
        let nsid = Nsid::new("tools.kryphocron.feed.postPrivate").unwrap();
        let rkey = Rkey::new("3jzfcijpj2z2a").unwrap();
        let resource = ResourceId::new(did.clone(), nsid.clone(), rkey);

        // ResourceId: direct
        assert_eq!(resource.resource_did(), &did);
        assert_eq!(resource.resource_nsid(), &nsid);

        // ManageAudienceSubject: forwards to inner resource
        let mas = ManageAudienceSubject {
            resource: resource.clone(),
            audience_list: AudienceListId::new(
                AtUri::new("at://did:plc:x/tools.kryphocron.policy.audience/3jzfcijpj2z2a")
                    .unwrap(),
            ),
        };
        assert_eq!(mas.resource_did(), &did);
        assert_eq!(mas.resource_nsid(), &nsid);

        // ModerationSubject: forwards to inner resource
        let mod_subj = ModerationSubject {
            resource: resource.clone(),
            case: ModerationCaseId::from_bytes([0u8; 16]),
        };
        assert_eq!(mod_subj.resource_did(), &did);
        assert_eq!(mod_subj.resource_nsid(), &nsid);
    }

    #[test]
    fn resource_id_construction_does_not_expose_private_field() {
        // This test exists to verify the construction path is
        // accessible. The `_private: PhantomData<sealed::Token>`
        // field prevents struct-literal construction from outside
        // the crate; see the trybuild tests for that assertion.
        let r = ResourceId::new(
            Did::new("did:plc:example").unwrap(),
            Nsid::new("tools.kryphocron.feed.postPrivate").unwrap(),
            Rkey::new("3jzfcijpj2z2a").unwrap(),
        );
        assert_eq!(r.did().as_str(), "did:plc:example");
    }
}