mirror of
https://github.com/rwinkhart/libmutton.git
synced 2026-08-27 20:36:29 -04:00
Drop support for non-continuous TOTP copy
This commit is contained in:
@@ -1,3 +1,5 @@
|
||||
//go:build (!android && !ios) || termux
|
||||
|
||||
package clip
|
||||
|
||||
import (
|
||||
@@ -9,37 +11,28 @@ import (
|
||||
"github.com/rwinkhart/libmutton/crypt"
|
||||
)
|
||||
|
||||
// CopyArgument copies a field from an entry to the clipboard.
|
||||
// If field is -1, it will one-time copy the TOTP code
|
||||
// (instead of keeping the clipboard up-to-date).
|
||||
func CopyArgument(targetLocation string, field int) error {
|
||||
// CopyShortcut, given a path, decrypts an
|
||||
// entry and copies a field to the clipboard.
|
||||
func CopyShortcut(targetLocation string, field int) error {
|
||||
// ensure targetLocation exists and is a file
|
||||
_, err := back.TargetIsFile(targetLocation, true)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// decrypt entry
|
||||
decSlice, err := crypt.DecryptFileToSlice(targetLocation)
|
||||
if err != nil {
|
||||
return errors.New("unable to decrypt entry: " + err.Error())
|
||||
}
|
||||
|
||||
// handle non-persistent TOTP copy
|
||||
var copySubject string
|
||||
var realField int
|
||||
if field == -1 {
|
||||
realField = 2
|
||||
} else {
|
||||
realField = field
|
||||
}
|
||||
|
||||
// if field exists in entry...
|
||||
if len(decSlice) > realField {
|
||||
if decSlice[realField] == "" {
|
||||
if len(decSlice) > field {
|
||||
if decSlice[field] == "" {
|
||||
return errors.New("field is empty")
|
||||
}
|
||||
|
||||
if realField == 2 { // TOTP mode
|
||||
if field == 2 { // TOTP mode
|
||||
fmt.Println(back.AnsiWarning + "[Starting]" + back.AnsiReset + " TOTP clipboard refresher")
|
||||
errorChan := make(chan error)
|
||||
go TOTPCopier(decSlice[2], field, errorChan, nil) // "done" is not needed because the process runs until the program is killed
|
||||
@@ -52,19 +45,16 @@ func CopyArgument(targetLocation string, field int) error {
|
||||
}
|
||||
select {} // block indefinitely
|
||||
} else { // other
|
||||
copySubject = decSlice[realField]
|
||||
// copy field to clipboard; launch clipboard clearing process
|
||||
err = CopyString(true, decSlice[field])
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
} else {
|
||||
return errors.New("field does not exist in entry")
|
||||
}
|
||||
|
||||
// copy field to clipboard; launch clipboard clearing process
|
||||
err = CopyString(false, copySubject)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// ClipClearArgument reads the assigned clipboard contents from stdin and passes them to clipClearProcess.
|
||||
+7
-14
@@ -1,3 +1,5 @@
|
||||
//go:build (!android && !ios) || termux
|
||||
|
||||
package clip
|
||||
|
||||
import (
|
||||
@@ -29,16 +31,7 @@ func GenTOTP(secret string, time time.Time, forSteam bool) (string, error) {
|
||||
|
||||
// TOTPCopier is meant to be run as a goroutine to keep
|
||||
// the clipboard up-to-date with the latest TOTP token.
|
||||
// Set oneTime to -1 for a one-time (non-continuous) TOTP copy.
|
||||
// For oneTime mode, this function can be used normally (not as a goroutine).
|
||||
func TOTPCopier(secret string, oneTime int, errorChan chan<- error, done <-chan bool) error {
|
||||
var writeErrorChan func(err error)
|
||||
if errorChan != nil {
|
||||
writeErrorChan = func(err error) { errorChan <- err }
|
||||
} else {
|
||||
writeErrorChan = func(err error) {}
|
||||
}
|
||||
|
||||
var forSteam bool
|
||||
if strings.HasPrefix(secret, "steam@") {
|
||||
secret = secret[6:]
|
||||
@@ -49,16 +42,16 @@ func TOTPCopier(secret string, oneTime int, errorChan chan<- error, done <-chan
|
||||
currentTime := time.Now()
|
||||
token, err := GenTOTP(secret, currentTime, forSteam)
|
||||
if err != nil {
|
||||
writeErrorChan(err)
|
||||
errorChan <- err
|
||||
return err // return for when not used as goroutine; should exit on error regardless
|
||||
}
|
||||
err = CopyString(true, token)
|
||||
err = CopyString(false, token)
|
||||
if err != nil {
|
||||
writeErrorChan(err)
|
||||
errorChan <- err
|
||||
return err // return for when not used as goroutine; should exit on error regardless
|
||||
}
|
||||
if oneTime != -1 {
|
||||
writeErrorChan(err)
|
||||
errorChan <- err
|
||||
time.Sleep(time.Duration(30-(currentTime.Second()%30)) * time.Second)
|
||||
} else {
|
||||
return nil
|
||||
@@ -67,7 +60,7 @@ func TOTPCopier(secret string, oneTime int, errorChan chan<- error, done <-chan
|
||||
// exit after sleep if indicated (will not update clipboard again)
|
||||
select {
|
||||
case <-done:
|
||||
writeErrorChan(nil)
|
||||
errorChan <- err
|
||||
return nil
|
||||
default:
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user