pwdm 0.3.1

Rudimentary command-line tool and Rust library for managing passwords.
Documentation
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
/*
Copyright 2024 Owain Davies
SPDX-License-Identifier: Apache-2.0
*/
mod cli;

use clap::Parser;
use cli::{Error, Result};
use colored::*;
use dialoguer::{theme::ColorfulTheme, Confirm, Input, Password, Select};
use pwdg::{PwdGen, PwdGenOptions};
use pwdm::{
  Error::{IncorrectMasterPassword, WeakPassword},
  PwdManager,
};
use std::path::{Path, PathBuf};

const PWDGEN_OPTIONS: Option<PwdGenOptions> = Some(PwdGenOptions {
  min_upper: 2,
  min_lower: 2,
  min_digit: 2,
  min_special: 2,
  exclude: None,
});
const PWDGEN_LEN: usize = 16;

/// Password Manager CLI
#[derive(Parser, Debug)]
#[clap(about, version, author)]
struct Args {
  /// Path to the database file.
  #[clap(short, long, value_parser = clap::value_parser!(std::path::PathBuf))]
  path: Option<std::path::PathBuf>,
}

fn main() -> Result<()> {
  let args = Args::parse();

  let db_path = determine_path(args)?;
  ensure_path_dir_exists(&db_path)?;

  let path = &db_path.to_string_lossy();

  let found_signature = PwdManager::found_signature(path);

  let mut pwd_manager: PwdManager;
  loop {
    clear_screen()?;

    print_header(path);
    let master_password: String = Password::new()
      .with_prompt("Enter master password")
      .interact()?;

    if !found_signature {
      let confirm: String =
        Password::new().with_prompt("Repeat password").interact()?;
      if master_password != confirm {
        print_if_password_confirmation_fails();
        press_enter_to_continue();
        continue;
      }
    }
    match PwdManager::new(path, &master_password) {
      Ok(manager) => {
        pwd_manager = manager;
        break;
      }
      Err(WeakPassword(feedback)) => {
        print_if_weak_password(feedback);
      }
      Err(IncorrectMasterPassword) => {
        eprintln!("{}", format!("Error: {}", IncorrectMasterPassword).red());
        std::process::exit(1);
      }
      Err(e) => return Err(Error::Manager(e)),
    }

    press_enter_to_continue();
  }

  let pwdgen = PwdGen::new(PWDGEN_LEN, PWDGEN_OPTIONS)?;

  let mut last_action: Option<Action> = None;
  loop {
    clear_screen()?;

    print_header(path);
    let selection = match last_action.take() {
      Some(action) => {
        print_selected_action(action);
        action
      }
      _ => select_action()?,
    };

    match match_action(selection, &pwdgen, &mut pwd_manager)? {
      UserAction::Back => continue,
      UserAction::Continue => {}
      UserAction::ContinueWithMessage(msg) => println!("{}", msg),
      UserAction::Exit => break,
      UserAction::TryAgain(action) => last_action = Some(action),
    }

    press_enter_to_continue();
  }

  Ok(())
}

enum UserAction<T> {
  Back,
  Continue,
  ContinueWithMessage(T),
  Exit,
  TryAgain(Action),
}

#[derive(Debug, PartialEq, Clone, Copy)]
enum Action {
  Add,
  Get,
  Delete,
  Update,
  List,
  UpdateMaster,
  Exit,
}

const SELECTIONS: &[Action] = &[
  Action::Add,
  Action::Get,
  Action::Delete,
  Action::Update,
  Action::List,
  Action::UpdateMaster,
  Action::Exit,
];

impl core::fmt::Display for Action {
  fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
    write!(
      f,
      "{}",
      match self {
        Action::Add => "Add",
        Action::Get => "Get",
        Action::Delete => "Delete",
        Action::Update => "Update",
        Action::List => "List",
        Action::UpdateMaster => "Update Master Password",
        Action::Exit => "Exit",
      }
    )
  }
}

fn select_action() -> Result<Action> {
  let selection = Select::with_theme(&ColorfulTheme::default())
    .with_prompt("Choose action")
    .default(0)
    .items(SELECTIONS)
    .interact()?;
  Ok(SELECTIONS[selection])
}

