silicon-remind-cli 0.7.0

Silicon Remind command line: durable reminders for Silicons, signed in with Silicon Accounts
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
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
//! `remind login` (device sign-in for Carbons, short-lived token for Silicons),
//! `remind login status` and `remind logout`.
use crate::{
    args::{LoginArgs, LoginCommand},
    output::{
        CliError, EXIT_AUTH, EXIT_FORBIDDEN, EXIT_INTERRUPTED, EXIT_OK, key_values, rfc3339, when,
    },
    session::{Ctx, SILICON_SIGN_IN, now, stored_from},
    state::StoredSignIn,
};
use serde_json::{Value, json};
use silicon_remind_client::{
    Error as ClientError, Secret,
    accounts::{DeviceProgress, SignIn, Tokens},
    models::{AccountKind, Identity},
};
use std::io::{IsTerminal as _, Read as _};

/// `remind login …`.
pub async fn login(ctx: &Ctx, args: &LoginArgs) -> anyhow::Result<u8> {
    if let Some(LoginCommand::Status { offline }) = &args.command {
        return status(ctx, *offline).await;
    }
    let slt = match (&args.token, &args.slt, args.slt_stdin) {
        (Some(token), _, _) | (None, Some(token), _) => Some(Secret::new(token.trim())),
        (None, None, true) => Some(read_slt_from_stdin()?),
        (None, None, false) => None,
    };
    // Check everything local first: a short-lived token is used up by its first exchange, so
    // make sure the sign-in can be saved and Remind's origin is usable before exchanging it.
    drop(ctx.home()?.lock()?);
    sign_in_client(ctx)?;
    let sign_in = ctx.sign_in_at(&ctx.accounts_url, &ctx.app_id)?;
    let (tokens, method) = match slt {
        Some(slt) => (sign_in.exchange_slt(&slt).await?, "slt"),
        None => match device(ctx, &sign_in, args).await? {
            Some(tokens) => (tokens, "device"),
            None => return Ok(EXIT_OK),
        },
    };
    finish(ctx, &sign_in, tokens, method).await
}

fn read_slt_from_stdin() -> anyhow::Result<Secret> {
    if std::io::stdin().is_terminal() {
        return Ok(Secret::new(
            rpassword::prompt_password("Short-lived token for Remind (slt_…): ")?
                .trim()
                .to_owned(),
        ));
    }
    let mut text = String::new();
    std::io::stdin().take(65_537).read_to_string(&mut text)?;
    let text = text.trim();
    if text.is_empty() || text.len() > 65_536 {
        return Err(CliError::usage(
            "--slt-stdin read nothing usable from standard input.",
            format!("Pipe the token in: `{SILICON_SIGN_IN}`."),
        )
        .into());
    }
    Ok(Secret::new(text))
}

/// The device sign-in. `None` when this home is already signed in and `--force` is absent.
async fn device(ctx: &Ctx, sign_in: &SignIn, args: &LoginArgs) -> anyhow::Result<Option<Tokens>> {
    let slot = ctx.sign_in_slot();
    if let Some(stored) = ctx.snapshot.state.sign_ins.get(&slot)
        && !args.force
        && !stored.ended(now())
    {
        let fallback = ctx.test.is_some() && ctx.explicit_test.is_none();
        let mut body = status_json(ctx, stored, false, fallback, None);
        body["already_signed_in"] = json!(true);
        ctx.out.either(
            &body,
            &format!(
                "Already signed in to Remind as {}.\nSign in again with `remind login --force`, or end this sign-in with `remind logout`.",
                who(stored)
            ),
        )?;
        return Ok(None);
    }
    let label = args.label.clone().unwrap_or_else(default_label);
    let started = sign_in.start_device(Some(&label)).await?;
    let opened = args.open && open_browser(started.browser_url());
    ctx.out.progress(
        &json!({
            "event": "device_code",
            "user_code": started.user_code,
            "verification_uri": started.verification_uri,
            "verification_uri_complete": started.verification_uri_complete,
            "expires_at": rfc3339(started.expires_at),
            "interval": started.interval,
            "browser_opened": opened,
        }),
        &format!(
            "To sign in to Remind, open {} and enter the code\n\n    {}\n\n{}Waiting for approval; the code expires in {} minutes (Ctrl-C to cancel).\nSilicons sign in with `{SILICON_SIGN_IN}` instead.",
            started.verification_uri,
            started.user_code,
            if opened {
                "Your browser was opened on that page.\n".to_owned()
            } else {
                format!("(Direct link: {})\n", started.browser_url())
            },
            started.expires_in.div_ceil(60),
        ),
    );
    let out = ctx.out;
    let wait = sign_in.wait_for_device(&started, |progress| match progress {
        DeviceProgress::SlowDown { interval } => out.progress(
            &json!({"event": "slow_down", "interval": interval}),
            &format!(
                "Silicon Accounts asked to check less often; checking every {interval} seconds."
            ),
        ),
        DeviceProgress::Retrying { message } => out.progress(
            &json!({"event": "retrying", "message": message}),
            &format!("warning: {message} Still waiting."),
        ),
        _ => {}
    });
    tokio::select! {
        tokens = wait => Ok(Some(tokens?)),
        _ = tokio::signal::ctrl_c() => Err(CliError::new(
            "interrupted",
            EXIT_INTERRUPTED,
            format!("Stopped waiting for approval of the code {}.", started.user_code),
            "Run `remind login` again for a new code.",
        )
        .into()),
    }
}

