-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathauthority.go
140 lines (119 loc) · 3.37 KB
/
authority.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
package polkit
import (
"github.com/godbus/dbus/v5"
)
type PKImplicitAuthorization uint32
const (
NotAuthorized PKImplicitAuthorization = iota
AuthenticationRequired
AdministratorAuthenticationRequired
AuthenticationRequiredRetained
AdministratorAuthenticationRequiredRetained
Authorized
)
func (i PKImplicitAuthorization) String() string {
switch i {
case NotAuthorized:
return "no"
case AuthenticationRequired:
return "auth_self"
case AdministratorAuthenticationRequired:
return "auth_admin"
case AuthenticationRequiredRetained:
return "auth_self_keep"
case AdministratorAuthenticationRequiredRetained:
return "auth_admin_keep"
case Authorized:
return "yes"
default:
panic("unknown flag value")
}
}
const (
CheckAuthorizationNone uint32 = iota
CheckAuthorizationAllowUserInteraction
)
type (
Authority struct {
conn *dbus.Conn
object dbus.BusObject
subject PKSubject
}
PKSubject struct {
Kind string `dbus:"subject_kind"`
Details map[string]dbus.Variant `dbus:"subject_details"`
}
PKAuthorizationResult struct {
IsAuthorized bool `dbus:"is_authorized"`
IsChallenge bool `dbus:"is_challenge"`
Details map[string]string `dbus:"details"`
}
PKActionDescription struct {
ActionID string `dbus:"action_id"`
Description string `dbus:"description"`
Message string `dbus:"message"`
VendorName string `dbus:"vendor_name"`
VendorURL string `dbus:"vendor_url"`
IconName string `dbus:"icon_name"`
ImplicitAny uint32 `dbus:"implicit_any"`
ImplicitInactive uint32 `dbus:"implicit_inactive"`
ImplicitActive uint32 `dbus:"implicit_active"`
Annotations map[string]string `dbus:"annotations"`
}
)
func NewAuthority() (*Authority, error) {
bus, err := dbus.SystemBus()
if err != nil {
return nil, err
}
names := bus.Names()
if len(names) == 0 {
panic("empty dbus names")
}
return &Authority{
conn: bus,
object: bus.Object("org.freedesktop.PolicyKit1", "/org/freedesktop/PolicyKit1/Authority"),
subject: PKSubject{
Kind: "system-bus-name",
Details: map[string]dbus.Variant{
"name": dbus.MakeVariant(names[0]),
},
},
}, nil
}
func (a *Authority) EnumerateActions(locale string) ([]PKActionDescription, error) {
var result []PKActionDescription
if err := a.call("EnumerateActions", &result, locale); err != nil {
return nil, err
}
return result, nil
}
func (a *Authority) CheckAuthorization(
actionID string,
details map[string]string,
flags uint32,
cancellationID string) (*PKAuthorizationResult, error) {
result := PKAuthorizationResult{}
if err := a.call("CheckAuthorization", &result, a.subject, actionID, details, flags, cancellationID); err != nil {
return nil, err
}
return &result, nil
}
func (a *Authority) CancelCheckAuthorization(cancellationID string) error {
if err := a.call("CancelCheckAuthorization", nil, cancellationID); err != nil {
return err
}
return nil
}
func (a *Authority) call(action string, result interface{}, args ...interface{}) error {
call := a.object.Call("org.freedesktop.PolicyKit1.Authority."+action, 0, args...)
if result != nil {
if err := call.Store(result); err != nil {
return err
}
}
return nil
}
func (a *Authority) Close() error {
return a.conn.Close()
}