]> git.0d.be Git - empathy.git/blobdiff - libempathy/empathy-sasl-mechanisms.c
Reorder header inclusions accordingly to the Telepathy coding style
[empathy.git] / libempathy / empathy-sasl-mechanisms.c
index 316e31b4e959c055ec6b35b46d4087431fa8dba1..a6829c4504adc508316fcd3fc30c0a8d6f7872f4 100644 (file)
  */
 
 #include "config.h"
+#include "empathy-sasl-mechanisms.h"
 
 #include <libsoup/soup.h>
-#include <string.h>
 
 #define DEBUG_FLAG EMPATHY_DEBUG_SASL
 #include "empathy-debug.h"
 #include "empathy-utils.h"
-#include "empathy-sasl-mechanisms.h"
 
 #define MECH_FACEBOOK "X-FACEBOOK-PLATFORM"
 #define MECH_WLM "X-MESSENGER-OAUTH2"
 #define MECH_GOOGLE "X-OAUTH2"
+#define MECH_PASSWORD "X-TELEPATHY-PASSWORD"
 
 typedef struct
 {
@@ -42,6 +42,10 @@ static SupportedMech supported_mechanisms[] = {
   { EMPATHY_SASL_MECHANISM_FACEBOOK, MECH_FACEBOOK },
   { EMPATHY_SASL_MECHANISM_WLM, MECH_WLM },
   { EMPATHY_SASL_MECHANISM_GOOGLE, MECH_GOOGLE },
+
+  /* Must be the last one, otherwise empathy_sasl_channel_select_mechanism()
+   * will prefer password over web auth for servers supporting both. */
+  { EMPATHY_SASL_MECHANISM_PASSWORD, MECH_PASSWORD }
 };
 
 static void
@@ -87,6 +91,7 @@ sasl_status_changed_cb (TpChannel *channel,
           DEBUG ("SASL failed: %s", error->message);
 
           g_simple_async_result_take_error (result, error);
+          g_simple_async_result_complete (result);
         }
         break;
 
@@ -296,6 +301,36 @@ empathy_sasl_auth_google_async (TpChannel *channel,
   g_object_unref (result);
 }
 
+void
+empathy_sasl_auth_password_async (TpChannel *channel,
+    const gchar *password,
+    GAsyncReadyCallback callback,
+    gpointer user_data)
+{
+  GSimpleAsyncResult *result;
+  GArray *password_array;
+
+  result = empathy_sasl_auth_common_async (channel, callback, user_data);
+
+  g_return_if_fail (result != NULL);
+  g_return_if_fail (empathy_sasl_channel_supports_mechanism (channel,
+      MECH_PASSWORD));
+  g_return_if_fail (!tp_str_empty (password));
+
+  DEBUG ("Start %s mechanism", MECH_PASSWORD);
+
+  password_array = g_array_sized_new (FALSE, FALSE, sizeof (gchar),
+      strlen (password));
+  g_array_append_vals (password_array, password, strlen (password));
+
+  tp_cli_channel_interface_sasl_authentication_call_start_mechanism_with_data (
+      channel, -1, MECH_PASSWORD, password_array,
+      generic_cb, g_object_ref (result), g_object_unref, NULL);
+
+  g_array_unref (password_array);
+  g_object_unref (result);
+}
+
 gboolean
 empathy_sasl_auth_finish (TpChannel *channel,
     GAsyncResult *result,
@@ -308,15 +343,22 @@ gboolean
 empathy_sasl_channel_supports_mechanism (TpChannel *channel,
     const gchar *mechanism)
 {
-  GHashTable *props;
-  const gchar * const *available_mechanisms;
+  GVariant *props;
+  GStrv available_mechanisms;
+  gboolean result;
 
-  props = tp_channel_borrow_immutable_properties (channel);
-  available_mechanisms = tp_asv_get_boxed (props,
+  props = tp_channel_dup_immutable_properties (channel);
+
+  g_variant_lookup (props,
       TP_PROP_CHANNEL_INTERFACE_SASL_AUTHENTICATION_AVAILABLE_MECHANISMS,
-      G_TYPE_STRV);
+      "^as", &available_mechanisms);
+
+  result = tp_strv_contains ((const gchar * const *) available_mechanisms,
+      mechanism);
 
-  return tp_strv_contains (available_mechanisms, mechanism);
+  g_variant_unref (props);
+  g_strfreev (available_mechanisms);
+  return result;
 }
 
 EmpathySaslMechanism