]> git.0d.be Git - empathy.git/blobdiff - libempathy/empathy-server-sasl-handler.c
individual-menu: remove link-contacts-activated signal
[empathy.git] / libempathy / empathy-server-sasl-handler.c
index 3cfc86fa8f66404122ebd8116172b7b5b6a35c5c..117d88f03880834291f642f06f26839c112a6f9d 100644 (file)
@@ -19,7 +19,9 @@
 
 #include "empathy-server-sasl-handler.h"
 
-#include <telepathy-glib/util.h>
+#include <telepathy-glib/telepathy-glib.h>
+
+#include <extensions/extensions.h>
 
 #include <string.h>
 
@@ -35,6 +37,7 @@ enum {
 
 /* signal enum */
 enum {
+  AUTH_PASSWORD_FAILED,
   INVALIDATED,
   LAST_SIGNAL,
 };
@@ -48,6 +51,7 @@ typedef struct {
   GSimpleAsyncResult *result;
 
   gchar *password;
+  gboolean save_password;
 
   GSimpleAsyncResult *async_init_res;
 } EmpathyServerSASLHandlerPriv;
@@ -68,6 +72,25 @@ static const gchar *sasl_statuses[] = {
   "client failed",
 };
 
+static void
+empathy_server_sasl_handler_set_password_cb (GObject *source,
+    GAsyncResult *result,
+    gpointer user_data)
+{
+  GError *error = NULL;
+
+  if (!empathy_keyring_set_account_password_finish (TP_ACCOUNT (source), result,
+          &error))
+    {
+      DEBUG ("Failed to set password: %s", error->message);
+      g_clear_error (&error);
+    }
+  else
+    {
+      DEBUG ("Password set successfully.");
+    }
+}
+
 static void
 sasl_status_changed_cb (TpChannel *channel,
     TpSASLStatus status,
@@ -76,6 +99,7 @@ sasl_status_changed_cb (TpChannel *channel,
     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 */
@@ -89,6 +113,15 @@ sasl_status_changed_cb (TpChannel *channel,
 
   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);
@@ -99,6 +132,13 @@ sasl_status_changed_cb (TpChannel *channel,
       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
@@ -124,7 +164,7 @@ empathy_server_sasl_handler_get_password_async_cb (GObject *source,
 
   priv = EMPATHY_SERVER_SASL_HANDLER (user_data)->priv;
 
-  password = empathy_keyring_get_password_finish (TP_ACCOUNT (source),
+  password = empathy_keyring_get_account_password_finish (TP_ACCOUNT (source),
       result, &error);
 
   if (password != NULL)
@@ -155,7 +195,7 @@ empathy_server_sasl_handler_init_async (GAsyncInitable *initable,
   priv->async_init_res = g_simple_async_result_new (G_OBJECT (self),
       callback, user_data, empathy_server_sasl_handler_new_async);
 
-  empathy_keyring_get_password_async (priv->account,
+  empathy_keyring_get_account_password_async (priv->account,
       empathy_server_sasl_handler_get_password_async_cb, self);
 }
 
@@ -286,7 +326,7 @@ empathy_server_sasl_handler_class_init (EmpathyServerSASLHandlerClass *klass)
   oclass->get_property = empathy_server_sasl_handler_get_property;
   oclass->set_property = empathy_server_sasl_handler_set_property;
   oclass->dispose = empathy_server_sasl_handler_dispose;
-  oclass->dispose = empathy_server_sasl_handler_finalize;
+  oclass->finalize = empathy_server_sasl_handler_finalize;
 
   g_type_class_add_private (klass, sizeof (EmpathyServerSASLHandlerPriv));
 
@@ -302,11 +342,18 @@ empathy_server_sasl_handler_class_init (EmpathyServerSASLHandlerClass *klass)
       G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS);
   g_object_class_install_property (oclass, PROP_ACCOUNT, pspec);
 
+  signals[AUTH_PASSWORD_FAILED] = g_signal_new ("auth-password-failed",
+      G_TYPE_FROM_CLASS (klass),
+      G_SIGNAL_RUN_LAST, 0,
+      NULL, NULL,
+      g_cclosure_marshal_generic,
+      G_TYPE_NONE, 1, G_TYPE_STRING);
+
   signals[INVALIDATED] = g_signal_new ("invalidated",
       G_TYPE_FROM_CLASS (klass),
       G_SIGNAL_RUN_LAST, 0,
       NULL, NULL,
-      g_cclosure_marshal_VOID__VOID,
+      g_cclosure_marshal_generic,
       G_TYPE_NONE, 0);
 }
 
@@ -367,25 +414,6 @@ start_mechanism_with_data_cb (TpChannel *proxy,
   DEBUG ("Started mechanism successfully");
 }
 
-static void
-empathy_server_sasl_handler_set_password_cb (GObject *source,
-    GAsyncResult *result,
-    gpointer user_data)
-{
-  GError *error = NULL;
-
-  if (!empathy_keyring_set_password_finish (TP_ACCOUNT (source), result,
-          &error))
-    {
-      DEBUG ("Failed to set password: %s", error->message);
-      g_clear_error (&error);
-    }
-  else
-    {
-      DEBUG ("Password set successfully.");
-    }
-}
-
 void
 empathy_server_sasl_handler_provide_password (
     EmpathyServerSASLHandler *handler,
@@ -394,6 +422,7 @@ empathy_server_sasl_handler_provide_password (
 {
   EmpathyServerSASLHandlerPriv *priv;
   GArray *array;
+  gboolean may_save_response, may_save_response_valid;
 
   g_return_if_fail (EMPATHY_IS_SERVER_SASL_HANDLER (handler));
 
@@ -414,10 +443,52 @@ empathy_server_sasl_handler_provide_password (
 
   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;
+    }
+
   if (remember)
     {
-      empathy_keyring_set_password_async (priv->account, password,
-          empathy_server_sasl_handler_set_password_cb, NULL);
+      if (may_save_response)
+        {
+          g_free (priv->password);
+
+          /* We'll save the password if we manage to connect */
+          priv->password = g_strdup (password);
+          priv->save_password = TRUE;
+        }
+      else if (tp_proxy_has_interface_by_id (priv->channel,
+            EMP_IFACE_QUARK_CHANNEL_INTERFACE_CREDENTIALS_STORAGE))
+        {
+          DEBUG ("Channel implements Ch.I.CredentialsStorage");
+        }
+      else
+        {
+          DEBUG ("Asked to remember password, but doing so is not permitted");
+        }
+    }
+
+  if (!may_save_response)
+    {
+      /* delete any password present, it shouldn't be there */
+      empathy_keyring_delete_account_password_async (priv->account, NULL, NULL);
+    }
+
+  /* Additionally, if we implement Ch.I.CredentialsStorage, inform that
+   * whether we want to remember the password */
+  if (tp_proxy_has_interface_by_id (priv->channel,
+        EMP_IFACE_QUARK_CHANNEL_INTERFACE_CREDENTIALS_STORAGE))
+    {
+      emp_cli_channel_interface_credentials_storage_call_store_credentials (
+          TP_PROXY (priv->channel), -1, remember, NULL, NULL, NULL, NULL);
     }
 }
 
@@ -450,6 +521,18 @@ empathy_server_sasl_handler_get_account (EmpathyServerSASLHandler *handler)
   return priv->account;
 }
 
+TpChannel *
+empathy_server_sasl_handler_get_channel (EmpathyServerSASLHandler *handler)
+{
+  EmpathyServerSASLHandlerPriv *priv;
+
+  g_return_val_if_fail (EMPATHY_IS_SERVER_SASL_HANDLER (handler), NULL);
+
+  priv = handler->priv;
+
+  return priv->channel;
+}
+
 gboolean
 empathy_server_sasl_handler_has_password (EmpathyServerSASLHandler *handler)
 {
@@ -461,3 +544,40 @@ empathy_server_sasl_handler_has_password (EmpathyServerSASLHandler *handler)
 
   return (priv->password != NULL);
 }
+
+/**
+ * empathy_server_sasl_handler_can_save_response_somewhere:
+ * @self:
+ *
+ * Returns: %TRUE if the response can be saved somewhere, either the keyring
+ *   or via Ch.I.CredentialsStorage
+ */
+gboolean
+empathy_server_sasl_handler_can_save_response_somewhere (
+    EmpathyServerSASLHandler *self)
+{
+  EmpathyServerSASLHandlerPriv *priv;
+  gboolean may_save_response, may_save_response_valid;
+  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;
+    }
+
+  has_storage_iface = tp_proxy_has_interface_by_id (priv->channel,
+      EMP_IFACE_QUARK_CHANNEL_INTERFACE_CREDENTIALS_STORAGE);
+
+  return may_save_response || has_storage_iface;
+}