-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathentities.rs
More file actions
292 lines (270 loc) · 9.21 KB
/
Copy pathentities.rs
File metadata and controls
292 lines (270 loc) · 9.21 KB
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
//! Database entity types for PDS.
//!
//! This module defines the data structures used to store and retrieve
//! data from the PDS SQLite database.
use chrono::{DateTime, Utc};
/// A blob stored in the PDS.
#[derive(Debug, Clone)]
pub struct Blob {
/// Content identifier (CID) of the blob.
pub cid: String,
/// MIME content type.
pub content_type: String,
/// Size in bytes.
pub content_length: i32,
}
/// An OAuth authorization request.
#[derive(Debug, Clone)]
pub struct OauthRequest {
/// Unique request URI identifier.
pub request_uri: String,
/// Expiration date in ISO 8601 format.
pub expires_date: String,
/// DPoP proof.
pub dpop: String,
/// Request body.
pub body: String,
/// Authorization code (set after user authorizes).
pub authorization_code: Option<String>,
/// Authentication type (e.g., "Legacy", "Passkey").
pub auth_type: Option<String>,
}
/// An OAuth session.
#[derive(Debug, Clone)]
pub struct OauthSession {
/// Unique session identifier.
pub session_id: String,
/// OAuth client identifier.
pub client_id: String,
/// Effective (resolved) scopes, recomputed from `requested_scope` at each
/// token issuance. Any `include:<nsid>` permission sets in the request are
/// expanded into concrete granular scopes here.
pub scope: String,
/// The scopes as originally requested by the client (with any `include:`
/// permission-set references intact). This is the immutable authorization
/// boundary the user consented to; `scope` is derived from it.
pub requested_scope: String,
/// DPoP JWK thumbprint for token binding.
pub dpop_jwk_thumbprint: String,
/// Refresh token value.
pub refresh_token: String,
/// Refresh token expiration date in ISO 8601 format.
pub refresh_token_expires_date: String,
/// Session creation date in ISO 8601 format.
pub created_date: String,
/// Client IP address.
pub ip_address: String,
/// Authentication type (e.g., "Legacy", "Passkey").
pub auth_type: String,
/// Last used date in ISO 8601 format.
pub last_used_date: String,
}
/// A legacy (non-OAuth) session.
#[derive(Debug, Clone)]
pub struct LegacySession {
/// Session creation date in ISO 8601 format.
pub created_date: String,
/// Access JWT token.
pub access_jwt: String,
/// Refresh JWT token.
pub refresh_jwt: String,
/// Client IP address.
pub ip_address: String,
/// Client user agent string.
pub user_agent: String,
/// Last used date in ISO 8601 format.
pub last_used_date: String,
}
/// An admin session for the PDS admin interface.
#[derive(Debug, Clone)]
pub struct AdminSession {
/// Unique session identifier.
pub session_id: String,
/// Client IP address.
pub ip_address: String,
/// Client user agent string.
pub user_agent: String,
/// Session creation date in ISO 8601 format.
pub created_date: String,
/// Authentication type.
pub auth_type: String,
/// Last used date in ISO 8601 format.
pub last_used_date: String,
}
/// A WebAuthn passkey credential.
#[derive(Debug, Clone)]
pub struct Passkey {
/// User-friendly name for the passkey.
pub name: String,
/// Creation date in ISO 8601 format.
pub created_date: String,
/// WebAuthn credential ID.
pub credential_id: String,
/// Public key in JWK format.
pub public_key: String,
}
/// A WebAuthn passkey challenge.
#[derive(Debug, Clone)]
pub struct PasskeyChallenge {
/// Challenge creation date in ISO 8601 format.
pub created_date: String,
/// Challenge value (base64url encoded).
pub challenge: String,
}
/// Key for identifying a statistic.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct StatisticKey {
/// Statistic name.
pub name: String,
/// Client IP address.
pub ip_address: String,
/// Client user agent string.
pub user_agent: String,
}
/// A statistic record.
#[derive(Debug, Clone)]
pub struct Statistic {
/// Statistic name.
pub name: String,
/// Client IP address.
pub ip_address: String,
/// Client user agent string.
pub user_agent: String,
/// Current value.
pub value: i64,
/// Last update date in ISO 8601 format.
pub last_updated_date: String,
}
/// A firehose event (AT Protocol event stream frame).
#[derive(Debug, Clone)]
pub struct FirehoseEvent {
/// Sequence number in the firehose stream.
pub sequence_number: i64,
/// Creation date in ISO 8601 format.
pub created_date: String,
/// Operation type (1 = message, -1 = error).
pub header_op: i32,
/// Event type (e.g., "#commit"). None if op is -1.
pub header_t: Option<String>,
/// Full header object in DAG-CBOR format (serialized bytes).
pub header_dag_cbor_bytes: Vec<u8>,
/// Full body object in DAG-CBOR format (serialized bytes).
pub body_dag_cbor_bytes: Vec<u8>,
}
impl FirehoseEvent {
/// Get the current datetime formatted for database storage.
pub fn get_new_created_date() -> String {
format_datetime_for_db(Utc::now())
}
/// Get a datetime string for N hours ago.
pub fn get_created_date_minus_hours(hours: i64) -> String {
let dt = Utc::now() - chrono::Duration::hours(hours);
format_datetime_for_db(dt)
}
}
/// Repository header stored in the database.
#[derive(Debug, Clone)]
pub struct DbRepoHeader {
/// CID of the repo commit (base32 encoded).
pub repo_commit_cid: String,
/// Version number.
pub version: i32,
}
/// Repository commit stored in the database.
#[derive(Debug, Clone)]
pub struct DbRepoCommit {
/// Version number.
pub version: i32,
/// CID of this commit (base32 encoded).
pub cid: String,
/// CID of the root MST node (base32 encoded).
pub root_mst_node_cid: String,
/// Revision string (TID).
pub rev: String,
/// CID of the previous MST node (base32 encoded), if any.
pub prev_mst_node_cid: Option<String>,
/// Signature bytes.
pub signature: Vec<u8>,
}
/// Repository record stored in the database.
#[derive(Debug, Clone)]
pub struct DbRepoRecord {
/// Collection name (NSID).
pub collection: String,
/// Record key.
pub rkey: String,
/// CID of the record (base32 encoded).
pub cid: String,
/// DAG-CBOR serialized record data.
pub dag_cbor_bytes: Vec<u8>,
}
/// A record stored in a permissioned space's repository.
///
/// This is the space-scoped parallel of [`DbRepoRecord`]: records created
/// through `com.atproto.space.createRecord` are stored here, keyed by the space
/// they belong to (`space_uri`) in addition to their collection and record key.
/// Like repo records, the record body is persisted as DAG-CBOR bytes.
#[derive(Debug, Clone)]
pub struct DbSpaceRepoRecord {
/// Canonical space URI the record belongs to
/// (`at://{authority}/space/{spaceType}/{skey}`).
pub space_uri: String,
/// Collection name (NSID).
pub collection: String,
/// Record key.
pub rkey: String,
/// CID of the record (base32 encoded).
pub cid: String,
/// DAG-CBOR serialized record data.
pub dag_cbor_bytes: Vec<u8>,
}
/// A permissioned space managed by the `simplespace` implementation.
///
/// Spaces are not implicit: they come into existence through
/// `com.atproto.simplespace.createSpace`, which anchors the space on the
/// owner's DID and persists its configuration (user `policy` and app
/// `appAccess`). `com.atproto.simplespace.getSpace` reads this record back.
#[derive(Debug, Clone)]
pub struct DbSpace {
/// Canonical space URI (`at://{owner}/space/{spaceType}/{skey}`).
pub uri: String,
/// DID the space is anchored on (its owner / authority).
pub owner_did: String,
/// The NSID of the space type (e.g. `my.bulletin.board`).
pub space_type: String,
/// The space key differentiating spaces of the same type under one owner.
pub skey: String,
/// Serialized user-access `policy` union (JSON object).
pub policy_json: String,
/// Serialized app-access `appAccess` union (JSON object).
pub app_access_json: String,
/// Creation timestamp (ISO 8601).
pub created_date: String,
}
/// A registration recording that a service wants to be notified about activity
/// in a permissioned space.
///
/// Created by `com.atproto.space.registerNotify` and removed by
/// `com.atproto.space.unregisterNotify`. This only records the intent to be
/// notified; delivering the notifications themselves is handled elsewhere.
///
/// A registration is keyed by the space URI plus the service identifier.
#[derive(Debug, Clone)]
pub struct DbSpaceNotifyRegistration {
/// Canonical space URI the service wants to be notified about.
pub space_uri: String,
/// Identifier of the service to notify (e.g. `did:web:bulletin.my#bulletin`).
pub service: String,
/// Creation timestamp (ISO 8601).
pub created_date: String,
}
/// Format a DateTime for database storage.
///
/// Uses ISO 8601 format with milliseconds: `yyyy-MM-ddTHH:mm:ss.fffZ`
pub fn format_datetime_for_db(dt: DateTime<Utc>) -> String {
dt.format("%Y-%m-%dT%H:%M:%S%.3fZ").to_string()
}
/// Get the current datetime formatted for database storage.
pub fn get_current_datetime_for_db() -> String {
format_datetime_for_db(Utc::now())
}