-
Notifications
You must be signed in to change notification settings - Fork 180
/
keyring_file.go
60 lines (46 loc) · 1.3 KB
/
keyring_file.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
package crypto
import (
"fmt"
"github.com/pydio/cells/v4/common/config"
)
type configProvider struct {
store config.Store
}
var (
// ErrNotFound is the expected error if the secret isn't found in the
// keyring.
ErrNotFound = fmt.Errorf("secret not found in keyring")
)
// Set stores user and pass in the keyring under the defined service
// name.
func (p *configProvider) Set(service, user, pass string) error {
if err := p.store.Val(service, user).Set(pass); err != nil {
return err
}
return p.store.Save("setting "+service+" for "+user, "system")
}
// Get gets a secret from the keyring given a service name and a user.
func (p *configProvider) Get(service, user string) (string, error) {
str := p.store.Val(service, user).String()
if str == "" {
return "", ErrNotFound
}
return str, nil
}
// Delete deletes a secret, identified by service & user, from the keyring.
func (p *configProvider) Delete(service, user string) error {
return p.store.Val(service, user).Del()
}
// NewConfigKeyring places the keyring in a config store
func NewConfigKeyring(store config.Store, opt ...KeyringOption) Keyring {
opts := new(KeyringOptions)
for _, o := range opt {
o(opts)
}
var provider Keyring
provider = &configProvider{store}
if opts.Auto {
provider = NewAutoKeyring(provider)
}
return provider
}