From d44df83c7f30618f979cf1cd6ffe11db34c30d6c Mon Sep 17 00:00:00 2001 From: Hatter Jiang Date: Sun, 18 Jul 2021 12:08:59 +0800 Subject: [PATCH] chore: use_clap & clippy --- Cargo.lock | 1 + Cargo.toml | 2 +- src/chall.rs | 4 ++-- src/challconfig.rs | 4 ++-- src/cmd.rs | 24 ------------------------ src/main.rs | 18 ++++++++++++++---- src/pgp.rs | 4 ++-- src/pgpcarddecrypt.rs | 4 ++-- src/pgpcardlist.rs | 4 ++-- src/pgpcardsign.rs | 4 ++-- src/piv.rs | 21 +++++++++++++++++---- src/register.rs | 4 ++-- src/sign.rs | 4 ++-- 13 files changed, 49 insertions(+), 49 deletions(-) delete mode 100644 src/cmd.rs diff --git a/Cargo.lock b/Cargo.lock index 67813d4..08f51ae 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1605,6 +1605,7 @@ version = "0.6.41" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "df24005feacce81f4ae340464b39c380f7e01e7225bfdef62d40cb44cb1c11d7" dependencies = [ + "clap", "lazy_static", "libc", "term", diff --git a/Cargo.toml b/Cargo.toml index ad75e13..cf393a8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,7 +9,7 @@ edition = "2018" [dependencies] authenticator = "0.3" clap = "2.33" -rust_util = "0.6" +rust_util = { version = "0.6", features = ["use_clap"] } sha2 = "0.9" rand = "0.8" base64 = "0.13" diff --git a/src/chall.rs b/src/chall.rs index 832b30f..db5a5ee 100644 --- a/src/chall.rs +++ b/src/chall.rs @@ -1,8 +1,8 @@ use clap::{ArgMatches, SubCommand, App, Arg}; -use crate::cmd::{Command, CommandError}; use yubico_manager::Yubico; use yubico_manager::config::{Config, Mode, Slot}; use std::ops::Deref; +use rust_util::util_clap::{Command, CommandError}; pub struct CommandImpl; @@ -55,6 +55,6 @@ impl Command for CommandImpl { warning!("Yubikey not found"); } - Ok(()) + Ok(None) } } diff --git a/src/challconfig.rs b/src/challconfig.rs index bbae27e..b33bfd8 100644 --- a/src/challconfig.rs +++ b/src/challconfig.rs @@ -1,9 +1,9 @@ use clap::{ArgMatches, SubCommand, App, Arg}; -use crate::cmd::{Command, CommandError}; use yubico_manager::Yubico; use yubico_manager::config::Config; use yubico_manager::hmacmode::HmacKey; use yubico_manager::configure::DeviceModeConfig; +use rust_util::util_clap::{Command, CommandError}; pub struct CommandImpl; @@ -58,6 +58,6 @@ impl Command for CommandImpl { warning!("Yubikey not found"); } - Ok(()) + Ok(None) } } diff --git a/src/cmd.rs b/src/cmd.rs deleted file mode 100644 index ca030a1..0000000 --- a/src/cmd.rs +++ /dev/null @@ -1,24 +0,0 @@ -use clap::{ArgMatches, App}; -use rust_util::XResult; - -pub type CommandError = XResult<()>; - -pub trait Command { - fn name(&self) -> &str; - fn subcommand<'a>(&self) -> App<'a, 'a>; - fn run(&self, arg_matches: &ArgMatches, _: &ArgMatches) -> CommandError; -} - - -pub struct DefaultCommandImpl; - -impl DefaultCommandImpl { - pub fn process_command<'a>(app: App<'a, 'a>) -> App<'a, 'a> { - app - // app.arg(Arg::with_name("verbose").long("verbose").short("v").multiple(true).help("Show verbose info")) - } - pub fn run(_arg_matches: &ArgMatches) -> CommandError { - information!("WebAuthn(OpenPGP) cli, use --help for help"); - Ok(()) - } -} \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index f64f55f..7057bd3 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,7 +1,7 @@ #[macro_use] extern crate rust_util; -mod cmd; +// mod cmd; mod fido; mod digest; mod register; @@ -15,10 +15,20 @@ mod piv; mod chall; mod challconfig; -use clap::{App, AppSettings}; -use cmd::{Command, CommandError}; -use cmd::DefaultCommandImpl; +use clap::{App, AppSettings, ArgMatches}; +use rust_util::util_clap::{CommandError, Command}; +pub struct DefaultCommandImpl; + +impl DefaultCommandImpl { + pub fn process_command<'a>(app: App<'a, 'a>) -> App<'a, 'a> { + app + } + pub fn run(_arg_matches: &ArgMatches) -> CommandError { + information!("WebAuthn(OpenPGP) cli, use --help for help"); + Ok(None) + } +} fn main() { if let Err(e) = inner_main() { diff --git a/src/pgp.rs b/src/pgp.rs index 9c75b27..6a73487 100644 --- a/src/pgp.rs +++ b/src/pgp.rs @@ -1,5 +1,4 @@ use clap::{ArgMatches, SubCommand, App, Arg}; -use crate::cmd::{Command, CommandError}; use sequoia_openpgp::parse::Parse; use sequoia_openpgp::parse::{PacketParser, PacketParserResult}; use sequoia_openpgp::Packet; @@ -10,6 +9,7 @@ use sequoia_openpgp::crypto::mpi::PublicKey; use openssl::bn::BigNum; use pem::Pem; use sequoia_openpgp::packet::signature::subpacket::SubpacketTag; +use rust_util::util_clap::{Command, CommandError}; pub struct CommandImpl; @@ -162,7 +162,7 @@ impl Command for CommandImpl { information!("FILE IS OTHER"); } } - Ok(()) + Ok(None) } } diff --git a/src/pgpcarddecrypt.rs b/src/pgpcarddecrypt.rs index 4a79f68..8ff5e97 100644 --- a/src/pgpcarddecrypt.rs +++ b/src/pgpcarddecrypt.rs @@ -1,7 +1,7 @@ use clap::{ArgMatches, SubCommand, App, Arg}; -use crate::cmd::{Command, CommandError}; use openpgp_card::DecryptMe; use std::collections::BTreeMap; +use rust_util::util_clap::{Command, CommandError}; pub struct CommandImpl; @@ -56,6 +56,6 @@ impl Command for CommandImpl { println!("{}", serde_json::to_string_pretty(&json).unwrap()); } - Ok(()) + Ok(None) } } diff --git a/src/pgpcardlist.rs b/src/pgpcardlist.rs index e4d79cb..685f9fa 100644 --- a/src/pgpcardlist.rs +++ b/src/pgpcardlist.rs @@ -1,7 +1,7 @@ use clap::{ArgMatches, SubCommand, App, Arg}; -use crate::cmd::{Command, CommandError}; use openpgp_card::{OpenPGPCard, DecryptMe, Hash, KeyType}; use std::collections::BTreeMap; +use rust_util::util_clap::{Command, CommandError}; pub struct CommandImpl; @@ -108,6 +108,6 @@ impl Command for CommandImpl { } } } - Ok(()) + Ok(None) } } diff --git a/src/pgpcardsign.rs b/src/pgpcardsign.rs index f528c5d..56cf8fd 100644 --- a/src/pgpcardsign.rs +++ b/src/pgpcardsign.rs @@ -1,8 +1,8 @@ use clap::{ArgMatches, SubCommand, App, Arg}; -use crate::cmd::{Command, CommandError}; use openpgp_card::Hash; use rust_util::XResult; use std::collections::BTreeMap; +use rust_util::util_clap::{Command, CommandError}; pub struct CommandImpl; @@ -84,7 +84,7 @@ impl Command for CommandImpl { println!("{}", serde_json::to_string_pretty(&json).unwrap()); } - Ok(()) + Ok(None) } } diff --git a/src/piv.rs b/src/piv.rs index 9ab11e1..effc590 100644 --- a/src/piv.rs +++ b/src/piv.rs @@ -1,5 +1,5 @@ +use std::time::Duration; use clap::{ArgMatches, SubCommand, App, Arg}; -use crate::cmd::{Command, CommandError}; use yubikey::{YubiKey, Certificate}; use yubikey::piv::SlotId; use rust_util::XResult; @@ -8,6 +8,8 @@ use x509_parser::parse_x509_certificate; use pem::Pem; use digest::Digest; use rust_util::util_msg::MessageType; +use chrono::Local; +use rust_util::util_clap::{Command, CommandError}; pub struct CommandImpl; @@ -18,7 +20,7 @@ impl Command for CommandImpl { SubCommand::with_name(self.name()).about("PIV subcommand") .arg(Arg::with_name("detail").long("detail").help("Detail output")) .arg(Arg::with_name("show-config").long("show-config").help("Show config output")) - // .arg(Arg::with_name("json").long("json").help("JSON output")) + // .arg(Arg::with_name("json").long("json").help("JSON output")) } fn run(&self, _arg_matches: &ArgMatches, sub_arg_matches: &ArgMatches) -> CommandError { @@ -47,7 +49,7 @@ impl Command for CommandImpl { for slot in yubikey::piv::SLOTS.iter().cloned() { print_cert_info(&mut yk, slot, detail_output).ok(); } - Ok(()) + Ok(None) } } @@ -89,7 +91,18 @@ fn print_cert_info(yubikey: &mut YubiKey, slot: SlotId, detail_output: bool) -> success!("Issuer: {}", cert.tbs_certificate.issuer); success!("Fingerprint(SHA256): {}", hex::encode(fingerprint_sha256)); success!("Not Before: {}", cert.tbs_certificate.validity.not_before.to_rfc2822()); - success!("Not After: {}", cert.tbs_certificate.validity.not_after.to_rfc2822()); + + let mut not_after_desc = String::new(); + let not_after_timestamp = cert.tbs_certificate.validity.not_after.timestamp(); + let now_timestamp = Local::now().timestamp(); + if not_after_timestamp < now_timestamp { + let expired_time = simpledateformat::format_human(Duration::from_secs((now_timestamp - not_after_timestamp) as u64)); + not_after_desc.push_str(&format!("(EXPIRED {})", expired_time)); + } else { + let valid_time = simpledateformat::format_human(Duration::from_secs((not_after_timestamp - now_timestamp) as u64)); + not_after_desc.push_str(&format!("(left {})", valid_time)); + } + success!("Not After: {} {}", cert.tbs_certificate.validity.not_after.to_rfc2822(), not_after_desc); } _ => { warning!("Failed to parse certificate"); diff --git a/src/register.rs b/src/register.rs index 6ecd062..c6dd248 100644 --- a/src/register.rs +++ b/src/register.rs @@ -1,5 +1,4 @@ use clap::{ArgMatches, SubCommand, App, Arg}; -use crate::cmd::{Command, CommandError}; use authenticator::authenticatorservice::AuthenticatorService; use authenticator::statecallback::StateCallback; use authenticator::RegisterFlags; @@ -7,6 +6,7 @@ use std::sync::mpsc::channel; use crate::fido; use crate::digest; use crate::fido::{U2fV2Challenge, U2fRegistrationData}; +use rust_util::util_clap::{Command, CommandError}; pub struct CommandImpl; @@ -85,7 +85,7 @@ impl Command for CommandImpl { return simple_error!("Parse registration data failed: {}", e); } } - Ok(()) + Ok(None) } } // diff --git a/src/sign.rs b/src/sign.rs index dea6e9f..827af3f 100644 --- a/src/sign.rs +++ b/src/sign.rs @@ -1,5 +1,4 @@ use clap::{ArgMatches, SubCommand, App, Arg}; -use crate::cmd::{Command, CommandError}; use authenticator::{KeyHandle, AuthenticatorTransports, SignFlags}; use std::sync::mpsc::channel; use authenticator::statecallback::StateCallback; @@ -7,6 +6,7 @@ use authenticator::authenticatorservice::AuthenticatorService; use crate::fido; use crate::digest; use crate::fido::U2fV2Challenge; +use rust_util::util_clap::{Command, CommandError}; pub struct CommandImpl; @@ -83,6 +83,6 @@ impl Command for CommandImpl { // u2f::authorization::parse_sign_response(app_id.to_string(), u2fv2_challenge_str.as_bytes().to_vec(), ) - Ok(()) + Ok(None) } } \ No newline at end of file