/// Checks the new tokens with Remind, saves them (replacing and ending any earlier sign-in
/// in this slot) and reports who signed in.
async fn finish(ctx: &Ctx, sign_in: &SignIn, tokens: Tokens, method: &str) -> anyhow::Result<u8> {
    let mut stored = stored_from(tokens, sign_in.accounts_url(), sign_in.app_id(), method);
    let client = sign_in_client(ctx)?.with_session(stored.access_token.clone())?;
    let (identity, warning) = match client.me().await {
        Ok(identity) => (Some(identity), None),
        Err(
            error @ ClientError::Api {
                status: 401 | 403, ..
            },
        ) => {
            let _ = sign_in.revoke(&stored.refresh_token).await;
            return Err(CliError::new(
                "token_refused_by_remind",
                if error.status() == Some(403) { EXIT_FORBIDDEN } else { EXIT_AUTH },
                format!(
                    "Silicon Accounts at {} signed {} in, but Remind at {} refused the token ({}: {}). Nothing was saved and that sign-in was ended.",
                    sign_in.accounts_url(),
                    stored.account.id,
                    ctx.url,
                    error.code(),
                    error.message()
                ),
                "Check that --url / REMIND_URL and --accounts-url / ACCOUNTS_URL name the same Remind deployment, then sign in again.",
            )
            .into());
        }
        Err(error) => (None, Some(error.to_string())),
    };
    if let Some(identity) = &identity {
        stored.account.id.clone_from(&identity.id);
        if !identity.display_name.is_empty() {
            stored
                .account
                .display_name
                .clone_from(&identity.display_name);
        }
    }
    let slot = ctx.sign_in_slot();
    let saved = stored.clone();
    let (previous, notices) = ctx
        .home()?
        .update(move |state| Ok(state.sign_ins.insert(slot, saved)))?;
    for notice in notices {
        ctx.out.warn(&notice);
    }
    if let Some(previous) = previous
        && previous.refresh_token != stored.refresh_token
        && let Ok(old) = ctx.sign_in_at(&previous.accounts_url, &previous.app_id)
        && old.revoke(&previous.refresh_token).await.is_err()
    {
        ctx.out.warn(&format!(
            "The previous sign-in here ({}) could not be ended at Silicon Accounts; end it from your list of sign-ins on the account site.",
            previous.account.id
        ));
    }
    if let Some(warning) = &warning {
        ctx.out.warn(&format!(
            "Signed in, but Remind at {} could not confirm it yet: {warning}",
            ctx.url
        ));
    }
    let fallback = ctx.test.is_some() && ctx.explicit_test.is_none();
    let mut body = status_json(
        ctx,
        &stored,
        identity.is_some(),
        fallback,
        identity.as_ref(),
    );
    body["method"] = json!(method);
    if let Some(warning) = warning {
        body["warning"] = json!(warning);
    }
    ctx.out.either(
        &body,
        &signed_in_text(ctx, &stored, identity.is_some(), fallback),
    )?;
    ctx.out.suggest(&next_steps(&stored));
    Ok(EXIT_OK)
}

