+def ensure_polkit_auth(sender, conn, privilege):
+ dbus_info = dbus.Interface(conn.get_object("org.freedesktop.DBus", "/org/freedesktop/DBus/Bus", False), "org.freedesktop.DBus")
+ pid = dbus_info.GetConnectionUnixProcessID(sender)
+ polkit = dbus.Interface(dbus.SystemBus().get_object("org.freedesktop.PolicyKit1", "/org/freedesktop/PolicyKit1/Authority", False), "org.freedesktop.PolicyKit1.Authority")
+ try:
+ (is_auth, _, _) = polkit.CheckAuthorization(
+ ("unix-process", {
+ "pid": dbus.UInt32(pid, variant_level=1),
+ "start-time": dbus.UInt64(0, variant_level=1)}),
+ privilege, {"AllowUserInteraction": "true"},
+ dbus.UInt32(1),
+ "",
+ timeout=300)
+ return is_auth
+ except dbus.DBusException:
+ raise PermissionError("Polkit: Authentication timed out")
+