Improve quickaccess keyboard navigation

This commit is contained in:
Bernd Schoolmann 2024-02-17 09:51:41 +01:00
parent 592ff48148
commit 606dd176ef
No known key found for this signature in database
3 changed files with 64 additions and 50 deletions

View file

@ -3,6 +3,8 @@
package cmd package cmd
import ( import (
"bufio"
"encoding/hex"
"os" "os"
"github.com/quexten/goldwarden/autotype" "github.com/quexten/goldwarden/autotype"
@ -15,14 +17,13 @@ var autofillCmd = &cobra.Command{
Short: "Autotype credentials", Short: "Autotype credentials",
Long: `Autotype credentials`, Long: `Autotype credentials`,
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
username, _ := cmd.Flags().GetString("username") reader := bufio.NewReader(os.Stdin)
// get pasword from env textHex, _ := reader.ReadString('\n')
password := os.Getenv("PASSWORD") text, _ := hex.DecodeString(textHex)
autotype.TypeString(username + "\t" + password) autotype.TypeString(string(text))
}, },
} }
func init() { func init() {
rootCmd.AddCommand(autofillCmd) rootCmd.AddCommand(autofillCmd)
autofillCmd.PersistentFlags().String("username", "", "")
} }

View file

@ -16,6 +16,25 @@ Notify.init("Goldwarden")
token = sys.stdin.readline() token = sys.stdin.readline()
goldwarden.create_authenticated_connection(token) goldwarden.create_authenticated_connection(token)
def autotype(text):
time.sleep(0.5)
goldwarden.autotype(text)
os._exit(0)
def set_clipboard(text):
print("Setting clipboard")
try:
clipboard.write(text)
except Exception as e:
print(e)
pass
display = Gdk.Display.get_default()
clipboard = Gdk.Display.get_clipboard(display)
clipboard.set(text)
print("Clipboard set", text)
time.sleep(0.5)
os._exit(0)
class MyApp(Adw.Application): class MyApp(Adw.Application):
def __init__(self, **kwargs): def __init__(self, **kwargs):
super().__init__(**kwargs) super().__init__(**kwargs)
@ -50,7 +69,7 @@ class MainWindow(Gtk.ApplicationWindow):
self.text_view = Adw.EntryRow() self.text_view = Adw.EntryRow()
self.text_view.set_title("Search") self.text_view.set_title("Search")
# on type func
def on_type(entry): def on_type(entry):
if len(entry.get_text()) > 1: if len(entry.get_text()) > 1:
self.results_list.show() self.results_list.show()
@ -94,12 +113,10 @@ class MainWindow(Gtk.ApplicationWindow):
keycont = Gtk.EventControllerKey() keycont = Gtk.EventControllerKey()
def handle_keypress(cotroller, keyval, keycode, state, user_data): def handle_keypress(cotroller, keyval, keycode, state, user_data):
# if ctrl is pressed ctrl_pressed = state & Gdk.ModifierType.CONTROL_MASK > 0
if state == 4: alt_pressed = state & Gdk.ModifierType.ALT_MASK > 0
print("ctrl")
if keycode == 9: if keycode == 9:
print("esc")
os._exit(0) os._exit(0)
if keyval == 65364: if keyval == 65364:
@ -112,43 +129,39 @@ class MainWindow(Gtk.ApplicationWindow):
return False return False
if keycode == 36: if keycode == 36:
print("enter")
self.hide() self.hide()
def do_autotype(username, password): autotypeThread = Thread(target=autotype, args=(f"{self.results_list.get_selected_row().username}\t{self.results_list.get_selected_row().password}",))
time.sleep(0.5)
goldwarden.autotype(username, password)
os._exit(0)
autotypeThread = Thread(target=do_autotype, args=(self.results_list.get_selected_row().username, self.results_list.get_selected_row().password,))
autotypeThread.start() autotypeThread.start()
print(self.results_list.get_selected_row().get_title())
if keyval == 112: if keyval == 112:
print("copy password") print("pass", ctrl_pressed, alt_pressed)
clipboard.write(self.results_list.get_selected_row().password) if ctrl_pressed and not alt_pressed:
Notify.Notification.new("Goldwarden", "Password Copied", "dialog-information").show() set_clipboard(self.results_list.get_selected_row().password)
if ctrl_pressed and alt_pressed:
self.hide()
autotype(self.results_list.get_selected_row().password)
elif keyval == 117: elif keyval == 117:
print("copy username") if ctrl_pressed and not alt_pressed:
clipboard.write(self.results_list.get_selected_row().username) set_clipboard(self.results_list.get_selected_row().username)
notification=Notify.Notification.new("Goldwarden", "Username Copied", "dialog-information") if ctrl_pressed and alt_pressed:
notification.set_timeout(5) self.hide()
notification.show() autotype(self.results_list.get_selected_row().username)
elif keyval == 118: elif keyval == 118:
print("open web vault") if ctrl_pressed and alt_pressed:
environment = goldwarden.get_environment() environment = goldwarden.get_environment()
if environment == None: if environment == None:
return return
item_uri = environment["vault"] + "#/vault?itemId=" + self.results_list.get_selected_row().uuid item_uri = environment["vault"] + "#/vault?itemId=" + self.results_list.get_selected_row().uuid
Gtk.show_uri(None, item_uri, Gdk.CURRENT_TIME) Gtk.show_uri(None, item_uri, Gdk.CURRENT_TIME)
elif keyval == 108: elif keyval == 108:
print("launch") if ctrl_pressed and alt_pressed:
print(self.results_list.get_selected_row().uri) Gtk.show_uri(None, self.results_list.get_selected_row().uri, Gdk.CURRENT_TIME)
Gtk.show_uri(None, self.results_list.get_selected_row().uri, Gdk.CURRENT_TIME)
elif keyval == 116: elif keyval == 116:
print("copy totp") totp_code = totp.totp(self.resuts_list.get_selected_row().totp)
totp_code = totp.totp(self.results_list.get_selected_row().totp) if ctrl_pressed and not alt_pressed:
clipboard.write(totp_code) set_clipboard(totp_code)
notification=Notify.Notification.new("Goldwarden", "Totp Copied", "dialog-information") if ctrl_pressed and alt_pressed:
notification.set_timeout(5) self.hide()
notification.show() autotype(totp_code)
elif keyval == 102: elif keyval == 102:
# focus search # focus search
self.text_view.grab_focus() self.text_view.grab_focus()

View file

@ -131,16 +131,16 @@ def get_runtime_config():
except Exception as e: except Exception as e:
return None return None
def autotype(username, password): def autotype(text):
env = os.environ.copy() goldwarden_cmd = f"{BINARY_PATH} autotype"
# todo convert to stdin process = subprocess.Popen(goldwarden_cmd.split(), stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
env["PASSWORD"] = password text_hex = text.encode("utf-8").hex()
goldwarden_cmd = f"{BINARY_PATH} autotype --username {username}" print("autotyping", text_hex)
result = subprocess.run(goldwarden_cmd.split(), capture_output=True, text=True, env=env) process.stdin.write(text_hex + "\n")
print(result.stderr) process.stdin.flush()
print(result.stdout) # wait for process to finish
if result.returncode != 0: process.wait()
raise Exception("Failed to initialize repository, err", result.stderr) print("autotype finished")
def is_daemon_running(): def is_daemon_running():
result = send_authenticated_command(f"vault status") result = send_authenticated_command(f"vault status")