-
Notifications
You must be signed in to change notification settings - Fork 3
/
prompt.go
106 lines (83 loc) · 2.26 KB
/
prompt.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
// Copyright 2019 Patrick Pacher. All rights reserved. Use of
// this source code is governed by the included Simplified BSD license.
package keyring
import (
"log"
"github.com/godbus/dbus/v5"
)
const (
promptMethodPrompt = PromptInterface + ".Prompt"
promptMethodDismiss = PromptInterface + ".Dismiss"
promptSignalCompleted = PromptInterface + ".Completed"
)
// Prompt provides interaction with the Prompt interface from Freedesktop.org's Secret Service API
// it's defined at https://specifications.freedesktop.org/secret-service/re05.html
type Prompt interface {
// Path returns the ObjectPath of the prompt
Path() dbus.ObjectPath
// Prompt performs the prompt
Prompt(windowID string) (<-chan *dbus.Variant, error)
// Dismiss dismisses the prompt. It is no longer valid after calling Dismiss()
Dismiss() error
}
// GetPrompt returns a Prompt client for the given path
func GetPrompt(conn *dbus.Conn, path dbus.ObjectPath) Prompt {
obj := conn.Object(SecretServiceDest, path)
return &prompt{
obj: obj,
conn: conn,
path: path,
}
}
// prompt implements the Prompt interface
type prompt struct {
conn *dbus.Conn
path dbus.ObjectPath
obj dbus.BusObject
}
// Path returns the ObjectPath of the prompt
func (p *prompt) Path() dbus.ObjectPath {
return p.path
}
// Prompt performs the prompt
func (p *prompt) Prompt(windowID string) (<-chan *dbus.Variant, error) {
call := p.obj.AddMatchSignal(PromptInterface, "Completed")
if call.Err != nil {
return nil, call.Err
}
ch := make(chan *dbus.Variant, 1)
sig := make(chan *dbus.Signal, 1)
p.conn.Signal(sig)
go func() {
defer close(sig)
defer p.conn.RemoveSignal(sig)
var res []interface{}
for s := range sig {
if s.Path == p.path {
res = s.Body
break
}
}
var dismissed bool
var result dbus.Variant
if err := dbus.Store(res, &dismissed, &result); err != nil {
// how to handle that?
ch <- nil
log.Println(err.Error())
return
}
if dismissed {
ch <- nil
return
}
ch <- &result
}()
if res := p.obj.Call(promptMethodPrompt, 0, windowID); res.Err != nil {
return nil, res.Err
}
return ch, nil
}
// Dismiss dismisses the prompt. It is no longer valid after calling Dismiss()
func (p *prompt) Dismiss() error {
return p.obj.Call(promptMethodDismiss, 0).Err
}