Skip to content

Commit

Permalink
Polkit: Correctly handle the user dismissing the authentication dialog (
Browse files Browse the repository at this point in the history
#1746)

Adapted from elementary/pantheon-agent-polkit#26:
* Throw Polkit.Error.CANCELLED when the user dismisses the dialog
* Listen for events in the Cancellable object instead of using it

Testing:
* `pkexec` -> click cancel on dialog -> pkexec exits as dismissed
* `pkexec` -> Ctrl-C in terminal -> authentication dialog closes

Fixes #1279
  • Loading branch information
LulzFTW authored and JoshStrobl committed Mar 18, 2019
1 parent 38b69f0 commit 7ba8696
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 6 deletions.
16 changes: 11 additions & 5 deletions src/polkit/polkitdialog.vala
Expand Up @@ -34,6 +34,8 @@ public class AgentDialog : Gtk.Window
[GtkChild]
private Gtk.Label? label_error;

public bool is_cancelled;

public PolkitAgent.Session? pk_session = null;
private Polkit.Identity? pk_identity = null;

Expand Down Expand Up @@ -92,6 +94,8 @@ public class AgentDialog : Gtk.Window
window_position = Gtk.WindowPosition.CENTER_ALWAYS;

key_release_event.connect(on_key_release);

cancellable.cancelled.connect(on_agent_cancelled);
}

bool on_key_release(Gdk.EventKey key)
Expand Down Expand Up @@ -127,7 +131,7 @@ public class AgentDialog : Gtk.Window
{
/* Not authed */
set_sensitive(true);
if (!authorized || cancellable.is_cancelled()) {
if (!authorized) {
label_error.set_text(_("Authentication failed"));
/* TODO: Cancel non existent spinner */
var session = pk_session;
Expand Down Expand Up @@ -266,9 +270,7 @@ public class AgentDialog : Gtk.Window
if (pk_session != null) {
pk_session.cancel();
}
if (!cancellable.is_cancelled()) {
cancellable.cancel();
}
is_cancelled = true;
done();
}

Expand All @@ -285,7 +287,7 @@ public class Agent : PolkitAgent.Listener
private Budgie.ThemeManager theme_manager;

public override async bool initiate_authentication(string action_id, string message, string icon_name,
Polkit.Details details, string cookie, GLib.List<Polkit.Identity?>? identities, GLib.Cancellable cancellable)
Polkit.Details details, string cookie, GLib.List<Polkit.Identity?>? identities, GLib.Cancellable cancellable) throws Polkit.Error
{

var dialog = new AgentDialog(action_id, message, "dialog-password-symbolic", cookie, cancellable);
Expand All @@ -305,6 +307,10 @@ public class Agent : PolkitAgent.Listener

dialog.destroy();

if (dialog.is_cancelled) {
throw new Polkit.Error.CANCELLED("Authentication dialog was dismissed by the user");
}

return true;
}

Expand Down
2 changes: 1 addition & 1 deletion vapi/polkit-agent-1.vapi
Expand Up @@ -6,7 +6,7 @@ namespace PolkitAgent {
public abstract class Listener : GLib.Object {
[CCode (has_construct_function = false)]
protected Listener ();
public virtual async bool initiate_authentication(string action_id, string message, string icon_name, Polkit.Details details, string cookie, GLib.List<Polkit.Identity?>? identities, GLib.Cancellable cancellable);
public virtual async bool initiate_authentication(string action_id, string message, string icon_name, Polkit.Details details, string cookie, GLib.List<Polkit.Identity?>? identities, GLib.Cancellable cancellable) throws Polkit.Error;
/*
public virtual void initiate_authentication(string action_id, string message, string icon_name, Polkit.Details details, string cookie, GLib.List<Polkit.Identity?>? identities, GLib.Cancellable cancellable, GLib.AsyncReadyCallback @callback);
public virtual bool initiate_authentication_finish (GLib.AsyncResult res) throws GLib.Error;*/
Expand Down

0 comments on commit 7ba8696

Please sign in to comment.