fn match_action(
  selection: Action,
  pwdgen: &PwdGen,
  pwd_manager: &mut PwdManager,
) -> Result<UserAction<String>> {
  match selection {
    Action::Add => add_password(pwd_manager, pwdgen),
    Action::Get => get_password(pwd_manager),
    Action::Delete => delete_password(pwd_manager),
    Action::Update => update_password(pwd_manager, pwdgen),
    Action::List => {
      list_passwords(pwd_manager)?;
      Ok(UserAction::Continue)
    }
    Action::UpdateMaster => update_master_password(pwd_manager),
    Action::Exit => Ok(UserAction::Exit),
  }
}

fn do_action<F>(
  prompt: &str,
  mut action: F,
  is_password: bool,
) -> Result<UserAction<String>>
where
  F: FnMut(&str) -> Result<()>,
{
  let result = if is_password {
    password_with_back_option(prompt, "b")?
  } else {
    input_with_back_option(prompt, "b")?
  };

  match result {
    UserAction::Back => Ok(UserAction::Back),
    UserAction::ContinueWithMessage(msg) => {
      action(&msg)?;
      Ok(UserAction::Continue)
    }
    UserAction::TryAgain(f) => Ok(UserAction::TryAgain(f)),
    _ => panic!("Unexpected UserAction"),
  }
}

fn add_password(
  pwd_manager: &PwdManager,
  pwdgen: &PwdGen,
) -> Result<UserAction<String>> {
  do_action(
    "Enter ID",
    |id| {
      if pwd_manager.get_password(id)?.is_none() {
        let password: String = generate_password(pwdgen, "Enter password")?;
        pwd_manager.add_password(id, &password)?;
        println!("Password added.");
      } else {
        println!("{}", "Password exists.".red());
      }
      Ok(())
    },
    false,
  )
}

fn get_password(pwd_manager: &PwdManager) -> Result<UserAction<String>> {
  do_action(
    "Enter ID",
    |id| {
      match pwd_manager.get_password(id)? {
        Some(password) => println!("Password: {}", password),
        None => print_no_password_found_for_id(id),
      }
      Ok(())
    },
    false,
  )
}

fn delete_password(pwd_manager: &PwdManager) -> Result<UserAction<String>> {
  do_action(
    "Enter ID",
    |id| {
      if pwd_manager.get_password(id)?.is_none() {
        print_no_password_found_for_id(id);
      } else if Confirm::new()
        .with_prompt(format!(
          "Are you sure you want to delete password for ID {}",
          id
        ))
        .interact()?
      {
        pwd_manager.delete_password(id)?;
        println!("Password deleted.");
      }
      Ok(())
    },
    false,
  )
}

fn update_password(
  pwd_manager: &PwdManager,
  pwdgen: &PwdGen,
) -> Result<UserAction<String>> {
  do_action(
    "Enter ID",
    |id| {
      if pwd_manager.get_password(id)?.is_none() {
        print_no_password_found_for_id(id);
      } else {
        let new_password: String =
          generate_password(pwdgen, "Enter new password")?;
        pwd_manager.update_password(id, &new_password)?;
        println!("Password updated.");
      }
      Ok(())
    },
    false,
  )
}

fn list_passwords(pwd_manager: &PwdManager) -> Result<()> {
  let ids = pwd_manager.list_passwords()?;
  if ids.is_empty() {
    println!("No passwords stored.");
  } else {
    println!("Stored passwords:");
    for id in ids {
      println!("- {}", id);
    }
  }
  Ok(())
}

fn update_master_password(
  pwd_manager: &mut PwdManager,
) -> Result<UserAction<String>> {
  let action = |new_master_password: &str| {
    pwd_manager.update_master_password(new_master_password)?;
    println!("Master password updated.");
    Ok::<_, Error>(())
  };

  match do_action("Enter new master password", action, true) {
    Err(Error::Manager(WeakPassword(feedback))) => {
      print_if_weak_password(feedback);
      Ok(UserAction::TryAgain(Action::UpdateMaster))
    }
    other => other,
  }
}

