-
Notifications
You must be signed in to change notification settings - Fork 9
/
service.go
141 lines (102 loc) · 3.33 KB
/
service.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
package libsecret
import "github.com/godbus/dbus"
const (
DBusServiceName = "org.freedesktop.secrets"
DBusPath = "/org/freedesktop/secrets"
)
type DBusObject interface {
Path() dbus.ObjectPath
}
type Service struct {
conn *dbus.Conn
dbus dbus.BusObject
}
func NewService() (*Service, error) {
conn, err := dbus.SessionBus()
if err != nil {
return &Service{}, err
}
return &Service{
conn: conn,
dbus: conn.Object(DBusServiceName, DBusPath),
}, nil
}
func (service Service) Path() dbus.ObjectPath {
return service.dbus.Path()
}
// OpenSession (IN String algorithm, IN Variant input, OUT Variant output, OUT ObjectPath result);
func (service *Service) Open() (*Session, error) {
var output dbus.Variant
var path dbus.ObjectPath
err := service.dbus.Call("org.freedesktop.Secret.Service.OpenSession", 0, "plain", dbus.MakeVariant("")).Store(&output, &path)
if err != nil {
return &Session{}, err
}
return NewSession(service.conn, path), nil
}
// READ Array<ObjectPath> Collections;
func (service *Service) Collections() ([]Collection, error) {
val, err := service.dbus.GetProperty("org.freedesktop.Secret.Service.Collections")
if err != nil {
return []Collection{}, err
}
collections := []Collection{}
for _, path := range val.Value().([]dbus.ObjectPath) {
collections = append(collections, *NewCollection(service.conn, path))
}
return collections, nil
}
// CreateCollection (IN Dict<String,Variant> properties, IN String alias, OUT ObjectPath collection, OUT ObjectPath prompt);
func (service *Service) CreateCollection(label string) (*Collection, error) {
properties := make(map[string]dbus.Variant)
properties["org.freedesktop.Secret.Collection.Label"] = dbus.MakeVariant(label)
var path dbus.ObjectPath
var prompt dbus.ObjectPath
err := service.dbus.Call("org.freedesktop.Secret.Service.CreateCollection", 0, properties, "").Store(&path, &prompt)
if err != nil {
return &Collection{}, err
}
if isPrompt(prompt) {
prompt := NewPrompt(service.conn, prompt)
result, err := prompt.Prompt()
if err != nil {
return &Collection{}, err
}
path = result.Value().(dbus.ObjectPath)
}
return NewCollection(service.conn, path), nil
}
// Unlock (IN Array<ObjectPath> objects, OUT Array<ObjectPath> unlocked, OUT ObjectPath prompt);
func (service *Service) Unlock(object DBusObject) error {
objects := []dbus.ObjectPath{object.Path()}
var unlocked []dbus.ObjectPath
var prompt dbus.ObjectPath
err := service.dbus.Call("org.freedesktop.Secret.Service.Unlock", 0, objects).Store(&unlocked, &prompt)
if err != nil {
return err
}
if isPrompt(prompt) {
prompt := NewPrompt(service.conn, prompt)
if _, err := prompt.Prompt(); err != nil {
return err
}
}
return nil
}
// Lock (IN Array<ObjectPath> objects, OUT Array<ObjectPath> locked, OUT ObjectPath Prompt);
func (service *Service) Lock(object DBusObject) error {
objects := []dbus.ObjectPath{object.Path()}
var locked []dbus.ObjectPath
var prompt dbus.ObjectPath
err := service.dbus.Call("org.freedesktop.Secret.Service.Lock", 0, objects).Store(&locked, &prompt)
if err != nil {
return err
}
if isPrompt(prompt) {
prompt := NewPrompt(service.conn, prompt)
if _, err := prompt.Prompt(); err != nil {
return err
}
}
return nil
}