Skip to content

Commit

Permalink
feat: execute totp command
Browse files Browse the repository at this point in the history
  • Loading branch information
ccrutchf committed Nov 18, 2024
1 parent 9077d20 commit 109f0ff
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 8 deletions.
39 changes: 33 additions & 6 deletions src/credential_manager.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand All @@ -23,23 +23,40 @@ pub struct Credential {

impl Credential {
pub fn new(user: &str, password: &str, totp_command: Option<String>) -> 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<String> {
match self.totp_command.clone() {
Some(totp_command) => {
let parts = totp_command.split(" ").collect::<Vec<&str>>();
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<Connection> {
// Get the path to the credential database
let mut path = app_root(AppDataType::UserConfig, &AppInfo{
Expand Down Expand Up @@ -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();
}
}
2 changes: 1 addition & 1 deletion src/subcommands/login_subcommand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ impl Subcommand for LoginSubcommand {
let url = arg_matches.get_one::<String>("URL").context("URL not provided.")?;
let user = arg_matches.get_one::<String>("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
Expand Down
2 changes: 1 addition & 1 deletion src/subcommands/logout_subcommand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ impl Subcommand for LogoutSubcommand {
fn execute(&self, arg_matches: &ArgMatches) -> Result<()> {
let url = arg_matches.get_one::<String>("URL").context("URL not provided.")?;

let credential_manager = CredentialManager { };
let credential_manager = CredentialManager::new();
credential_manager.remove_credential(url)?;

Ok(())
Expand Down

0 comments on commit 109f0ff

Please sign in to comment.