/// The Remind client a new sign-in is checked with: inside the test environment named with
/// `--test`, else production (a selected environment uses the production sign-in).
fn sign_in_client(ctx: &Ctx) -> anyhow::Result<silicon_remind_client::Client> {
    if ctx.explicit_test.is_some() {
        ctx.client()
    } else {
        ctx.production_client()
    }
}

fn next_steps(stored: &StoredSignIn) -> String {
    match stored.account.kind {
        AccountKind::Silicon => "Next: remind webhook subscribe <url> (where due reminders go), then remind create --text '…' --cron '0 9 * * *' --timezone <IANA timezone>.".into(),
        AccountKind::Carbon => "Next: remind silicons (the Silicons whose reminders you can read), then remind list --silicon si:<id>.".into(),
    }
}

fn who(stored: &StoredSignIn) -> String {
    if stored.account.display_name.is_empty() {
        stored.account.id.clone()
    } else {
        format!("{} ({})", stored.account.id, stored.account.display_name)
    }
}

/// `{"authenticated":true,…}` for a saved sign-in.
fn status_json(
    ctx: &Ctx,
    stored: &StoredSignIn,
    verified: bool,
    fallback: bool,
    identity: Option<&Identity>,
) -> Value {
    let mut body = json!({
        "authenticated": true,
        "uuid": stored.account.uuid,
        "id": stored.account.id,
        "kind": stored.account.kind.as_str(),
        "expires_at": rfc3339(stored.expires_at),
        "refresh_expires_at": stored.refresh_expires_at.map(rfc3339),
        "verified": verified,
        "url": ctx.url,
        "accounts_url": stored.accounts_url,
        "app_id": stored.app_id,
        "method": stored.method,
    });
    if !stored.account.display_name.is_empty() {
        body["display_name"] = json!(stored.account.display_name);
    }
    if let Some(custodian) = &stored.account.custodian {
        body["custodian"] = json!({"uuid": custodian.uuid, "id": custodian.id});
    }
    if let Some(identity) = identity {
        body["can_manage_reminders"] = json!(identity.can_manage_reminders);
        body["visible_silicons"] = json!(identity.visible_silicons);
    }
    if let Some(id) = ctx.test {
        body["test_environment"] = json!(id);
        if fallback {
            body["uses_production_sign_in"] = json!(true);
        }
    }
    body
}

fn signed_in_text(ctx: &Ctx, stored: &StoredSignIn, verified: bool, fallback: bool) -> String {
    let now = now();
    let mut rows = vec![
        ("uuid", stored.account.uuid.clone()),
        ("remind", ctx.url.clone()),
        ("accounts", stored.accounts_url.clone()),
    ];
    if let Some(custodian) = &stored.account.custodian {
        rows.push(("custodian", custodian.id.clone()));
    }
    if let Some(id) = ctx.test {
        rows.push((
            "test environment",
            if fallback {
                format!("{id} (using the production sign-in)")
            } else {
                id.to_string()
            },
        ));
    }
    rows.push((
        "access token",
        format!("{} (refreshed automatically)", when(stored.expires_at, now)),
    ));
    rows.push((
        "sign-in ends",
        stored
            .refresh_expires_at
            .map_or_else(|| "not stated".into(), |at| when(at, now)),
    ));
    rows.push((
        "verified",
        if verified {
            "yes, Remind accepted it just now"
        } else {
            "no (not checked with Remind)"
        }
        .into(),
    ));
    format!(
        "Signed in to Remind as {}, a {}.\n{}",
        who(stored),
        stored.account.kind.title(),
        key_values(&rows)
    )
}

fn default_label() -> String {
    let host = std::process::Command::new("hostname")
        .output()
        .ok()
        .filter(|output| output.status.success())
        .and_then(|output| String::from_utf8(output.stdout).ok())
        .map(|host| host.trim().to_owned())
        .filter(|host| !host.is_empty());
    let label = match host {
        Some(host) => format!("remind on {host}"),
        None => format!(
            "remind on {} {}",
            std::env::consts::OS,
            std::env::consts::ARCH
        ),
    };
    label.chars().take(100).collect()
}

