Implement initial TOTP support (secret in third notes line, will eventually have its own dedicated field)

This commit is contained in:
2024-03-18 16:44:22 -04:00
parent 56eaec02c6
commit c1a415e5b0
8 changed files with 90 additions and 47 deletions
+30 -7
View File
@@ -3,9 +3,12 @@ package offline
import (
"bufio"
"fmt"
"github.com/pquerna/otp/totp"
"os"
"time"
)
// CopyArgument copies a field from an entry to the clipboard
func CopyArgument(targetLocation string, field int, executableName string) {
if isFile, _ := TargetIsFile(targetLocation, true, 2); isFile {
@@ -14,23 +17,43 @@ func CopyArgument(targetLocation string, field int, executableName string) {
// ensure field exists in entry
if len(decryptedEntry) > field {
copySubject = decryptedEntry[field]
// ensure field is not empty
if decryptedEntry[field] == "" {
fmt.Println(AnsiError + "Field is empty" + AnsiReset)
os.Exit(1)
}
if field != 5 { // TODO Update field after removed from notes (breaking sshyp entry compatibility)
copySubject = decryptedEntry[field]
} else { // TOTP mode
var err error
var currentSecond int // track current second for TOTP code refresh
var copied bool // track whether the TOTP code has been copied to the clipboard for the first time
for { // keep field copied to clipboard, refresh on 30-second intervals
currentSecond = time.Now().Second()
if currentSecond == 0 || currentSecond == 30 || !copied {
copySubject, err = totp.GenerateCode(decryptedEntry[5], time.Now())
if err != nil {
fmt.Println(AnsiError + "Error generating TOTP code" + AnsiReset)
os.Exit(1)
}
copyField(copySubject, "")
}
time.Sleep(1 * time.Second)
}
}
} else {
fmt.Println(AnsiError + "Field does not exist in entry" + AnsiReset)
os.Exit(1)
}
// ensure field is not blank
if copySubject == "" {
fmt.Println(AnsiError + "Field is empty" + AnsiReset)
os.Exit(1)
}
// copy field to clipboard, launch clipboard clearing process
copyField(copySubject, executableName)
}
}
// ClipClearArgument is called to clear the clipboard after 30 seconds if the contents have not been modified
func ClipClearArgument() {
// read previous clipboard contents from stdin
clipScanner := bufio.NewScanner(os.Stdin)
+12 -10
View File
@@ -12,7 +12,7 @@ import (
// TODO MacOS support is entirely untested - I would appreciate feedback on this implementation
// CopyField copies a field from an entry to the clipboard
// copyField copies a field from an entry to the clipboard
func copyField(copySubject string, executableName string) {
cmd := exec.Command("pbcopy")
writeToStdin(cmd, copySubject)
@@ -22,18 +22,20 @@ func copyField(copySubject string, executableName string) {
os.Exit(1)
}
cmd = exec.Command(executableName, "clipclear")
writeToStdin(cmd, copySubject)
err = cmd.Start()
if err != nil {
fmt.Println(AnsiError + "Failed to launch automated clipboard clearing process - does this libmutton implementation support the \"clipclear\" argument?" + AnsiReset)
os.Exit(1)
// launch clipboard clearing process if executableName is provided
if executableName != "" {
cmd = exec.Command(executableName, "clipclear")
writeToStdin(cmd, copySubject)
err = cmd.Start()
if err != nil {
fmt.Println(AnsiError + "Failed to launch automated clipboard clearing process - does this libmutton implementation support the \"clipclear\" argument?" + AnsiReset)
os.Exit(1)
}
os.Exit(0) // only exit if clipboard clearing process is launched, otherwise assume continuous clipboard refresh
}
os.Exit(0)
}
// ClipClear is called in a separate process to clear the clipboard after 30 seconds
// clipClear is called in a separate process to clear the clipboard after 30 seconds
func clipClear(oldContents string) {
time.Sleep(30 * time.Second)
+12 -10
View File
@@ -10,7 +10,7 @@ import (
"time"
)
// CopyField copies a field from an entry to the clipboard
// copyField copies a field from an entry to the clipboard
func copyField(copySubject string, executableName string) {
cmd := exec.Command("termux-clipboard-set")
writeToStdin(cmd, copySubject)
@@ -20,18 +20,20 @@ func copyField(copySubject string, executableName string) {
os.Exit(1)
}
cmd = exec.Command(executableName, "clipclear")
writeToStdin(cmd, copySubject)
err = cmd.Start()
if err != nil {
fmt.Println(AnsiError + "Failed to launch automated clipboard clearing process - does this libmutton implementation support the \"clipclear\" argument?" + AnsiReset)
os.Exit(1)
// launch clipboard clearing process if executableName is provided
if executableName != "" {
cmd = exec.Command(executableName, "clipclear")
writeToStdin(cmd, copySubject)
err = cmd.Start()
if err != nil {
fmt.Println(AnsiError + "Failed to launch automated clipboard clearing process - does this libmutton implementation support the \"clipclear\" argument?" + AnsiReset)
os.Exit(1)
}
os.Exit(0) // only exit if clipboard clearing process is launched, otherwise assume continuous clipboard refresh
}
os.Exit(0)
}
// ClipClear is called in a separate process to clear the clipboard after 30 seconds
// clipClear is called in a separate process to clear the clipboard after 30 seconds
func clipClear(oldContents string) {
time.Sleep(30 * time.Second)
+12 -10
View File
@@ -10,7 +10,7 @@ import (
"time"
)
// CopyField copies a field from an entry to the clipboard
// copyField copies a field from an entry to the clipboard
func copyField(copySubject string, executableName string) {
var envSet bool // track whether environment variables are set
var cmd *exec.Cmd
@@ -31,18 +31,20 @@ func copyField(copySubject string, executableName string) {
os.Exit(1)
}
cmd = exec.Command(executableName, "clipclear")
writeToStdin(cmd, copySubject)
err = cmd.Start()
if err != nil {
fmt.Println(AnsiError + "Failed to launch automated clipboard clearing process - does this libmutton implementation support the \"clipclear\" argument?" + AnsiReset)
os.Exit(1)
// launch clipboard clearing process if executableName is provided
if executableName != "" {
cmd = exec.Command(executableName, "clipclear")
writeToStdin(cmd, copySubject)
err = cmd.Start()
if err != nil {
fmt.Println(AnsiError + "Failed to launch automated clipboard clearing process - does this libmutton implementation support the \"clipclear\" argument?" + AnsiReset)
os.Exit(1)
}
os.Exit(0) // only exit if clipboard clearing process is launched, otherwise assume continuous clipboard refresh
}
os.Exit(0)
}
// ClipClear is called in a separate process to clear the clipboard after 30 seconds
// clipClear is called in a separate process to clear the clipboard after 30 seconds
func clipClear(oldContents string) {
time.Sleep(30 * time.Second)
+12 -10
View File
@@ -10,7 +10,7 @@ import (
"time"
)
// CopyField copies a field from an entry to the clipboard
// copyField copies a field from an entry to the clipboard
func copyField(copySubject string, executableName string) {
cmd := exec.Command("powershell.exe", "-c", fmt.Sprintf("echo '%s' | Set-Clipboard", strings.ReplaceAll(copySubject, "'", "''")))
err := cmd.Run()
@@ -19,18 +19,20 @@ func copyField(copySubject string, executableName string) {
os.Exit(1)
}
cmd = exec.Command(executableName, "clipclear")
writeToStdin(cmd, copySubject)
err = cmd.Start()
if err != nil {
fmt.Println(AnsiError + "Failed to launch automated clipboard clearing process - does this libmutton implementation support the \"clipclear\" argument?" + AnsiReset)
os.Exit(1)
// launch clipboard clearing process if executableName is provided
if executableName != "" {
cmd = exec.Command(executableName, "clipclear")
writeToStdin(cmd, copySubject)
err = cmd.Start()
if err != nil {
fmt.Println(AnsiError + "Failed to launch automated clipboard clearing process - does this libmutton implementation support the \"clipclear\" argument?" + AnsiReset)
os.Exit(1)
}
os.Exit(0) // only exit if clipboard clearing process is launched, otherwise assume continuous clipboard refresh
}
os.Exit(0)
}
// ClipClear is called in a separate process to clear the clipboard after 30 seconds
// clipClear is called in a separate process to clear the clipboard after 30 seconds
func clipClear(oldContents string) {
time.Sleep(30 * time.Second)