-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathkeyring_linux.go
153 lines (136 loc) · 4.27 KB
/
keyring_linux.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
//go:build !gnome_keyring
// +build !gnome_keyring
package keyring
import (
"fmt"
dbus "github.com/godbus/dbus/v5"
)
const (
ssServiceName = "org.freedesktop.secrets"
ssServicePath = "/org/freedesktop/secrets"
ssCollectionPath = "/org/freedesktop/secrets/aliases/default"
ssServiceIface = "org.freedesktop.Secret.Service."
ssSessionIface = "org.freedesktop.Secret.Session."
ssCollectionIface = "org.freedesktop.Secret.Collection."
ssItemIface = "org.freedesktop.Secret.Item."
ssPromptIface = "org.freedesktop.Secret.Prompt."
)
// ssSecret corresponds to org.freedesktop.Secret.Item
// Note: Order is important
type ssSecret struct {
Session dbus.ObjectPath
Parameters []byte
Value []byte
ContentType string `dbus:"content_type"`
}
// newSSSecret prepares an ssSecret for use
// Uses text/plain as the Content-type which may need to change in the future
func newSSSecret(session dbus.ObjectPath, secret string) (s ssSecret) {
s = ssSecret{
ContentType: "text/plain; charset=utf8",
Parameters: []byte{},
Session: session,
Value: []byte(secret),
}
return
}
// ssProvider implements the provider interface freedesktop SecretService
type ssProvider struct {
*dbus.Conn
srv dbus.BusObject
}
// This is used to open a seassion for every get/set. Alternative might be to
// defer() the call to close when constructing the ssProvider
func (s *ssProvider) openSession() (dbus.BusObject, error) {
var disregard dbus.Variant
var sessionPath dbus.ObjectPath
method := fmt.Sprint(ssServiceIface, "OpenSession")
err := s.srv.Call(method, 0, "plain", dbus.MakeVariant("")).Store(&disregard, &sessionPath)
if err != nil {
return nil, err
}
return s.Object(ssServiceName, sessionPath), nil
}
// Unsure how the .Prompt call surfaces, it hasn't come up.
func (s *ssProvider) unlock(p dbus.ObjectPath) error {
var unlocked []dbus.ObjectPath
var prompt dbus.ObjectPath
method := fmt.Sprint(ssServiceIface, "Unlock")
err := s.srv.Call(method, 0, []dbus.ObjectPath{p}).Store(&unlocked, &prompt)
if err != nil {
return fmt.Errorf("keyring/dbus: Unlock error: %s", err)
}
if prompt != dbus.ObjectPath("/") {
method = fmt.Sprint(ssPromptIface, "Prompt")
call := s.Object(ssServiceName, prompt).Call(method, 0, "unlock")
return call.Err
}
return nil
}
func (s *ssProvider) Get(c, u string) (string, error) {
results := []dbus.ObjectPath{}
var secret ssSecret
search := map[string]string{
"username": u,
"service": c,
}
session, err := s.openSession()
if err != nil {
return "", err
}
defer session.Call(fmt.Sprint(ssSessionIface, "Close"), 0)
s.unlock(ssCollectionPath)
collection := s.Object(ssServiceName, ssCollectionPath)
method := fmt.Sprint(ssCollectionIface, "SearchItems")
call := collection.Call(method, 0, search)
err = call.Store(&results)
if call.Err != nil {
return "", call.Err
}
// results is a slice. Just grab the first one.
if len(results) == 0 {
return "", ErrNotFound
}
method = fmt.Sprint(ssItemIface, "GetSecret")
err = s.Object(ssServiceName, results[0]).Call(method, 0, session.Path()).Store(&secret)
if err != nil {
return "", err
}
return string(secret.Value), nil
}
func (s *ssProvider) Set(c, u, p string) error {
var item, prompt dbus.ObjectPath
properties := map[string]dbus.Variant{
"org.freedesktop.Secret.Item.Label": dbus.MakeVariant(fmt.Sprintf("%s - %s", u, c)),
"org.freedesktop.Secret.Item.Attributes": dbus.MakeVariant(map[string]string{
"username": u,
"service": c,
}),
}
session, err := s.openSession()
if err != nil {
return err
}
defer session.Call(fmt.Sprint(ssSessionIface, "Close"), 0)
s.unlock(ssCollectionPath)
collection := s.Object(ssServiceName, ssCollectionPath)
secret := newSSSecret(session.Path(), p)
// the bool is "replace"
err = collection.Call(fmt.Sprint(ssCollectionIface, "CreateItem"), 0, properties, secret, true).Store(&item, &prompt)
if err != nil {
return fmt.Errorf("keyring/dbus: CreateItem error: %s", err)
}
if prompt != "/" {
s.Object(ssServiceName, prompt).Call(fmt.Sprint(ssPromptIface, "Prompt"), 0, "unlock")
}
return nil
}
func initializeProvider() (provider, error) {
conn, err := dbus.SessionBus()
if err != nil {
return nil, err
}
srv := conn.Object(ssServiceName, ssServicePath)
p := &ssProvider{conn, srv}
return p, nil
}