fn open_browser(url: &str) -> bool {
    let mut command = if cfg!(target_os = "macos") {
        std::process::Command::new("open")
    } else if cfg!(windows) {
        let mut command = std::process::Command::new("cmd");
        command.args(["/C", "start", ""]);
        command
    } else {
        std::process::Command::new("xdg-open")
    };
    command
        .arg(url)
        .stdout(std::process::Stdio::null())
        .stderr(std::process::Stdio::null())
        .status()
        .is_ok_and(|status| status.success())
}

/// What checking a saved sign-in with Remind found.
enum Check {
    /// Remind accepted it just now.
    Verified(Box<(Identity, StoredSignIn)>),
    /// It no longer works: a stable reason and a message.
    Ended(String, String),
    /// It could not be checked (network, Remind or Silicon Accounts unavailable).
    Unverified(String),
}

async fn check(ctx: &Ctx) -> Check {
    let mut authed = match crate::session::Authed::new(ctx).await {
        Ok(authed) => authed,
        Err(error) => return classify(&error),
    };
    match authed.call(async |client| client.me().await).await {
        Ok(identity) => Check::Verified(Box::new((identity, authed.sign_in().clone()))),
        Err(error) => classify(&error),
    }
}

fn classify(error: &anyhow::Error) -> Check {
    if let Some(error) = error.downcast_ref::<CliError>()
        && matches!(
            error.code,
            "sign_in_ended" | "not_signed_in" | "sign_in_changed"
        )
    {
        return Check::Ended(error.code.into(), error.message.clone());
    }
    if let Some(
        error @ ClientError::Api {
            status: 401 | 403, ..
        },
    ) = error.downcast_ref::<ClientError>()
    {
        return Check::Ended(error.code().into(), error.message());
    }
    if let Some(error @ ClientError::SignInRefused { .. }) = error.downcast_ref::<ClientError>() {
        return Check::Ended(error.code().into(), error.message());
    }
    Check::Unverified(format!("{error:#}"))
}

/// `remind login status [--offline]`. With `--json` it always exits 0.
pub async fn status(ctx: &Ctx, offline: bool) -> anyhow::Result<u8> {
    if let Err(error) = ctx.home() {
        return signed_out(ctx, Some(("home_unavailable", error.to_string())));
    }
    if let Some(reason) = &ctx.snapshot.unreadable {
        return signed_out(
            ctx,
            Some((
                "state_unreadable",
                format!(
                    "The saved state could not be read ({reason}); signing in again moves it aside."
                ),
            )),
        );
    }
    let Some((_, stored, fallback)) = ctx.effective(&ctx.snapshot.state) else {
        let reason = ctx.snapshot.legacy_sign_ins.then(|| {
            (
                "sign_in_again",
                "The sign-in saved by an earlier Remind no longer works; sign in again.".to_owned(),
            )
        });
        return signed_out(ctx, reason);
    };
    if stored.ended(now()) {
        return signed_out(
            ctx,
            Some((
                "sign_in_ended",
                format!("The sign-in of {} ended; sign in again.", stored.account.id),
            )),
        );
    }
    if offline {
        let body = status_json(ctx, &stored, false, fallback, None);
        ctx.out
            .either(&body, &signed_in_text(ctx, &stored, false, fallback))?;
        return Ok(EXIT_OK);
    }
    match check(ctx).await {
        Check::Verified(verified) => {
            let (identity, mut current) = *verified;
            if identity.id != current.account.id
                || (!identity.display_name.is_empty()
                    && identity.display_name != current.account.display_name)
            {
                current.account.id.clone_from(&identity.id);
                if !identity.display_name.is_empty() {
                    current
                        .account
                        .display_name
                        .clone_from(&identity.display_name);
                }
                remember_profile(ctx, &current);
            }
            let body = status_json(ctx, &current, true, fallback, Some(&identity));
            ctx.out
                .either(&body, &signed_in_text(ctx, &current, true, fallback))?;
            Ok(EXIT_OK)
        }
        Check::Ended(code, message) => signed_out(ctx, Some((&code, message))),
        Check::Unverified(warning) => {
            ctx.out.warn(&format!(
                "{warning} Reporting the saved sign-in without checking it."
            ));
            let mut body = status_json(ctx, &stored, false, fallback, None);
            body["warning"] = json!(warning);
            ctx.out
                .either(&body, &signed_in_text(ctx, &stored, false, fallback))?;
            Ok(EXIT_OK)
        }
    }
}

