kstore-cred/main.go

84 lines
1.5 KiB
Go
Raw Permalink Normal View History

2022-08-14 08:05:35 +00:00
package main
import (
"errors"
"fmt"
"os"
"path/filepath"
"strings"
"github.com/danieljoos/wincred"
)
const (
CRED_NAME = "RBASH-KSTORE"
SECRET_NAME = "_AUTH_SECRET"
)
func usage(stdfd *os.File) {
_name := filepath.Base(os.Args[0])
_space := strings.Repeat(" ", len(_name))
fmt.Fprintf(stdfd, "Usage: %s get\n", _name)
fmt.Fprintf(stdfd, " %s set passphrase\n", _space)
2023-10-08 20:08:18 +00:00
fmt.Fprintf(stdfd, " %s del\n", _space)
2022-08-14 08:05:35 +00:00
fmt.Fprintf(stdfd, " %s help\n", _space)
}
func getAuthSecret() (*wincred.GenericCredential, error) {
cred, err := wincred.GetGenericCredential(CRED_NAME)
return cred, err
}
func delAuthSecret() error {
cred, _ := getAuthSecret()
if cred == nil {
return nil
}
return cred.Delete()
}
func setAuthSecret(newSecret string) error {
cred, _ := getAuthSecret()
if cred == nil {
cred = wincred.NewGenericCredential(CRED_NAME)
}
cred.CredentialBlob = []byte(newSecret)
return cred.Write()
}
func main() {
if len(os.Args) < 2 {
usage(os.Stderr)
os.Exit(1)
}
var err error
switch os.Args[1] {
case "get":
cred, _err := getAuthSecret()
if _err == nil {
2023-10-08 20:08:18 +00:00
fmt.Print(string(cred.CredentialBlob))
2022-08-14 08:05:35 +00:00
os.Exit(0)
}
err = _err
case "set":
if len(os.Args[2:]) < 1 {
err = errors.New("passphrase is required")
} else {
err = setAuthSecret(os.Args[2])
}
case "del":
err = delAuthSecret()
case "help":
usage(os.Stdout)
}
if err == nil {
os.Exit(0)
}
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}