forked from 99designs/keyring
-
Notifications
You must be signed in to change notification settings - Fork 0
/
keyring.go
92 lines (78 loc) · 2.41 KB
/
keyring.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
// Package keyring provides a uniform API over a range of desktop credential storage engines
//
// See project homepage at https://github.com/99designs/keyring for more background
package keyring
import (
"errors"
"log"
)
// All currently supported secure storage backends
const (
InvalidBackend BackendType = "invalid"
SecretServiceBackend BackendType = "secret-service"
KeychainBackend BackendType = "keychain"
)
type BackendType string
var supportedBackends = map[BackendType]opener{}
// AvailableBackends provides a slice of all available backend keys on the current OS
func AvailableBackends() []BackendType {
b := []BackendType{}
for k := range supportedBackends {
b = append(b, k)
}
// make sure FileBackend is last
return b
}
type opener func(cfg Config) (Keyring, error)
// Open will open a specific keyring backend
func Open(cfg Config) (Keyring, BackendType, error) {
if cfg.AllowedBackends == nil {
cfg.AllowedBackends = AvailableBackends()
}
debugf("Considering backends: %v", cfg.AllowedBackends)
for _, backend := range cfg.AllowedBackends {
if opener, ok := supportedBackends[backend]; ok {
openBackend, err := opener(cfg)
if err != nil {
debugf("Failed backend %s: %s", backend, err)
continue
}
return openBackend, backend, nil
}
}
return nil, InvalidBackend, ErrNoAvailImpl
}
// Item is a thing stored on the keyring
type Item struct {
Key string
Data []byte
Label string
Description string
// Backend specific config
KeychainNotTrustApplication bool
KeychainNotSynchronizable bool
}
// Keyring provides the uniform interface over the underlying backends
type Keyring interface {
// Returns an Item matching the key or ErrKeyNotFound
Get(key string) (Item, error)
// Stores an Item on the keyring
Set(item Item) error
// Removes the item with matching key
Remove(key string) error
// Provides a slice of all keys stored on the keyring
Keys() ([]string, error)
}
// ErrNoAvailImpl is returned by Open when a backend cannot be found
var ErrNoAvailImpl = errors.New("Specified keyring backend not available")
// ErrKeyNotFound is returned by Keyring Get when the item is not on the keyring
var ErrKeyNotFound = errors.New("The specified item could not be found in the keyring.")
var (
// Whether to print debugging output
Debug bool
)
func debugf(pattern string, args ...interface{}) {
if Debug {
log.Printf("[keyring] "+pattern, args...)
}
}