[librest] Add RestProxy::authenticate signal



commit a61a286cf9d875c462d6d06596afb234314319aa
Author: Christophe Fergeau <cfergeau redhat com>
Date:   Tue Jun 12 10:46:52 2012 +0200

    Add RestProxy::authenticate signal
    
    If caught by application, this signal can be used to set the
    credentials to use when authentication is needed. If not caught,
    librest behaviour will be unchanged (try to use what the
    username/password properties were set to first, and don't try to reuse
    them if this fails).
    
    https://bugzilla.gnome.org/show_bug.cgi?id=658937

 rest/rest-proxy.c |   55 ++++++++++++++++++++++++++++++++++++++++++++++++----
 rest/rest-proxy.h |    3 +-
 2 files changed, 52 insertions(+), 6 deletions(-)
---
diff --git a/rest/rest-proxy.c b/rest/rest-proxy.c
index 4aed44d..1dc18bd 100644
--- a/rest/rest-proxy.c
+++ b/rest/rest-proxy.c
@@ -62,6 +62,14 @@ enum
   PROP_SSL_STRICT
 };
 
+enum {
+  AUTHENTICATE,
+  LAST_SIGNAL
+};
+
+static guint signals[LAST_SIGNAL] = { 0 };
+
+
 static gboolean _rest_proxy_simple_run_valist (RestProxy *proxy, 
                                                char     **payload, 
                                                goffset   *len,
@@ -191,6 +199,16 @@ rest_proxy_dispose (GObject *object)
   G_OBJECT_CLASS (rest_proxy_parent_class)->dispose (object);
 }
 
+static gboolean
+default_authenticate_cb (RestProxy *self, gboolean retrying)
+{
+  /* We only want to try the credentials once, otherwise we get in an
+   * infinite loop with failed credentials, retrying the same invalid
+   * ones again and again
+   */
+  return !retrying;
+}
+
 static void
 authenticate (RestProxy   *self,
               SoupMessage *msg,
@@ -198,10 +216,12 @@ authenticate (RestProxy   *self,
               gboolean     retrying,
               SoupSession *session)
 {
-    RestProxyPrivate *priv = GET_PRIVATE (self);
+  RestProxyPrivate *priv = GET_PRIVATE (self);
+  gboolean try_auth;
 
-    if (!retrying)
-      soup_auth_authenticate (auth, priv->username, priv->password);
+  g_signal_emit(self, signals[AUTHENTICATE], 0, retrying, &try_auth);
+  if (try_auth)
+    soup_auth_authenticate (auth, priv->username, priv->password);
 }
 
 static void
@@ -299,7 +319,7 @@ rest_proxy_class_init (RestProxyClass *klass)
                                "username",
                                "The username for authentication",
                                NULL,
-                               G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS);
+                               G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
   g_object_class_install_property (object_class,
                                    PROP_USERNAME,
                                    pspec);
@@ -308,7 +328,7 @@ rest_proxy_class_init (RestProxyClass *klass)
                                "password",
                                "The password for authentication",
                                NULL,
-                               G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS);
+                               G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
   g_object_class_install_property (object_class,
                                    PROP_PASSWORD,
                                    pspec);
@@ -321,6 +341,31 @@ rest_proxy_class_init (RestProxyClass *klass)
   g_object_class_install_property (object_class,
                                    PROP_SSL_STRICT,
                                    pspec);
+
+  /**
+   * RestProxy::authenticate:
+   * @proxy: the proxy
+   * @retrying: %TRUE if this is the second (or later) attempt
+   *
+   * Emitted when the proxy requires authentication. If
+   * credentials are available, set the 'username' and 'password'
+   * properties on @proxy and return TRUE from the callback.
+   * This will cause the signal emission to stop, and librest will
+   * try to connect with these credentials
+   * If these credentials fail, the signal will be
+   * emitted again, with @retrying set to %TRUE, which will
+   * continue until FALSE is returned from the callback.
+   **/
+  signals[AUTHENTICATE] =
+      g_signal_new ("authenticate",
+                    G_OBJECT_CLASS_TYPE (object_class),
+                    G_SIGNAL_RUN_LAST,
+                    G_STRUCT_OFFSET (RestProxyClass, authenticate),
+                    g_signal_accumulator_true_handled, NULL, NULL,
+                    G_TYPE_BOOLEAN, 1,
+                    G_TYPE_BOOLEAN);
+
+  proxy_class->authenticate = default_authenticate_cb;
 }
 
 static void
diff --git a/rest/rest-proxy.h b/rest/rest-proxy.h
index 342ce59..077b134 100644
--- a/rest/rest-proxy.h
+++ b/rest/rest-proxy.h
@@ -77,10 +77,11 @@ struct _RestProxyClass {
   RestProxyCall *(*new_call)(RestProxy *proxy);
   gboolean (*simple_run_valist)(RestProxy *proxy, gchar **payload,
       goffset *len, GError **error, va_list params);
+  gboolean (*authenticate)(RestProxy *proxy, gboolean retrying);
 
   /*< private >*/
   /* padding for future expansion */
-  gpointer _padding_dummy[8];
+  gpointer _padding_dummy[7];
 };
 
 #define REST_PROXY_ERROR rest_proxy_error_quark ()



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