70 lines
1.8 KiB
Python
Executable File
70 lines
1.8 KiB
Python
Executable File
{# 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()
|
|
|