auths-core 0.1.2

Core cryptography and keychain integration for Auths
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
//! Network port and DID resolution.

use std::future::Future;

use auths_keri::Prefix;
use auths_verifier::core::Ed25519PublicKey;

/// Domain error for outbound network operations.
///
/// Adapters map transport-specific failures (e.g., HTTP timeouts, connection
/// refused) into these variants. Domain logic never sees transport details.
///
/// Usage:
/// ```ignore
/// use auths_core::ports::network::NetworkError;
///
/// fn handle(err: NetworkError) {
///     match err {
///         NetworkError::Unreachable { endpoint } => eprintln!("cannot reach {endpoint}"),
///         NetworkError::Timeout { endpoint } => eprintln!("timed out: {endpoint}"),
///         NetworkError::NotFound { resource } => eprintln!("missing: {resource}"),
///         NetworkError::Unauthorized => eprintln!("not authorized"),
///         NetworkError::InvalidResponse { detail } => eprintln!("bad response: {detail}"),
///         NetworkError::Internal(inner) => eprintln!("bug: {inner}"),
///     }
/// }
/// ```
#[derive(Debug, thiserror::Error)]
#[non_exhaustive]
pub enum NetworkError {
    /// The endpoint could not be reached.
    #[error("endpoint unreachable: {endpoint}")]
    Unreachable {
        /// The unreachable endpoint URL.
        endpoint: String,
    },

    /// The request timed out.
    #[error("request timed out: {endpoint}")]
    Timeout {
        /// The endpoint that timed out.
        endpoint: String,
    },

    /// The requested resource was not found.
    #[error("resource not found: {resource}")]
    NotFound {
        /// The missing resource identifier.
        resource: String,
    },

    /// Authentication or authorisation failed.
    #[error("unauthorized")]
    Unauthorized,

    /// The server returned an unexpected response.
    #[error("invalid response: {detail}")]
    InvalidResponse {
        /// Details about the invalid response.
        detail: String,
    },

    /// An unexpected internal error.
    #[error("internal network error: {0}")]
    Internal(Box<dyn std::error::Error + Send + Sync>),
}

impl auths_crypto::AuthsErrorInfo for NetworkError {
    fn error_code(&self) -> &'static str {
        match self {
            Self::Unreachable { .. } => "AUTHS-E3601",
            Self::Timeout { .. } => "AUTHS-E3602",
            Self::NotFound { .. } => "AUTHS-E3603",
            Self::Unauthorized => "AUTHS-E3604",
            Self::InvalidResponse { .. } => "AUTHS-E3605",
            Self::Internal(_) => "AUTHS-E3606",
        }
    }

    fn suggestion(&self) -> Option<&'static str> {
        match self {
            Self::Unreachable { .. } => Some("Check your internet connection"),
            Self::Timeout { .. } => Some("The server may be overloaded — retry later"),
            Self::Unauthorized => Some("Check your authentication credentials"),
            Self::NotFound { .. } => Some(
                "The requested resource was not found on the server; verify the URL or identifier",
            ),
            Self::InvalidResponse { .. } => {
                Some("The server returned an unexpected response; check server compatibility")
            }
            Self::Internal(_) => Some(
                "The server encountered an internal error; retry later or contact the server administrator",
            ),
        }
    }
}

/// Domain error for identity resolution operations.
///
/// Distinguishes resolution-specific failures (unknown DID, revoked key)
/// from general transport failures via the `Network` variant.
///
/// Usage:
/// ```ignore
/// use auths_core::ports::network::ResolutionError;
///
/// fn handle(err: ResolutionError) {
///     match err {
///         ResolutionError::DidNotFound { did } => eprintln!("unknown: {did}"),
///         ResolutionError::InvalidDid { did, reason } => eprintln!("{did}: {reason}"),
///         ResolutionError::KeyRevoked { did } => eprintln!("revoked: {did}"),
///         ResolutionError::Network(inner) => eprintln!("transport: {inner}"),
///     }
/// }
/// ```
#[derive(Debug, thiserror::Error)]
#[non_exhaustive]
pub enum ResolutionError {
    /// The DID was not found.
    #[error("DID not found: {did}")]
    DidNotFound {
        /// The DID that was not found.
        did: String,
    },

    /// The DID is malformed.
    #[error("invalid DID {did}: {reason}")]
    InvalidDid {
        /// The malformed DID.
        did: String,
        /// Reason the DID is invalid.
        reason: String,
    },

    /// The key for this DID has been revoked.
    #[error("key revoked for DID: {did}")]
    KeyRevoked {
        /// The DID whose key was revoked.
        did: String,
    },

