[gnome-online-accounts] oauthprovider: Provide better error messages for request token failures



commit 24e84de87662ada747945f2c08879aed9dc9943d
Author: Debarshi Ray <debarshir gnome org>
Date:   Thu Apr 12 22:24:59 2012 +0200

    oauthprovider: Provide better error messages for request token failures
    
    Added a parse_request_token_error pure virtual method to
    GoaOAuthProvider to let the providers parse the error and provide a
    human readable string. Currently, this has only been implemented in
    the GoaGoogleProvider.
    
    Fixes: https://bugzilla.gnome.org/673959

 src/examples/Makefile.am             |    2 +-
 src/goabackend/goa-backend-1.0.pc.in |    2 +-
 src/goabackend/goagoogleprovider.c   |   21 +++++++++++++++++
 src/goabackend/goaoauthprovider.c    |   41 ++++++++++++++++++++++++++++-----
 src/goabackend/goaoauthprovider.h    |    7 +++++
 5 files changed, 64 insertions(+), 9 deletions(-)
---
diff --git a/src/examples/Makefile.am b/src/examples/Makefile.am
index 829c9e5..d383f1f 100644
--- a/src/examples/Makefile.am
+++ b/src/examples/Makefile.am
@@ -17,7 +17,7 @@ INCLUDES = 							\
 noinst_PROGRAMS = list-providers list-accounts tweet
 
 list_providers_SOURCES = list-providers.c
-list_providers_CFLAGS = $(GLIB_CFLAGS) $(GTK_CFLAGS)
+list_providers_CFLAGS = $(GLIB_CFLAGS) $(GTK_CFLAGS) $(REST_CFLAGS)
 list_providers_LDADD = $(GLIB_LIBS) $(GTK_LIBS) ../goabackend/libgoa-backend-1.0.la ../goa/libgoa-1.0.la
 
 list_accounts_SOURCES = list-accounts.c
diff --git a/src/goabackend/goa-backend-1.0.pc.in b/src/goabackend/goa-backend-1.0.pc.in
index 7a0e8ff..185b09f 100644
--- a/src/goabackend/goa-backend-1.0.pc.in
+++ b/src/goabackend/goa-backend-1.0.pc.in
@@ -6,6 +6,6 @@ includedir= includedir@
 Name: Goa Backends
 Description: Backends for GNOME Online Accounts Library
 Version: @VERSION@
-Requires: goa-1.0,gtk+-3.0
+Requires: goa-1.0,gtk+-3.0,rest-0.7
 Libs: -L${libdir} -lgoa-backend-1.0
 Cflags: -I${includedir}/goa-1.0
