note-to-self-cli 0.1.2

Encrypted local-first journaling CLI with sync and locked journals.
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
use crate::config::{normalize_server_url, Config, SyncConfig};
use crate::store::LocalStore;
use anyhow::{bail, Context, Result};
use note_to_self_lib::{auth_token, DerivedKeys, SyncRequest, SyncResponse};
use reqwest::header::{HeaderMap, HeaderValue, AUTHORIZATION};
use reqwest::StatusCode;
use serde::{Deserialize, Serialize};
use std::time::Duration;

#[derive(Debug, Serialize)]
struct AuthRequest<'a> {
    username: &'a str,
    auth_key: &'a str,
}

#[derive(Debug, Serialize)]
struct JournalRequest<'a> {
    name: &'a str,
    locked: bool,
    verifier: Option<&'a str>,
}

#[derive(Debug, Serialize)]
struct JournalMetadataRequest<'a> {
    locked: bool,
    verifier: Option<&'a str>,
}

#[derive(Debug, Deserialize)]
struct JournalList {
    journals: Vec<RemoteJournal>,
}

#[derive(Debug, Clone, Deserialize, PartialEq, Eq)]
pub struct RemoteJournal {
    pub name: String,
    #[serde(default)]
    pub locked: bool,
    #[serde(default)]
    pub verifier: Option<String>,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum AuthMode {
    LoggedIn,
    Registered,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum AuthAttempt {
    Success,
    Unauthorized,
    Conflict,
    Failed { status: StatusCode, body: String },
}

#[derive(Debug)]
enum SyncAttempt {
    Success(SyncResponse),
    Unauthorized,
    Failed { status: StatusCode, body: String },
}

#[cfg(test)]
pub async fn register(server_url: &str, username: &str, keys: &DerivedKeys) -> Result<()> {
    match register_attempt(server_url, username, keys).await? {
        AuthAttempt::Success => Ok(()),
        AuthAttempt::Conflict => bail!("user {username} already exists"),
        AuthAttempt::Unauthorized => bail!("registration was unauthorized"),
        AuthAttempt::Failed { status, body } => {
            bail!("registration failed with {status}: {body}")
        }
    }
}

pub async fn verify_auth_attempt(
    server_url: &str,
    username: &str,
    keys: &DerivedKeys,
) -> Result<AuthAttempt> {
    verify_attempt(server_url, username, keys).await
}

pub async fn register_auth_attempt(
    server_url: &str,
    username: &str,
    keys: &DerivedKeys,
) -> Result<AuthAttempt> {
    register_attempt(server_url, username, keys).await
}

pub async fn login_or_register(
    server_url: &str,
    username: &str,
    keys: &DerivedKeys,
) -> Result<AuthMode> {
    match verify_attempt(server_url, username, keys).await? {
        AuthAttempt::Success => return Ok(AuthMode::LoggedIn),
        AuthAttempt::Unauthorized => {}
        AuthAttempt::Conflict => bail!("login failed because the account is in conflict"),
        AuthAttempt::Failed { status, body } => bail!("login failed with {status}: {body}"),
    }

    match register_attempt(server_url, username, keys).await? {
        AuthAttempt::Success => Ok(AuthMode::Registered),
        AuthAttempt::Conflict => {
            bail!("account {username} already exists, but the password did not match")
        }
        AuthAttempt::Unauthorized => bail!("registration was unauthorized"),
        AuthAttempt::Failed { status, body } => {
            bail!("registration failed with {status}: {body}")
        }
    }
}

async fn register_attempt(
    server_url: &str,
    username: &str,
    keys: &DerivedKeys,
) -> Result<AuthAttempt> {
    let client = client()?;
    let token = auth_token(&keys.auth_key);
    let url = format!("{}/api/v1/auth/register", normalize_server_url(server_url));
    let response = client
        .post(url)
        .json(&AuthRequest {
            username,
            auth_key: &token,
        })
        .send()
        .await
        .context("registration request failed")?;
    classify_auth_response(response).await
}

async fn verify_attempt(
    server_url: &str,
    username: &str,
    keys: &DerivedKeys,
) -> Result<AuthAttempt> {
    let client = client()?;
    let url = format!("{}/api/v1/auth/verify", normalize_server_url(server_url));
    let response = client
        .post(url)
        .headers(auth_headers(username, keys)?)
        .send()
        .await
        .context("login verification request failed")?;
    classify_auth_response(response).await
}

pub async fn sync_once(store: &mut LocalStore, config: &Config, keys: &DerivedKeys) -> Result<()> {
    let Some(sync) = &config.sync else {
        return Ok(());
    };

    match sync_attempt(store, config, keys).await? {
        SyncAttempt::Success(response) => apply_sync_response(store, response),
        SyncAttempt::Unauthorized => {
            recover_missing_account(sync, keys).await?;

            match sync_attempt(store, config, keys).await? {
                SyncAttempt::Success(response) => apply_sync_response(store, response),
                SyncAttempt::Unauthorized => bail!("sync failed: invalid username or password"),
                SyncAttempt::Failed { status, body } => {
                    bail!("sync failed with {status}: {body}")
                }
            }
        }
        SyncAttempt::Failed { status, body } => bail!("sync failed with {status}: {body}"),
    }
}

async fn sync_attempt(
    store: &LocalStore,
    config: &Config,
    keys: &DerivedKeys,
) -> Result<SyncAttempt> {
    let Some(sync) = &config.sync else {
        return Ok(SyncAttempt::Success(SyncResponse {
            download: Vec::new(),
            ack_uploaded: Vec::new(),
            server_manifest: store.manifest.clone(),
        }));
    };
    let client = client()?;
    let upload = store.upload_blobs()?;
    let request = SyncRequest {
        device_id: config.device_id,
        manifest: store.client_manifest()?,
        upload,
    };
    let url = format!(
        "{}/api/v1/sync/{}",
        normalize_server_url(&sync.server_url),
        store.journal()
    );
    let response = client
        .post(url)
        .headers(auth_headers(&sync.username, keys)?)
        .json(&request)
        .send()
        .await
        .context("sync request failed")?;
    if !response.status().is_success() {
        let status = response.status();
        let body = response.text().await.unwrap_or_default();
        return Ok(match status {
            StatusCode::UNAUTHORIZED => SyncAttempt::Unauthorized,
            _ => SyncAttempt::Failed { status, body },
        });
    }
    let response: SyncResponse = response
        .json()
        .await
        .context("failed to decode sync response")?;
    Ok(SyncAttempt::Success(response))
}

fn apply_sync_response(store: &mut LocalStore, response: SyncResponse) -> Result<()> {
    for blob in &response.download {
        store.apply_download(blob)?;
    }
    store.adopt_server_manifest(response.server_manifest)?;
    Ok(())
}

pub async fn create_remote_journal(
    config: &Config,
    keys: &DerivedKeys,
    name: &str,
    locked: bool,
    verifier: Option<&str>,
) -> Result<()> {
    let Some(sync) = &config.sync else {
        return Ok(());
    };
    let client = client()?;
    let mut response = client
        .post(format!(
            "{}/api/v1/journals",
            normalize_server_url(&sync.server_url)
        ))
        .headers(auth_headers(&sync.username, keys)?)
        .json(&JournalRequest {
            name,
            locked,
            verifier,
        })
        .send()
        .await
        .context("remote journal creation failed")?;
    if response.status() == StatusCode::UNAUTHORIZED {
        recover_missing_account(sync, keys).await?;
        response = client
            .post(format!(
                "{}/api/v1/journals",
                normalize_server_url(&sync.server_url)
            ))
            .headers(auth_headers(&sync.username, keys)?)
            .json(&JournalRequest {
                name,
                locked,
                verifier,
            })
            .send()
            .await
            .context("remote journal creation failed")?;
    }
    ensure_success(response).await
}

pub async fn update_remote_journal_metadata(
    config: &Config,
    keys: &DerivedKeys,
    name: &str,
    locked: bool,
    verifier: Option<&str>,
) -> Result<()> {
    let Some(sync) = &config.sync else {
        return Ok(());
    };
    let client = client()?;
    let mut response = client
        .patch(format!(
            "{}/api/v1/journals/{}",
            normalize_server_url(&sync.server_url),
            name
        ))
        .headers(auth_headers(&sync.username, keys)?)
        .json(&JournalMetadataRequest { locked, verifier })
        .send()
        .await
        .context("remote journal metadata update failed")?;
    if response.status() == StatusCode::UNAUTHORIZED {
        recover_missing_account(sync, keys).await?;
        response = client
            .patch(format!(
                "{}/api/v1/journals/{}",
                normalize_server_url(&sync.server_url),
                name
            ))
            .headers(auth_headers(&sync.username, keys)?)
            .json(&JournalMetadataRequest { locked, verifier })
            .send()
            .await
            .context("remote journal metadata update failed")?;
    }
    ensure_success(response).await
}

pub async fn delete_remote_journal(config: &Config, keys: &DerivedKeys, name: &str) -> Result<()> {
    let Some(sync) = &config.sync else {
        return Ok(());
    };
    let client = client()?;
    let mut response = client
        .delete(format!(
            "{}/api/v1/journals/{}",
            normalize_server_url(&sync.server_url),
            name
        ))
        .headers(auth_headers(&sync.username, keys)?)
        .send()
        .await
        .context("remote journal deletion failed")?;
    if response.status() == StatusCode::UNAUTHORIZED {
        recover_missing_account(sync, keys).await?;
        response = client
            .delete(format!(
                "{}/api/v1/journals/{}",
                normalize_server_url(&sync.server_url),
                name
            ))
            .headers(auth_headers(&sync.username, keys)?)
            .send()
            .await
            .context("remote journal deletion failed")?;
    }
    ensure_success(response).await
}

#[cfg(test)]
pub async fn list_remote_journals(config: &Config, keys: &DerivedKeys) -> Result<Vec<String>> {
    Ok(list_remote_journal_infos(config, keys)
        .await?
        .into_iter()
        .map(|journal| journal.name)
        .collect())
}

pub async fn list_remote_journal_infos(
    config: &Config,
    keys: &DerivedKeys,
) -> Result<Vec<RemoteJournal>> {
    let Some(sync) = &config.sync else {
        return Ok(vec![]);
    };
    let client = client()?;
    let mut response = client
        .get(format!(
            "{}/api/v1/journals",
            normalize_server_url(&sync.server_url)
        ))
        .headers(auth_headers(&sync.username, keys)?)
        .send()
        .await
        .context("remote journal listing failed")?;
    if response.status() == StatusCode::UNAUTHORIZED {
        recover_missing_account(sync, keys).await?;
        response = client
            .get(format!(
                "{}/api/v1/journals",
                normalize_server_url(&sync.server_url)
            ))
            .headers(auth_headers(&sync.username, keys)?)
            .send()
            .await
            .context("remote journal listing failed")?;
    }
    if !response.status().is_success() {
        let status = response.status();
        let body = response.text().await.unwrap_or_default();
        bail!("journal listing failed with {status}: {body}");
    }
    let list: JournalList = response
        .json()
        .await
        .context("failed to decode remote journal list")?;
    Ok(list.journals)
}

async fn recover_missing_account(sync: &SyncConfig, keys: &DerivedKeys) -> Result<()> {
    match login_or_register(&sync.server_url, &sync.username, keys).await? {
        AuthMode::LoggedIn => Ok(()),
        AuthMode::Registered => {
            eprintln!(
                "user {} not found on server; creating account",
                sync.username
            );
            Ok(())
        }
    }
}

fn client() -> Result<reqwest::Client> {
    reqwest::Client::builder()
        .timeout(Duration::from_secs(3))
        .build()
        .context("failed to build HTTP client")
}

fn auth_headers(username: &str, keys: &DerivedKeys) -> Result<HeaderMap> {
    let mut headers = HeaderMap::new();
    let bearer = format!("Bearer {}", auth_token(&keys.auth_key));
    headers.insert(AUTHORIZATION, HeaderValue::from_str(&bearer)?);
    headers.insert("x-note-username", HeaderValue::from_str(username)?);
    Ok(headers)
}

async fn ensure_success(response: reqwest::Response) -> Result<()> {
    if response.status().is_success() {
        return Ok(());
    }
    let status = response.status();
    let body = response.text().await.unwrap_or_default();
    bail!("request failed with {status}: {body}")
}

async fn classify_auth_response(response: reqwest::Response) -> Result<AuthAttempt> {
    if response.status().is_success() {
        return Ok(AuthAttempt::Success);
    }
    let status = response.status();
    let body = response.text().await.unwrap_or_default();
    Ok(match status {
        StatusCode::UNAUTHORIZED => AuthAttempt::Unauthorized,
        StatusCode::CONFLICT => AuthAttempt::Conflict,
        _ => AuthAttempt::Failed { status, body },
    })
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::config::Config;
    use crate::store::LocalStore;
    use note_to_self_lib::derive_keys;
    use note_to_self_server::bucket::{Bucket, MockS3};
    use tokio::net::TcpListener;

    struct TestServer {
        base_url: String,
        _task: tokio::task::JoinHandle<()>,
    }

    #[tokio::test]
    async fn sync_once_creates_missing_remote_account_and_retries() {
        let server = spawn_server().await;
        let temp = tempfile::tempdir().unwrap();
        let keys = derive_keys("new-user", "correct horse").unwrap();
        let config = Config::new(
            Some("new-user".to_string()),
            "personal".to_string(),
            Some(server.base_url.clone()),
        );
        let mut store =
            LocalStore::open(temp.path(), "personal", &config, &keys.encryption_key).unwrap();
        store
            .add_journal_entry("created before account exists".to_string(), &[], false)
            .unwrap();

        sync_once(&mut store, &config, &keys).await.unwrap();

        let journals = list_remote_journals(&config, &keys).await.unwrap();
        assert_eq!(journals, vec!["personal"]);
    }

    #[tokio::test]
    async fn sync_once_reports_password_mismatch_without_raw_401() {
        let server = spawn_server().await;
        let temp = tempfile::tempdir().unwrap();
        let correct_keys = derive_keys("alice", "correct").unwrap();
        let wrong_keys = derive_keys("alice", "wrong").unwrap();
        let config = Config::new(
            Some("alice".to_string()),
            "personal".to_string(),
            Some(server.base_url.clone()),
        );
        register(&server.base_url, "alice", &correct_keys)
            .await
            .unwrap();
        let mut store =
            LocalStore::open(temp.path(), "personal", &config, &wrong_keys.encryption_key).unwrap();
        store
            .add_journal_entry("should not sync".to_string(), &[], false)
            .unwrap();

        let error = sync_once(&mut store, &config, &wrong_keys)
            .await
            .unwrap_err()
            .to_string();

        assert!(error.contains("password did not match"), "{error}");
        assert!(!error.contains("401"), "{error}");
    }

    async fn spawn_server() -> TestServer {
        let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
        let address = listener.local_addr().unwrap();
        let app = note_to_self_server::app_with_bucket(Bucket::mock_s3(MockS3::new()))
            .await
            .unwrap();
        let task = tokio::spawn(async move {
            axum::serve(listener, app).await.unwrap();
        });
        TestServer {
            base_url: format!("http://{address}"),
            _task: task,
        }
    }
}