From 164fb35375846c5c44d9520e02a0781ef9f28f51 Mon Sep 17 00:00:00 2001 From: Daniel-I-Am Date: Sat, 4 Jun 2022 00:30:04 +0200 Subject: [PATCH] WIP - rofi bitwarden --- templates/i3/i3 | 2 +- templates/rofi/rofi-bitwarden.py | 69 ++++++++++++++++++++++++++++++++ 2 files changed, 70 insertions(+), 1 deletion(-) create mode 100755 templates/rofi/rofi-bitwarden.py diff --git a/templates/i3/i3 b/templates/i3/i3 index cd8c4e7..d244a4b 100644 --- a/templates/i3/i3 +++ b/templates/i3/i3 @@ -10,7 +10,7 @@ # Please see https://i3wm.org/docs/userguide.html for a complete reference! set $menu "rofi -show drun" -set $pwdManager "rofi -modi 'bitwarden:~/bin/rofi-bitwarden.py' -show bitwarden" +set $pwdManager "~/bin/rofi-bitwarden.py &" set $term alacritty set $compositor "picom --daemon --inactive-opacity 1 --menu-opacity 1" diff --git a/templates/rofi/rofi-bitwarden.py b/templates/rofi/rofi-bitwarden.py new file mode 100755 index 0000000..b42e28e --- /dev/null +++ b/templates/rofi/rofi-bitwarden.py @@ -0,0 +1,69 @@ +{# symlink{~/bin/rofi-bitwarden.py} #} +#!/usr/bin/env python3 + +import os +import sys +import json + +BW_SESSION_FILE = os.path.join('/var/run/user', str(os.getuid()), 'bitwarden-session') + +def does_bw_session_exist(): + return os.path.exists(BW_SESSION_FILE) + +def show_rofi_pwd_prompt(): + return os.popen('rofi -dmenu -password -i -no-fixed-num-lines -p "Master Password" -theme ~/.config/rofi/themes/askpass.rasi').read().strip() + +def get_bw_session(): + if not does_bw_session_exist(): + return None + + with open(BW_SESSION_FILE, 'r') as file: + return file.read() + +def is_logged_in(): + return os.system('bw login --check > /dev/null') == 0 + +def login(): + os.spawnl(os.P_WAIT, '/usr/bin/alacritty', 'alacritty', '-e', 'sh', '-c', 'bw login') + +def unlock(): + password = show_rofi_pwd_prompt() + + if len(password) > 0: + # TODO: pipe in the password + os.system(f'bw unlock "{password}" --raw > {BW_SESSION_FILE}') + +def lock(): + if not does_bw_session_exist(): + return False + + return os.system(f'bw --session "{get_bw_session()}" --nointeraction lock > /dev/null') == 0 + +def is_vault_unlocked(): + if not does_bw_session_exist(): + return False + + return os.system(f'bw --session "{get_bw_session()}" --nointeraction list items > /dev/null 2>&1') == 0 + +def debug(msg): + os.spawnl(os.P_WAIT, '/usr/bin/alacritty', 'alacritty', '-e', 'sh', '-c', f'echo "{msg}" && cat -') + +def main(): + if not is_logged_in(): + login() + if not is_logged_in(): + print('You did nog log in', file=sys.stderr) + exit(1) + + if not is_vault_unlocked(): + unlock() + if not is_vault_unlocked(): + print('You did not unlock the vault', file=sys.stderr) + exit(1) + + print("Signed in and unlocked, cool") + debug("Done") + +if __name__ == '__main__': + main() +