    /// A network error occurred during resolution.
    #[error("network error: {0}")]
    Network(#[from] NetworkError),
}

impl auths_crypto::AuthsErrorInfo for ResolutionError {
    fn error_code(&self) -> &'static str {
        match self {
            Self::DidNotFound { .. } => "AUTHS-E3701",
            Self::InvalidDid { .. } => "AUTHS-E3702",
            Self::KeyRevoked { .. } => "AUTHS-E3703",
            Self::Network(_) => "AUTHS-E3704",
        }
    }

    fn suggestion(&self) -> Option<&'static str> {
        match self {
            Self::DidNotFound { .. } => Some("Verify the DID is correct and the identity exists"),
            Self::InvalidDid { .. } => {
                Some("Check the DID format (e.g., did:key:z6Mk... or did:keri:E...)")
            }
            Self::KeyRevoked { .. } => {
                Some("This key has been revoked — contact the identity owner")
            }
            Self::Network(_) => Some("Check your internet connection"),
        }
    }
}

/// Cryptographic material resolved from a decentralized identifier.
///
/// Usage:
/// ```ignore
/// use auths_core::ports::network::ResolvedIdentity;
///
/// let identity: ResolvedIdentity = resolver.resolve_identity("did:key:z...").await?;
/// let pk = identity.public_key();
/// ```
#[derive(Debug, Clone)]
pub enum ResolvedIdentity {
    /// Static did:key (no rotation possible).
    Key {
        /// The resolved DID string.
        did: String,
        /// The public key.
        public_key: Ed25519PublicKey,
    },
    /// KERI-based identity with rotation capability.
    Keri {
        /// The resolved DID string.
        did: String,
        /// The public key.
        public_key: Ed25519PublicKey,
        /// Current KEL sequence number.
        sequence: u128,
        /// Whether key rotation is available.
        can_rotate: bool,
    },
}

impl ResolvedIdentity {
    /// Returns the DID string.
    pub fn did(&self) -> &str {
        match self {
            ResolvedIdentity::Key { did, .. } | ResolvedIdentity::Keri { did, .. } => did,
        }
    }

    /// Returns the public key.
    pub fn public_key(&self) -> &Ed25519PublicKey {
        match self {
            ResolvedIdentity::Key { public_key, .. }
            | ResolvedIdentity::Keri { public_key, .. } => public_key,
        }
    }

    /// Returns `true` if this is a `did:key` resolution.
    pub fn is_key(&self) -> bool {
        matches!(self, ResolvedIdentity::Key { .. })
    }

    /// Returns `true` if this is a `did:keri` resolution.
    pub fn is_keri(&self) -> bool {
        matches!(self, ResolvedIdentity::Keri { .. })
    }
}

/// Resolves a decentralized identifier to its current cryptographic material.
///
/// Implementations may fetch data from local stores, remote registries, or
/// peer-to-peer networks. The domain only provides a DID string and receives
/// the resolved key material.
///
/// Usage:
/// ```ignore
/// use auths_core::ports::network::IdentityResolver;
///
/// async fn verify_signer(resolver: &dyn IdentityResolver, did: &str) -> Vec<u8> {
///     let resolved = resolver.resolve_identity(did).await.unwrap();
///     resolved.public_key
/// }
/// ```
pub trait IdentityResolver: Send + Sync {
    /// Resolves a DID string to its current public key and method metadata.
    ///
    /// Args:
    /// * `did`: The decentralized identifier to resolve (e.g., `"did:keri:EAbcdef..."`).
    ///
    /// Usage:
    /// ```ignore
    /// let identity = resolver.resolve_identity("did:key:z6Mk...").await?;
    /// ```
    fn resolve_identity(
        &self,
        did: &str,
    ) -> impl Future<Output = Result<ResolvedIdentity, ResolutionError>> + Send;
}

/// Submits key events and queries receipts from the witness infrastructure.
///
/// Witnesses observe and receipt key events to provide accountability.
/// Implementations handle the transport details; the domain provides
/// serialized events and receives receipts as opaque byte arrays.
///
/// Usage:
/// ```ignore
/// use auths_core::ports::network::WitnessClient;
///
/// async fn witness_inception(client: &dyn WitnessClient, endpoint: &str, event: &[u8]) {
///     let receipt = client.submit_event(endpoint, event).await.unwrap();
/// }
/// ```
pub trait WitnessClient: Send + Sync {
    /// Submits a serialized key event to a witness and returns the receipt bytes.
    ///
    /// Args:
    /// * `endpoint`: The witness endpoint identifier.
    /// * `event`: The serialized key event bytes.
    ///
    /// Usage:
    /// ```ignore
    /// let receipt = client.submit_event("witness-1.example.com", &event_bytes).await?;
    /// ```
    fn submit_event(
        &self,
        endpoint: &str,
        event: &[u8],
    ) -> impl Future<Output = Result<Vec<u8>, NetworkError>> + Send;

