tlpt 0.8.0

A set of protocols for building local-first distributed systems
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
use std::{
    cell::RefCell,
    collections::{hash_map::Entry, HashMap, HashSet},
    pin::Pin,
    rc::Rc,
    str::FromStr,
};

use crew_rs::{
    AddRole, CredentialSource, CredentialSourceHash, CrewChange, CrewID, MakeStatement, Member,
    MemberCredential, RevealSecret, SimpleCredentialSource, ADMIN_ROLE, CONTENT_SECRET,
    PARTICIPATION_SECRET,
};
use endr::{Remote, StorageBackend};
use futures::{future, Future, FutureExt, Stream, StreamExt};
use jmbl::{Input, JMBLViewRef, Value};
use litl::Val;
use mofo::Mofo;
use ridl::{
    asymm_encr::RecipientID,
    signing::{SignatureError, Signed},
};
use tracing::{debug, error};

use crate::{
    conventions::{
        named_doc_path, CREDENTIALS_DOC_NAME, INCLUDE_LOG_STR, INTRODUCTION_PREFIX,
        NAMED_DOC_PREFIX, PROFILE_DOC_NAME,
    },
    doc_content::jmbl_content::JMBLContent,
    team::{Team, TeamID},
    Doc, DocID, Tlpt,
};

#[derive(Clone)]
pub struct AccountCredentialSource {
    account_id: TeamID,
    account_credential: MemberCredential,
    credentials_doc: Doc,
    cached: Rc<RefCell<HashMap<Val, MemberCredential>>>,
}

impl CredentialSource for AccountCredentialSource {
    fn add_credential<'a>(
        &'a self,
        crew_id: CrewID,
        credential: MemberCredential,
    ) -> std::pin::Pin<Box<dyn futures::Future<Output = ()> + 'a>> {
        if crew_id == self.account_id.0 {
            panic!("Can't add credential for account document");
        }

        let credentials_doc = self.credentials_doc.clone();
        async move {
            let credentials_doc_view = credentials_doc.content().expect_jmbl().start_writing(false);

            credentials_doc_view
                .get_root()
                .if_map_mut()
                .expect("Expected credentials doc to be a map")
                .insert(
                    crew_id.to_string(),
                    Value::Plain(litl::to_val(&credential).unwrap()),
                );

            credentials_doc
                .content()
                .expect_jmbl()
                .finish_writing(credentials_doc_view);

            // TODO: a better way to wait for the update to be applied
            credentials_doc
                .content()
                .expect_jmbl()
                .updates("Awaiting new credential added".to_string())
                .filter(|(root, _)| {
                    future::ready(
                        root.if_map()
                            .unwrap()
                            .get(&crew_id.to_string())
                            .if_plain()
                            .unwrap()
                            == &litl::to_val(&credential).unwrap(),
                    )
                })
                .next()
                .await;
        }
        .boxed_local()
    }

    fn current_credentials_for<'a>(&'a self, crew_id: &'a CrewID) -> Vec<MemberCredential> {
        if crew_id == &self.account_id.0 {
            return vec![self.account_credential.clone()];
        }

        if crew_id == &self.credentials_doc.doc_crew().id() {
            // prevent recursion, caller should use credentials from parent team
            vec![]
        } else {
            let root = self.credentials_doc.content().expect_jmbl().current_root();

            let val = root
                .if_map()
                .expect("Expected root to be map")
                .get(&crew_id.to_string())
                .if_plain()
                .expect("Expected credential to be plain val")
                .clone();

            if val.is_null() {
                return vec![MemberCredential::literally_everyone()];
            }

            match self.cached.borrow_mut().entry(val.clone()) {
                Entry::Occupied(entry) =>  vec![entry.get().clone()],
                Entry::Vacant(entry) => {
                    match litl::from_val::<MemberCredential>(val.clone()) {
                        Ok(credential) => {
                            entry.insert(credential.clone());
                            vec![credential]
                        }
                        Err(err) => {
                            error!("Error deserializing credential: {:?}", err);
                            vec![MemberCredential::literally_everyone()]
                        }
                    }
                }
            }
        }
    }

    fn id_hash(&self) -> CredentialSourceHash {
        CredentialSourceHash::from_hashable(self.account_id)
    }

    fn clone_ref(&self) -> Box<dyn CredentialSource> {
        Box::new(self.clone())
    }

    fn current_state(&self) -> std::collections::HashMap<CrewID, Vec<MemberCredential>> {
        let mut map = HashMap::new();
        map.insert(
            self.account_id.0.clone(),
            vec![self.account_credential.clone()],
        );

        let root = self.credentials_doc.content().expect_jmbl().current_root();

        let jmbl_map = root.if_map().expect("Expected root to be map");

        for (crew_id, val) in jmbl_map.entries() {
            let credential = match litl::from_val(val) {
                Ok(credential) => credential,
                Err(err) => {
                    error!("Error deserializing credential: {:?}", err);
                    continue;
                }
            };

            map.entry(CrewID::from_str(&crew_id).unwrap())
                .or_insert_with(Vec::new)
                .push(credential);
        }

        map
    }
}

