forked from Versent/saml2aws
-
Notifications
You must be signed in to change notification settings - Fork 0
/
linuxkeyring.go
72 lines (59 loc) · 1.68 KB
/
linuxkeyring.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 linuxkeyring
import (
"encoding/json"
"github.com/99designs/keyring"
"github.com/sirupsen/logrus"
"github.com/versent/saml2aws/helper/credentials"
)
var logger = logrus.WithField("helper", "linuxkeyring")
type KeyringHelper struct {
keyring keyring.Keyring
}
func NewKeyringHelper() (*KeyringHelper, error) {
kr, err := keyring.Open(keyring.Config{
AllowedBackends: []keyring.BackendType{
keyring.KWalletBackend,
keyring.SecretServiceBackend,
keyring.PassBackend,
},
LibSecretCollectionName: "login",
PassPrefix: "saml2aws",
})
if err != nil {
return nil, err
}
return &KeyringHelper{
keyring: kr,
}, nil
}
func (kr *KeyringHelper) Add(creds *credentials.Credentials) error {
encoded, err := json.Marshal(creds)
if err != nil {
return err
}
return kr.keyring.Set(keyring.Item{
Key: creds.ServerURL,
Label: credentials.CredsLabel,
Data: encoded,
KeychainNotTrustApplication: false,
})
}
func (kr *KeyringHelper) Delete(serverURL string) error {
return kr.keyring.Remove(serverURL)
}
func (kr *KeyringHelper) Get(serverURL string) (string, string, error) {
item, err := kr.keyring.Get(serverURL)
if err != nil {
logger.WithField("err", err).Error("keychain Get returned error")
return "", "", credentials.ErrCredentialsNotFound
}
var creds credentials.Credentials
if err = json.Unmarshal(item.Data, &creds); err != nil {
logger.WithField("err", err).Error("stored credential malformed")
return "", "", credentials.ErrCredentialsNotFound
}
return creds.Username, creds.Secret, nil
}
func (KeyringHelper) SupportsCredentialStorage() bool {
return true
}