    /// Queries all receipts a witness holds for the given KERI prefix.
    ///
    /// Args:
    /// * `endpoint`: The witness endpoint identifier.
    /// * `prefix`: The KERI prefix to query receipts for.
    ///
    /// Usage:
    /// ```ignore
    /// let prefix = Prefix::new_unchecked("EAbcdef...".into());
    /// let receipts = client.query_receipts("witness-1.example.com", &prefix).await?;
    /// ```
    fn query_receipts(
        &self,
        endpoint: &str,
        prefix: &Prefix,
    ) -> impl Future<Output = Result<Vec<Vec<u8>>, NetworkError>> + Send;
}

/// Rate limit information extracted from HTTP response headers.
///
/// Populated from standard `X-RateLimit-*` headers when present in the
/// registry response.
#[derive(Debug, Clone, Default)]
pub struct RateLimitInfo {
    /// Maximum requests allowed in the current window.
    pub limit: Option<i32>,
    /// Remaining requests in the current window.
    pub remaining: Option<i32>,
    /// Unix timestamp when the rate limit window resets.
    pub reset: Option<i64>,
    /// The access tier for this identity (e.g., "free", "team").
    pub tier: Option<String>,
}

/// Response from a registry POST operation.
///
/// Carries the HTTP status code and body so callers can dispatch on
/// status-specific business logic (e.g., 201 Created vs. 409 Conflict).
#[derive(Debug)]
pub struct RegistryResponse {
    /// HTTP status code.
    pub status: u16,
    /// Response body bytes.
    pub body: Vec<u8>,
    /// Rate limit information extracted from response headers, if present.
    pub rate_limit: Option<RateLimitInfo>,
}

/// Fetches and pushes data to a remote registry service.
///
/// Implementations handle the transport protocol (e.g., HTTP, gRPC).
/// The domain provides logical paths and receives raw bytes.
///
/// Usage:
/// ```ignore
/// use auths_core::ports::network::RegistryClient;
///
/// async fn sync_identity(client: &dyn RegistryClient, url: &str) {
///     let data = client.fetch_registry_data(url, "identities/abc123").await.unwrap();
/// }
/// ```
pub trait RegistryClient: Send + Sync {
    /// Fetches data from a registry at the given logical path.
    ///
    /// Args:
    /// * `registry_url`: The registry service identifier.
    /// * `path`: The logical path within the registry.
    ///
    /// Usage:
    /// ```ignore
    /// let data = client.fetch_registry_data("registry.example.com", "identities/abc123").await?;
    /// ```
    fn fetch_registry_data(
        &self,
        registry_url: &str,
        path: &str,
    ) -> impl Future<Output = Result<Vec<u8>, NetworkError>> + Send;

    /// Pushes data to a registry at the given logical path.
    ///
    /// Args:
    /// * `registry_url`: The registry service identifier.
    /// * `path`: The logical path within the registry.
    /// * `data`: The raw bytes to push.
    ///
    /// Usage:
    /// ```ignore
    /// client.push_registry_data("registry.example.com", "identities/abc123", &bytes).await?;
    /// ```
    fn push_registry_data(
        &self,
        registry_url: &str,
        path: &str,
        data: &[u8],
    ) -> impl Future<Output = Result<(), NetworkError>> + Send;

    /// POSTs a JSON payload to a registry endpoint and returns the raw response.
    ///
    /// Args:
    /// * `registry_url`: Base URL of the registry service.
    /// * `path`: The logical path within the registry (e.g., `"v1/identities"`).
    /// * `json_body`: Serialized JSON bytes to send as the request body.
    ///
    /// Usage:
    /// ```ignore
    /// let resp = client.post_json("https://registry.example.com", "v1/identities", &body).await?;
    /// match resp.status {
    ///     201 => { /* success */ }
    ///     409 => { /* conflict */ }
    ///     _ => { /* error */ }
    /// }
    /// ```
    fn post_json(
        &self,
        registry_url: &str,
        path: &str,
        json_body: &[u8],
    ) -> impl Future<Output = Result<RegistryResponse, NetworkError>> + Send;
}