Compare commits
2 Commits
5350b380e2
...
3794c4d175
Author | SHA1 | Date | |
---|---|---|---|
3794c4d175 | |||
ac9f033656 |
36
src/main.rs
36
src/main.rs
@ -1,7 +1,7 @@
|
|||||||
use std::time::{SystemTime, UNIX_EPOCH};
|
use std::time::{SystemTime, UNIX_EPOCH};
|
||||||
|
|
||||||
use base32::Alphabet;
|
use base32::Alphabet;
|
||||||
use clap::Parser;
|
use clap::{Parser, Subcommand};
|
||||||
use totp_rfc6238::{HashAlgorithm, TotpGenerator};
|
use totp_rfc6238::{HashAlgorithm, TotpGenerator};
|
||||||
|
|
||||||
/// Get the current time since epoch
|
/// Get the current time since epoch
|
||||||
@ -14,6 +14,18 @@ pub fn time() -> u64 {
|
|||||||
#[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)]
|
||||||
|
command: SubCommands
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Subcommand, Debug)]
|
||||||
|
enum SubCommands {
|
||||||
|
/// Setup configuration
|
||||||
|
Setup,
|
||||||
|
|
||||||
|
/// Get a TOTP code
|
||||||
|
#[clap(arg_required_else_help = true)]
|
||||||
|
Code {
|
||||||
/// The secret to use
|
/// The secret to use
|
||||||
#[clap(short, long)]
|
#[clap(short, long)]
|
||||||
secret: String,
|
secret: String,
|
||||||
@ -25,20 +37,34 @@ struct Args {
|
|||||||
/// Size of generated secret
|
/// Size of generated secret
|
||||||
#[clap(short, long, default_value_t = 6)]
|
#[clap(short, long, default_value_t = 6)]
|
||||||
len: usize,
|
len: usize,
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let args: Args = Args::parse();
|
let args: Args = Args::parse();
|
||||||
|
|
||||||
|
match args.command {
|
||||||
|
SubCommands::Setup => {
|
||||||
|
println!("To configure TOPT :");
|
||||||
|
println!("1. Please open https://mysignins.microsoft.com/security-info");
|
||||||
|
println!("2. Click on \"Add method\"");
|
||||||
|
println!("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!("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");
|
||||||
|
}
|
||||||
|
|
||||||
|
SubCommands::Code { secret, topt_step, len } => {
|
||||||
let totp_generator = TotpGenerator::new()
|
let totp_generator = TotpGenerator::new()
|
||||||
.set_digit(args.len).unwrap()
|
.set_digit(len).unwrap()
|
||||||
.set_step(args.topt_step).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 },
|
Alphabet::RFC4648 { padding: true },
|
||||||
&args.secret,
|
&secret,
|
||||||
).unwrap();
|
).unwrap();
|
||||||
|
|
||||||
let code = totp_generator.get_code(&key);
|
let code = totp_generator.get_code(&key);
|
||||||
@ -47,3 +73,5 @@ fn main() {
|
|||||||
println!("Secret = {}", code);
|
println!("Secret = {}", code);
|
||||||
println!("Next update = {} seconds", next_update - time());
|
println!("Next update = {} seconds", next_update - time());
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user