use std::{path::Path, time};
use totp_rs::TOTP;
use super::cryptography::decrypt;
use crate::{clipboard::copy_to_clipboard, Error};
pub fn copy_id(pass_id: String) -> Result<(), Error> {
copy_to_clipboard(&pass_id, false)
}
pub fn decrypt_password_file(file_path: &Path) -> Result<String, Error> {
let cipher = std::fs::read(file_path)?;
decrypt(&cipher)
}
pub fn copy_password(file_path: &Path) -> Result<(), Error> {
let file_contents = decrypt_password_file(file_path)?;
let password = file_contents
.lines()
.next()
.ok_or_else(|| Error::Pass("no password found".to_string()))?;
copy_to_clipboard(password, true)
}
pub fn copy_login(file_path: &Path) -> Result<(), Error> {
let file_contents = decrypt_password_file(file_path)?;
let login = file_contents
.lines()
.nth(1)
.ok_or_else(|| Error::Pass("no login found".to_string()))?;
copy_to_clipboard(login, true)
}
pub fn generate_otp(file_path: &Path) -> Result<String, Error> {
let file_contents = decrypt_password_file(file_path)?;
let otpauth = file_contents
.lines()
.find(|line| line.starts_with("otpauth://"))
.ok_or_else(|| Error::Pass("no OTP URL found".to_string()))?;
let totp = TOTP::from_url(otpauth)?;
totp.generate_current()
.map_err(|e: time::SystemTimeError| Error::Pass(format!("failed to generate OTP: {}", e)))
}
pub fn copy_otp(file_path: &Path) -> Result<(), Error> {
let otp = generate_otp(file_path)?;
copy_to_clipboard(&otp, false)
}