/// Saves the account's current id and display name after Remind reported a change.
fn remember_profile(ctx: &Ctx, current: &StoredSignIn) {
    let Ok(home) = ctx.home() else { return };
    let current = current.clone();
    let _ = home.update(move |state| {
        for stored in state.sign_ins.values_mut() {
            if stored.account.uuid == current.account.uuid {
                stored.account.id.clone_from(&current.account.id);
                stored
                    .account
                    .display_name
                    .clone_from(&current.account.display_name);
            }
        }
        Ok(())
    });
}

fn signed_out(ctx: &Ctx, reason: Option<(&str, String)>) -> anyhow::Result<u8> {
    let mut body = json!({"authenticated": false});
    let mut text = match ctx.test {
        Some(id) => format!(
            "Not signed in to Remind at {} (test environment {id}).",
            ctx.url
        ),
        None => format!("Not signed in to Remind at {}.", ctx.url),
    };
    if let Some((code, message)) = reason {
        body["reason"] = json!(code);
        body["message"] = json!(message);
        text = format!("{text}\n{message}");
    }
    ctx.out.either(&body, &text)?;
    ctx.out.suggest(&format!(
        "Sign in: `remind login` (Carbons) or `{SILICON_SIGN_IN}` (Silicons)."
    ));
    Ok(if ctx.out.json { EXIT_OK } else { 1 })
}

/// `remind logout`: ends this machine's sign-in at Silicon Accounts and forgets it.
pub async fn logout(ctx: &Ctx) -> anyhow::Result<u8> {
    let home = ctx.home()?;
    let own = ctx.sign_in_slot();
    let nothing = |ctx: &Ctx| -> anyhow::Result<u8> {
        let (reason, text) = if ctx.explicit_test.is_some()
            && ctx
                .effective_for(&ctx.snapshot.state, ctx.explicit_test)
                .is_some()
        {
            (
                "no_test_environment_sign_in",
                "This test environment has no sign-in of its own; it uses your production sign-in. End that with `remind logout` (without --test).".to_owned(),
            )
        } else {
            (
                "not_signed_in",
                format!(
                    "You were not signed in to Remind at {}; nothing to do.",
                    ctx.url
                ),
            )
        };
        ctx.out
            .either(&json!({"signed_out": false, "reason": reason}), &text)?;
        Ok(EXIT_OK)
    };
    if !ctx.snapshot.state.sign_ins.contains_key(&own) {
        return nothing(ctx);
    }
    let (removed, notices) = home.update(move |state| Ok(state.sign_ins.remove(&own)))?;
    for notice in notices {
        ctx.out.warn(&notice);
    }
    let Some(stored) = removed else {
        return nothing(ctx);
    };
    let revoked = match ctx.sign_in_at(&stored.accounts_url, &stored.app_id) {
        Ok(sign_in) => match sign_in.revoke(&stored.refresh_token).await {
            Ok(revoked) => revoked,
            Err(error) => {
                ctx.out.warn(&format!(
                    "Could not end the sign-in at Silicon Accounts ({}). It is deleted here anyway; end it from your list of sign-ins on the account site.",
                    error.message()
                ));
                false
            }
        },
        Err(error) => {
            ctx.out.warn(&format!("{error:#}"));
            false
        }
    };
    ctx.out.either(
        &json!({
            "signed_out": true,
            "uuid": stored.account.uuid,
            "id": stored.account.id,
            "kind": stored.account.kind.as_str(),
            "revoked": revoked,
        }),
        &format!("Signed out of Remind at {}: {}.", ctx.url, who(&stored)),
    )?;
    ctx.out.suggest(&format!(
        "Sign in again with `remind login` (Carbons) or `{SILICON_SIGN_IN}` (Silicons)."
    ));
    Ok(EXIT_OK)
}