diff --git a/src/goabackend/goagoogleprovider.c b/src/goabackend/goagoogleprovider.c
index 92c0958..57cbb60 100644
--- a/src/goabackend/goagoogleprovider.c
+++ b/src/goabackend/goagoogleprovider.c
@@ -263,6 +263,26 @@ get_identity_sync (GoaOAuthProvider  *provider,
 
 /* ---------------------------------------------------------------------------------------------------- */
 
+static gchar *
+parse_request_token_error (GoaOAuthProvider *provider, RestProxyCall *call)
+{
+  const gchar *payload;
+  gchar *msg;
+  guint status;
+
+  msg = NULL;
+
+  payload = rest_proxy_call_get_payload (call);
+  status = rest_proxy_call_get_status_code (call);
+
+  if (status == 400 && g_str_has_prefix (payload, "Timestamp is too far from current time"))
+    msg = g_strdup (_("Your system time is invalid. Check your date and time settings."));
+
+  return msg;
+}
+
+/* ---------------------------------------------------------------------------------------------------- */
+
 static gboolean
 build_object (GoaProvider         *provider,
               GoaObjectSkeleton   *object,
@@ -544,6 +564,7 @@ goa_google_provider_class_init (GoaGoogleProviderClass *klass)
   oauth_class->build_authorization_uri  = build_authorization_uri;
   oauth_class->get_use_external_browser = get_use_external_browser;
   oauth_class->add_account_key_values   = add_account_key_values;
+  oauth_class->parse_request_token_error = parse_request_token_error;
 }
 
 /* ---------------------------------------------------------------------------------------------------- */
diff --git a/src/goabackend/goaoauthprovider.c b/src/goabackend/goaoauthprovider.c
index 15249b9..01b8462 100644
--- a/src/goabackend/goaoauthprovider.c
+++ b/src/goabackend/goaoauthprovider.c
@@ -401,6 +401,29 @@ goa_oauth_provider_get_identity_sync (GoaOAuthProvider *provider,
   return GOA_OAUTH_PROVIDER_GET_CLASS (provider)->get_identity_sync (provider, access_token, access_token_secret, out_presentation_identity, cancellable, error);
 }
 
+/**
+ * goa_oauth_provider_parse_request_token_error:
+ * @provider: A #GoaOAuthProvider.
+ * @call: The #RestProxyCall that was used to fetch the request token.
+ *
+ * Tries to parse the headers and payload within @call to provide a
+ * human readable error message in case the request token could not
+ * be fetched.
+ *
+ * This is a pure virtual method - a subclass must provide an
+ * implementation.
+ *
+ * Returns: A human readable error message or %NULL if the cause of the
+ * error could not be determined. The returned string must be freed with
+ * g_free().
+ */
+gchar *
+goa_oauth_provider_parse_request_token_error (GoaOAuthProvider *provider, RestProxyCall *call)
+{
+  g_return_val_if_fail (GOA_IS_OAUTH_PROVIDER (provider), NULL);
+  return GOA_OAUTH_PROVIDER_GET_CLASS (provider)->parse_request_token_error (provider, call);
+}
+
 /* ---------------------------------------------------------------------------------------------------- */
 
 static gchar *
@@ -718,13 +741,17 @@ get_tokens_and_identity (GoaOAuthProvider *provider,
 
   if (rest_proxy_call_get_status_code (call) != 200)
     {
-      g_set_error (&data.error,
-                   GOA_ERROR,
-                   GOA_ERROR_FAILED,
-                   /* Translators: the %d is a HTTP status code and the %s is a textual description of it */
-                   _("Expected status 200 for getting a Request Token, instead got status %d (%s)"),
-                   rest_proxy_call_get_status_code (call),
-                   rest_proxy_call_get_status_message (call));
+      gchar *msg;
+
+      msg = goa_oauth_provider_parse_request_token_error (provider, call);
+      if (msg == NULL)
+        /* Translators: the %d is a HTTP status code and the %s is a textual description of it */
+        msg = g_strdup_printf (_("Expected status 200 for getting a Request Token, instead got status %d (%s)"),
+                               rest_proxy_call_get_status_code (call),
+                               rest_proxy_call_get_status_message (call));
+
+      g_set_error_literal (&data.error, GOA_ERROR, GOA_ERROR_FAILED, msg);
+      g_free (msg);
       goto out;
     }
   f = soup_form_decode (rest_proxy_call_get_payload (call));
diff --git a/src/goabackend/goaoauthprovider.h b/src/goabackend/goaoauthprovider.h
index 05a47d0..3b6519e 100644
--- a/src/goabackend/goaoauthprovider.h
+++ b/src/goabackend/goaoauthprovider.h
@@ -29,6 +29,7 @@
 
 #include <goabackend/goabackendtypes.h>
 #include <goabackend/goaprovider.h>
+#include <rest/rest-proxy-call.h>
 
 G_BEGIN_DECLS
 
@@ -67,6 +68,7 @@ struct _GoaOAuthProvider
  * @get_callback_uri: Virtual function for goa_oauth_provider_get_callback_uri().
  * @get_authentication_cookie: Virtual function for goa_oauth_provider_get_authentication_cookie().
  * @get_identity_sync: Virtual function for goa_oauth_provider_get_identity_sync().
+ * @parse_request_token_error: Virtual function for goa_oauth_provider_parse_request_token_error().
  * @build_authorization_uri: Virtual function for goa_oauth_provider_build_authorization_uri().
  * @get_use_external_browser: Virtual function for goa_oauth_provider_get_use_external_browser().
  * @get_request_uri_params: Virtual function for goa_oauth_provider_get_request_uri_params().
@@ -94,6 +96,9 @@ struct _GoaOAuthProviderClass
                                             GCancellable      *cancellable,
                                             GError           **error);
 
+  gchar       *(*parse_request_token_error) (GoaOAuthProvider *provider,
+                                             RestProxyCall    *call);
+
   /* virtual but with default implementation */
   gchar       *(*build_authorization_uri)  (GoaOAuthProvider  *provider,
                                             const gchar       *authorization_uri,
@@ -123,6 +128,8 @@ gchar       *goa_oauth_provider_get_identity_sync        (GoaOAuthProvider  *pro
                                                           gchar            **out_presentation_identity,
                                                           GCancellable      *cancellable,
                                                           GError           **error);
+gchar       *goa_oauth_provider_parse_request_token_error (GoaOAuthProvider *provider,
+                                                           RestProxyCall    *call);
 gchar       *goa_oauth_provider_get_access_token_sync    (GoaOAuthProvider  *provider,
                                                           GoaObject         *object,
                                                           gboolean           force_refresh,



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