Öffentliche Dateiansicht: Raw-Dateien, Tree, Releases und Issues sind ohne Login verfügbar.
vault/keychain_darwin.go Raw
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
//go:build darwin

package vault

import (
	"errors"
	"fmt"
	"os"
	"os/exec"
	"strings"
)

const keychainService = "envault"

func keychainAccount() string {
	if u := os.Getenv("USER"); u != "" {
		return u
	}
	return "envault"
}

// KeychainGet retrieves the master password from macOS Keychain.
func KeychainGet() (string, error) {
	out, err := exec.Command(
		"security", "find-generic-password",
		"-a", keychainAccount(),
		"-s", keychainService,
		"-w",
	).Output()
	if err != nil {
		return "", errors.New("no password in keychain")
	}
	return strings.TrimSpace(string(out)), nil
}

// KeychainSet stores the master password in macOS Keychain.
func KeychainSet(password string) error {
	err := exec.Command(
		"security", "add-generic-password",
		"-U",
		"-a", keychainAccount(),
		"-s", keychainService,
		"-w", password,
	).Run()
	if err != nil {
		return fmt.Errorf("saving to keychain: %w", err)
	}
	return nil
}

// KeychainDelete removes the stored password from macOS Keychain.
func KeychainDelete() error {
	return exec.Command(
		"security", "delete-generic-password",
		"-a", keychainAccount(),
		"-s", keychainService,
	).Run()
}