-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathdbus.go
More file actions
136 lines (118 loc) · 4.6 KB
/
dbus.go
File metadata and controls
136 lines (118 loc) · 4.6 KB
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
package examplebroker
import (
"context"
"errors"
"fmt"
"os"
"path/filepath"
"github.com/godbus/dbus/v5"
"github.com/godbus/dbus/v5/introspect"
"github.com/ubuntu/decorate"
)
const (
dbusObjectPath = "/com/ubuntu/authd/ExampleBroker"
busName = "com.ubuntu.authd.ExampleBroker"
// we need to redeclare the interface here to avoid include cycles.
dbusInterface = "com.ubuntu.authd.Broker"
)
// Bus is the D-Bus object that will answer calls for the broker.
type Bus struct {
broker *Broker
}
// StartBus starts the D-Bus service and exports it on the system bus.
func StartBus(cfgPath string) (conn *dbus.Conn, err error) {
defer decorate.OnError(&err, "could not start example broker bus")
conn, err = dbus.ConnectSystemBus()
if err != nil {
return nil, err
}
b, _, _ := New("ExampleBroker")
obj := Bus{broker: b}
err = conn.Export(&obj, dbusObjectPath, dbusInterface)
if err != nil {
return nil, err
}
if err = conn.Export(introspect.NewIntrospectable(&introspect.Node{
Name: dbusObjectPath,
Interfaces: []introspect.Interface{
introspect.IntrospectData,
{
Name: dbusInterface,
Methods: introspect.Methods(&obj),
},
},
}), dbusObjectPath, introspect.IntrospectData.Name); err != nil {
return nil, err
}
reply, err := conn.RequestName(busName, dbus.NameFlagDoNotQueue)
if err != nil {
return nil, err
}
if reply != dbus.RequestNameReplyPrimaryOwner {
return nil, errors.New("D-Bus name already taken")
}
if err = os.WriteFile(filepath.Join(cfgPath, "examplebroker.conf"),
[]byte(fmt.Sprintf(`[authd]
name = ExampleBroker
brand_icon = /usr/share/backgrounds/warty-final-ubuntu.png
dbus_name = %s
dbus_object = %s
`, busName, dbusObjectPath)),
0600); err != nil {
return nil, err
}
return conn, nil
}
// NewSession is the method through which the broker and the daemon will communicate once dbusInterface.NewSession is called.
func (b *Bus) NewSession(username, lang, mode string) (sessionID, encryptionKey string, dbusErr *dbus.Error) {
sessionID, encryptionKey, err := b.broker.NewSession(context.Background(), username, lang, mode)
if err != nil {
return "", "", dbus.MakeFailedError(err)
}
return sessionID, encryptionKey, nil
}
// GetAuthenticationModes is the method through which the broker and the daemon will communicate once dbusInterface.GetAuthenticationModes is called.
func (b *Bus) GetAuthenticationModes(sessionID string, supportedUILayouts []map[string]string) (authenticationModes []map[string]string, msg string, dbusErr *dbus.Error) {
authenticationModes, msg, err := b.broker.GetAuthenticationModes(context.Background(), sessionID, supportedUILayouts)
if err != nil {
return nil, "", dbus.MakeFailedError(err)
}
return authenticationModes, msg, nil
}
// SelectAuthenticationMode is the method through which the broker and the daemon will communicate once dbusInterface.SelectAuthenticationMode is called.
func (b *Bus) SelectAuthenticationMode(sessionID, authenticationModeName string) (uiLayoutInfo map[string]string, dbusErr *dbus.Error) {
uiLayoutInfo, err := b.broker.SelectAuthenticationMode(context.Background(), sessionID, authenticationModeName)
if err != nil {
return nil, dbus.MakeFailedError(err)
}
return uiLayoutInfo, nil
}
// IsAuthenticated is the method through which the broker and the daemon will communicate once dbusInterface.IsAuthenticated is called.
func (b *Bus) IsAuthenticated(sessionID, authenticationData string) (access, data string, dbusErr *dbus.Error) {
access, data, err := b.broker.IsAuthenticated(context.Background(), sessionID, authenticationData)
if err != nil {
return "", "", dbus.MakeFailedError(err)
}
return access, data, nil
}
// EndSession is the method through which the broker and the daemon will communicate once dbusInterface.EndSession is called.
func (b *Bus) EndSession(sessionID string) (dbusErr *dbus.Error) {
err := b.broker.EndSession(context.Background(), sessionID)
if err != nil {
return dbus.MakeFailedError(err)
}
return nil
}
// CancelIsAuthenticated is the method through which the broker and the daemon will communicate once dbusInterface.CancelIsAuthenticated is called.
func (b *Bus) CancelIsAuthenticated(sessionID string) (dbusErr *dbus.Error) {
b.broker.CancelIsAuthenticated(context.Background(), sessionID)
return nil
}
// UserPreCheck is the method through which the broker and the daemon will communicate once dbusInterface.UserPreCheck is called.
func (b *Bus) UserPreCheck(username string) (userinfo string, dbusErr *dbus.Error) {
userinfo, err := b.broker.UserPreCheck(context.Background(), username)
if err != nil {
return "", dbus.MakeFailedError(err)
}
return userinfo, nil
}