2 * empathy-server-sasl-handler.c - Source for EmpathyServerSASLHandler
3 * Copyright (C) 2010 Collabora Ltd.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 #include "empathy-server-sasl-handler.h"
22 #include <telepathy-glib/telepathy-glib.h>
24 #include <extensions/extensions.h>
28 #define DEBUG_FLAG EMPATHY_DEBUG_SASL
29 #include "empathy-debug.h"
30 #include "empathy-keyring.h"
44 static guint signals[LAST_SIGNAL] = {0};
50 GSimpleAsyncResult *result;
53 gboolean save_password;
55 GSimpleAsyncResult *async_init_res;
56 } EmpathyServerSASLHandlerPriv;
58 static void async_initable_iface_init (GAsyncInitableIface *iface);
60 G_DEFINE_TYPE_WITH_CODE (EmpathyServerSASLHandler, empathy_server_sasl_handler,
62 G_IMPLEMENT_INTERFACE (G_TYPE_ASYNC_INITABLE, async_initable_iface_init));
64 static const gchar *sasl_statuses[] = {
75 empathy_server_sasl_handler_set_password_cb (GObject *source,
81 if (!empathy_keyring_set_account_password_finish (TP_ACCOUNT (source), result,
84 DEBUG ("Failed to set password: %s", error->message);
85 g_clear_error (&error);
89 DEBUG ("Password set successfully.");
94 sasl_status_changed_cb (TpChannel *channel,
101 EmpathyServerSASLHandlerPriv *priv = EMPATHY_SERVER_SASL_HANDLER (weak_object)->priv;
104 if (status >= G_N_ELEMENTS (sasl_statuses))
106 DEBUG ("SASL status changed to unknown status");
110 DEBUG ("SASL status changed to '%s'", sasl_statuses[status]);
112 if (status == TP_SASL_STATUS_SERVER_SUCCEEDED)
114 if (priv->save_password)
116 DEBUG ("Saving password in keyring");
118 empathy_keyring_set_account_password_async (priv->account,
119 priv->password, empathy_server_sasl_handler_set_password_cb,
123 DEBUG ("Calling AcceptSASL");
124 tp_cli_channel_interface_sasl_authentication_call_accept_sasl (
125 priv->channel, -1, NULL, NULL, NULL, NULL);
127 else if (status == TP_SASL_STATUS_SUCCEEDED)
129 DEBUG ("SASL succeeded, calling Close");
130 tp_cli_channel_call_close (priv->channel, -1,
131 NULL, NULL, NULL, NULL);
136 empathy_server_sasl_handler_give_password (gpointer data)
138 EmpathyServerSASLHandler *self = data;
139 EmpathyServerSASLHandlerPriv *priv = self->priv;
141 empathy_server_sasl_handler_provide_password (self,
142 priv->password, FALSE);
148 empathy_server_sasl_handler_get_password_async_cb (GObject *source,
149 GAsyncResult *result,
152 EmpathyServerSASLHandlerPriv *priv;
153 const gchar *password;
154 GError *error = NULL;
156 priv = EMPATHY_SERVER_SASL_HANDLER (user_data)->priv;
158 password = empathy_keyring_get_account_password_finish (TP_ACCOUNT (source),
161 if (password != NULL)
163 priv->password = g_strdup (password);
165 /* Do this in an idle so the async result will get there
167 g_idle_add (empathy_server_sasl_handler_give_password, user_data);
170 g_simple_async_result_complete (priv->async_init_res);
171 tp_clear_object (&priv->async_init_res);
175 empathy_server_sasl_handler_init_async (GAsyncInitable *initable,
177 GCancellable *cancellable,
178 GAsyncReadyCallback callback,
181 EmpathyServerSASLHandler *self = EMPATHY_SERVER_SASL_HANDLER (initable);
182 EmpathyServerSASLHandlerPriv *priv = self->priv;
184 g_assert (priv->account != NULL);
186 priv->async_init_res = g_simple_async_result_new (G_OBJECT (self),
187 callback, user_data, empathy_server_sasl_handler_new_async);
189 empathy_keyring_get_account_password_async (priv->account,
190 empathy_server_sasl_handler_get_password_async_cb, self);
194 empathy_server_sasl_handler_init_finish (GAsyncInitable *initable,
198 if (g_simple_async_result_propagate_error (G_SIMPLE_ASYNC_RESULT (res),
206 async_initable_iface_init (GAsyncInitableIface *iface)
208 iface->init_async = empathy_server_sasl_handler_init_async;
209 iface->init_finish = empathy_server_sasl_handler_init_finish;
213 channel_invalidated_cb (TpProxy *proxy,
217 EmpathyServerSASLHandler *self)
219 g_signal_emit (self, signals[INVALIDATED], 0);
223 empathy_server_sasl_handler_constructed (GObject *object)
225 EmpathyServerSASLHandlerPriv *priv = EMPATHY_SERVER_SASL_HANDLER (object)->priv;
226 GError *error = NULL;
228 tp_cli_channel_interface_sasl_authentication_connect_to_sasl_status_changed (
229 priv->channel, sasl_status_changed_cb, NULL, NULL, object, &error);
233 DEBUG ("Failed to connect to SASLStatusChanged: %s", error->message);
234 g_clear_error (&error);
237 tp_g_signal_connect_object (priv->channel, "invalidated",
238 G_CALLBACK (channel_invalidated_cb), object, 0);
242 empathy_server_sasl_handler_get_property (GObject *object,
247 EmpathyServerSASLHandlerPriv *priv = EMPATHY_SERVER_SASL_HANDLER (object)->priv;
252 g_value_set_object (value, priv->channel);
255 g_value_set_object (value, priv->account);
258 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
264 empathy_server_sasl_handler_set_property (GObject *object,
269 EmpathyServerSASLHandlerPriv *priv = EMPATHY_SERVER_SASL_HANDLER (object)->priv;
274 priv->channel = g_value_dup_object (value);
277 priv->account = g_value_dup_object (value);
280 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
286 empathy_server_sasl_handler_dispose (GObject *object)
288 EmpathyServerSASLHandlerPriv *priv = EMPATHY_SERVER_SASL_HANDLER (object)->priv;
290 DEBUG ("%p", object);
292 tp_clear_object (&priv->channel);
293 tp_clear_object (&priv->account);
295 G_OBJECT_CLASS (empathy_server_sasl_handler_parent_class)->dispose (object);
299 empathy_server_sasl_handler_finalize (GObject *object)
301 EmpathyServerSASLHandlerPriv *priv = EMPATHY_SERVER_SASL_HANDLER (object)->priv;
303 DEBUG ("%p", object);
305 tp_clear_pointer (&priv->password, g_free);
307 G_OBJECT_CLASS (empathy_server_sasl_handler_parent_class)->finalize (object);
311 empathy_server_sasl_handler_class_init (EmpathyServerSASLHandlerClass *klass)
313 GObjectClass *oclass = G_OBJECT_CLASS (klass);
316 oclass->constructed = empathy_server_sasl_handler_constructed;
317 oclass->get_property = empathy_server_sasl_handler_get_property;
318 oclass->set_property = empathy_server_sasl_handler_set_property;
319 oclass->dispose = empathy_server_sasl_handler_dispose;
320 oclass->finalize = empathy_server_sasl_handler_finalize;
322 g_type_class_add_private (klass, sizeof (EmpathyServerSASLHandlerPriv));
324 pspec = g_param_spec_object ("channel", "The TpChannel",
325 "The TpChannel this handler is supposed to handle.",
327 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS);
328 g_object_class_install_property (oclass, PROP_CHANNEL, pspec);
330 pspec = g_param_spec_object ("account", "The TpAccount",
331 "The TpAccount this channel belongs to.",
333 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS);
334 g_object_class_install_property (oclass, PROP_ACCOUNT, pspec);
336 signals[INVALIDATED] = g_signal_new ("invalidated",
337 G_TYPE_FROM_CLASS (klass),
338 G_SIGNAL_RUN_LAST, 0,
340 g_cclosure_marshal_VOID__VOID,
345 empathy_server_sasl_handler_init (EmpathyServerSASLHandler *self)
347 self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
348 EMPATHY_TYPE_SERVER_SASL_HANDLER, EmpathyServerSASLHandlerPriv);
351 EmpathyServerSASLHandler *
352 empathy_server_sasl_handler_new_finish (GAsyncResult *result,
355 GObject *object, *source_object;
357 source_object = g_async_result_get_source_object (result);
359 object = g_async_initable_new_finish (G_ASYNC_INITABLE (source_object),
361 g_object_unref (source_object);
364 return EMPATHY_SERVER_SASL_HANDLER (object);
370 empathy_server_sasl_handler_new_async (TpAccount *account,
372 GAsyncReadyCallback callback,
375 g_return_if_fail (TP_IS_ACCOUNT (account));
376 g_return_if_fail (TP_IS_CHANNEL (channel));
377 g_return_if_fail (callback != NULL);
379 g_async_initable_new_async (EMPATHY_TYPE_SERVER_SASL_HANDLER,
380 G_PRIORITY_DEFAULT, NULL, callback, user_data,
387 start_mechanism_with_data_cb (TpChannel *proxy,
390 GObject *weak_object)
394 DEBUG ("Failed to start mechanism: %s", error->message);
398 DEBUG ("Started mechanism successfully");
402 empathy_server_sasl_handler_provide_password (
403 EmpathyServerSASLHandler *handler,
404 const gchar *password,
407 EmpathyServerSASLHandlerPriv *priv;
409 gboolean may_save_response, may_save_response_valid;
411 g_return_if_fail (EMPATHY_IS_SERVER_SASL_HANDLER (handler));
413 priv = handler->priv;
415 array = g_array_sized_new (TRUE, FALSE,
416 sizeof (gchar), strlen (password));
418 g_array_append_vals (array, password, strlen (password));
420 DEBUG ("Calling StartMechanismWithData with our password");
422 tp_cli_channel_interface_sasl_authentication_call_start_mechanism_with_data (
423 priv->channel, -1, "X-TELEPATHY-PASSWORD", array,
424 start_mechanism_with_data_cb, NULL, NULL, G_OBJECT (handler));
426 g_array_unref (array);
428 DEBUG ("%sremembering the password", remember ? "" : "not ");
430 /* determine if we are permitted to save the password locally */
431 may_save_response = tp_asv_get_boolean (
432 tp_channel_borrow_immutable_properties (priv->channel),
433 TP_PROP_CHANNEL_INTERFACE_SASL_AUTHENTICATION_MAY_SAVE_RESPONSE,
434 &may_save_response_valid);
436 if (!may_save_response_valid)
438 DEBUG ("MaySaveResponse unknown, assuming TRUE");
439 may_save_response = TRUE;
444 if (may_save_response)
446 g_free (priv->password);
448 /* We'll save the password if we manage to connect */
449 priv->password = g_strdup (password);
450 priv->save_password = TRUE;
452 else if (tp_proxy_has_interface_by_id (priv->channel,
453 EMP_IFACE_QUARK_CHANNEL_INTERFACE_CREDENTIALS_STORAGE))
455 DEBUG ("Channel implements Ch.I.CredentialsStorage");
459 DEBUG ("Asked to remember password, but doing so is not permitted");
463 if (!may_save_response)
465 /* delete any password present, it shouldn't be there */
466 empathy_keyring_delete_account_password_async (priv->account, NULL, NULL);
469 /* Additionally, if we implement Ch.I.CredentialsStorage, inform that
470 * whether we want to remember the password */
471 if (tp_proxy_has_interface_by_id (priv->channel,
472 EMP_IFACE_QUARK_CHANNEL_INTERFACE_CREDENTIALS_STORAGE))
474 emp_cli_channel_interface_credentials_storage_call_store_credentials (
475 TP_PROXY (priv->channel), -1, remember, NULL, NULL, NULL, NULL);
480 empathy_server_sasl_handler_cancel (EmpathyServerSASLHandler *handler)
482 EmpathyServerSASLHandlerPriv *priv;
484 g_return_if_fail (EMPATHY_IS_SERVER_SASL_HANDLER (handler));
486 priv = handler->priv;
488 DEBUG ("Cancelling SASL mechanism...");
490 tp_cli_channel_interface_sasl_authentication_call_abort_sasl (
491 priv->channel, -1, TP_SASL_ABORT_REASON_USER_ABORT,
492 "User cancelled the authentication",
493 NULL, NULL, NULL, NULL);
497 empathy_server_sasl_handler_get_account (EmpathyServerSASLHandler *handler)
499 EmpathyServerSASLHandlerPriv *priv;
501 g_return_val_if_fail (EMPATHY_IS_SERVER_SASL_HANDLER (handler), NULL);
503 priv = handler->priv;
505 return priv->account;
509 empathy_server_sasl_handler_get_channel (EmpathyServerSASLHandler *handler)
511 EmpathyServerSASLHandlerPriv *priv;
513 g_return_val_if_fail (EMPATHY_IS_SERVER_SASL_HANDLER (handler), NULL);
515 priv = handler->priv;
517 return priv->channel;
521 empathy_server_sasl_handler_has_password (EmpathyServerSASLHandler *handler)
523 EmpathyServerSASLHandlerPriv *priv;
525 g_return_val_if_fail (EMPATHY_IS_SERVER_SASL_HANDLER (handler), FALSE);
527 priv = handler->priv;
529 return (priv->password != NULL);
533 * empathy_server_sasl_handler_can_save_response_somewhere:
536 * Returns: %TRUE if the response can be saved somewhere, either the keyring
537 * or via Ch.I.CredentialsStorage
540 empathy_server_sasl_handler_can_save_response_somewhere (
541 EmpathyServerSASLHandler *self)
543 EmpathyServerSASLHandlerPriv *priv;
544 gboolean may_save_response, may_save_response_valid;
545 gboolean has_storage_iface;
547 g_return_val_if_fail (EMPATHY_IS_SERVER_SASL_HANDLER (self), FALSE);
551 /* determine if we are permitted to save the password locally */
552 may_save_response = tp_asv_get_boolean (
553 tp_channel_borrow_immutable_properties (priv->channel),
554 TP_PROP_CHANNEL_INTERFACE_SASL_AUTHENTICATION_MAY_SAVE_RESPONSE,
555 &may_save_response_valid);
557 if (!may_save_response_valid)
559 DEBUG ("MaySaveResponse unknown, assuming TRUE");
560 may_save_response = TRUE;
563 has_storage_iface = tp_proxy_has_interface_by_id (priv->channel,
564 EMP_IFACE_QUARK_CHANNEL_INTERFACE_CREDENTIALS_STORAGE);
566 return may_save_response || has_storage_iface;