This commit is contained in:
Robin Appelman 2023-01-01 16:38:16 +01:00
commit 675d17d088
6 changed files with 237 additions and 0 deletions

1
.envrc Normal file
View file

@ -0,0 +1 @@
use flake

3
.gitignore vendored Normal file
View file

@ -0,0 +1,3 @@
/target
/Cargo.lock
.direnv

13
Cargo.toml Normal file
View file

@ -0,0 +1,13 @@
[package]
name = "ykoath-cli"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
ykoath = { version = "0.1.0", git = "https://github.com/icewind1991/ykoath-rs", branch = "api" }
anyhow = "1"
chrono = "0.4"
fuzzy_finder = "0.3.0"
cli-clipboard = "0.3.0"

139
flake.lock generated Normal file
View file

@ -0,0 +1,139 @@
{
"nodes": {
"fenix": {
"inputs": {
"nixpkgs": [
"riff",
"nixpkgs"
],
"rust-analyzer-src": "rust-analyzer-src"
},
"locked": {
"lastModified": 1662186434,
"narHash": "sha256-N867s0Z20q9LNirVftWoKAmkEARmmHXZHyRTEmekf94=",
"owner": "nix-community",
"repo": "fenix",
"rev": "e228867146581fa5e4be25dafc0dd68fdd22f0ad",
"type": "github"
},
"original": {
"owner": "nix-community",
"repo": "fenix",
"type": "github"
}
},
"naersk": {
"inputs": {
"nixpkgs": [
"riff",
"nixpkgs"
]
},
"locked": {
"lastModified": 1662220400,
"narHash": "sha256-9o2OGQqu4xyLZP9K6kNe1pTHnyPz0Wr3raGYnr9AIgY=",
"owner": "nix-community",
"repo": "naersk",
"rev": "6944160c19cb591eb85bbf9b2f2768a935623ed3",
"type": "github"
},
"original": {
"owner": "nix-community",
"repo": "naersk",
"type": "github"
}
},
"nixpkgs": {
"locked": {
"lastModified": 1666014999,
"narHash": "sha256-gvKl8xlPJreezNG1NVTJv/HdGC69MSrM+IpCxS+eFvw=",
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "1935dd8fdab8e022a9d958419663162fd840014c",
"type": "github"
},
"original": {
"id": "nixpkgs",
"ref": "release-22.05",
"type": "indirect"
}
},
"nixpkgs_2": {
"locked": {
"lastModified": 1662099760,
"narHash": "sha256-MdZLCTJPeHi/9fg6R9fiunyDwP3XHJqDd51zWWz9px0=",
"owner": "nixos",
"repo": "nixpkgs",
"rev": "67e45078141102f45eff1589a831aeaa3182b41e",
"type": "github"
},
"original": {
"owner": "nixos",
"ref": "nixos-22.05",
"repo": "nixpkgs",
"type": "github"
}
},
"riff": {
"inputs": {
"fenix": "fenix",
"naersk": "naersk",
"nixpkgs": "nixpkgs_2"
},
"locked": {
"lastModified": 1665590088,
"narHash": "sha256-7mnx7J0AacL2P2mNuNNB+kKE7VR8nniVG+PSrwpZixE=",
"owner": "DeterminateSystems",
"repo": "riff",
"rev": "159e7c5b7ee3770828cc0c936d939de5d1b58f83",
"type": "github"
},
"original": {
"owner": "DeterminateSystems",
"repo": "riff",
"type": "github"
}
},
"root": {
"inputs": {
"nixpkgs": "nixpkgs",
"riff": "riff",
"utils": "utils"
}
},
"rust-analyzer-src": {
"flake": false,
"locked": {
"lastModified": 1662066014,
"narHash": "sha256-DE4FsE2sxd9nFtG+8+lnv/IBbtf+6rAlKjIdfpWN488=",
"owner": "rust-lang",
"repo": "rust-analyzer",
"rev": "93c52e41ec0d297c7512adf5936d8c464c820618",
"type": "github"
},
"original": {
"owner": "rust-lang",
"ref": "nightly",
"repo": "rust-analyzer",
"type": "github"
}
},
"utils": {
"locked": {
"lastModified": 1659877975,
"narHash": "sha256-zllb8aq3YO3h8B/U0/J1WBgAL8EX5yWf5pMj3G0NAmc=",
"owner": "numtide",
"repo": "flake-utils",
"rev": "c0e246b9b83f637f4681389ecabcb2681b4f3af0",
"type": "github"
},
"original": {
"owner": "numtide",
"repo": "flake-utils",
"type": "github"
}
}
},
"root": "root",
"version": 7
}

24
flake.nix Normal file
View file

@ -0,0 +1,24 @@
{
inputs = {
utils.url = "github:numtide/flake-utils";
nixpkgs.url = "nixpkgs/release-22.05";
riff.url = "github:DeterminateSystems/riff";
};
outputs = {
self,
nixpkgs,
utils,
riff,
}:
utils.lib.eachDefaultSystem (system: let
pkgs = (import nixpkgs) {
inherit system;
};
in rec {
# `nix develop`
devShell = pkgs.mkShell {
nativeBuildInputs = with pkgs; [rustc cargo bacon cargo-edit cargo-outdated clippy gcc pkg-config pcsclite xorg.libX11.dev];
};
});
}

57
src/main.rs Normal file
View file

@ -0,0 +1,57 @@
use chrono::offset::Utc;
use fuzzy_finder::{item::Item, FuzzyFinder};
use ykoath::{BulkResponse, BulkResponseData, YubiKey};
fn main() -> anyhow::Result<()> {
let mut buf = Vec::new();
let yubikey = YubiKey::connect(&mut buf)?;
yubikey.select(&mut buf)?;
let challenge = Utc::now().timestamp() / 30;
let keys: Vec<_> = yubikey
.calculate_all(true, challenge, &mut buf)?
.into_iter()
.filter_map(Result::ok)
.map(|result| Item::new(result.name.into(), result))
.collect();
let result = FuzzyFinder::find(keys, 8)?;
println!();
let code = match result {
Some(BulkResponse {
name,
data: BulkResponseData::Totp(mut code),
}) => {
let new_challenge = Utc::now().timestamp() / 30;
let mut buf = Vec::new();
if new_challenge / 30 > challenge / 30 {
code = yubikey.calculate(true, name.as_bytes(), new_challenge, &mut buf)?;
}
code
}
Some(BulkResponse {
name,
data: BulkResponseData::Touch,
}) => {
eprintln!("Touch YubiKey ...");
let new_challenge = Utc::now().timestamp() / 30;
let mut buf = Vec::new();
yubikey.calculate(true, name.as_bytes(), new_challenge, &mut buf)?
}
_ => {
return Ok(());
}
};
if let Err(_) = cli_clipboard::set_contents(code.to_string()) {
println!("{}", code);
} else {
println!("{} - Copied", code);
}
Ok(())
}