peer to peer dbus support added
[platform/upstream/libgsignon-glib.git] / libsignon-glib / signon-auth-session.c
1 /* vi: set et sw=4 ts=4 cino=t0,(0: */
2 /* -*- Mode: C; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 /*
4  * This file is part of libsignon-glib
5  *
6  * Copyright (C) 2009-2010 Nokia Corporation.
7  * Copyright (C) 2012 Canonical Ltd.
8  * Copyright (C) 2012-2013 Intel Corporation.
9  *
10  * Contact: Alberto Mardegan <alberto.mardegan@canonical.com>
11  * Contact: Jussi Laako <jussi.laako@linux.intel.com>
12  *
13  * This library is free software; you can redistribute it and/or
14  * modify it under the terms of the GNU Lesser General Public License
15  * version 2.1 as published by the Free Software Foundation.
16  *
17  * This library is distributed in the hope that it will be useful, but
18  * WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20  * Lesser General Public License for more details.
21  *
22  * You should have received a copy of the GNU Lesser General Public
23  * License along with this library; if not, write to the Free Software
24  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
25  * 02110-1301 USA
26  */
27
28 /**
29  * SECTION:signon-auth-session
30  * @title: SignonAuthSession
31  * @short_description: Authentication session handler.
32  *
33  * The #SignonAuthSession object is responsible for handling the client
34  * authentication. #SignonAuthSession objects can be created from existing
35  * identities (via signon_identity_create_session() or by passing a non-zero ID
36  * to signon_auth_session_new()), in which case the authentication data such as
37  * username and password will be implicitly taken from the identity, or they
38  * can be created with no existing identity bound to them, in which case all
39  * the authentication data must be filled in by the client when
40  * signon_auth_session_process() is called.
41  */
42
43 #include "signon-internals.h"
44 #include "signon-auth-session.h"
45 #include "signon-dbus-queue.h"
46 #include "signon-errors.h"
47 #include "signon-marshal.h"
48 #include "signon-utils.h"
49 #include "signon-identity.h"
50 #include "sso-auth-service.h"
51 #include "sso-auth-session-gen.h"
52
53
54 G_DEFINE_TYPE (SignonAuthSession, signon_auth_session, G_TYPE_OBJECT);
55
56 enum
57 {
58     PROP_0,
59     PROP_IDENTITY,
60     PROP_APPCTX
61 };
62
63 /* Signals */
64 enum
65 {
66     STATE_CHANGED,
67     LAST_SIGNAL
68 };
69
70 static guint auth_session_signals[LAST_SIGNAL] = { 0 };
71 static const gchar auth_session_process_pending_message[] =
72     "The request is added to queue.";
73 static const gchar data_key_process[] = "signon-process";
74
75 struct _SignonAuthSessionPrivate
76 {
77     SsoAuthSession *proxy;
78     SignonIdentity *identity;
79     GCancellable *cancellable;
80
81     guint id;
82     gchar *method_name;
83
84     gboolean registering;
85     gboolean busy;
86     gboolean canceled;
87     gboolean dispose_has_run;
88
89     guint signal_state_changed;
90     guint signal_unregistered;
91 };
92
93 typedef struct _AuthSessionQueryAvailableMechanismsData
94 {
95     gchar **wanted_mechanisms;
96     gpointer cb_data;
97 } AuthSessionQueryAvailableMechanismsData;
98
99 typedef struct _AuthSessionProcessData
100 {
101     GVariant *session_data;
102     gchar *mechanism;
103     GCancellable *cancellable;
104 } AuthSessionProcessData;
105
106 typedef struct _AuthSessionQueryAvailableMechanismsCbData
107 {
108     SignonAuthSession *self;
109     SignonAuthSessionQueryAvailableMechanismsCb cb;
110     gpointer user_data;
111 } AuthSessionQueryAvailableMechanismsCbData;
112
113 typedef struct _AuthSessionProcessCbData
114 {
115     SignonAuthSessionProcessCb cb;
116     gpointer user_data;
117 } AuthSessionProcessCbData;
118
119 #define SIGNON_AUTH_SESSION_PRIV(obj) (SIGNON_AUTH_SESSION(obj)->priv)
120 #define SIGNON_AUTH_SESSION_GET_PRIV(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), SIGNON_TYPE_AUTH_SESSION, SignonAuthSessionPrivate))
121
122
123 static void auth_session_state_changed_cb (GDBusProxy *proxy, gint state, gchar *message, gpointer user_data);
124 static void auth_session_remote_object_destroyed_cb (GDBusProxy *proxy, gpointer user_data);
125
126 static gboolean auth_session_priv_init (SignonAuthSession *self, const gchar *method_name, GError **err);
127
128 static void auth_session_query_available_mechanisms_ready_cb (gpointer object, const GError *error, gpointer user_data);
129 static void auth_session_cancel_ready_cb (gpointer object, const GError *error, gpointer user_data);
130
131 static void auth_session_check_remote_object(SignonAuthSession *self);
132
133 static void
134 auth_session_process_data_free (AuthSessionProcessData *process_data)
135 {
136     g_free (process_data->mechanism);
137     g_variant_unref (process_data->session_data);
138     g_slice_free (AuthSessionProcessData, process_data);
139 }
140
141 static void
142 auth_session_process_reply (GObject *object, GAsyncResult *res,
143                             gpointer userdata)
144 {
145     SignonAuthSession *self;
146     SsoAuthSession *proxy = SSO_AUTH_SESSION (object);
147     GSimpleAsyncResult *res_process = (GSimpleAsyncResult *)userdata;
148     GVariant *reply;
149     GError *error = NULL;
150
151     g_return_if_fail (res_process != NULL);
152     DEBUG ("%s %d", G_STRFUNC, __LINE__);
153
154     sso_auth_session_call_process_finish (proxy, &reply, res, &error);
155
156     self = SIGNON_AUTH_SESSION (g_async_result_get_source_object (
157         (GAsyncResult *)res_process));
158     self->priv->busy = FALSE;
159
160     if (G_LIKELY (error == NULL))
161     {
162         g_simple_async_result_set_op_res_gpointer (res_process, reply,
163                                                    (GDestroyNotify)
164                                                    g_variant_unref);
165     }
166     else
167     {
168         g_simple_async_result_take_error (res_process, error);
169     }
170
171     /* We use the idle variant in order to avoid the following critical
172      * message:
173      * g_main_context_pop_thread_default: assertion `g_queue_peek_head (stack) == context' failed
174      */
175     g_simple_async_result_complete_in_idle (res_process);
176     g_object_unref (self);
177 }
178
179 static void
180 auth_session_process_ready_cb (gpointer object, const GError *error, gpointer user_data)
181 {
182     SignonAuthSession *self = SIGNON_AUTH_SESSION (object);
183     SignonAuthSessionPrivate *priv;
184     GSimpleAsyncResult *res = G_SIMPLE_ASYNC_RESULT (user_data);
185     AuthSessionProcessData *process_data;
186
187     g_return_if_fail (self != NULL);
188     priv = self->priv;
189
190     if (error != NULL)
191     {
192         DEBUG ("AuthSessionError: %s", error->message);
193         g_simple_async_result_set_from_error (res, error);
194         g_simple_async_result_complete (res);
195         return;
196     }
197
198     if (priv->canceled)
199     {
200         priv->busy = FALSE;
201         priv->canceled = FALSE;
202         g_simple_async_result_set_error (res,
203                                          signon_error_quark (),
204                                          SIGNON_ERROR_SESSION_CANCELED,
205                                          "Authentication session was canceled");
206         g_simple_async_result_complete (res);
207         return;
208     }
209
210     DEBUG ("%s %d", G_STRFUNC, __LINE__);
211
212     process_data = g_object_get_data ((GObject *)res, data_key_process);
213     g_return_if_fail (process_data != NULL);
214
215     sso_auth_session_call_process (priv->proxy,
216                                    process_data->session_data,
217                                    process_data->mechanism,
218                                    process_data->cancellable,
219                                    auth_session_process_reply,
220                                    res);
221 }
222
223 static void
224 process_async_cb_wrapper (GObject *object, GAsyncResult *res,
225                           gpointer user_data)
226 {
227     AuthSessionProcessCbData *cb_data = user_data;
228     SignonAuthSession *self = SIGNON_AUTH_SESSION (object);
229     GVariant *v_reply;
230     GHashTable *reply = NULL;
231     GError *error = NULL;
232     gboolean cancelled;
233
234     DEBUG ("%s %d", G_STRFUNC, __LINE__);
235
236     v_reply = signon_auth_session_process_finish (self, res, &error);
237
238     cancelled = error != NULL &&
239         error->domain == G_IO_ERROR &&
240         error->code == G_IO_ERROR_CANCELLED;
241
242     /* Do not invoke the callback if the operation was cancelled */
243     if (cb_data->cb != NULL && !cancelled)
244     {
245         if (v_reply != NULL)
246             reply = signon_hash_table_from_variant (v_reply);
247
248         cb_data->cb (self, reply, error, cb_data->user_data);
249         if (reply != NULL)
250             g_hash_table_unref (reply);
251     }
252     g_variant_unref (v_reply);
253
254     g_slice_free (AuthSessionProcessCbData, cb_data);
255     g_clear_error (&error);
256     g_object_unref (res);
257 }
258
259 static GQuark
260 auth_session_object_quark ()
261 {
262   static GQuark quark = 0;
263
264   if (!quark)
265     quark = g_quark_from_static_string ("auth_session_object_quark");
266
267   return quark;
268 }
269
270 static void
271 signon_auth_session_set_property (GObject *object,
272                                   guint property_id,
273                                   const GValue *value,
274                                   GParamSpec *pspec)
275 {
276     SignonAuthSession *self = SIGNON_AUTH_SESSION (object);
277
278     switch (property_id)
279     {
280         case PROP_IDENTITY:
281             self->priv->identity = g_value_dup_object (value);
282             break;
283         default:
284             G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
285     }
286 }
287
288 static void
289 signon_auth_session_get_property (GObject *object,
290                                   guint property_id,
291                                   GValue *value,
292                                   GParamSpec *pspec)
293 {
294     SignonAuthSession *self = SIGNON_AUTH_SESSION (object);
295
296     switch (property_id)
297     {
298         case PROP_IDENTITY:
299             g_value_set_object (value, self->priv->identity);
300             break;
301         default:
302             G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
303     }
304 }
305
306 static void
307 signon_auth_session_init (SignonAuthSession *self)
308 {
309     self->priv = SIGNON_AUTH_SESSION_GET_PRIV (self);
310     self->priv->cancellable = g_cancellable_new ();
311 }
312
313 static void
314 signon_auth_session_dispose (GObject *object)
315 {
316     g_return_if_fail (SIGNON_IS_AUTH_SESSION (object));
317     SignonAuthSession *self = SIGNON_AUTH_SESSION (object);
318     SignonAuthSessionPrivate *priv = self->priv;
319     g_return_if_fail (priv != NULL);
320
321     if (priv->dispose_has_run)
322         return;
323
324     if (priv->cancellable)
325     {
326         g_cancellable_cancel (priv->cancellable);
327         priv->cancellable = NULL;
328     }
329
330     if (priv->proxy)
331     {
332         g_signal_handler_disconnect (priv->proxy, priv->signal_state_changed);
333         g_signal_handler_disconnect (priv->proxy, priv->signal_unregistered);
334         g_object_unref (priv->proxy);
335
336         priv->proxy = NULL;
337     }
338
339     G_OBJECT_CLASS (signon_auth_session_parent_class)->dispose (object);
340
341     priv->dispose_has_run = TRUE;
342 }
343
344 static void
345 signon_auth_session_finalize (GObject *object)
346 {
347     g_return_if_fail (SIGNON_IS_AUTH_SESSION(object));
348
349     SignonAuthSession *self = SIGNON_AUTH_SESSION(object);
350     SignonAuthSessionPrivate *priv = self->priv;
351     g_return_if_fail (priv != NULL);
352
353     g_free (priv->method_name);
354     g_object_unref (priv->identity);
355
356     G_OBJECT_CLASS (signon_auth_session_parent_class)->finalize (object);
357 }
358
359 static void
360 signon_auth_session_class_init (SignonAuthSessionClass *klass)
361 {
362     GObjectClass *object_class = G_OBJECT_CLASS (klass);
363     GParamSpec *pspec;
364
365     object_class->set_property = signon_auth_session_set_property;
366     object_class->get_property = signon_auth_session_get_property;
367
368     pspec = g_param_spec_object ("identity",
369                                  "Identity Object",
370                                  "Identity Object construct parameter",
371                                  SIGNON_TYPE_IDENTITY,
372                                  G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY);
373     g_object_class_install_property (object_class,
374                                      PROP_IDENTITY,
375                                      pspec);
376
377     g_type_class_add_private (object_class, sizeof (SignonAuthSessionPrivate));
378
379     /**
380      * SignonAuthSession::state-changed:
381      * @auth_session: the #SignonAuthSession
382      * @state: the current state of the #SignonAuthSession
383      * @message: the message associated with the state change
384      *
385      * Emitted when the state of the #SignonAuthSession changes.
386      */
387     auth_session_signals[STATE_CHANGED] =
388             g_signal_new ("state-changed",
389                           G_TYPE_FROM_CLASS (klass),
390                           G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
391                           0,
392                           NULL,
393                           NULL,
394                           _signon_marshal_VOID__INT_STRING,
395                           G_TYPE_NONE, 2,
396                           G_TYPE_INT,
397                           G_TYPE_STRING);
398
399     object_class->dispose = signon_auth_session_dispose;
400     object_class->finalize = signon_auth_session_finalize;
401 }
402
403 /**
404  * signon_auth_session_new:
405  * @parent: #SignonIdentity parent object.
406  * @method_name: the name of the authentication method to be used.
407  * @err: a pointer to a location which will contain the error, in case this
408  * function fails.
409  *
410  * Creates a new #SignonAuthSession, which can be used to authenticate using
411  * the specified method.
412  *
413  * Returns: a new #SignonAuthSession.
414  */
415 SignonAuthSession *
416 signon_auth_session_new (GObject *parent,
417                          const gchar *method_name,
418                          GError **err)
419 {
420     if (!SIGNON_IS_IDENTITY(parent))
421     {
422         g_set_error (err,
423                      signon_error_quark(),
424                      SIGNON_ERROR_UNKNOWN,
425                      "Parent object is wrong type");
426         return NULL;
427     }
428     SignonIdentity *identity = SIGNON_IDENTITY(parent);
429
430     DEBUG ("%s %d", G_STRFUNC, __LINE__);
431
432     SignonAuthSession *self = SIGNON_AUTH_SESSION(g_object_new (
433                                      SIGNON_TYPE_AUTH_SESSION,
434                                      "identity", identity,
435                                      NULL));
436     g_return_val_if_fail (self != NULL, NULL);
437
438     if (!auth_session_priv_init(self, method_name, err))
439     {
440         if (*err)
441             g_warning ("%s returned error: %s", G_STRFUNC, (*err)->message);
442
443         g_object_unref (self);
444         return NULL;
445     }
446
447     return self;
448 }
449
450 /**
451  * signon_auth_session_get_method:
452  * @self: the #SignonAuthSession.
453  *
454  * Get the current authentication method.
455  *
456  * Returns: the authentication method being used, or %NULL on failure.
457  */
458 const gchar *
459 signon_auth_session_get_method (SignonAuthSession *self)
460 {
461     g_return_val_if_fail (SIGNON_IS_AUTH_SESSION (self), NULL);
462     SignonAuthSessionPrivate *priv = self->priv;
463
464     g_return_val_if_fail (priv != NULL, NULL);
465
466     return priv->method_name;
467 }
468
469 /**
470  * SignonAuthSessionQueryAvailableMechanismsCb:
471  * @self: the #SignonAuthSession.
472  * @mechanisms: (transfer full) (type GStrv): list of available mechanisms.
473  * @error: a #GError if an error occurred, %NULL otherwise.
474  * @user_data: the user data that was passed when installing this callback.
475  *
476  * Callback to be passed to signon_auth_session_query_available_mechanisms().
477  */
478
479 /**
480  * signon_auth_session_query_available_mechanisms:
481  * @self: the #SignonAuthSession.
482  * @wanted_mechanisms: a %NULL-terminated list of mechanisms supported by the client.
483  * @cb: (scope async): a callback which will be called with the result.
484  * @user_data: user data to be passed to the callback.
485  *
486  * Queries the mechanisms available for this authentication session. the result
487  * will be the intersection between @wanted_mechanisms and the mechanisms
488  * supported by the authentication plugin.
489  */
490 void
491 signon_auth_session_query_available_mechanisms (SignonAuthSession *self,
492                                                 const gchar **wanted_mechanisms,
493                                                 SignonAuthSessionQueryAvailableMechanismsCb cb,
494                                                 gpointer user_data)
495 {
496     g_return_if_fail (SIGNON_IS_AUTH_SESSION (self));
497     SignonAuthSessionPrivate* priv = self->priv;
498
499     g_return_if_fail (priv != NULL);
500
501     AuthSessionQueryAvailableMechanismsCbData *cb_data = g_slice_new0 (AuthSessionQueryAvailableMechanismsCbData);
502     cb_data->self = self;
503     cb_data->cb = cb;
504     cb_data->user_data = user_data;
505
506     AuthSessionQueryAvailableMechanismsData *operation_data = g_slice_new0 (AuthSessionQueryAvailableMechanismsData);
507     operation_data->wanted_mechanisms = g_strdupv ((gchar **)wanted_mechanisms);
508     operation_data->cb_data = cb_data;
509
510     auth_session_check_remote_object(self);
511     _signon_object_call_when_ready (self,
512                                     auth_session_object_quark(),
513                                     auth_session_query_available_mechanisms_ready_cb,
514                                     operation_data);
515 }
516
517 /**
518  * SignonAuthSessionProcessCb:
519  * @self: the #SignonAuthSession.
520  * @session_data: (transfer full) (element-type utf8 GValue): a dictionary with
521  * the response.
522  * @error: a #GError if an error occurred, %NULL otherwise.
523  * @user_data: the user data that was passed when installing this callback.
524  *
525  * This callback is invoked when the authentication plugin delivers the result
526  * of the signon_auth_session_process() operation.
527  */
528
529 /**
530  * signon_auth_session_process:
531  * @self: the #SignonAuthSession.
532  * @session_data: (transfer none) (element-type utf8 GValue): a dictionary of parameters.
533  * @mechanism: the authentication mechanism to be used.
534  * @cb: (scope async): a callback which will be called with the result.
535  * @user_data: user data to be passed to the callback.
536  *
537  * Performs one step of the authentication process. If the #SignonAuthSession
538  * object is bound to an existing identity, the identity properties such as
539  * username and password will be also passed to the authentication plugin, so
540  * there's no need to fill them into @session_data.
541  * @session_data can be used to add additional authentication parameters to the
542  * session, or to override the parameters otherwise taken from the identity.
543  *
544  * Deprecated: 1.8: Use signon_auth_session_process_async() instead.
545  */
546 void
547 signon_auth_session_process (SignonAuthSession *self,
548                              const GHashTable *session_data,
549                              const gchar* mechanism,
550                              SignonAuthSessionProcessCb cb,
551                              gpointer user_data)
552 {
553     GVariant *v_session_data;
554
555     g_return_if_fail (SIGNON_IS_AUTH_SESSION (self));
556     DEBUG ("%s %d", G_STRFUNC, __LINE__);
557
558     AuthSessionProcessCbData *cb_data = g_slice_new0 (AuthSessionProcessCbData);
559     cb_data->cb = cb;
560     cb_data->user_data = user_data;
561
562     v_session_data = signon_hash_table_to_variant (session_data);
563
564     signon_auth_session_process_async (self, v_session_data, mechanism, NULL,
565                                        process_async_cb_wrapper, cb_data);
566 }
567
568 /**
569  * signon_auth_session_process_async:
570  * @self: the #SignonAuthSession.
571  * @session_data: (transfer floating): a dictionary of parameters.
572  * @mechanism: the authentication mechanism to be used.
573  * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
574  * @callback: (scope async): a callback which will be called when the
575  * authentication reply is available.
576  * @user_data: user data to be passed to the callback.
577  *
578  * Performs one step of the authentication process. If the #SignonAuthSession
579  * object is bound to an existing identity, the identity properties such as
580  * username and password will be also passed to the authentication plugin, so
581  * there's no need to fill them into @session_data.
582  * @session_data can be used to add additional authentication parameters to the
583  * session, or to override the parameters otherwise taken from the identity.
584  *
585  * Since: 1.8
586  */
587 void
588 signon_auth_session_process_async (SignonAuthSession *self,
589                                    GVariant *session_data,
590                                    const gchar *mechanism,
591                                    GCancellable *cancellable,
592                                    GAsyncReadyCallback callback,
593                                    gpointer user_data)
594 {
595     SignonAuthSessionPrivate *priv;
596     AuthSessionProcessData *process_data;
597     GSimpleAsyncResult *res;
598
599     g_return_if_fail (SIGNON_IS_AUTH_SESSION (self));
600     priv = self->priv;
601
602     g_return_if_fail (session_data != NULL);
603
604     res = g_simple_async_result_new ((GObject *)self, callback, user_data,
605                                      signon_auth_session_process_async);
606     g_simple_async_result_set_check_cancellable (res, cancellable);
607
608     process_data = g_slice_new0 (AuthSessionProcessData);
609     process_data->session_data = g_variant_ref_sink (session_data);
610     process_data->mechanism = g_strdup (mechanism);
611     process_data->cancellable = cancellable;
612     g_object_set_data_full ((GObject *)res, data_key_process, process_data,
613                             (GDestroyNotify)auth_session_process_data_free);
614
615     priv->busy = TRUE;
616
617     auth_session_check_remote_object(self);
618     _signon_object_call_when_ready (self,
619                                     auth_session_object_quark(),
620                                     auth_session_process_ready_cb,
621                                     res);
622 }
623
624 /**
625  * signon_auth_session_process_finish:
626  * @self: the #SignonAuthSession.
627  * @res: A #GAsyncResult obtained from the #GAsyncReadyCallback passed to
628  * signon_auth_session_process_async().
629  * @error: return location for error, or %NULL.
630  *
631  * Collect the result of the signon_auth_session_process_async() operation.
632  *
633  * Returns: a #GVariant of type %G_VARIANT_TYPE_VARDICT containing the
634  * authentication reply.
635  *
636  * Since: 1.8
637  */
638 GVariant *
639 signon_auth_session_process_finish (SignonAuthSession *self, GAsyncResult *res,
640                                     GError **error)
641 {
642     GSimpleAsyncResult *async_result;
643     GVariant *reply;
644
645     g_return_val_if_fail (SIGNON_IS_AUTH_SESSION (self), NULL);
646
647     async_result = (GSimpleAsyncResult *)res;
648     if (g_simple_async_result_propagate_error (async_result, error))
649         return NULL;
650
651     reply = g_simple_async_result_get_op_res_gpointer (async_result);
652     return g_variant_ref (reply);
653 }
654
655 /**
656  * signon_auth_session_cancel:
657  * @self: the #SignonAuthSession.
658  *
659  * Cancel the authentication session.
660  */
661 void
662 signon_auth_session_cancel (SignonAuthSession *self)
663 {
664     g_return_if_fail (SIGNON_IS_AUTH_SESSION (self));
665     SignonAuthSessionPrivate *priv = self->priv;
666
667     g_return_if_fail (priv != NULL);
668
669     auth_session_check_remote_object(self);
670
671     if (!priv->busy)
672         return;
673
674     priv->canceled = TRUE;
675     _signon_object_call_when_ready (self,
676                                     auth_session_object_quark(),
677                                     auth_session_cancel_ready_cb,
678                                     NULL);
679 }
680
681 static void
682 auth_session_state_changed_cb (GDBusProxy *proxy,
683                                gint state,
684                                gchar *message,
685                                gpointer user_data)
686 {
687     g_return_if_fail (SIGNON_IS_AUTH_SESSION (user_data));
688     SignonAuthSession *self = SIGNON_AUTH_SESSION (user_data);
689
690     g_signal_emit ((GObject *)self,
691                     auth_session_signals[STATE_CHANGED],
692                     0,
693                     state,
694                     message);
695 }
696
697 static void auth_session_remote_object_destroyed_cb (GDBusProxy *proxy,
698                                                      gpointer user_data)
699 {
700     g_return_if_fail (SIGNON_IS_AUTH_SESSION (user_data));
701     SignonAuthSession *self = SIGNON_AUTH_SESSION (user_data);
702     SignonAuthSessionPrivate *priv = self->priv;
703     g_return_if_fail (priv != NULL);
704     DEBUG ("remote object unregistered");
705
706     if (priv->proxy)
707     {
708         g_object_unref (priv->proxy);
709         priv->proxy = NULL;
710     }
711
712     /*
713      * as remote object is destroyed only
714      * when the session core is destroyed,
715      * so there should not be any processes
716      * running
717      * */
718     priv->busy = FALSE;
719     priv->canceled = FALSE;
720     _signon_object_not_ready(self);
721 }
722
723 static gboolean
724 auth_session_priv_init (SignonAuthSession *self,
725                         const gchar *method_name, GError **err)
726 {
727     g_return_val_if_fail (SIGNON_IS_AUTH_SESSION (self), FALSE);
728     SignonAuthSessionPrivate *priv = SIGNON_AUTH_SESSION_PRIV (self);
729     g_return_val_if_fail (priv, FALSE);
730
731     priv->method_name = g_strdup (method_name);
732
733     priv->registering = FALSE;
734     priv->busy = FALSE;
735     priv->canceled = FALSE;
736     return TRUE;
737 }
738
739 static void
740 auth_session_query_mechanisms_reply (GObject *object, GAsyncResult *res,
741                                      gpointer userdata)
742 {
743     SsoAuthSession *proxy = SSO_AUTH_SESSION (object);
744     gchar **mechanisms = NULL;
745     GError *error = NULL;
746     AuthSessionQueryAvailableMechanismsCbData *cb_data =
747         (AuthSessionQueryAvailableMechanismsCbData *)userdata;
748     g_return_if_fail (cb_data != NULL);
749
750     sso_auth_session_call_query_available_mechanisms_finish (proxy,
751                                                              &mechanisms,
752                                                              res,
753                                                              &error);
754     SIGNON_RETURN_IF_CANCELLED (error);
755     (cb_data->cb) (cb_data->self, mechanisms, error, cb_data->user_data);
756
757     if (error)
758         g_error_free (error);
759
760     g_slice_free (AuthSessionQueryAvailableMechanismsCbData, cb_data);
761 }
762
763 static void
764 auth_session_query_available_mechanisms_ready_cb (gpointer object, const GError *error,
765                                                   gpointer user_data)
766 {
767     g_return_if_fail (SIGNON_IS_AUTH_SESSION (object));
768     SignonAuthSession *self = SIGNON_AUTH_SESSION (object);
769     SignonAuthSessionPrivate *priv = self->priv;
770     g_return_if_fail (priv != NULL);
771
772     AuthSessionQueryAvailableMechanismsData *operation_data =
773         (AuthSessionQueryAvailableMechanismsData *)user_data;
774     g_return_if_fail (operation_data != NULL);
775
776     AuthSessionQueryAvailableMechanismsCbData *cb_data = operation_data->cb_data;
777     g_return_if_fail (cb_data != NULL);
778
779     if (error)
780     {
781         (cb_data->cb)
782             (self, NULL, error, cb_data->user_data);
783
784         g_slice_free (AuthSessionQueryAvailableMechanismsCbData, cb_data);
785     }
786     else
787     {
788         g_return_if_fail (priv->proxy != NULL);
789         sso_auth_session_call_query_available_mechanisms (
790             priv->proxy,
791             (const char **)operation_data->wanted_mechanisms,
792             priv->cancellable,
793             auth_session_query_mechanisms_reply,
794             cb_data);
795
796         g_signal_emit (self,
797                        auth_session_signals[STATE_CHANGED],
798                        0,
799                        SIGNON_AUTH_SESSION_STATE_PROCESS_PENDING,
800                        auth_session_process_pending_message);
801     }
802
803     g_strfreev (operation_data->wanted_mechanisms);
804     g_slice_free (AuthSessionQueryAvailableMechanismsData, operation_data);
805 }
806
807 static void
808 auth_session_cancel_ready_cb (gpointer object, const GError *error, gpointer user_data)
809 {
810     g_return_if_fail (SIGNON_IS_AUTH_SESSION (object));
811     g_return_if_fail (user_data == NULL);
812
813     SignonAuthSession *self = SIGNON_AUTH_SESSION (object);
814     SignonAuthSessionPrivate *priv = self->priv;
815     g_return_if_fail (priv != NULL);
816
817     if (error)
818     {
819         //TODO: in general this function does not return any values,
820         // that is why I think it should not emit anything for this particular case
821         DEBUG("error during initialization");
822     }
823     else if (priv->proxy && priv->busy)
824         sso_auth_session_call_cancel_sync (priv->proxy,
825                                            priv->cancellable,
826                                            NULL);
827
828     priv->busy = FALSE;
829     priv->canceled = FALSE;
830 }
831
832 static void
833 signon_auth_session_complete (SignonAuthSession *self,
834                               GError *error,
835                               GDBusConnection *connection,
836                               const gchar *bus_name,
837                               const gchar *object_path)
838 {
839     SignonAuthSessionPrivate *priv = self->priv;
840     g_return_if_fail (priv != NULL);
841
842     DEBUG ("%s %d", G_STRFUNC, __LINE__);
843
844     priv->registering = FALSE;
845     if (!g_strcmp0(object_path, "") || error)
846     {
847         if (error)
848             DEBUG ("Error message is %s", error->message);
849         else
850             error = g_error_new (signon_error_quark(),
851                                  SIGNON_ERROR_RUNTIME,
852                                  "Cannot create remote AuthSession object");
853     }
854     else
855     {
856         GError *proxy_error = NULL;
857
858         priv->proxy =
859             sso_auth_session_proxy_new_sync (connection,
860                                              G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES,
861                                              bus_name,
862                                              object_path,
863                                              priv->cancellable,
864                                              &proxy_error);
865         if (G_UNLIKELY (proxy_error != NULL))
866         {
867             g_warning ("Failed to initialize AuthSession proxy: %s",
868                        proxy_error->message);
869             g_clear_error (&proxy_error);
870         }
871
872         g_dbus_proxy_set_default_timeout ((GDBusProxy *)priv->proxy,
873                                           G_MAXINT);
874
875         priv->signal_state_changed =
876             g_signal_connect (priv->proxy,
877                               "state-changed",
878                               G_CALLBACK (auth_session_state_changed_cb),
879                               self);
880
881         priv->signal_unregistered =
882            g_signal_connect (priv->proxy,
883                              "unregistered",
884                              G_CALLBACK (auth_session_remote_object_destroyed_cb),
885                              self);
886     }
887
888     DEBUG ("Object path received: %s", object_path);
889     _signon_object_ready (self, auth_session_object_quark (), error);
890 }
891
892 static void
893 auth_session_check_remote_object(SignonAuthSession *self)
894 {
895     g_return_if_fail (self != NULL);
896     SignonAuthSessionPrivate *priv = self->priv;
897     g_return_if_fail (priv != NULL);
898
899     if (priv->proxy != NULL)
900         return;
901
902     g_return_if_fail (priv->identity != NULL);
903
904     if (!priv->registering)
905     {
906         DEBUG ("%s %d", G_STRFUNC, __LINE__);
907
908         priv->registering = TRUE;
909         signon_identity_get_auth_session (priv->identity,
910                                           self,
911                                           priv->method_name,
912                                           signon_auth_session_complete);
913
914     }
915 }
916