Reprompt For Password

This commit is contained in:
Brian Buller 2024-05-17 07:50:03 -05:00
parent 65d8a457a3
commit c02b8f3dff
2 changed files with 27 additions and 29 deletions

View File

@ -26,45 +26,41 @@ func init() {
} }
func runRofi(cmd *cobra.Command, args []string) error { func runRofi(cmd *cobra.Command, args []string) error {
var db *models.KeePassDB
var argIsPw bool
pwFile := fmt.Sprintf("%s%s", ConfigDir, "pw")
pass, err := util.ReadPWFile(pwFile)
if err != nil {
// See if we have a password...
if len(args) == 1 {
pass = args[0]
argIsPw = true
} else {
fmt.Println("Enter Master Password")
return nil
}
}
db, err = models.NewKeePassDB(viper.GetString("database"), pass)
if err != nil {
util.RemovePWFile(pwFile)
return fmt.Errorf("Error opening DB. Re-enter Master Password:\n%v", err)
} else if err = util.WritePWFile(pwFile, pass); err != nil {
return fmt.Errorf("DB Opened, but failed to persist password: %v", err)
}
var path []string var path []string
for i := range args { for i := range args {
path = append(path, strings.Split(args[i], "/")...) path = append(path, strings.Split(args[i], "/")...)
} }
var db *models.KeePassDB if len(path) == 0 || argIsPw {
var havePassword bool
pwFile := fmt.Sprintf("%s%s", ConfigDir, "pw")
pass, err := util.ReadPWFile(pwFile)
if err != nil {
havePassword = false
// See if we have a password...
if len(args) == 1 {
pass = args[0]
} else {
fmt.Println("Enter Master Password")
return nil
}
} else {
havePassword = true
}
db, err = models.NewKeePassDB(viper.GetString("database"), pass)
if err != nil {
return err
} else if !havePassword {
if err = util.WritePWFile(pwFile, pass); err != nil {
return err
}
path = []string{}
}
if len(path) == 0 {
list := db.GetAllEntriesFromRoot() list := db.GetAllEntriesFromRoot()
for i := range list { for i := range list {
fmt.Println(strings.Join(list[i], "/")) fmt.Println(strings.Join(list[i], "/"))
} }
} else { } else {
// Only one... Could be a single group nested, or it's an entry
if entry, entryErr := db.FindEntryFromRoot(path); entryErr != nil { if entry, entryErr := db.FindEntryFromRoot(path); entryErr != nil {
return entryErr return entryErr
} else { } else {

View File

@ -5,6 +5,7 @@ package util
import ( import (
"io/ioutil" "io/ioutil"
"os"
) )
func ReadFile(path string) (string, error) { func ReadFile(path string) (string, error) {
@ -35,3 +36,4 @@ func WritePWFile(path, pw string) error {
} }
return ioutil.WriteFile(path, []byte(pass), 0600) return ioutil.WriteFile(path, []byte(pass), 0600)
} }
func RemovePWFile(path string) error { return os.Remove(path) }