Skip to content

Commit

Permalink
feat: allow prompting for credentials
Browse files Browse the repository at this point in the history
  • Loading branch information
taturosati committed Aug 3, 2024
1 parent a49b3b0 commit 3ae2601
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 25 deletions.
22 changes: 22 additions & 0 deletions cli/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ app_dirs2 = "2.5.5"
clap = { version = "4.2.7", features = ["derive"] }
passphrasex_common = { version = "0.2.0", path = "../common" }
reqwest = { version = "0.11.18", features = ["json"] }
rpassword = "7.3.1"
serde = { version = "1.0.163", features = ["serde_derive"] }
serde_json = "1.0.96"
tokio = { version = "1.28.1", features = ["macros", "rt-multi-thread"] }
7 changes: 5 additions & 2 deletions cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ use std::string::String;

use app_dirs2::AppInfo;

use crate::file::{read_app_data, read_private_key, read_signing_key, write_app_data, write_private_key, write_signing_key};
use crate::file::{
read_app_data, read_private_key, read_signing_key, write_app_data, write_private_key,
write_signing_key,
};

use passphrasex_common::crypto::asymmetric::{KeyPair, SeedPhrase};
use passphrasex_common::model::password::Password;
Expand Down Expand Up @@ -80,7 +83,7 @@ impl App {
let key_pair = KeyPair::try_from_private_keys(
private_key_enc.as_slice(),
signing_key_enc.as_slice(),
device_pass
device_pass,
)?;

let api = Api::new(key_pair.clone());
Expand Down
81 changes: 58 additions & 23 deletions cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@ enum Commands {
/// Create your credentials
Register {
#[clap(short, long)]
device_pass: String,
device_pass: Option<String>,
},
/// Authenticate device using your seed phrase
Login {
#[clap(short, long)]
seed_phrase: String,
seed_phrase: Option<String>,
#[clap(short, long)]
device_pass: String,
device_pass: Option<String>,
},
/// Add a new password
Add {
Expand All @@ -38,9 +38,9 @@ enum Commands {
#[clap(short, long)]
username: String,
#[clap(short, long)]
password: String,
password: Option<String>,
#[clap(short, long)]
device_pass: String,
device_pass: Option<String>,
},
/// Get a password
Get {
Expand All @@ -49,7 +49,7 @@ enum Commands {
#[clap(short, long)]
username: Option<String>,
#[clap(short, long)]
device_pass: String,
device_pass: Option<String>,
},
/// Modify a password
Edit {
Expand All @@ -58,9 +58,9 @@ enum Commands {
#[clap(short, long)]
username: String,
#[clap(short, long)]
password: String,
password: Option<String>,
#[clap(short, long)]
device_pass: String,
device_pass: Option<String>,
},
/// Delete a password
Delete {
Expand All @@ -69,7 +69,7 @@ enum Commands {
#[clap(short, long)]
username: String,
#[clap(short, long)]
device_pass: String,
device_pass: Option<String>,
},
/// Generate a random password
Generate {
Expand All @@ -83,17 +83,24 @@ async fn main() -> Result<(), Box<dyn Error>> {
let args = Args::parse();

match args.command {
Commands::Register { device_pass } => match register(&device_pass).await {
Ok(seed_phrase) => println!(
"Successfully registered!\nYour seed phrase is: \n{}",
seed_phrase.get_phrase()
),
Err(e) => println!("Failed to create user: {}", e),
},
Commands::Register { device_pass } => {
match register(&get_or_prompt_device_password(device_pass)).await {
Ok(seed_phrase) => println!(
"Successfully registered!\nYour seed phrase is: \n{}",
seed_phrase.get_phrase()
),
Err(e) => println!("Failed to create user: {}", e),
}
}
Commands::Login {
seed_phrase,
device_pass,
} => match auth_device(&seed_phrase, &device_pass).await {
} => match auth_device(
&get_or_prompt_seed_phrase(seed_phrase),
&get_or_prompt_device_password(device_pass),
)
.await
{
Ok(_) => println!("Successfully authenticated!"),
Err(e) => println!("Failed to authenticate: {}", e),
},
Expand All @@ -103,9 +110,9 @@ async fn main() -> Result<(), Box<dyn Error>> {
password,
device_pass,
} => {
match App::new(&device_pass)
match App::new(&get_or_prompt_device_password(device_pass))
.await?
.add(site, username, password)
.add(site, username, get_or_prompt_password(password))
.await
{
Ok(_) => println!("Password added successfully"),
Expand All @@ -116,7 +123,11 @@ async fn main() -> Result<(), Box<dyn Error>> {
site,
username,
device_pass,
} => match App::new(&device_pass).await?.get(site, username).await {
} => match App::new(&get_or_prompt_device_password(device_pass))
.await?
.get(site, username)
.await
{
Ok(passwords) => {
for credential in passwords {
println!(
Expand All @@ -133,9 +144,9 @@ async fn main() -> Result<(), Box<dyn Error>> {
password,
device_pass,
} => {
match App::new(&device_pass)
match App::new(&get_or_prompt_device_password(device_pass))
.await?
.edit(site, username, password)
.edit(site, username, get_or_prompt_password(password))
.await
{
Ok(_) => println!("Password edited successfully"),
Expand All @@ -146,7 +157,11 @@ async fn main() -> Result<(), Box<dyn Error>> {
site,
username,
device_pass,
} => match App::new(&device_pass).await?.delete(site, username).await {
} => match App::new(&get_or_prompt_device_password(device_pass))
.await?
.delete(site, username)
.await
{
Ok(_) => println!("Password deleted successfully"),
Err(e) => println!("Failed to delete password: {}", e),
},
Expand All @@ -157,3 +172,23 @@ async fn main() -> Result<(), Box<dyn Error>> {

Ok(())
}

fn get_or_prompt(password: Option<String>, name: &str) -> String {
if let Some(password) = password {
password
} else {
rpassword::prompt_password(format!("Enter {name}:")).expect("Failed to read password")
}
}

fn get_or_prompt_device_password(password: Option<String>) -> String {
get_or_prompt(password, "device password")
}

fn get_or_prompt_seed_phrase(password: Option<String>) -> String {
get_or_prompt(password, "seed phrase")
}

fn get_or_prompt_password(password: Option<String>) -> String {
get_or_prompt(password, "site password")
}

0 comments on commit 3ae2601

Please sign in to comment.