impl Tlpt {
    pub async fn new_with_account<S: StorageBackend + 'static>(
        name: String,
        background: Mofo,
        account_id: TeamID,
        account_credential: MemberCredential,
        storage: S,
        remotes: impl Stream<Item = Remote> + Unpin + 'static,
    ) -> Self {
        let root_credential_source = Box::new(SimpleCredentialSource::new());
        root_credential_source
            .add_credential(account_id.0.clone(), account_credential.clone())
            .await;

        let endr = endr::Node::new(background.clone(), storage.clone_ref());

        let endr_for_remotes = endr.clone();
        background.clone().add_background_task(
            remotes
                .for_each(move |remote| {
                    let endr_for_remotes = endr_for_remotes.clone();
                    async move { endr_for_remotes.add_remote(remote).await }
                })
                .boxed_local(),
        );

        let root_tlpt = Tlpt::new(
            format!("{}_root", name),
            background.clone(),
            endr.clone_ref(),
            root_credential_source,
            None,
        );

        let account_team = root_tlpt.load_team(&account_id).await;
        let account_team_for_credentials_doc = account_team.clone();

        let credentials_doc_id = account_team_for_credentials_doc
            .wait_for_named_doc(CREDENTIALS_DOC_NAME)
            .await;

        let credentials_doc = root_tlpt.load_doc(credentials_doc_id).await;

        let account_credential_source = AccountCredentialSource {
            account_id,
            account_credential,
            credentials_doc,
            cached: Rc::new(RefCell::new(HashMap::new())),
        };

        let tlpt = Tlpt::new(
            format!("{}_credential_source", name),
            background.clone(),
            endr,
            Box::new(account_credential_source),
            Some(account_id),
        );

        tlpt.load_team(&account_team.id())
            .await
            .wait_for_named_doc(PROFILE_DOC_NAME)
            .await;

        tlpt
    }

    pub async fn new_with_create_account<
        C: FnOnce(JMBLViewRef) -> Value + 'static,
        S: StorageBackend + 'static,
    >(
        name: String,
        background: Mofo,
        profile_constructor: C,
        storage: S,
        remotes: impl Stream<Item = Remote> + Unpin + 'static,
    ) -> (Tlpt, TeamID, MemberCredential) {
        let endr = endr::Node::new(background.clone(), storage.clone_ref());

        let endr_for_remotes = endr.clone();
        background.clone().add_background_task(
            remotes
                .for_each(move |remote| {
                    let endr_for_remotes = endr_for_remotes.clone();
                    async move { endr_for_remotes.add_remote(remote).await }
                })
                .boxed_local(),
        );

        let root_credential_source = Box::new(SimpleCredentialSource::new());
        let root_tlpt = Tlpt::new(
            format!("{}_root", name),
            background.clone(),
            endr.clone_ref(),
            root_credential_source,
            None,
        );

        let (account_team, account_credential) = root_tlpt.create_team_without_intro(None).await;

        let credentials_doc = root_tlpt
            .create_document_without_intro(
                &account_team,
                JMBLContent::create(|mut view| view.create_map::<Value, &str, _>([])),
            )
            .await;

        account_team
            .crew
            .make_changes([CrewChange::MakeStatement(MakeStatement {
                path: named_doc_path(CREDENTIALS_DOC_NAME),
                value: litl::to_val(credentials_doc.id()).unwrap(),
            })])
            .await
            .unwrap();

        let account_credential_source = Box::new(AccountCredentialSource {
            account_id: account_team.id(),
            account_credential: account_credential.clone(),
            credentials_doc,
            cached: Rc::new(RefCell::new(HashMap::new())),
        });

        let tlpt_for_profile = Tlpt::new(
            format!("{}_create_profile", name),
            background.clone(),
            endr.clone_ref(),
            account_credential_source.clone_ref(),
            None,
        );

        let profile = tlpt_for_profile
            .create_profile(&account_team.id(), profile_constructor)
            .await;

        let tlpt = Tlpt::new(
            name,
            background,
            endr,
            account_credential_source,
            Some(account_team.id()),
        );

        tlpt.load_team(&account_team.id())
            .await
            .wait_for_named_doc(PROFILE_DOC_NAME)
            .await;

        (tlpt, account_team.id(), account_credential)
    }

    pub async fn create_profile<C: FnOnce(JMBLViewRef) -> Value + 'static>(
        &self,
        in_account: &TeamID,
        constructor: C,
    ) -> Doc {
        let (profile_team, profile_credential) = self.create_team_without_intro(None).await;

        let profile_doc = self
            .create_document_without_intro(&profile_team, JMBLContent::create(constructor))
            .await;

        let profile_team_content_secret =
            profile_team.crew.get_shared_secret(CONTENT_SECRET).unwrap();

        profile_team
            .crew
            .make_changes([
                CrewChange::AddRole(AddRole {
                    to: Member::literally_everyone(),
                    role: "reader".to_owned(),
                }),
                CrewChange::RevealSecret(RevealSecret {
                    secret_kind: CONTENT_SECRET.to_owned(),
                    to: Member::literally_everyone().signer,
                    encr: Member::literally_everyone()
                        .encrypt_from_anon(&profile_team_content_secret),
                }),
            ])
            .await
            .unwrap();

        profile_team
            .crew
            .make_changes([CrewChange::MakeStatement(MakeStatement {
                path: named_doc_path(PROFILE_DOC_NAME),
                value: litl::to_val(profile_doc.id()).unwrap(),
            })])
            .await
            .unwrap();

        let account = self.load_team(in_account).await;

        account
            .crew
            .wait_for_state(|state| state.shared_secrets.contains_key(PARTICIPATION_SECRET))
            .await;

        account
            .crew
            .make_changes([CrewChange::MakeStatement(MakeStatement {
                path: named_doc_path(PROFILE_DOC_NAME),
                value: litl::to_val(profile_doc.id()).unwrap(),
            })])
            .await
            .unwrap();

        profile_doc
    }

    pub async fn wait_for_account_profile(&self, account_id: &TeamID) -> Doc {
        let account = self.load_team(account_id).await;
        let profile_doc_id = account.wait_for_named_doc(PROFILE_DOC_NAME).await;

        self.load_doc(profile_doc_id).await
    }

    pub fn get_account_profile_id(&self, account_id: &TeamID) -> Option<DocID> {
        let account = self.get_loaded_team(account_id)?;
        account.get_named_doc(PROFILE_DOC_NAME)
    }

    pub async fn introduce_in_as(&self, team: &Team, account_id: &TeamID) {
        debug!("Getting profile for {:?}", account_id);
        let profile = self.wait_for_account_profile(account_id).await;

        debug!("Getting profile credential for {:?}", account_id);

        let (profile_credential, _, _) = profile
            .doc_crew()
            .get_credentials_with_role(&HashSet::from_iter([ADMIN_ROLE.to_owned()]))
            .into_iter()
            .next()
            .expect("Expected to have crendential for profile to sign intro with");

        debug!("Got profile credential for {:?}", account_id);

        team.crew
            .make_changes([CrewChange::MakeStatement(MakeStatement {
                path: format!("{}{}", INTRODUCTION_PREFIX, profile.id()),
                value: litl::to_val(profile_credential.signer().sign(profile.id())).unwrap(),
            })])
            .await
            .unwrap();

        debug!(
            "Made intro in {:?} for {:?} with profile {:?}",
            team.id(),
            account_id,
            profile.id()
        );
    }

    pub async fn async_is_introducted_in(&self, team: &Team) -> bool {
        let account_id = self.account().expect("Account required");

        let profile = self.wait_for_account_profile(&account_id).await;

        let expected_path = format!("{}{}", INTRODUCTION_PREFIX, profile.id());

        team.crew
            .current_state()
            .expect("Expected crew state when checking for intro")
            .statements
            .iter()
            .any(|(path, value, _)| {
                if path == &expected_path {
                    if let Ok(introduced_profile) = litl::from_val::<Signed<DocID>>(value.clone()) {
                        // we can skip the full check for ourselves?
                        introduced_profile.attested == profile.id()
                    } else {
                        false
                    }
                } else {
                    false
                }
            })
    }

    pub fn is_currently_introducted_in(&self, team: &Team) -> bool {
        let account_id = self.account().expect("Account required");

        let profile_id = self
            .get_account_profile_id(&account_id)
            .expect("Expected profile id to be loaded");

        let expected_path = format!("{}{}", INTRODUCTION_PREFIX, profile_id);

        team.crew
            .current_state()
            .expect("Expected crew state when checking for intro")
            .statements
            .iter()
            .any(|(path, value, _)| {
                if path == &expected_path {
                    if let Ok(introduced_profile) = litl::from_val::<Signed<DocID>>(value.clone()) {
                        // we can skip the full check for ourselves?
                        introduced_profile.attested == profile_id
                    } else {
                        false
                    }
                } else {
                    false
                }
            })
    }

    pub async fn ensure_introduced_in(&self, team: &Team) {
        if !self.async_is_introducted_in(team).await {
            let account_id = self.account().expect("Account required");
            self.introduce_in_as(team, &account_id).await;
        }
    }

    pub async fn verify_profile(
        &self,
        signed_id: Signed<DocID>,
    ) -> Result<Doc, Vec<SignatureError>> {
        let profile = self.load_doc(signed_id.attested).await;

        let already_verified =
            VERIFIED_PROFILE_CACHE.with(|cache| cache.borrow().contains(&signed_id.attested));

        if already_verified {
            return Ok(profile);
        }

        match profile
            .doc_crew()
            .is_signed_by_member_with_role(&signed_id, "admin")
        {
            Ok(()) => {
                VERIFIED_PROFILE_CACHE.with(|cache| cache.borrow_mut().insert(signed_id.attested));
                Ok(profile)
            }
            Err(e) => Err(e),
        }
    }
}

thread_local! {
    pub static VERIFIED_PROFILE_CACHE: RefCell<HashSet<DocID>> = RefCell::new(HashSet::new());
}