[gnome-online-accounts/wip/rishi/fedora-provider: 1/2] identity: Split out the details of adding a temporary Kerberos account



commit aa1439670224bc238688bfc042922fbee3bfa4ee
Author: Debarshi Ray <debarshir gnome org>
Date:   Mon Aug 5 00:38:33 2019 +0200

    identity: Split out the details of adding a temporary Kerberos account
    
    This is meant to make room for adding a temporary Fedora account, with
    the possibility of falling back to a Kerberos account, in the following
    commit.
    
    https://gitlab.gnome.org/GNOME/gnome-online-accounts/issues/26

 src/goaidentity/goaidentityservice.c | 189 +++++++++++++++++++++++------------
 1 file changed, 127 insertions(+), 62 deletions(-)
---
diff --git a/src/goaidentity/goaidentityservice.c b/src/goaidentity/goaidentityservice.c
index 94695558..aa86fa95 100644
--- a/src/goaidentity/goaidentityservice.c
+++ b/src/goaidentity/goaidentityservice.c
@@ -1,6 +1,6 @@
 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
 /*
- * Copyright © 2012 – 2017 Red Hat, Inc.
+ * Copyright © 2012 – 2019 Red Hat, Inc.
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
@@ -772,39 +772,25 @@ on_identity_signed_in (GoaIdentityManager *manager,
   g_object_unref (operation_result);
 }
 
+/* ---------------------------------------------------------------------------------------------------- */
+
 static void
