Skip to content

Commit

Permalink
Merge pull request #177 from quexten/feature/persistent-autotype-perm…
Browse files Browse the repository at this point in the history
…ission

Add experimental persistent autotype
  • Loading branch information
quexten authored Apr 28, 2024
2 parents 7265154 + bb354b9 commit 695ba11
Showing 1 changed file with 66 additions and 3 deletions.
69 changes: 66 additions & 3 deletions autotype/libportalautotype.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ package autotype

import (
"fmt"
"io/ioutil"
"os"
"time"

"github.com/godbus/dbus/v5"
Expand All @@ -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()
Expand Down Expand Up @@ -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{
"types": dbus.MakeVariant(uint32(1)),
})
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())
}
Expand All @@ -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 {
Expand Down

0 comments on commit 695ba11

Please sign in to comment.