More fixes to make autotype work

This commit is contained in:
Bernd Schoolmann 2024-02-08 18:17:51 +01:00
parent ca17380758
commit fe64cc7807
No known key found for this signature in database
7 changed files with 50 additions and 40 deletions

View File

@ -13,10 +13,10 @@ import time
import os
root_path = os.path.abspath(os.path.join(os.path.dirname(os.path.abspath(__file__)), os.pardir, os.pardir))
is_flatpak = os.path.exists("/.flatpak-info")
def main():
token = secrets.token_hex(32)
print("token", token)
if not os.environ.get("GOLDWARDEN_DAEMON_AUTH_TOKEN") == None:
token = os.environ["GOLDWARDEN_DAEMON_AUTH_TOKEN"]
@ -33,27 +33,20 @@ def main():
exit()
# start daemons
dbus_autofill_monitor.run_daemon() # todo: remove after migration
dbus_autofill_monitor.run_daemon(token) # todo: remove after migration
dbus_monitor.run_daemon(token)
if not "--hidden" in sys.argv:
subprocess.Popen(["python3", "-m", "src.ui.settings"], cwd=root_path, start_new_session=True)
p = subprocess.Popen(["python3", "-m", "src.ui.settings"], stdin=subprocess.PIPE, stdout=subprocess.PIPE, cwd=root_path, start_new_session=True)
p.stdin.write(f"{token}\n".encode())
p.stdin.flush()
# try:
# subprocess.Popen(["python3", f'{source_path}/background.py'], start_new_session=True)
# except Exception as e:
# pass
if is_flatpak:
# to autostart the appes
try:
subprocess.Popen(["python3", f'{source_path}/background.py'], start_new_session=True)
except Exception as e:
pass
while True:
time.sleep(60)
# def run_daemon():
# # todo: do a proper check
# if is_hidden:
# time.sleep(20)
# print("IS daemon running", goldwarden.is_daemon_running())
# if not goldwarden.is_daemon_running():
# print("running daemon")
# goldwarden.run_daemon()
# print("daemon running")

View File

@ -7,7 +7,7 @@ import dbus.service
from dbus.mainloop.glib import DBusGMainLoop
from threading import Thread
on_autofill = lambda: None
daemon_token = None
class GoldwardenDBUSService(dbus.service.Object):
def __init__(self):
@ -16,7 +16,9 @@ class GoldwardenDBUSService(dbus.service.Object):
@dbus.service.method('com.quexten.Goldwarden.Autofill')
def autofill(self):
on_autofill()
p = subprocess.Popen(["python3", "-m", "src.ui.quickaccess"], stdin=subprocess.PIPE, stdout=subprocess.PIPE, cwd=root_path, start_new_session=True)
p.stdin.write(f"{daemon_token}\n".encode())
p.stdin.flush()
return ""
def daemon():
@ -25,7 +27,9 @@ def daemon():
from gi.repository import GLib, GObject as gobject
gobject.MainLoop().run()
def run_daemon():
def run_daemon(token):
global daemon_token
daemon_token = token
thread = Thread(target=daemon)
thread.start()

View File

@ -17,15 +17,15 @@ class GoldwardenDBUSService(dbus.service.Object):
@dbus.service.method('com.quexten.Goldwarden.ui.QuickAccess')
def quickaccess(self):
p = subprocess.Popen(["python3", "-m", "src.ui.quickaccess"], stdin=subprocess.PIPE, stdout=subprocess.PIPE, cwd=root_path, start_new_session=True)
print("writing token")
p.stdin.write(f"{daemon_token}\n".encode())
p.stdin.flush()
print("reading token")
return ""
@dbus.service.method('com.quexten.Goldwarden.ui.Settings')
def settings(self):
subprocess.Popen(["python3", "-m", "src.ui.settings"], cwd=root_path, start_new_session=True)
subprocess.Popen(["python3", "-m", "src.ui.settings"], stdin=subprocess.PIPE, stdout=subprocess.PIPE, cwd=root_path, start_new_session=True)
p.stdin.write(f"{daemon_token}\n".encode())
p.stdin.flush()
return ""
def daemon():

View File

@ -9,10 +9,12 @@ BINARY_PATHS = [
str(Path.home()) + "/go/src/github.com/quexten/goldwarden/goldwarden"
]
BINARY_PATH = ""
authenticated_connection = None
def create_authenticated_connection(token):
global authenticated_connection
global BINARY_PATH
BINARY_PATH = None
for path in BINARY_PATHS:
if os.path.exists(path):
@ -123,16 +125,16 @@ def get_runtime_config():
except Exception as e:
return None
# def autotype(username, password):
# # environment
# env = os.environ.copy()
# env["PASSWORD"] = password
# restic_cmd = f"{BINARY_PATH} autotype --username {username}"
# result = subprocess.run(restic_cmd.split(), capture_output=True, text=True, env=env)
# print(result.stderr)
# print(result.stdout)
# if result.returncode != 0:
# raise Exception("Failed to initialize repository, err", result.stderr)
def autotype(username, password):
env = os.environ.copy()
# todo convert to stdin
env["PASSWORD"] = password
goldwarden_cmd = f"{BINARY_PATH} autotype --username {username}"
result = subprocess.run(goldwarden_cmd.split(), capture_output=True, text=True, env=env)
print(result.stderr)
print(result.stdout)
if result.returncode != 0:
raise Exception("Failed to initialize repository, err", result.stderr)
def is_daemon_running():
result = send_authenticated_command(f"vault status")

View File

@ -20,15 +20,20 @@ class MyApp(Adw.Application):
super().__init__(**kwargs)
self.connect('activate', self.on_activate)
def on_activate(self, app):
self.autofill_window = MainWindow(application=app)
self.autofill_window.logins = []
self.autofill_window.present()
def update_logins(self):
logins = goldwarden.get_vault_logins()
if logins == None:
os._exit(0)
return
app.autofill_window.logins = logins
self.app.autofill_window.logins = logins
def on_activate(self, app):
self.autofill_window = MainWindow(application=app)
self.autofill_window.logins = []
self.autofill_window.present()
self.app = app
thread = Thread(target=self.update_logins)
thread.start()
class MainWindow(Gtk.ApplicationWindow):
def __init__(self, *args, **kwargs):

View File

@ -12,6 +12,8 @@ import subprocess
from . import components
import os
root_path = os.path.abspath(os.path.join(os.path.dirname(os.path.abspath(__file__)), os.pardir, os.pardir))
token = sys.stdin.readline()
goldwarden.create_authenticated_connection(None)
class SettingsWinvdow(Gtk.ApplicationWindow):
@ -48,7 +50,11 @@ class SettingsWinvdow(Gtk.ApplicationWindow):
self.autotype_button = Gtk.Button()
self.autotype_button.set_label("Quick Access")
self.autotype_button.set_margin_top(10)
self.autotype_button.connect("clicked", lambda button: subprocess.Popen(["python3", "/app/bin/autofill.py"], start_new_session=True))
def quickaccess_button_clicked():
p = subprocess.Popen(["python3", "-m", "src.ui.quickaccess"], stdin=subprocess.PIPE, stdout=subprocess.PIPE, cwd=root_path, start_new_session=True)
p.stdin.write(f"{token}\n".encode())
p.stdin.flush()
self.autotype_button.connect("clicked", lambda button: quickaccess_button_clicked())
self.autotype_button.get_style_context().add_class("suggested-action")
self.action_preferences_group.add(self.autotype_button)