forked from 99designs/keyring
-
Notifications
You must be signed in to change notification settings - Fork 0
/
kwallet.go
218 lines (174 loc) · 4.58 KB
/
kwallet.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
// +build linux
package keyring
import (
"encoding/json"
"github.com/aulanov/go.dbus"
)
const (
dbusServiceName = "org.kde.kwalletd"
dbusPath = "/modules/kwalletd"
)
func init() {
// silently fail if dbus isn't available
_, err := dbus.SessionBus()
if err != nil {
return
}
supportedBackends[KWalletBackend] = opener(func(cfg Config) (Keyring, error) {
if cfg.ServiceName == "" {
cfg.ServiceName = "kdewallet"
}
if cfg.KWalletAppID == "" {
cfg.KWalletAppID = "keyring"
}
if cfg.KWalletFolder == "" {
cfg.KWalletFolder = "keyring"
}
wallet, err := newKwallet()
if err != nil {
return nil, err
}
ring := &kwalletKeyring{
wallet: *wallet,
name: cfg.ServiceName,
appID: cfg.KWalletAppID,
folder: cfg.KWalletFolder,
}
return ring, ring.openWallet()
})
}
type kwalletKeyring struct {
wallet kwalletBinding
name string
handle int32
appID string
folder string
}
func (k *kwalletKeyring) openWallet() error {
isOpen, err := k.wallet.IsOpen(k.handle)
if err != nil {
return err
}
if !isOpen {
handle, err := k.wallet.Open(k.name, 0, k.appID)
if err != nil {
return err
}
k.handle = handle
}
return nil
}
func (k *kwalletKeyring) Get(key string) (Item, error) {
err := k.openWallet()
if err != nil {
return Item{}, err
}
data, err := k.wallet.ReadEntry(k.handle, k.folder, key, k.appID)
if err != nil {
return Item{}, err
}
item := Item{}
err = json.Unmarshal(data, &item)
if err != nil {
return Item{}, err
}
return item, nil
}
func (k *kwalletKeyring) Set(item Item) error {
err := k.openWallet()
if err != nil {
return err
}
data, err := json.Marshal(item)
if err != nil {
return err
}
err = k.wallet.WriteEntry(k.handle, k.folder, item.Key, data, k.appID)
if err != nil {
return err
}
return nil
}
func (k *kwalletKeyring) Remove(key string) error {
err := k.openWallet()
if err != nil {
return err
}
err = k.wallet.RemoveEntry(k.handle, k.folder, key, k.appID)
if err != nil {
return err
}
return nil
}
func (k *kwalletKeyring) Keys() ([]string, error) {
err := k.openWallet()
if err != nil {
return []string{}, err
}
entries, err := k.wallet.EntryList(k.handle, k.folder, k.appID)
if err != nil {
return []string{}, err
}
return entries, nil
}
func newKwallet() (*kwalletBinding, error) {
conn, err := dbus.SessionBus()
if err != nil {
return nil, err
}
return &kwalletBinding{
conn.Object(dbusServiceName, dbusPath),
}, nil
}
// Dumb Dbus bindings for kwallet bindings with types
type kwalletBinding struct {
dbus dbus.BusObject
}
// method bool org.kde.KWallet.isOpen(int handle)
func (k *kwalletBinding) IsOpen(handle int32) (bool, error) {
call := k.dbus.Call("org.kde.KWallet.isOpen", 0, handle)
if call.Err != nil {
return false, call.Err
}
return call.Body[0].(bool), call.Err
}
// method int org.kde.KWallet.open(QString wallet, qlonglong wId, QString appid)
func (k *kwalletBinding) Open(name string, wID int64, appid string) (int32, error) {
call := k.dbus.Call("org.kde.KWallet.open", 0, name, wID, appid)
if call.Err != nil {
return 0, call.Err
}
return call.Body[0].(int32), call.Err
}
// method QStringList org.kde.KWallet.entryList(int handle, QString folder, QString appid)
func (k *kwalletBinding) EntryList(handle int32, folder string, appid string) ([]string, error) {
call := k.dbus.Call("org.kde.KWallet.entryList", 0, handle, folder, appid)
if call.Err != nil {
return []string{}, call.Err
}
return call.Body[0].([]string), call.Err
}
// method int org.kde.KWallet.writeEntry(int handle, QString folder, QString key, QByteArray value, QString appid)
func (k *kwalletBinding) WriteEntry(handle int32, folder string, key string, value []byte, appid string) error {
call := k.dbus.Call("org.kde.KWallet.writeEntry", 0, handle, folder, key, value, appid)
if call.Err != nil {
return call.Err
}
return call.Err
}
// method int org.kde.KWallet.removeEntry(int handle, QString folder, QString key, QString appid)
func (k *kwalletBinding) RemoveEntry(handle int32, folder string, key string, appid string) error {
call := k.dbus.Call("org.kde.KWallet.removeEntry", 0, handle, folder, key, appid)
if call.Err != nil {
return call.Err
}
return call.Err
}
// method QByteArray org.kde.KWallet.readEntry(int handle, QString folder, QString key, QString appid)
func (k *kwalletBinding) ReadEntry(handle int32, folder string, key string, appid string) ([]byte, error) {
call := k.dbus.Call("org.kde.KWallet.readEntry", 0, handle, folder, key, appid)
if call.Err != nil {
return []byte{}, call.Err
}
return call.Body[0].([]byte), call.Err
}