Add first unit test
This commit is contained in:
parent
f0bcdced6b
commit
fd2ae7f3e6
49
src/main.rs
49
src/main.rs
@ -6,16 +6,18 @@ use totp_rfc6238::{HashAlgorithm, TotpGenerator};
|
|||||||
|
|
||||||
/// Get the current time since epoch
|
/// Get the current time since epoch
|
||||||
pub fn time() -> u64 {
|
pub fn time() -> u64 {
|
||||||
SystemTime::now().duration_since(UNIX_EPOCH).unwrap().as_secs()
|
SystemTime::now()
|
||||||
|
.duration_since(UNIX_EPOCH)
|
||||||
|
.unwrap()
|
||||||
|
.as_secs()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/// TOTP - One Time Password generator
|
/// TOTP - One Time Password generator
|
||||||
#[derive(Parser, Debug)]
|
#[derive(Parser, Debug)]
|
||||||
#[clap(author, version, about, long_about = None)]
|
#[clap(author, version, about, long_about = None)]
|
||||||
struct Args {
|
struct Args {
|
||||||
#[clap(subcommand)]
|
#[clap(subcommand)]
|
||||||
command: SubCommands
|
command: SubCommands,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Subcommand, Debug)]
|
#[derive(Subcommand, Debug)]
|
||||||
@ -48,24 +50,34 @@ fn main() {
|
|||||||
println!("To configure TOPT :");
|
println!("To configure TOPT :");
|
||||||
println!("1. Please open https://mysignins.microsoft.com/security-info");
|
println!("1. Please open https://mysignins.microsoft.com/security-info");
|
||||||
println!("2. Click on \"Add method\"");
|
println!("2. Click on \"Add method\"");
|
||||||
println!("3. On the popup that appears, choose \"Authenticator app\" and click \"Add\".");
|
println!(
|
||||||
println!("4. Click on \"I want to use a different authenticator app\" and click \"Next\"");
|
"3. On the popup that appears, choose \"Authenticator app\" and click \"Add\"."
|
||||||
|
);
|
||||||
|
println!(
|
||||||
|
"4. Click on \"I want to use a different authenticator app\" and click \"Next\""
|
||||||
|
);
|
||||||
println!("5. Click on \"Can't save image ?\" and copy the \"Secret key\"");
|
println!("5. Click on \"Can't save image ?\" and copy the \"Secret key\"");
|
||||||
println!("6. Re-run this program running {} code --secret <SECRET>", std::env::args().next().unwrap());
|
println!(
|
||||||
|
"6. Re-run this program running {} code --secret <SECRET>",
|
||||||
|
std::env::args().next().unwrap()
|
||||||
|
);
|
||||||
println!("7. Back on the browser, click Next and paste copied code");
|
println!("7. Back on the browser, click Next and paste copied code");
|
||||||
}
|
}
|
||||||
|
|
||||||
SubCommands::Code { secret, topt_step, len } => {
|
SubCommands::Code {
|
||||||
|
secret,
|
||||||
|
topt_step,
|
||||||
|
len,
|
||||||
|
} => {
|
||||||
let totp_generator = TotpGenerator::new()
|
let totp_generator = TotpGenerator::new()
|
||||||
.set_digit(len).unwrap()
|
.set_digit(len)
|
||||||
.set_step(topt_step).unwrap()
|
.unwrap()
|
||||||
|
.set_step(topt_step)
|
||||||
|
.unwrap()
|
||||||
.set_hash_algorithm(HashAlgorithm::SHA1)
|
.set_hash_algorithm(HashAlgorithm::SHA1)
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
let key = base32::decode(
|
let key = base32::decode(Alphabet::RFC4648 { padding: true }, &secret).unwrap();
|
||||||
Alphabet::RFC4648 { padding: true },
|
|
||||||
&secret,
|
|
||||||
).unwrap();
|
|
||||||
|
|
||||||
let code = totp_generator.get_code(&key);
|
let code = totp_generator.get_code(&key);
|
||||||
let next_update = totp_generator.get_next_update_time().unwrap();
|
let next_update = totp_generator.get_next_update_time().unwrap();
|
||||||
@ -75,3 +87,14 @@ fn main() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod test {
|
||||||
|
use crate::Args;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn verify_cli() {
|
||||||
|
use clap::CommandFactory;
|
||||||
|
Args::command().debug_assert()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user