fn generate_password(pwdgen: &PwdGen, prompt: &str) -> Result<String> {
  let use_autogenerated_password = Confirm::new()
    .with_prompt("Do you want to generate a password automatically?")
    .interact()?;
  Ok(if use_autogenerated_password {
    pwdgen.gen()
  } else {
    Password::new().with_prompt(prompt).interact()?
  })
}

fn clear_screen() -> Result<()> {
  clearscreen::ClearScreen::Terminfo.clear()?;
  Ok(())
}

fn input_with_back_option(
  prompt: &str,
  back_keyword: &str,
) -> Result<UserAction<String>> {
  let input: String = Input::new()
    .with_prompt(format!(
      "{} (or type '{}' to go back)",
      prompt, back_keyword
    ))
    .interact_text()?;

  if input == back_keyword {
    Ok(UserAction::Back)
  } else {
    Ok(UserAction::ContinueWithMessage(input))
  }
}

fn password_with_back_option(
  prompt: &str,
  back_keyword: &str,
) -> Result<UserAction<String>> {
  let input = Password::new()
    .with_prompt(format!("{} ('{}' to go back)", prompt, back_keyword))
    .interact()?;
  if input == back_keyword {
    return Ok(UserAction::Back);
  }

  let confirm = Password::new()
    .with_prompt(format!("Repeat password ('{}' to go back)", back_keyword))
    .interact()?;
  if confirm == back_keyword {
    return Ok(UserAction::Back);
  }

  if input != confirm {
    print_if_password_confirmation_fails();
    Ok(UserAction::TryAgain(Action::UpdateMaster))
  } else {
    if Confirm::new()
      .with_prompt("Are you sure you want to change the master password?")
      .interact()?
    {
      return Ok(UserAction::ContinueWithMessage(input));
    }
    Ok(UserAction::Back)
  }
}

fn determine_path(args: Args) -> Result<PathBuf> {
  args
    .path
    .or_else(|| {
      std::env::var("PWDM_PATH")
        .ok()
        .map(std::path::PathBuf::from)
        .or_else(|| {
          dirs::home_dir().map(|mut path| {
            path.push(".pwdm/passwords.db");
            path
          })
        })
    })
    .ok_or_else(|| Error::Path("Failed to determine database path"))
}

fn ensure_path_dir_exists(path: &Path) -> Result<()> {
  if let Some(parent_dir) = path.parent() {
    if !parent_dir.exists() {
      std::fs::create_dir_all(parent_dir)?;
    }
  }
  Ok(())
}

fn message_if_weak_password(opt: Option<zxcvbn::feedback::Feedback>) -> String {
  let mut details: String = String::new();

  if let Some(feedback) = opt {
    if let Some(warning) = feedback.warning() {
      details = format!("Warning: {}", warning);
    }

    let suggestions: Vec<String> = feedback
      .suggestions()
      .iter()
      .map(|s| s.to_string())
      .collect();

    if !suggestions.is_empty() {
      let sugg_str = suggestions.join(" ");
      details.push_str(&format!("\nSuggestions: {}", sugg_str));
    }
  }

  details
}

fn press_enter_to_continue() {
  println!("\nPress Enter to continue...");
  let _ = std::io::stdin().read_line(&mut String::new());
}

fn print_if_weak_password(feedback: Option<zxcvbn::feedback::Feedback>) {
  println!("{}", "\nThe password entered is weak.".red());
  let msg = message_if_weak_password(feedback).red();
  if !msg.is_empty() {
    println!("{}", msg);
  }
  println!("{}", "Please try again.".red());
}

fn print_header(path: &str) {
  println!("{}", "pwdm - Password Manager".bright_magenta().bold());
  println!("{}: {}\n", "Database".green(), path);
}

fn print_no_password_found_for_id(id: &str) {
  println!("No password found for ID: {}", id.to_string().cyan())
}

fn print_if_password_confirmation_fails() {
  println!(
    "\n{}",
    "Error: the passwords don't match. Please try again.".red()
  );
}

// TODO: this should be temporary. It's currently used to emulate the
// dialoguer output after choosing an option from `Select` so that if a try
// again path is used, everything looks the same. This approach is not ideal and
// needs to be investigated.
fn print_selected_action(action: Action) {
  println!(
    "{} Choose action · {}",
    "".green(),
    action.to_string().green()
  );
}