This repository has been archived by the owner on Jun 28, 2023. It is now read-only.
/
keytar_windows.go
72 lines (57 loc) · 1.88 KB
/
keytar_windows.go
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package keytar
import (
"fmt"
"github.com/danieljoos/wincred"
)
// Utility function to format service/account into something Windows can store
// AND query. Credentials actually have a username field, but you can't query
// on it, so it wouldn't allow us to store multiple credentials for the same
// service.
func targetFormat(service, account string) string {
return fmt.Sprintf("%s@%s", account, service)
}
// keychainWindows implements the Keychain interface on Windows by using the
// Credential Vault infrastructure to store items.
type keychainWindows struct{}
func (*keychainWindows) AddPassword(service, account, password string) error {
// Validate input
serviceValid := isValidNonNullUTF8(service)
accountValid := isValidNonNullUTF8(account)
passwordValid := isValidNonNullUTF8(password)
if !(serviceValid && accountValid && passwordValid) {
return ErrInvalidValue
}
cred := wincred.NewGenericCredential(targetFormat(service, account))
cred.CredentialBlob = []byte(password)
return cred.Write()
}
func (*keychainWindows) GetPassword(service, account string) (string, error) {
// Validate input
serviceValid := isValidNonNullUTF8(service)
accountValid := isValidNonNullUTF8(account)
if !(serviceValid && accountValid) {
return "", ErrInvalidValue
}
cred, err := wincred.GetGenericCredential(targetFormat(service, account))
if err != nil {
return "", err
}
return string(cred.CredentialBlob), nil
}
func (*keychainWindows) DeletePassword(service, account string) error {
// Validate input
serviceValid := isValidNonNullUTF8(service)
accountValid := isValidNonNullUTF8(account)
if !(serviceValid && accountValid) {
return ErrInvalidValue
}
cred, err := wincred.GetGenericCredential(targetFormat(service, account))
if err != nil {
return err
}
return cred.Delete()
}
func init() {
// Register the Windows keychain implementation
keychain = &keychainWindows{}
}