#include "empathy-server-sasl-handler.h"
-#include <telepathy-glib/util.h>
+#include <telepathy-glib/telepathy-glib.h>
+
+#include <extensions/extensions.h>
#include <string.h>
/* signal enum */
enum {
+ AUTH_PASSWORD_FAILED,
INVALIDATED,
LAST_SIGNAL,
};
GSimpleAsyncResult *result;
gchar *password;
+ gboolean save_password;
GSimpleAsyncResult *async_init_res;
} EmpathyServerSASLHandlerPriv;
"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,
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 == 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);
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
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)
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);
}
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));
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);
}
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,
{
EmpathyServerSASLHandlerPriv *priv;
GArray *array;
+ gboolean may_save_response, may_save_response_valid;
g_return_if_fail (EMPATHY_IS_SERVER_SASL_HANDLER (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;
+ }
+
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);
}
}
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)
{
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;
+}