diff --git a/src/credential_manager.rs b/src/credential_manager.rs index 786fda8..cf492a5 100644 --- a/src/credential_manager.rs +++ b/src/credential_manager.rs @@ -1,4 +1,4 @@ -use std::fs::create_dir_all; +use std::{fs::create_dir_all, process::Command}; use aes_gcm::{aead::{Aead, OsRng}, AeadCore, Aes256Gcm, Key, KeyInit, Nonce}; use app_dirs2::{AppDataType, AppInfo, app_root}; @@ -23,23 +23,40 @@ pub struct Credential { impl Credential { pub fn new(user: &str, password: &str, totp_command: Option) -> Credential { - let totp_command = match totp_command { - Some(totp_command) => Some(totp_command.to_string()), - None => None - }; - Credential { user: user.to_string(), password: password.to_string(), totp_command } } + + pub fn totp(&self) -> Option { + match self.totp_command.clone() { + Some(totp_command) => { + let parts = totp_command.split(" ").collect::>(); + let command = parts.first()?.to_string(); + let args = totp_command[..command.len()].to_string(); + + let output = Command::new(command) + .arg(args) + .output() + .expect("failed to execute process"); + + Some(String::from_utf8_lossy(&output.stdout).to_string()) + }, + None => None + } + } } pub struct CredentialManager { } impl CredentialManager { + pub fn new() -> CredentialManager { + CredentialManager { } + } + fn get_database(&self) -> Result { // Get the path to the credential database let mut path = app_root(AppDataType::UserConfig, &AppInfo{ @@ -178,4 +195,14 @@ impl CredentialManager { Err(_) => Err(anyhow::Error::msg("An error occurred closig the database.")) } } +} + +#[cfg(test)] +mod tests { + use super::CredentialManager; + + #[test] + fn create_credential_manager() { + let credential_manager = CredentialManager::new(); + } } \ No newline at end of file diff --git a/src/subcommands/login_subcommand.rs b/src/subcommands/login_subcommand.rs index e4302f2..5bd0319 100644 --- a/src/subcommands/login_subcommand.rs +++ b/src/subcommands/login_subcommand.rs @@ -12,7 +12,7 @@ impl Subcommand for LoginSubcommand { let url = arg_matches.get_one::("URL").context("URL not provided.")?; let user = arg_matches.get_one::("USER").context("USER not provided.")?; - let credential_manager = CredentialManager { }; + let credential_manager = CredentialManager::new(); if !credential_manager.has_credential(url)? { // TODO need to ask for password from user diff --git a/src/subcommands/logout_subcommand.rs b/src/subcommands/logout_subcommand.rs index a7604b8..94846ef 100644 --- a/src/subcommands/logout_subcommand.rs +++ b/src/subcommands/logout_subcommand.rs @@ -11,7 +11,7 @@ impl Subcommand for LogoutSubcommand { fn execute(&self, arg_matches: &ArgMatches) -> Result<()> { let url = arg_matches.get_one::("URL").context("URL not provided.")?; - let credential_manager = CredentialManager { }; + let credential_manager = CredentialManager::new(); credential_manager.remove_credential(url)?; Ok(())