Skip to content

Commit

Permalink
[CLI] fixup shared recovery
Browse files Browse the repository at this point in the history
  • Loading branch information
AureliaDolo committed Dec 20, 2024
1 parent 8c7c0ee commit f406ad7
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 24 deletions.
27 changes: 13 additions & 14 deletions cli/src/commands/invite/claim.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

use std::{path::PathBuf, sync::Arc};

use anyhow::anyhow;
use libparsec::{
internal::{
claimer_retrieve_info, AnyClaimRetrievedInfoCtx, DeviceClaimFinalizeCtx,
Expand All @@ -20,7 +21,7 @@ use libparsec_client::{
};

use crate::utils::*;
use dialoguer::Select;
use dialoguer::{Input, Select};

crate::clap_parser_with_shared_opts_builder!(
#[with = config_dir, data_dir, password_stdin]
Expand Down Expand Up @@ -151,10 +152,10 @@ async fn step0(
fn step05_shamir(
ctx: &ShamirRecoveryClaimPickRecipientCtx,
) -> anyhow::Result<ShamirRecoveryClaimInitialCtx> {
let recipients = ctx.recipients();
let recipients = ctx.yet_to_contact_recipients();
let human_recipients: Vec<_> = recipients.iter().map(|r| r.human_handle.clone()).collect();
let selection = Select::new()
.with_prompt("Choose a person to contact first")
.with_prompt("Choose a person to contact now")
.items(&human_recipients)
.interact()?;
Ok(ctx.pick_recipient(recipients[selection].user_id)?)
Expand Down Expand Up @@ -246,18 +247,17 @@ async fn step2_device(ctx: DeviceClaimInProgress1Ctx) -> anyhow::Result<DeviceCl
async fn step2_shamir(
ctx: ShamirRecoveryClaimInProgress1Ctx,
) -> anyhow::Result<ShamirRecoveryClaimInProgress2Ctx> {
let mut input = String::new();
let sas_codes = ctx.generate_greeter_sas_choices(3);

for (i, sas_code) in sas_codes.iter().enumerate() {
println!(" {i} - {YELLOW}{sas_code}{RESET}")
let selected_sas = Select::new()
.items(&sas_codes)
.with_prompt("Select code provided by greeter")
.interact()?;
if &sas_codes[selected_sas] != ctx.greeter_sas() {
Err(anyhow!("Invalid SAS code"))
} else {
Ok(ctx.do_signify_trust().await?)
}

println!("Select code provided by greeter (0, 1, 2)");

choose_sas_code(&mut input, &sas_codes, ctx.greeter_sas())?;

Ok(ctx.do_signify_trust().await?)
}

/// Step 3: wait peer trust
Expand Down Expand Up @@ -356,8 +356,7 @@ async fn step4_shamir(
async fn step5_shamir(
ctx: ShamirRecoveryClaimRecoverDeviceCtx,
) -> anyhow::Result<ShamirRecoveryClaimMaybeFinalizeCtx> {
let mut input = String::new();
let device_label = choose_device_label(&mut input)?;
let device_label = Input::new().with_prompt("Enter device label").interact()?;

let mut handle = start_spinner("Waiting for greeter".into());

Expand Down
19 changes: 10 additions & 9 deletions cli/src/commands/invite/greet.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Parsec Cloud (https://parsec.cloud) Copyright (c) BUSL-1.1 2016-present Scille SAS

use anyhow::Context;
use dialoguer::Select;
use libparsec::{
authenticated_cmds::latest::invite_list::InviteListItem,
internal::{
Expand Down Expand Up @@ -83,6 +84,7 @@ async fn step0(
let invitation = match invitations.into_iter().find(|invitation| match invitation {
InviteListItem::User { token, .. } if *token == invitation_token => true,
InviteListItem::Device { token, .. } if *token == invitation_token => true,
InviteListItem::ShamirRecovery { token, .. } if *token == invitation_token => true,
_ => false,
}) {
Some(invitation) => invitation,
Expand Down Expand Up @@ -213,17 +215,16 @@ async fn step3_device(ctx: DeviceGreetInProgress2Ctx) -> anyhow::Result<DeviceGr
async fn step3_shamir(
ctx: ShamirRecoveryGreetInProgress2Ctx,
) -> anyhow::Result<ShamirRecoveryGreetInProgress3Ctx> {
let mut input = String::new();
let sas_codes = ctx.generate_claimer_sas_choices(3);
for (i, sas_code) in sas_codes.iter().enumerate() {
println!(" {i} - {YELLOW}{sas_code}{RESET}")
let selected_sas = Select::new()
.items(&sas_codes)
.with_prompt("Select code provided by claimer")
.interact()?;
if &sas_codes[selected_sas] != ctx.claimer_sas() {
Err(anyhow::anyhow!("Invalid SAS code"))
} else {
Ok(ctx.do_signify_trust().await?)
}

println!("Select code provided by claimer (0, 1, 2)");

choose_sas_code(&mut input, &sas_codes, ctx.claimer_sas())?;

Ok(ctx.do_signify_trust().await?)
}

/// Step 4: get claim requests
Expand Down
7 changes: 7 additions & 0 deletions libparsec/crates/client/src/invite/claimer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,13 @@ impl ShamirRecoveryClaimPickRecipientCtx {
&self.claimer_human_handle
}

pub fn yet_to_contact_recipients(&self) -> Vec<&ShamirRecoveryRecipient> {
self.recipients
.iter()
.filter(|r| !self.shares.contains_key(&r.user_id))
.collect()
}

pub fn is_recoverable(&self) -> bool {
self.recipients
.iter()
Expand Down
2 changes: 1 addition & 1 deletion libparsec/crates/types/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ pub enum DataError {
#[error("Invalid encryption")]
Decryption,

#[error("Invalid serialization: format {} step {step}", match .format { Some(format) => format!("{}", format), None => "<unknown>".to_string() })]
#[error("Invalid serialization: format {} step <{step}>", match .format { Some(format) => format!("{}", format), None => "<unknown>".to_string() })]
BadSerialization {
format: Option<u8>,
step: &'static str,
Expand Down

0 comments on commit f406ad7

Please sign in to comment.