Chain session object creation properly with identity object creation
authorJussi Laako <jussi.laako@linux.intel.com>
Mon, 18 Feb 2013 17:04:29 +0000 (19:04 +0200)
committerJussi Laako <jussi.laako@linux.intel.com>
Fri, 31 May 2013 09:27:44 +0000 (12:27 +0300)
libsignon-glib/signon-auth-session.c
libsignon-glib/signon-auth-session.h
libsignon-glib/signon-identity.c
libsignon-glib/signon-identity.h
libsignon-glib/signon-internals.h
tests/check_signon.c

index 20fed1bfce5babae4f0f44607614e8b5dcdb2975..08fdee3cb17fc255c944c7d5bc3bf42d76db98b4 100644 (file)
@@ -46,9 +46,9 @@
 #include "signon-errors.h"
 #include "signon-marshal.h"
 #include "signon-utils.h"
+#include "signon-identity.h"
 #include "sso-auth-service.h"
 #include "sso-auth-session-gen.h"
-#include "sso-identity-gen.h"
 
 
 G_DEFINE_TYPE (SignonAuthSession, signon_auth_session, G_TYPE_OBJECT);
@@ -75,7 +75,7 @@ static const gchar data_key_process[] = "signon-process";
 struct _SignonAuthSessionPrivate
 {
     SsoAuthSession *proxy;
-    SsoIdentity *identity_proxy;
+    SignonIdentity *identity;
     GCancellable *cancellable;
 
     guint id;
@@ -279,7 +279,7 @@ signon_auth_session_set_property (GObject *object,
     switch (property_id)
     {
         case PROP_IDENTITY:
-            self->priv->identity_proxy = g_value_dup_object (value);
+            self->priv->identity = g_value_dup_object (value);
             break;
         default:
             G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
@@ -297,7 +297,7 @@ signon_auth_session_get_property (GObject *object,
     switch (property_id)
     {
         case PROP_IDENTITY:
-            g_value_set_object (value, self->priv->identity_proxy);
+            g_value_set_object (value, self->priv->identity);
             break;
         default:
             G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
@@ -337,12 +337,6 @@ signon_auth_session_dispose (GObject *object)
         priv->proxy = NULL;
     }
 
-    if (priv->identity_proxy)
-    {
-        g_object_unref (priv->identity_proxy);
-        priv->identity_proxy = NULL;
-    }
-
     G_OBJECT_CLASS (signon_auth_session_parent_class)->dispose (object);
 
     priv->dispose_has_run = TRUE;
@@ -372,9 +366,9 @@ signon_auth_session_class_init (SignonAuthSessionClass *klass)
     object_class->get_property = signon_auth_session_get_property;
 
     pspec = g_param_spec_object ("identity",
-                                 "Identity Proxy Object",
-                                 "Identity Proxy Object construct parameter",
-                                 TYPE_SSO_IDENTITY,
+                                 "Identity Object",
+                                 "Identity Object construct parameter",
+                                 SIGNON_TYPE_IDENTITY,
                                  G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY);
     g_object_class_install_property (object_class,
                                      PROP_IDENTITY,
@@ -408,8 +402,7 @@ signon_auth_session_class_init (SignonAuthSessionClass *klass)
 
 /**
  * signon_auth_session_new:
- * @id: the id of the #SignonIdentity to be used. Can be 0, if this session is
- * not bound to any stored identity.
+ * @parent: #SignonIdentity parent object.
  * @method_name: the name of the authentication method to be used.
  * @err: a pointer to a location which will contain the error, in case this
  * function fails.
@@ -420,11 +413,19 @@ signon_auth_session_class_init (SignonAuthSessionClass *klass)
  * Returns: a new #SignonAuthSession.
  */
 SignonAuthSession *
-signon_auth_session_new (gpointer identity_proxy,
+signon_auth_session_new (GObject *parent,
                          const gchar *method_name,
                          GError **err)
 {
-    SsoIdentity *identity = SSO_IDENTITY(identity_proxy);
+    if (!SIGNON_IS_IDENTITY(parent))
+    {
+        g_set_error (err,
+                     signon_error_quark(),
+                     SIGNON_ERROR_UNKNOWN,
+                     "Parent object is wrong type");
+        return NULL;
+    }
+    SignonIdentity *identity = SIGNON_IDENTITY(parent);
 
     SignonAuthSession *self = SIGNON_AUTH_SESSION(g_object_new (
                                      SIGNON_TYPE_AUTH_SESSION,
@@ -674,80 +675,6 @@ signon_auth_session_cancel (SignonAuthSession *self)
                                     NULL);
 }
 
-static void
-identity_get_auth_session_reply (GObject *object, GAsyncResult *res,
-                                 gpointer userdata)
-{
-    SsoIdentity *proxy = SSO_IDENTITY (object);
-    gchar *object_path = NULL;
-    GError *error = NULL;
-
-    sso_identity_call_get_auth_session_finish (proxy,
-                                               &object_path,
-                                               res,
-                                               &error);
-    SIGNON_RETURN_IF_CANCELLED (error);
-
-    g_return_if_fail (SIGNON_IS_AUTH_SESSION (userdata));
-    SignonAuthSession *self = SIGNON_AUTH_SESSION (userdata);
-    SignonAuthSessionPrivate *priv = self->priv;
-    g_return_if_fail (priv != NULL);
-
-    priv->registering = FALSE;
-    if (!g_strcmp0(object_path, "") || error)
-    {
-        if (error)
-            DEBUG ("Error message is %s", error->message);
-        else
-            error = g_error_new (signon_error_quark(),
-                                 SIGNON_ERROR_RUNTIME,
-                                 "Cannot create remote AuthSession object");
-    }
-    else
-    {
-        GDBusConnection *connection;
-        const gchar *bus_name;
-        GError *proxy_error = NULL;
-
-        connection = g_dbus_proxy_get_connection ((GDBusProxy *)proxy);
-        bus_name = g_dbus_proxy_get_name ((GDBusProxy *)proxy);
-
-        priv->proxy =
-            sso_auth_session_proxy_new_sync (connection,
-                                             G_DBUS_PROXY_FLAGS_NONE,
-                                             bus_name,
-                                             object_path,
-                                             priv->cancellable,
-                                             &proxy_error);
-        if (G_UNLIKELY (proxy_error != NULL))
-        {
-            g_warning ("Failed to initialize AuthSession proxy: %s",
-                       proxy_error->message);
-            g_clear_error (&proxy_error);
-        }
-
-        g_dbus_proxy_set_default_timeout ((GDBusProxy *)priv->proxy,
-                                          G_MAXINT);
-
-        priv->signal_state_changed =
-            g_signal_connect (priv->proxy,
-                              "state-changed",
-                              G_CALLBACK (auth_session_state_changed_cb),
-                              self);
-
-        priv->signal_unregistered =
-           g_signal_connect (priv->proxy,
-                             "unregistered",
-                             G_CALLBACK (auth_session_remote_object_destroyed_cb),
-                             self);
-    }
-
-    DEBUG ("Object path received: %s", object_path);
-    g_free (object_path);
-    _signon_object_ready (self, auth_session_object_quark (), error);
-    g_clear_error (&error);
-}
-
 static void
 auth_session_state_changed_cb (GDBusProxy *proxy,
                                gint state,
@@ -800,13 +727,7 @@ auth_session_priv_init (SignonAuthSession *self,
 
     priv->method_name = g_strdup (method_name);
 
-    priv->registering = TRUE;
-    sso_identity_call_get_auth_session (
-                                        priv->identity_proxy,
-                                        method_name,
-                                        priv->cancellable,
-                                        identity_get_auth_session_reply,
-                                        self);
+    priv->registering = FALSE;
     priv->busy = FALSE;
     priv->canceled = FALSE;
     return TRUE;
@@ -905,6 +826,64 @@ auth_session_cancel_ready_cb (gpointer object, const GError *error, gpointer use
     priv->canceled = FALSE;
 }
 
+static void
+signon_auth_session_complete (SignonAuthSession *self,
+                              GError *error,
+                              GDBusConnection *connection,
+                              const gchar *bus_name,
+                              const gchar *object_path)
+{
+    SignonAuthSessionPrivate *priv = self->priv;
+    g_return_if_fail (priv != NULL);
+
+    priv->registering = FALSE;
+    if (!g_strcmp0(object_path, "") || error)
+    {
+        if (error)
+            DEBUG ("Error message is %s", error->message);
+        else
+            error = g_error_new (signon_error_quark(),
+                                 SIGNON_ERROR_RUNTIME,
+                                 "Cannot create remote AuthSession object");
+    }
+    else
+    {
+        GError *proxy_error = NULL;
+
+        priv->proxy =
+            sso_auth_session_proxy_new_sync (connection,
+                                             G_DBUS_PROXY_FLAGS_NONE,
+                                             bus_name,
+                                             object_path,
+                                             priv->cancellable,
+                                             &proxy_error);
+        if (G_UNLIKELY (proxy_error != NULL))
+        {
+            g_warning ("Failed to initialize AuthSession proxy: %s",
+                       proxy_error->message);
+            g_clear_error (&proxy_error);
+        }
+
+        g_dbus_proxy_set_default_timeout ((GDBusProxy *)priv->proxy,
+                                          G_MAXINT);
+
+        priv->signal_state_changed =
+            g_signal_connect (priv->proxy,
+                              "state-changed",
+                              G_CALLBACK (auth_session_state_changed_cb),
+                              self);
+
+        priv->signal_unregistered =
+           g_signal_connect (priv->proxy,
+                             "unregistered",
+                             G_CALLBACK (auth_session_remote_object_destroyed_cb),
+                             self);
+    }
+
+    DEBUG ("Object path received: %s", object_path);
+    _signon_object_ready (self, auth_session_object_quark (), error);
+}
+
 static void
 auth_session_check_remote_object(SignonAuthSession *self)
 {
@@ -915,17 +894,16 @@ auth_session_check_remote_object(SignonAuthSession *self)
     if (priv->proxy != NULL)
         return;
 
-    g_return_if_fail (priv->identity_proxy != NULL);
+    g_return_if_fail (priv->identity != NULL);
 
     if (!priv->registering)
     {
         priv->registering = TRUE;
-        sso_identity_call_get_auth_session (
-            priv->identity_proxy,
-            priv->method_name,
-            priv->cancellable,
-            identity_get_auth_session_reply,
-            self);
+        signon_identity_get_auth_session (priv->identity,
+                                          self,
+                                          priv->method_name,
+                                          signon_auth_session_complete);
+
     }
 }
 
index c828b8a49709db4193c9bc640539ff322509153e..66bca20ec26dee1bcb370094a7788bbebd67823d 100644 (file)
@@ -144,7 +144,7 @@ struct _SignonAuthSessionClass {
 
 GType signon_auth_session_get_type (void) G_GNUC_CONST;
 
-SignonAuthSession *signon_auth_session_new(gpointer identity_proxy,
+SignonAuthSession *signon_auth_session_new(GObject *parent,
                                            const gchar *method_name,
                                            GError **err);
 
index 3b2179ecacc259dfb7e17fd4cbd910a1ef8fd768..c4d14ee27f7d68bf755352f3d600020e901b91fb 100644 (file)
@@ -115,6 +115,19 @@ typedef enum {
     SIGNON_SIGNOUT
 } IdentityOperation;
 
+typedef struct _IdentitySessionCbData
+{
+    SignonIdentity *self;
+    SignonAuthSession *session;
+    SignonIdentitySessionReadyCb cb;
+} IdentitySessionCbData;
+
+typedef struct _IdentitySessionData
+{
+    const gchar *method;
+    gpointer cb_data;
+} IdentitySessionData;
+
 typedef struct _IdentityVerifyCbData
 {
     SignonIdentity *self;
@@ -155,7 +168,6 @@ static void identity_store_credentials_ready_cb (gpointer object, const GError *
 static void identity_store_credentials_reply (GObject *object,
                                               GAsyncResult *res,
                                               gpointer userdata);
-static void identity_session_object_destroyed_cb (gpointer data, GObject *where_the_session_was);
 static void identity_verify_data (SignonIdentity *self, const gchar *data_to_send, gint operation,
                                     SignonIdentityVerifyCb cb, gpointer user_data);
 static void identity_verify_ready_cb (gpointer object, const GError *error, gpointer user_data);
@@ -167,6 +179,13 @@ static void identity_info_ready_cb (gpointer object, const GError *error, gpoint
 static void identity_process_signout (SignonIdentity *self);
 static void identity_process_updated (SignonIdentity *self);
 static void identity_process_removed (SignonIdentity *self);
+static void identity_get_auth_session_reply (GObject *object,
+                                             GAsyncResult *res,
+                                             gpointer userdata);
+static void identity_session_ready_cb (gpointer object, const GError *error,
+                                       gpointer user_data);
+static void identity_session_object_destroyed_cb (gpointer data,
+                                                  GObject *where_the_session_was);
 
 static GQuark
 identity_object_quark ()
@@ -234,6 +253,7 @@ signon_identity_init (SignonIdentity *identity)
                                                   SignonIdentityPrivate);
 
     priv = identity->priv;
+    priv->proxy = NULL;
     priv->auth_service_proxy = sso_auth_service_get_instance();
     priv->cancellable = g_cancellable_new ();
     priv->registration_state = NOT_REGISTERED;
@@ -627,21 +647,6 @@ signon_identity_new (const gchar *application_context)
     return identity;
 }
 
-static void
-identity_session_object_destroyed_cb(gpointer data,
-                                     GObject *where_the_session_was)
-{
-    g_return_if_fail (SIGNON_IS_IDENTITY (data));
-    DEBUG ("%s %d", G_STRFUNC, __LINE__);
-
-    SignonIdentity *self = SIGNON_IDENTITY (data);
-    SignonIdentityPrivate *priv = self->priv;
-    g_return_if_fail (priv != NULL);
-
-    priv->sessions = g_slist_remove(priv->sessions, (gpointer)where_the_session_was);
-    g_object_unref (self);
-}
-
 /**
  * signon_identity_create_session:
  * @self: the #SignonIdentity.
@@ -692,7 +697,7 @@ signon_identity_create_session(SignonIdentity *self,
         list = list->next;
     }
 
-    SignonAuthSession *session = signon_auth_session_new (priv->proxy,
+    SignonAuthSession *session = signon_auth_session_new (G_OBJECT(self),
                                                           method,
                                                           error);
     if (session)
@@ -1499,3 +1504,118 @@ void signon_identity_query_info(SignonIdentity *self,
                             SIGNON_INFO,
                             cb_data);
 }
+
+static void
+identity_get_auth_session_reply (GObject *object, GAsyncResult *res,
+                                 gpointer userdata)
+{
+    SsoIdentity *proxy = SSO_IDENTITY (object);
+    gchar *object_path = NULL;
+    GError *error = NULL;
+
+    sso_identity_call_get_auth_session_finish (proxy,
+                                               &object_path,
+                                               res,
+                                               &error);
+    SIGNON_RETURN_IF_CANCELLED (error);
+
+    IdentitySessionCbData *cb_data = (IdentitySessionCbData *) userdata;
+    g_return_if_fail (cb_data != NULL);
+    g_return_if_fail (cb_data->cb != NULL);
+
+    (cb_data->cb) (cb_data->session,
+                   error,
+                   g_dbus_proxy_get_connection ((GDBusProxy *)proxy),
+                   g_dbus_proxy_get_name ((GDBusProxy *)proxy),
+                   object_path);
+
+    g_slice_free (IdentitySessionCbData, cb_data);
+    g_free (object_path);
+    g_clear_error (&error);
+}
+
+static void
+identity_session_ready_cb(gpointer object, const GError *error, gpointer user_data)
+{
+    g_return_if_fail (SIGNON_IS_IDENTITY (object));
+
+    SignonIdentity *self = SIGNON_IDENTITY (object);
+    SignonIdentityPrivate *priv = self->priv;
+    g_return_if_fail (priv != NULL);
+
+    DEBUG ("%s %d", G_STRFUNC, __LINE__);
+
+    IdentitySessionData *operation_data = (IdentitySessionData *) user_data;
+    g_return_if_fail (operation_data != NULL);
+
+    IdentitySessionCbData *cb_data = operation_data->cb_data;
+    g_return_if_fail (cb_data != NULL);
+
+    if (priv->removed == TRUE)
+    {
+        GError *new_error = g_error_new (signon_error_quark(),
+                                         SIGNON_ERROR_IDENTITY_NOT_FOUND,
+                                         "Already removed from database.");
+        if (cb_data->cb)
+        {
+            (cb_data->cb) (cb_data->session, new_error, NULL, NULL, NULL);
+        }
+
+        g_error_free (new_error);
+    }
+    else
+    {
+        g_return_if_fail (priv->proxy != NULL);
+
+        sso_identity_call_get_auth_session (
+            priv->proxy,
+            operation_data->method,
+            priv->cancellable,
+            identity_get_auth_session_reply,
+            cb_data);
+    }
+
+    g_slice_free (IdentitySessionData, operation_data);
+}
+
+static void
+identity_session_object_destroyed_cb(gpointer data,
+                                     GObject *where_the_session_was)
+{
+    g_return_if_fail (SIGNON_IS_IDENTITY (data));
+    DEBUG ("%s %d", G_STRFUNC, __LINE__);
+
+    SignonIdentity *self = SIGNON_IDENTITY (data);
+    SignonIdentityPrivate *priv = self->priv;
+    g_return_if_fail (priv != NULL);
+
+    priv->sessions = g_slist_remove(priv->sessions, (gpointer)where_the_session_was);
+    g_object_unref (self);
+}
+
+void signon_identity_get_auth_session (SignonIdentity *self,
+                                       SignonAuthSession *session,
+                                       const gchar *method,
+                                       SignonIdentitySessionReadyCb cb)
+{
+    g_return_if_fail (SIGNON_IS_IDENTITY (self));
+
+    SignonIdentityPrivate *priv = self->priv;
+    g_return_if_fail (priv != NULL);
+
+    IdentitySessionCbData *cb_data = g_slice_new0 (IdentitySessionCbData);
+    cb_data->self = self;
+    cb_data->session = session;
+    cb_data->cb = cb;
+
+    IdentitySessionData *operation_data = g_slice_new0 (IdentitySessionData);
+    operation_data->method = method;
+    operation_data->cb_data = cb_data;
+
+    identity_check_remote_registration (self);
+    _signon_object_call_when_ready (self,
+                                    identity_object_quark(),
+                                    identity_session_ready_cb,
+                                    operation_data);
+}
+
index 79242857cff4d37936d785ff42280033b84b82fd..9dfac4aca91f80f4f8c2d18847b3eeabf2c72a94 100644 (file)
@@ -202,6 +202,17 @@ void signon_identity_remove_reference(SignonIdentity *self,
                             SignonIdentityReferenceRemovedCb cb,
                             gpointer user_data);
 
+typedef void (*SignonIdentitySessionReadyCb) (SignonAuthSession *self,
+                                              GError *error,
+                                              GDBusConnection *connection,
+                                              const gchar *bus_name,
+                                              const gchar *object_path);
+void signon_identity_get_auth_session(SignonIdentity *self,
+                                      SignonAuthSession *session,
+                                      const gchar *method,
+                                      SignonIdentitySessionReadyCb cb);
+                                         
+
 G_END_DECLS
 
 #endif /* _SIGNON_IDENTITY_H_ */
index a907fe32d4dc1d0f9552c342af9fbeae831f91ea..34358c1c338137e97eaf47edad625daa68f0127a 100644 (file)
 #define SIGNOND_USER_ERROR_ERR_NAME SIGNOND_ERR_PREFIX "User"
 
 
-#include "signon-identity.h"
-#include "signon-auth-session.h"
+#include "signon-identity-info.h"
 
 G_BEGIN_DECLS
 
index 8f15b4fa2143a45ad09c0dfdbe3533ae2942d859..7e9f0ec96071b745ee1e7c63f012b48a6899b998 100644 (file)
@@ -420,7 +420,7 @@ START_TEST(test_auth_session_creation)
 
     g_debug("%s", G_STRFUNC);
     SignonIdentity *idty = signon_identity_new(NULL);
-    fail_unless (idty != NULL, "Cannot create Iddentity object");
+    fail_unless (idty != NULL, "Cannot create Identity object");
 
     SignonAuthSession *auth_session = signon_identity_create_session(idty,
                                                                     "ssotest",