forked from tmc/keyring
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkeyring.go
58 lines (48 loc) · 1.39 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
package keyring
import (
"errors"
"sync"
)
var (
// ErrNotFound means the requested password was not found
ErrNotFound = errors.New("keyring: Password not found")
// ErrNoDefault means that no default keyring provider has been found
ErrNoDefault = errors.New("keyring: No suitable keyring provider found (check your build flags)")
providerInitOnce sync.Once
defaultProvider provider
providerInitError error
)
// provider provides a simple interface to keychain sevice
type provider interface {
Get(service, username string) (string, error)
Set(service, username, password string) error
}
func setupProvider() (provider, error) {
providerInitOnce.Do(func() {
defaultProvider, providerInitError = initializeProvider()
})
if providerInitError != nil {
return nil, providerInitError
} else if defaultProvider == nil {
return nil, ErrNoDefault
}
return defaultProvider, nil
}
// Get gets the password for a paricular Service and Username using the
// default keyring provider.
func Get(service, username string) (string, error) {
p, err := setupProvider()
if err != nil {
return "", err
}
return p.Get(service, username)
}
// Set sets the password for a particular Service and Username using the
// default keyring provider.
func Set(service, username, password string) error {
p, err := setupProvider()
if err != nil {
return err
}
return p.Set(service, username, password)
}