localconfig/creds.go

106 lines
2.6 KiB
Go
Raw Permalink Normal View History

2023-01-28 13:40:19 +01:00
package localconfig
import (
"bufio"
"encoding/json"
"errors"
"fmt"
"math/rand"
"os"
"strings"
"time"
"github.com/ProtonMail/gopenpgp/v2/helper"
"github.com/howeyc/gopass"
)
// Creds is user credentials
type Creds struct {
Login string
Password string
}
func randomString(n int) string {
rand.Seed(time.Now().UnixNano())
var letters = []rune("яшертёыуиопющэъасдфгчйкльжзхцвбнмabéàèùàçïôÖcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789&([-|_@])=+§!:;,*%$£µ")
s := make([]rune, n)
for i := range s {
s[i] = letters[rand.Intn(len(letters))]
}
return string(s)
}
func loadRootKey() []byte {
2023-01-28 19:26:58 +01:00
rkpath := GetConfigPath("rk")
2023-01-28 13:40:19 +01:00
rk, err := os.ReadFile(rkpath)
if err != nil {
rk = []byte(randomString(64))
os.WriteFile(rkpath, rk, 0600)
}
return rk
}
func (creds *Creds) Exists(filename string) bool {
2023-01-28 19:26:58 +01:00
if _, err := os.Stat(GetConfigPath(filename)); err == nil {
2023-01-28 13:40:19 +01:00
return true
} else if errors.Is(err, os.ErrNotExist) {
return false
} else {
return false
}
}
// Init will initalize login/password
func (creds *Creds) Init(filename string) {
fmt.Print("Enter login: ")
var login string
reader := bufio.NewReader(os.Stdin)
login, _ = reader.ReadString('\n')
creds.Login = strings.Trim(login, "\r\n")
if login != "" {
fmt.Print("Enter password:")
bytePassword, _ := gopass.GetPasswd()
silentPassword := string(bytePassword)
if silentPassword != "" {
creds.Password = silentPassword
b, _ := json.Marshal(creds)
2023-01-28 19:26:58 +01:00
fmt.Println("Writing credentials to : " + GetConfigPath(filename))
2023-01-28 13:40:19 +01:00
armor, _ := helper.EncryptMessageWithPassword(loadRootKey(), string(b))
2023-01-28 19:26:58 +01:00
os.WriteFile(GetConfigPath(filename), []byte(armor), 0600)
2023-01-28 13:40:19 +01:00
}
}
}
// Pw : update password
func (creds *Creds) NewPw(filename string) {
fmt.Print("Enter new password:")
bytePassword, _ := gopass.GetPasswd()
silentPassword := string(bytePassword)
if silentPassword != "" {
creds.Password = silentPassword
b, _ := json.Marshal(creds)
armor, _ := helper.EncryptMessageWithPassword(loadRootKey(), string(b))
2023-01-28 19:26:58 +01:00
os.WriteFile(GetConfigPath(filename), []byte(armor), 0600)
2023-01-28 13:40:19 +01:00
}
}
// Load credentials if available or creates them
func (creds *Creds) Load(filename string) error {
2023-01-28 19:26:58 +01:00
indata, err := os.ReadFile(GetConfigPath(filename))
2023-01-28 13:40:19 +01:00
if err != nil {
creds.Init(filename)
return nil
}
pass, err := helper.DecryptMessageWithPassword(loadRootKey(), string(indata))
if err != nil {
creds.Init(filename)
return nil
}
err = json.Unmarshal([]byte(pass), &creds)
if err != nil {
creds.Init(filename)
return nil
}
return nil
}