1 /* libsecret - GLib wrapper for Secret Prompt
3 * Copyright 2011 Collabora Ltd.
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU Lesser General Public License as published
7 * by the Free Software Foundation; either version 2.1 of the licence or (at
8 * your option) any later version.
10 * See the included COPYING file for more information.
12 * Author: Stef Walter <stefw@gnome.org>
17 #include "secret-dbus-generated.h"
18 #include "secret-private.h"
19 #include "secret-prompt.h"
22 #include <glib/gi18n-lib.h>
25 * SECTION:secret-prompt
26 * @title: SecretPrompt
27 * @short_description: a prompt in the Service
29 * A #SecretPrompt represents a prompt in the Secret Service.
31 * Certain actions on the Secret Service require user prompting to complete,
32 * such as creating a collection, or unlocking a collection. When such a prompt
33 * is necessary, then a #SecretPrompt object is created by this library, and
34 * passed to the secret_service_prompt() method. In this way it is handled
37 * In order to customize prompt handling, override the
38 * SecretServiceClass::prompt_async and SecretServiceClass::prompt_finish
39 * virtual methods of the #SecretService class.
47 * A proxy object representing a prompt that the Secret Service will display
53 * @parent_class: the parent class
55 * The class for #SecretPrompt.
58 struct _SecretPromptPrivate {
62 G_DEFINE_TYPE (SecretPrompt, secret_prompt, G_TYPE_DBUS_PROXY);
65 secret_prompt_init (SecretPrompt *self)
67 self->pv = G_TYPE_INSTANCE_GET_PRIVATE (self, SECRET_TYPE_PROMPT,
72 secret_prompt_class_init (SecretPromptClass *klass)
74 g_type_class_add_private (klass, sizeof (SecretPromptPrivate));
83 on_prompt_run_complete (GObject *source,
87 RunClosure *closure = user_data;
88 closure->result = g_object_ref (result);
89 g_main_loop_quit (closure->loop);
93 _secret_prompt_instance (SecretService *service,
94 const gchar *prompt_path)
100 g_return_val_if_fail (SECRET_IS_SERVICE (service), NULL);
101 g_return_val_if_fail (prompt_path != NULL, NULL);
103 proxy = G_DBUS_PROXY (service);
104 prompt = g_initable_new (SECRET_TYPE_PROMPT, NULL, &error,
105 "g-flags", G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES,
106 "g-interface-info", _secret_gen_prompt_interface_info (),
107 "g-name", g_dbus_proxy_get_name (proxy),
108 "g-connection", g_dbus_proxy_get_connection (proxy),
109 "g-object-path", prompt_path,
110 "g-interface-name", SECRET_PROMPT_INTERFACE,
114 g_warning ("couldn't create SecretPrompt object: %s", error->message);
115 g_clear_error (&error);
125 * @window_id: XWindow id for parent window to be transient for
126 * @cancellable: optional cancellation object
127 * @return_type: the variant type of the prompt result
128 * @error: location to place an error on failure
130 * Runs a prompt and performs the prompting. Returns a variant result if the
131 * prompt was completed and not dismissed. The type of result depends on the
132 * action the prompt is completing, and is defined in the Secret Service DBus
135 * If @window_id is non-zero then it is used as an XWindow id. The Secret
136 * Service can make its prompt transient for the window with this id. In some
137 * Secret Service implementations this is not possible, so the behavior
138 * depending on this should degrade gracefully.
140 * This runs the dialog in a recursive mainloop. When run from a user interface
141 * thread, this means the user interface will remain responsive. Care should be
142 * taken that appropriate user interface actions are disabled while running the
145 * Returns: (transfer full): %NULL if the prompt was dismissed or an error occurred
148 secret_prompt_run (SecretPrompt *self,
150 GCancellable *cancellable,
151 const GVariantType *return_type,
154 GMainContext *context;
158 g_return_val_if_fail (SECRET_IS_PROMPT (self), NULL);
159 g_return_val_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable), NULL);
160 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
162 context = g_main_context_get_thread_default ();
164 closure = g_new0 (RunClosure, 1);
165 closure->loop = g_main_loop_new (context, FALSE);
167 secret_prompt_perform (self, window_id, cancellable,
168 on_prompt_run_complete, closure);
170 g_main_loop_run (closure->loop);
172 retval = secret_prompt_perform_finish (self, closure->result, return_type, error);
174 g_main_loop_unref (closure->loop);
175 g_object_unref (closure->result);
182 * secret_prompt_perform_sync:
184 * @window_id: XWindow id for parent window to be transient for
185 * @cancellable: optional cancellation object
186 * @return_type: the variant type of the prompt result
187 * @error: location to place an error on failure
189 * Runs a prompt and performs the prompting. Returns a variant result if the
190 * prompt was completed and not dismissed. The type of result depends on the
191 * action the prompt is completing, and is defined in the Secret Service DBus
194 * If @window_id is non-zero then it is used as an XWindow id. The Secret
195 * Service can make its prompt transient for the window with this id. In some
196 * Secret Service implementations this is not possible, so the behavior
197 * depending on this should degrade gracefully.
199 * This method may block indefinitely and should not be used in user interface
202 * Returns: (transfer full): %NULL if the prompt was dismissed or an error occurred
205 secret_prompt_perform_sync (SecretPrompt *self,
207 GCancellable *cancellable,
208 const GVariantType *return_type,
211 GMainContext *context;
214 g_return_val_if_fail (SECRET_IS_PROMPT (self), NULL);
215 g_return_val_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable), NULL);
216 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
218 context = g_main_context_new ();
219 g_main_context_push_thread_default (context);
221 retval = secret_prompt_run (self, window_id, cancellable, return_type, error);
223 /* Needed to prevent memory leaks */
224 while (g_main_context_iteration (context, FALSE));
226 g_main_context_pop_thread_default (context);
227 g_main_context_unref (context);
233 GDBusConnection *connection;
234 GCancellable *call_cancellable;
235 GCancellable *async_cancellable;
236 gulong cancelled_sig;
247 perform_closure_free (gpointer data)
249 PerformClosure *closure = data;
250 g_object_unref (closure->call_cancellable);
251 g_clear_object (&closure->async_cancellable);
252 g_object_unref (closure->connection);
254 g_variant_unref (closure->result);
255 g_assert (closure->signal == 0);
256 g_assert (closure->watch == 0);
257 g_slice_free (PerformClosure, closure);
261 perform_prompt_complete (GSimpleAsyncResult *res,
264 PerformClosure *closure = g_simple_async_result_get_op_res_gpointer (res);
266 closure->dismissed = dismissed;
267 if (closure->completed)
269 closure->completed = TRUE;
272 g_dbus_connection_signal_unsubscribe (closure->connection, closure->signal);
276 g_bus_unwatch_name (closure->watch);
279 if (closure->cancelled_sig)
280 g_signal_handler_disconnect (closure->async_cancellable, closure->cancelled_sig);
281 closure->cancelled_sig = 0;
283 g_simple_async_result_complete (res);
287 on_prompt_completed (GDBusConnection *connection,
288 const gchar *sender_name,
289 const gchar *object_path,
290 const gchar *interface_name,
291 const gchar *signal_name,
292 GVariant *parameters,
295 GSimpleAsyncResult *res = G_SIMPLE_ASYNC_RESULT (user_data);
296 SecretPrompt *self = SECRET_PROMPT (g_async_result_get_source_object (user_data));
297 PerformClosure *closure = g_simple_async_result_get_op_res_gpointer (res);
300 closure->prompting = FALSE;
302 if (!g_variant_is_of_type (parameters, G_VARIANT_TYPE ("(bv)"))) {
303 g_warning ("SecretPrompt received invalid %s signal of type %s",
304 signal_name, g_variant_get_type_string (parameters));
305 perform_prompt_complete (res, TRUE);
308 g_variant_get (parameters, "(bv)", &dismissed, &closure->result);
309 perform_prompt_complete (res, dismissed);
312 g_object_unref (self);
316 on_prompt_prompted (GObject *source,
317 GAsyncResult *result,
320 GSimpleAsyncResult *res = G_SIMPLE_ASYNC_RESULT (user_data);
321 PerformClosure *closure = g_simple_async_result_get_op_res_gpointer (res);
322 SecretPrompt *self = SECRET_PROMPT (source);
323 GError *error = NULL;
326 retval = g_dbus_proxy_call_finish (G_DBUS_PROXY (self), result, &error);
329 g_variant_unref (retval);
330 if (closure->vanished)
331 g_clear_error (&error);
334 _secret_util_strip_remote_error (&error);
335 g_simple_async_result_take_error (res, error);
336 perform_prompt_complete (res, TRUE);
339 closure->prompting = TRUE;
340 g_atomic_int_set (&self->pv->prompted, 1);
342 /* And now we wait for the signal */
345 g_object_unref (res);
349 on_prompt_vanished (GDBusConnection *connection,
353 GSimpleAsyncResult *res = G_SIMPLE_ASYNC_RESULT (user_data);
354 PerformClosure *closure = g_simple_async_result_get_op_res_gpointer (res);
355 closure->vanished = TRUE;
356 g_cancellable_cancel (closure->call_cancellable);
357 perform_prompt_complete (res, TRUE);
361 on_prompt_dismissed (GObject *source,
362 GAsyncResult *result,
365 GSimpleAsyncResult *res = G_SIMPLE_ASYNC_RESULT (user_data);
366 PerformClosure *closure = g_simple_async_result_get_op_res_gpointer (res);
367 SecretPrompt *self = SECRET_PROMPT (source);
368 GError *error = NULL;
371 retval = g_dbus_proxy_call_finish (G_DBUS_PROXY (self), result, &error);
374 g_variant_unref (retval);
375 if (closure->vanished)
376 g_clear_error (&error);
377 if (g_error_matches (error, G_DBUS_ERROR, G_DBUS_ERROR_UNKNOWN_METHOD))
378 g_clear_error (&error);
381 _secret_util_strip_remote_error (&error);
382 g_simple_async_result_take_error (res, error);
383 perform_prompt_complete (res, TRUE);
386 g_object_unref (res);
390 on_prompt_cancelled (GCancellable *cancellable,
393 GSimpleAsyncResult *res = G_SIMPLE_ASYNC_RESULT (user_data);
394 PerformClosure *closure = g_simple_async_result_get_op_res_gpointer (res);
395 SecretPrompt *self = SECRET_PROMPT (g_async_result_get_source_object (user_data));
397 /* Instead of cancelling our dbus calls, we cancel the prompt itself via this dbus call */
399 g_dbus_proxy_call (G_DBUS_PROXY (self), "Dismiss", g_variant_new ("()"),
400 G_DBUS_CALL_FLAGS_NO_AUTO_START, -1,
401 closure->call_cancellable,
402 on_prompt_dismissed, g_object_ref (res));
404 g_object_unref (self);
408 * secret_prompt_perform:
410 * @window_id: XWindow id for parent window to be transient for
411 * @cancellable: optional cancellation object
412 * @callback: called when the operation completes
413 * @user_data: data to be passed to the callback
415 * Runs a prompt and performs the prompting. Returns %TRUE if the prompt
416 * was completed and not dismissed.
418 * If @window_id is non-zero then it is used as an XWindow id. The Secret
419 * Service can make its prompt transient for the window with this id. In some
420 * Secret Service implementations this is not possible, so the behavior
421 * depending on this should degrade gracefully.
423 * This method will return immediately and complete asynchronously.
426 secret_prompt_perform (SecretPrompt *self,
428 GCancellable *cancellable,
429 GAsyncReadyCallback callback,
432 GSimpleAsyncResult *res;
433 PerformClosure *closure;
434 const gchar *owner_name;
435 const gchar *object_path;
440 g_return_if_fail (SECRET_IS_PROMPT (self));
441 g_return_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable));
443 prompted = g_atomic_int_get (&self->pv->prompted);
445 g_warning ("The prompt object has already had its prompt called.");
449 proxy = G_DBUS_PROXY (self);
451 res = g_simple_async_result_new (G_OBJECT (self), callback, user_data,
452 secret_prompt_perform);
453 closure = g_slice_new0 (PerformClosure);
454 closure->connection = g_object_ref (g_dbus_proxy_get_connection (proxy));
455 closure->call_cancellable = g_cancellable_new ();
456 closure->async_cancellable = cancellable ? g_object_ref (cancellable) : NULL;
457 g_simple_async_result_set_op_res_gpointer (res, closure, perform_closure_free);
460 window = g_strdup ("");
462 window = g_strdup_printf ("%lu", window_id);
464 owner_name = g_dbus_proxy_get_name_owner (proxy);
465 object_path = g_dbus_proxy_get_object_path (proxy);
467 closure->signal = g_dbus_connection_signal_subscribe (closure->connection, owner_name,
468 SECRET_PROMPT_INTERFACE,
469 SECRET_PROMPT_SIGNAL_COMPLETED,
471 G_DBUS_SIGNAL_FLAGS_NONE,
476 closure->watch = g_bus_watch_name_on_connection (closure->connection, owner_name,
477 G_BUS_NAME_WATCHER_FLAGS_NONE, NULL,
482 if (closure->async_cancellable) {
483 closure->cancelled_sig = g_cancellable_connect (closure->async_cancellable,
484 G_CALLBACK (on_prompt_cancelled),
488 g_dbus_proxy_call (proxy, "Prompt", g_variant_new ("(s)", window),
489 G_DBUS_CALL_FLAGS_NO_AUTO_START, -1,
490 closure->call_cancellable, on_prompt_prompted, g_object_ref (res));
493 g_object_unref (res);
497 * secret_prompt_perform_finish:
499 * @result: the asynchronous result passed to the callback
500 * @return_type: the variant type of the prompt result
501 * @error: location to place an error on failure
503 * Complete asynchronous operation to run a prompt and perform the prompting.
505 * Returns a variant result if the prompt was completed and not dismissed. The
506 * type of result depends on the action the prompt is completing, and is
507 * defined in the Secret Service DBus API specification.
509 * Returns: (transfer full): %NULL if the prompt was dismissed or an error occurred,
510 * a variant result if the prompt was successful
513 secret_prompt_perform_finish (SecretPrompt *self,
514 GAsyncResult *result,
515 const GVariantType *return_type,
518 PerformClosure *closure;
519 GSimpleAsyncResult *res;
522 g_return_val_if_fail (SECRET_IS_PROMPT (self), NULL);
523 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
524 g_return_val_if_fail (g_simple_async_result_is_valid (result, G_OBJECT (self),
525 secret_prompt_perform), NULL);
527 res = G_SIMPLE_ASYNC_RESULT (result);
529 if (g_simple_async_result_propagate_error (res, error))
532 closure = g_simple_async_result_get_op_res_gpointer (res);
533 if (closure->result == NULL)
535 if (return_type != NULL && !g_variant_is_of_type (closure->result, return_type)) {
536 string = g_variant_type_dup_string (return_type);
537 g_warning ("received unexpected result type %s from Completed signal instead of expected %s",
538 g_variant_get_type_string (closure->result), string);
542 return g_variant_ref (closure->result);