Add experimental persistent autotype permission

This commit is contained in:
Bernd Schoolmann 2024-04-28 21:50:22 +02:00
parent 7265154b9a
commit bb354b99c7
No known key found for this signature in database
1 changed files with 66 additions and 3 deletions

View File

@ -4,6 +4,8 @@ package autotype
import (
"fmt"
"io/ioutil"
"os"
"time"
"github.com/godbus/dbus/v5"
@ -16,6 +18,47 @@ const autoTypeDelay = 1 * time.Millisecond
var log = logging.GetLogger("Goldwarden", "Autotype")
// todo need to store this encrypted. will be done when migrating this file to python
func persistToken(token string) error {
tokenPath := ""
userHome, err := os.UserHomeDir()
if err != nil {
return err
}
if _, err := os.Stat("/.flatpak-info"); err == nil {
tokenPath = userHome + "/.var/app/com.quexten.Goldwarden/config/autotype_token.txt"
} else {
tokenPath = userHome + "/.config/goldwarden/autotype_token.txt"
}
err = ioutil.WriteFile(tokenPath, []byte(token), 0644)
if err != nil {
return err
}
return nil
}
func readToken() (string, error) {
tokenPath := ""
userHome, err := os.UserHomeDir()
if err != nil {
return "", err
}
if _, err := os.Stat("/.flatpak-info"); err == nil {
tokenPath = userHome + "/.var/app/com.quexten.Goldwarden/config/autotype_token.txt"
} else {
tokenPath = userHome + "/.config/goldwarden/autotype_token.txt"
}
token, err := ioutil.ReadFile(tokenPath)
if err != nil {
return "", err
}
return string(token), nil
}
func TypeString(textToType string) {
log.Info("Starting to Type String")
bus, err := dbus.SessionBus()
@ -49,9 +92,16 @@ func TypeString(textToType string) {
result := message.Body[1].(map[string]dbus.Variant)
resultSessionHandle := result["session_handle"]
sessionHandle = dbus.ObjectPath(resultSessionHandle.String()[1 : len(resultSessionHandle.String())-1])
res := obj.Call("org.freedesktop.portal.RemoteDesktop.SelectDevices", 0, sessionHandle, map[string]dbus.Variant{
options := map[string]dbus.Variant{
"types": dbus.MakeVariant(uint32(1)),
})
"persist_mode": dbus.MakeVariant(uint32(2)),
}
if token, err := readToken(); err == nil {
log.Info("Restoring token, no confirmation prompt")
options["restore_token"] = dbus.MakeVariant(token)
}
res := obj.Call("org.freedesktop.portal.RemoteDesktop.SelectDevices", 0, sessionHandle, options)
if res.Err != nil {
log.Error("Error selecting devices: %s", res.Err.Error())
}
@ -64,6 +114,19 @@ func TypeString(textToType string) {
}
state = 2
case 2:
// try to cast to interface array
if len(message.Body) == 2 {
if resMap, ok := message.Body[1].(map[string]dbus.Variant); ok {
// check if restore token in response
if restoreToken, ok := resMap["restore_token"]; ok {
token := restoreToken.Value().(string)
if err := persistToken(token); err != nil {
log.Error("Error persisting token: %s", err.Error())
}
}
}
}
log.Info("Performing Typing")
time.Sleep(1000 * time.Millisecond)
for _, char := range textToType {