[gnome-online-accounts] utils: Add goa_utils_get_credentials helper



commit 50f9b5c06cf80d7e7862c520528be0fbd37e10d7
Author: Christophe Fergeau <cfergeau redhat com>
Date:   Sat May 2 12:43:53 2015 +0200

    utils: Add goa_utils_get_credentials helper
    
    This helper gathers code which is duplicated among several
    password-based providers. Eventually, a GoaPasswordBasedProvider base
    class could be introduced similarly to what is done for the oauth based
    providers.
    
    The addition of this new helper is used as an opportunity for small
    cleanups in the code which was copied/pasted between the various
    providers:
    - g_set_error is used instead of g_error_new
    - goa_object_get_account and goa_account_dup_identity are used instead
      of goa_object_peek_account and goa_account_get_identity as using the
      former has some thread-safety issues
    
    https://bugzilla.gnome.org/show_bug.cgi?id=755316

 src/goabackend/goautils.c |   55 +++++++++++++++++++++++++++++++++++++++++++++
 src/goabackend/goautils.h |    8 ++++++
 2 files changed, 63 insertions(+), 0 deletions(-)
---
diff --git a/src/goabackend/goautils.c b/src/goabackend/goautils.c
index 6144276..498b8c5 100644
--- a/src/goabackend/goautils.c
+++ b/src/goabackend/goautils.c
@@ -633,3 +633,58 @@ goa_utils_set_error_ssl (GError **err, GTlsCertificateFlags flags)
 
   g_set_error_literal (err, GOA_ERROR, GOA_ERROR_SSL, error_msg);
 }
+
+gboolean
+goa_utils_get_credentials (GoaProvider    *provider,
+                           GoaObject      *object,
+                           const gchar    *id,
+                           gchar         **out_username,
+                           gchar         **out_password,
+                           GCancellable   *cancellable,
+                           GError        **error)
+{
+  GVariant *credentials = NULL;
+  GoaAccount *account = NULL;
+  gboolean ret = FALSE;
+  gchar *username = NULL;
+  gchar *password = NULL;
+
+  credentials = goa_utils_lookup_credentials_sync (provider,
+                                                   object,
+                                                   cancellable,
+                                                   error);
+  if (credentials == NULL)
+    goto out;
+
+  account = goa_object_get_account (object);
+  username = goa_account_dup_identity (account);
+
+  if (!g_variant_lookup (credentials, id, "s", &password))
+    {
+      g_set_error (error, GOA_ERROR, GOA_ERROR_FAILED, /* TODO: more specific */
+                   _("Did not find %s with identity ā€˜%sā€™ in credentials"),
+                   id, username);
+      goto out;
+    }
+
+  if (out_username)
+    {
+      *out_username = username;
+      username = NULL;
+    }
+
+  if (out_password)
+    {
+      *out_password = password;
+      password = NULL;
+    }
+
+  ret = TRUE;
+
+out:
+  g_clear_object (&account);
+  g_clear_pointer (&credentials, (GDestroyNotify) g_variant_unref);
+  g_free (username);
+  g_free (password);
+  return ret;
+}
diff --git a/src/goabackend/goautils.h b/src/goabackend/goautils.h
index ca3671b..840ca8c 100644
--- a/src/goabackend/goautils.h
+++ b/src/goabackend/goautils.h
@@ -88,6 +88,14 @@ void             goa_utils_set_error_soup (GError **err, SoupMessage *msg);
 
 void             goa_utils_set_error_ssl (GError **err, GTlsCertificateFlags flags);
 
+gboolean         goa_utils_get_credentials (GoaProvider    *provider,
+                                            GoaObject      *object,
+                                            const gchar    *id,
+                                            gchar         **username,
+                                            gchar         **password,
+                                            GCancellable   *cancellable,
+                                            GError        **error);
+
 G_END_DECLS
 
 #endif /* __GOA_UTILS_H__ */


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