Skip to content

Commit

Permalink
Use dialoguer's select in each invitation process.
Browse files Browse the repository at this point in the history
  • Loading branch information
AureliaDolo committed Jan 17, 2025
1 parent bdf1be4 commit acd5060
Show file tree
Hide file tree
Showing 8 changed files with 327 additions and 251 deletions.
31 changes: 5 additions & 26 deletions cli/src/commands/invite/claim.rs
Original file line number Diff line number Diff line change
Expand Up @@ -233,32 +233,18 @@ async fn step1_shamir(

/// Step 2: signify trust
async fn step2_user(ctx: UserClaimInProgress1Ctx) -> anyhow::Result<UserClaimInProgress2Ctx> {
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}")
}

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

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

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

/// Step 2: signify trust
async fn step2_device(ctx: DeviceClaimInProgress1Ctx) -> anyhow::Result<DeviceClaimInProgress2Ctx> {
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}")
}

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

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

Ok(ctx.do_signify_trust().await?)
}
Expand All @@ -269,16 +255,9 @@ async fn step2_shamir(
) -> anyhow::Result<ShamirRecoveryClaimInProgress2Ctx> {
let sas_codes = ctx.generate_greeter_sas_choices(3);

let selected_sas = Select::new()
.default(0)
.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?)
}
choose_sas_code(&sas_codes, ctx.greeter_sas(), "greeter")?;

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

/// Step 3: wait peer trust
Expand Down
41 changes: 7 additions & 34 deletions cli/src/commands/invite/greet.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// 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 @@ -34,11 +33,7 @@ pub async fn device_greet(args: Args, client: &StartedClient) -> anyhow::Result<
let Args { token, .. } = args;
log::trace!("Greeting invitation");

{
let mut spinner = start_spinner("Poll server for new certificates".into());
client.poll_server_for_new_certificates().await?;
spinner.stop_with_symbol(GREEN_CHECKMARK);
}
poll_server_for_new_certificates(client).await?;

let invitation = step0(client, token).await?;

Expand Down Expand Up @@ -183,30 +178,16 @@ async fn step2_shamir(

/// Step 3: signify trust
async fn step3_user(ctx: UserGreetInProgress2Ctx) -> anyhow::Result<UserGreetInProgress3Ctx> {
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}")
}

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

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

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

/// Step 3: signify trust
async fn step3_device(ctx: DeviceGreetInProgress2Ctx) -> anyhow::Result<DeviceGreetInProgress3Ctx> {
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}")
}

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

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

Ok(ctx.do_signify_trust().await?)
}
Expand All @@ -216,16 +197,9 @@ async fn step3_shamir(
ctx: ShamirRecoveryGreetInProgress2Ctx,
) -> anyhow::Result<ShamirRecoveryGreetInProgress3Ctx> {
let sas_codes = ctx.generate_claimer_sas_choices(3);
let selected_sas = Select::new()
.default(0)
.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?)
}
choose_sas_code(&sas_codes, ctx.claimer_sas(), "claimer")?;

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

/// Step 4: get claim requests
Expand All @@ -245,13 +219,12 @@ async fn step4_shamir(ctx: ShamirRecoveryGreetInProgress3Ctx) -> anyhow::Result<

/// Step 5: create new user
async fn step5_user(ctx: UserGreetInProgress4Ctx) -> anyhow::Result<()> {
let mut input = String::new();
let device_label = ctx.requested_device_label.clone();
let human_handle = ctx.requested_human_handle.clone();
println!("New device label: [{device_label}]");
println!("New user: [{human_handle}]");

let profile = choose_user_profile(&mut input)?;
let profile = choose_user_profile()?;

let mut handle = start_spinner("Creating the user in the server".into());

Expand Down
6 changes: 1 addition & 5 deletions cli/src/commands/invite/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,7 @@ crate::build_main_with_client!(main, list_invite);

pub async fn list_invite(_args: Args, client: &StartedClient) -> anyhow::Result<()> {
log::trace!("Listing invitations");
{
let mut spinner = start_spinner("Poll server for new certificates".into());
client.poll_server_for_new_certificates().await?;
spinner.stop_with_symbol(GREEN_CHECKMARK);
}
poll_server_for_new_certificates(client).await?;

let mut handle = start_spinner("Listing invitations".into());

Expand Down
46 changes: 23 additions & 23 deletions cli/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

use std::{fmt::Display, ops::Deref, path::Path, sync::Arc};

use anyhow::anyhow;
use dialoguer::Select;
use libparsec::{
internal::{Client, EventBus},
list_available_devices, AuthenticatedCmds, AvailableDevice, DeviceAccessStrategy,
Expand Down Expand Up @@ -398,35 +400,33 @@ pub fn choose_human_handle(input: &mut String) -> anyhow::Result<HumanHandle> {
}

pub fn choose_sas_code(
input: &mut String,
sas_codes: &[SASCode],
expected: &SASCode,
provided_by: &str,
) -> anyhow::Result<()> {
std::io::stdin().read_line(input)?;

match sas_codes.get(input.trim().parse::<usize>()?) {
Some(sas_code) if sas_code == expected => Ok(()),
Some(_) => Err(anyhow::anyhow!("Invalid SAS code")),
None => Err(anyhow::anyhow!("Invalid input")),
let selected_sas = Select::new()
.default(0)
.items(sas_codes)
.with_prompt(format!("Select code provided by {provided_by}"))
.interact()?;
if &sas_codes[selected_sas] != expected {
return Err(anyhow!("Invalid SAS code"));
}
Ok(())
}

pub fn choose_user_profile(input: &mut String) -> anyhow::Result<UserProfile> {
println!("Which profile? (0, 1, 2)");
println!(" 0 - {YELLOW}Standard{RESET}");
println!(" 1 - {YELLOW}Admin{RESET}");
println!(" 2 - {YELLOW}Outsider{RESET}");
loop {
input.clear();
std::io::stdin().read_line(input)?;

match input.trim() {
"0" => return Ok(UserProfile::Standard),
"1" => return Ok(UserProfile::Admin),
"2" => return Ok(UserProfile::Outsider),
_ => eprintln!("Invalid input, choose between 0, 1 or 2"),
}
}
pub fn choose_user_profile() -> anyhow::Result<UserProfile> {
let profiles = [
UserProfile::Standard,
UserProfile::Admin,
UserProfile::Outsider,
];
let selected_profile = Select::new()
.default(0)
.items(&profiles)
.with_prompt("Which profile?")
.interact()?;
Ok(profiles[selected_profile])
}

pub async fn poll_server_for_new_certificates(client: &StartedClient) -> anyhow::Result<()> {
Expand Down
Loading

0 comments on commit acd5060

Please sign in to comment.