Switch to python implementation of hbw for TOTP

This commit is contained in:
traxys 2022-12-12 13:08:29 +01:00
parent 25f7880c0d
commit ce2258679e
5 changed files with 64 additions and 15 deletions

View file

@ -40,7 +40,8 @@ in {
comma
raclette
oscclip
nvfetcher
nvfetcher
hbw
];
services = {

View file

@ -7,6 +7,7 @@
wowup = callPackage ./wowup.nix {};
simulationcraft = callPackage ./simulationcraft.nix {simulationcraft-src = sources.simulationcraft;};
proton-ge = callPackage ./proton-ge.nix {proton-ge-src = sources.proton-ge;};
hbw = callPackage ./hbw {};
kabalist_cli = callPackage ./kabalist.nix {
inherit naersk;
kabalist-src = sources.kabalist;

27
pkgs/hbw/default.nix Normal file
View file

@ -0,0 +1,27 @@
{
stdenv,
lib,
python3,
bitwarden-cli,
makeWrapper,
...
}:
stdenv.mkDerivation {
pname = "hbw";
version = "0.1";
src = ./hbw.py;
buildInputs = [
(python3.withPackages (ps: with ps; [pyotp]))
bitwarden-cli
makeWrapper
];
dontUnpack = true;
dontBuild = true;
installPhase = ''
mkdir -p $out/bin
cp $src $out/bin/hbw
wrapProgram $out/bin/hbw --prefix PATH : ${lib.makeBinPath [bitwarden-cli]}
'';
}

34
pkgs/hbw/hbw.py Executable file
View file

@ -0,0 +1,34 @@
#!/usr/bin/env python3
import sys
import subprocess
import json
import pyotp
import datetime
if len(sys.argv) < 2:
print("Missing search term")
sys.exit(1)
search = sys.argv[1]
process = subprocess.Popen(
["bw", "list", "items", "--search", search], stdout=subprocess.PIPE
)
process.wait()
data, _ = process.communicate()
if process.returncode != 0:
sys.exit(1)
data = json.loads(data)
for item in data:
print(f"==== {item['name']} ====")
print(f" {item['login']['username']}")
print(f" {item['login']['password']}")
if "totp" in item["login"] and item["login"]["totp"] is not None:
totp = pyotp.TOTP(item["login"]["totp"])
time_remaining = (
totp.interval - datetime.datetime.now().timestamp() % totp.interval
)
print(f" {totp.now()} ({int(time_remaining)})")

View file

@ -1,14 +0,0 @@
#!/bin/sh
NAME=$1
if [ -z $1 ]; then
exit 0
fi
bw list items --search $NAME |\
jq 'reduce .[] as $item (
"";
. + "=====" + $item.name + "====\n "
+ $item.login.username + "\n "
+ $item.login.password + "\n\n")' -r |\
head -n -2