]> git.0d.be Git - empathy.git/blobdiff - libempathy/empathy-server-sasl-handler.c
Use a flat namespace for internal includes
[empathy.git] / libempathy / empathy-server-sasl-handler.c
index 1518f3374345595302c964d2baaaea27ef6c05b0..b9b908ed7a4045a23d71c4d7737a867a9de11a8c 100644 (file)
 
 #include "empathy-server-sasl-handler.h"
 
-#include <telepathy-glib/telepathy-glib.h>
-
-#include <extensions/extensions.h>
-
-#include <string.h>
+#include "extensions.h"
 
 #define DEBUG_FLAG EMPATHY_DEBUG_SASL
 #include "empathy-debug.h"
 #include "empathy-keyring.h"
+#include "empathy-sasl-mechanisms.h"
 
 enum {
   PROP_CHANNEL = 1,
@@ -64,16 +61,6 @@ G_DEFINE_TYPE_WITH_CODE (EmpathyServerSASLHandler, empathy_server_sasl_handler,
     G_TYPE_OBJECT,
     G_IMPLEMENT_INTERFACE (G_TYPE_ASYNC_INITABLE, async_initable_iface_init));
 
-static const gchar *sasl_statuses[] = {
-  "not started",
-  "in progress",
-  "server succeeded",
-  "client accepted",
-  "succeeded",
-  "server failed",
-  "client failed",
-};
-
 static void
 empathy_server_sasl_handler_set_password_cb (GObject *source,
     GAsyncResult *result,
@@ -93,56 +80,6 @@ empathy_server_sasl_handler_set_password_cb (GObject *source,
     }
 }
 
-static void
-sasl_status_changed_cb (TpChannel *channel,
-    TpSASLStatus status,
-    const gchar *error,
-    GHashTable *details,
-    gpointer user_data,
-    GObject *weak_object)
-{
-  EmpathyServerSASLHandler *self = EMPATHY_SERVER_SASL_HANDLER (weak_object);
-  EmpathyServerSASLHandlerPriv *priv = EMPATHY_SERVER_SASL_HANDLER (weak_object)->priv;
-
-  /* buh boh */
-  if (status >= G_N_ELEMENTS (sasl_statuses))
-    {
-      DEBUG ("SASL status changed to unknown status");
-      return;
-    }
-
-  DEBUG ("SASL status changed to '%s'", sasl_statuses[status]);
-
-  if (status == TP_SASL_STATUS_SERVER_SUCCEEDED)
-    {
-      if (priv->save_password)
-        {
-          DEBUG ("Saving password in keyring");
-
-          empathy_keyring_set_account_password_async (priv->account,
-              priv->password, empathy_server_sasl_handler_set_password_cb,
-              NULL);
-        }
-
-      DEBUG ("Calling AcceptSASL");
-      tp_cli_channel_interface_sasl_authentication_call_accept_sasl (
-          priv->channel, -1, NULL, NULL, NULL, NULL);
-    }
-  else if (status == TP_SASL_STATUS_SUCCEEDED)
-    {
-      DEBUG ("SASL succeeded, calling Close");
-      tp_cli_channel_call_close (priv->channel, -1,
-          NULL, NULL, NULL, NULL);
-    }
-  else if (status == TP_SASL_STATUS_SERVER_FAILED)
-   {
-     if (!tp_strdiff (error, TP_ERROR_STR_AUTHENTICATION_FAILED))
-       {
-         g_signal_emit (self, signals[AUTH_PASSWORD_FAILED], 0, priv->password);
-       }
-   }
-}
-
 static gboolean
 empathy_server_sasl_handler_give_password (gpointer data)
 {
@@ -236,9 +173,6 @@ empathy_server_sasl_handler_constructed (GObject *object)
   EmpathyServerSASLHandlerPriv *priv = EMPATHY_SERVER_SASL_HANDLER (object)->priv;
   GError *error = NULL;
 
-  tp_cli_channel_interface_sasl_authentication_connect_to_sasl_status_changed (
-      priv->channel, sasl_status_changed_cb, NULL, NULL, object, &error);
-
   if (error != NULL)
     {
       DEBUG ("Failed to connect to SASLStatusChanged: %s", error->message);
@@ -402,18 +336,54 @@ empathy_server_sasl_handler_new_async (TpAccount *account,
 }
 
 static void
-start_mechanism_with_data_cb (TpChannel *proxy,
-    const GError *error,
-    gpointer user_data,
-    GObject *weak_object)
+auth_cb (GObject *source,
+    GAsyncResult *result,
+    gpointer user_data)
 {
-  if (error != NULL)
+  EmpathyServerSASLHandler *self = user_data;
+  EmpathyServerSASLHandlerPriv *priv = self->priv;
+  GError *error = NULL;
+
+  if (!empathy_sasl_auth_finish (priv->channel, result, &error))
+    {
+      if (g_error_matches (error, TP_ERROR, TP_ERROR_AUTHENTICATION_FAILED))
+        {
+          g_signal_emit (self, signals[AUTH_PASSWORD_FAILED], 0, priv->password);
+        }
+      g_clear_error (&error);
+    }
+  else
+    {
+      DEBUG ("Saving password in keyring");
+      empathy_keyring_set_account_password_async (priv->account,
+          priv->password, priv->save_password,
+          empathy_server_sasl_handler_set_password_cb,
+          NULL);
+    }
+
+  tp_channel_close_async (priv->channel, NULL, NULL);
+  g_object_unref (self);
+}
+
+static gboolean
+channel_has_may_save_response (TpChannel *channel)
+{
+  /* determine if we are permitted to save the password locally */
+  GVariant *props;
+  gboolean may_save_response;
+
+  props = tp_channel_dup_immutable_properties (channel);
+
+  if (!g_variant_lookup (props,
+        TP_PROP_CHANNEL_INTERFACE_SASL_AUTHENTICATION_MAY_SAVE_RESPONSE,
+        "b", &may_save_response))
     {
-      DEBUG ("Failed to start mechanism: %s", error->message);
-      return;
+      DEBUG ("MaySaveResponse unknown, assuming TRUE");
+      may_save_response = TRUE;
     }
 
-  DEBUG ("Started mechanism successfully");
+  g_variant_unref (props);
+  return may_save_response;
 }
 
 void
@@ -423,39 +393,18 @@ empathy_server_sasl_handler_provide_password (
     gboolean remember)
 {
   EmpathyServerSASLHandlerPriv *priv;
-  GArray *array;
-  gboolean may_save_response, may_save_response_valid;
+  gboolean may_save_response;
 
   g_return_if_fail (EMPATHY_IS_SERVER_SASL_HANDLER (handler));
 
   priv = handler->priv;
 
-  array = g_array_sized_new (TRUE, FALSE,
-      sizeof (gchar), strlen (password));
-
-  g_array_append_vals (array, password, strlen (password));
-
-  DEBUG ("Calling StartMechanismWithData with our password");
-
-  tp_cli_channel_interface_sasl_authentication_call_start_mechanism_with_data (
-      priv->channel, -1, "X-TELEPATHY-PASSWORD", array,
-      start_mechanism_with_data_cb, NULL, NULL, G_OBJECT (handler));
-
-  g_array_unref (array);
+  empathy_sasl_auth_password_async (priv->channel, password,
+      auth_cb, g_object_ref (handler));
 
   DEBUG ("%sremembering the password", remember ? "" : "not ");
 
-  /* determine if we are permitted to save the password locally */
-  may_save_response = tp_asv_get_boolean (
-      tp_channel_borrow_immutable_properties (priv->channel),
-      TP_PROP_CHANNEL_INTERFACE_SASL_AUTHENTICATION_MAY_SAVE_RESPONSE,
-      &may_save_response_valid);
-
-  if (!may_save_response_valid)
-    {
-      DEBUG ("MaySaveResponse unknown, assuming TRUE");
-      may_save_response = TRUE;
-    }
+  may_save_response = channel_has_may_save_response (priv->channel);
 
   if (remember)
     {
@@ -559,24 +508,14 @@ empathy_server_sasl_handler_can_save_response_somewhere (
     EmpathyServerSASLHandler *self)
 {
   EmpathyServerSASLHandlerPriv *priv;
-  gboolean may_save_response, may_save_response_valid;
+  gboolean may_save_response;
   gboolean has_storage_iface;
 
   g_return_val_if_fail (EMPATHY_IS_SERVER_SASL_HANDLER (self), FALSE);
 
   priv = self->priv;
 
-  /* determine if we are permitted to save the password locally */
-  may_save_response = tp_asv_get_boolean (
-      tp_channel_borrow_immutable_properties (priv->channel),
-      TP_PROP_CHANNEL_INTERFACE_SASL_AUTHENTICATION_MAY_SAVE_RESPONSE,
-      &may_save_response_valid);
-
-  if (!may_save_response_valid)
-    {
-      DEBUG ("MaySaveResponse unknown, assuming TRUE");
-      may_save_response = TRUE;
-    }
+  may_save_response = channel_has_may_save_response (priv->channel);
 
   has_storage_iface = tp_proxy_has_interface_by_id (priv->channel,
       EMP_IFACE_QUARK_CHANNEL_INTERFACE_CREDENTIALS_STORAGE);