-on_temporary_account_added (GoaManager   *manager,
-                            GAsyncResult *result,
-                            GTask        *task)
+add_temporary_account_as_kerberos_add_account (GObject *source_object, GAsyncResult *res, gpointer user_data)
 {
   GoaIdentityService *self;
   GDBusObjectManager *object_manager;
-  const char *principal;
-  char *object_path;
-  GoaIdentity *identity;
-  GoaObject *object;
   GError *error;
+  GTask *task = G_TASK (user_data);
+  GoaManager *manager = GOA_MANAGER (source_object);
+  GoaObject *object = NULL;
+  gchar *object_path = NULL;
 
   self = GOA_IDENTITY_SERVICE (g_task_get_source_object (task));
-  object_path = NULL;
-  object = NULL;
-  error = NULL;
 
-  identity = GOA_IDENTITY (g_task_get_task_data (task));
-
-  principal = goa_identity_get_identifier (identity);
-  g_hash_table_remove (self->priv->pending_temporary_account_results, principal);
-
-  if (!goa_manager_call_add_account_finish (manager,
-                                            &object_path,
-                                            result,
-                                            &error))
+  error = NULL;
+  if (!goa_manager_call_add_account_finish (manager, &object_path, res, &error))
     {
-      const char *identifier;
-
-      identifier = goa_identity_get_identifier (identity);
-      g_debug ("Could not add temporary account for identity %s: %s", identifier, error->message);
-      g_error_free (error);
+      g_task_return_error (task, error);
       goto out;
     }
 
@@ -812,9 +798,17 @@ on_temporary_account_added (GoaManager   *manager,
 
   object_manager = goa_client_get_object_manager (self->priv->client);
   object = GOA_OBJECT (g_dbus_object_manager_get_object (object_manager, object_path));
+  if (object == NULL)
+    {
+      g_task_return_new_error (task,
+                               GOA_ERROR,
+                               GOA_ERROR_FAILED, /* TODO: more specific */
+                               _("Couldn't get GoaObject for object path %s"),
+                               object_path);
+      goto out;
+    }
 
-  if (object != NULL)
-    ensure_account_credentials (self, object);
+  g_task_return_pointer (task, g_object_ref (object), g_object_unref);
 
  out:
   g_free (object_path);
@@ -823,50 +817,38 @@ on_temporary_account_added (GoaManager   *manager,
 }
 
 static void
-add_temporary_account (GoaIdentityService *self,
-                       GoaIdentity        *identity)
+add_temporary_account_as_kerberos (GoaIdentityService *self,
+                                   GoaIdentity *identity,
+                                   GCancellable *cancellable,
+                                   GAsyncReadyCallback callback,
+                                   gpointer user_data)
 {
-  char               *realm;
-  char               *preauth_source;
-  const char         *principal;
-  GTask              *task;
-  GVariantBuilder     credentials;
-  GVariantBuilder     details;
-  GoaManager         *manager;
+  GTask *task = NULL;
+  GVariantBuilder credentials;
+  GVariantBuilder details;
+  GoaManager *manager;
+  const gchar *principal;
+  gchar *preauth_source = NULL;
+  gchar *realm = NULL;
+
+  task = g_task_new (self, cancellable, callback, user_data);
+  g_task_set_source_tag (task, add_temporary_account_as_kerberos);
+  g_task_set_task_data (task, g_object_ref (identity), g_object_unref);
 
   principal = goa_identity_get_identifier (identity);
-  if (g_hash_table_contains (self->priv->pending_temporary_account_results, principal))
-    {
-      g_debug ("GoaIdentityService: would add temporary identity %s, but it's already pending", principal);
-      return;
-    }
-
-  g_debug ("GoaIdentityService: adding temporary identity %s", principal);
-
-  /* If there's no account for this identity then create a temporary one.
-   */
-
-  realm = goa_kerberos_identity_get_realm_name (GOA_KERBEROS_IDENTITY (identity));
-  preauth_source = goa_kerberos_identity_get_preauthentication_source (GOA_KERBEROS_IDENTITY (identity));
 
   g_variant_builder_init (&credentials, G_VARIANT_TYPE_VARDICT);
 
   g_variant_builder_init (&details, G_VARIANT_TYPE ("a{ss}"));
+  realm = goa_kerberos_identity_get_realm_name (GOA_KERBEROS_IDENTITY (identity));
   g_variant_builder_add (&details, "{ss}", "Realm", realm);
   g_variant_builder_add (&details, "{ss}", "IsTemporary", "true");
+
+  preauth_source = goa_kerberos_identity_get_preauthentication_source (GOA_KERBEROS_IDENTITY (identity));
   if (preauth_source != NULL)
     g_variant_builder_add (&details, "{ss}", "PreauthenticationSource", preauth_source);
-  g_variant_builder_add (&details, "{ss}", "TicketingEnabled", "true");
-
-
-  g_debug ("GoaIdentityService: asking to sign back in");
 
-  task = g_task_new (self, NULL, NULL, NULL);
-  g_task_set_task_data (task, g_object_ref (identity), g_object_unref);
-
-  g_hash_table_insert (self->priv->pending_temporary_account_results,
-                       g_strdup (principal),
-                       g_object_ref (task));
+  g_variant_builder_add (&details, "{ss}", "TicketingEnabled", "true");
 
   manager = goa_client_get_manager (self->priv->client);
   goa_manager_call_add_account (manager,
@@ -876,14 +858,97 @@ add_temporary_account (GoaIdentityService *self,
                                 g_variant_builder_end (&credentials),
                                 g_variant_builder_end (&details),
                                 NULL,
-                                (GAsyncReadyCallback)
-                                on_temporary_account_added,
+                                add_temporary_account_as_kerberos_add_account,
                                 g_object_ref (task));
-  g_free (realm);
+
   g_free (preauth_source);
+  g_free (realm);
   g_object_unref (task);
 }
 
+static GoaObject *
+add_temporary_account_as_kerberos_finish (GoaIdentityService *self, GAsyncResult *res, GError **error)
+{
+  GTask *task;
+
+  g_return_val_if_fail (g_task_is_valid (res, self), NULL);
+  task = G_TASK (res);
+
+  g_return_val_if_fail (g_task_get_source_tag (task) == add_temporary_account_as_kerberos, NULL);
+  g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
+
+  return g_task_propagate_pointer (task, error);
+}
+
+/* ---------------------------------------------------------------------------------------------------- */
+
+static void
+on_temporary_account_added_as_kerberos (GObject *source_object, GAsyncResult *result, gpointer user_data)
+{
+  GoaIdentityService *self = GOA_IDENTITY_SERVICE (source_object);
+  GoaIdentity *identity = GOA_IDENTITY (user_data);
+  const char *principal;
+
+  GoaObject *object;
+  GError *error;
+
+  error = NULL;
+
+  principal = goa_identity_get_identifier (identity);
+  g_hash_table_remove (self->priv->pending_temporary_account_results, principal);
+
+  object = add_temporary_account_as_kerberos_finish (self, result, &error);
+  if (error != NULL)
+    {
+      const char *identifier;
+
+      identifier = goa_identity_get_identifier (identity);
+      g_debug ("Could not add temporary Kerberos account for identity %s: %s", identifier, error->message);
+      g_error_free (error);
+      goto out;
+    }
+
+  ensure_account_credentials (self, object);
+
+ out:
+  g_free (object_path);
+  g_clear_object (&object);
+  g_object_unref (identity);
+}
+
+static void
+add_temporary_account (GoaIdentityService *self,
+                       GoaIdentity        *identity)
+{
+  const char *principal;
+
+  principal = goa_identity_get_identifier (identity);
+  if (g_hash_table_contains (self->priv->pending_temporary_account_results, principal))
+    {
+      g_debug ("GoaIdentityService: would add temporary identity %s, but it's already pending", principal);
+      return;
+    }
+
+  g_debug ("GoaIdentityService: adding temporary identity %s", principal);
+
+  /* If there's no account for this identity then create a temporary one.
+   */
+
+  g_debug ("GoaIdentityService: asking to sign back in");
+
+  g_hash_table_insert (self->priv->pending_temporary_account_results,
+                       g_strdup (principal),
+                       g_object_ref (identity));
+
+  add_temporary_account_as_kerberos (self,
+                                     identity,
+                                     NULL,
+                                     on_temporary_account_added_as_kerberos,
+                                     g_object_ref (identity));
+}
+
+/* ---------------------------------------------------------------------------------------------------- */
+
 static void
 on_identity_added (GoaIdentityManager *identity_manager,
                    GoaIdentity        *identity,


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]