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
22 #include "empathy-server-sasl-handler.h"
24 #include <telepathy-glib/telepathy-glib.h>
26 #include <extensions/extensions.h>
30 #define DEBUG_FLAG EMPATHY_DEBUG_SASL
31 #include "empathy-debug.h"
32 #include "empathy-keyring.h"
33 #include "empathy-sasl-mechanisms.h"
48 static guint signals[LAST_SIGNAL] = {0};
54 GSimpleAsyncResult *result;
57 gboolean save_password;
59 GSimpleAsyncResult *async_init_res;
60 } EmpathyServerSASLHandlerPriv;
62 static void async_initable_iface_init (GAsyncInitableIface *iface);
64 G_DEFINE_TYPE_WITH_CODE (EmpathyServerSASLHandler, empathy_server_sasl_handler,
66 G_IMPLEMENT_INTERFACE (G_TYPE_ASYNC_INITABLE, async_initable_iface_init));
69 empathy_server_sasl_handler_set_password_cb (GObject *source,
75 if (!empathy_keyring_set_account_password_finish (TP_ACCOUNT (source), result,
78 DEBUG ("Failed to set password: %s", error->message);
79 g_clear_error (&error);
83 DEBUG ("Password set successfully.");
88 empathy_server_sasl_handler_give_password (gpointer data)
90 EmpathyServerSASLHandler *self = data;
91 EmpathyServerSASLHandlerPriv *priv = self->priv;
93 empathy_server_sasl_handler_provide_password (self,
94 priv->password, FALSE);
100 empathy_server_sasl_handler_get_password_async_cb (GObject *source,
101 GAsyncResult *result,
104 EmpathyServerSASLHandlerPriv *priv;
105 const gchar *password;
106 GError *error = NULL;
108 priv = EMPATHY_SERVER_SASL_HANDLER (user_data)->priv;
110 password = empathy_keyring_get_account_password_finish (TP_ACCOUNT (source),
113 if (password != NULL)
115 priv->password = g_strdup (password);
117 /* Do this in an idle so the async result will get there
119 g_idle_add (empathy_server_sasl_handler_give_password, user_data);
122 g_simple_async_result_complete (priv->async_init_res);
123 tp_clear_object (&priv->async_init_res);
127 empathy_server_sasl_handler_init_async (GAsyncInitable *initable,
129 GCancellable *cancellable,
130 GAsyncReadyCallback callback,
133 EmpathyServerSASLHandler *self = EMPATHY_SERVER_SASL_HANDLER (initable);
134 EmpathyServerSASLHandlerPriv *priv = self->priv;
136 g_assert (priv->account != NULL);
138 priv->async_init_res = g_simple_async_result_new (G_OBJECT (self),
139 callback, user_data, empathy_server_sasl_handler_new_async);
141 empathy_keyring_get_account_password_async (priv->account,
142 empathy_server_sasl_handler_get_password_async_cb, self);
146 empathy_server_sasl_handler_init_finish (GAsyncInitable *initable,
150 if (g_simple_async_result_propagate_error (G_SIMPLE_ASYNC_RESULT (res),
158 async_initable_iface_init (GAsyncInitableIface *iface)
160 iface->init_async = empathy_server_sasl_handler_init_async;
161 iface->init_finish = empathy_server_sasl_handler_init_finish;
165 channel_invalidated_cb (TpProxy *proxy,
169 EmpathyServerSASLHandler *self)
171 g_signal_emit (self, signals[INVALIDATED], 0);
175 empathy_server_sasl_handler_constructed (GObject *object)
177 EmpathyServerSASLHandlerPriv *priv = EMPATHY_SERVER_SASL_HANDLER (object)->priv;
178 GError *error = NULL;
182 DEBUG ("Failed to connect to SASLStatusChanged: %s", error->message);
183 g_clear_error (&error);
186 tp_g_signal_connect_object (priv->channel, "invalidated",
187 G_CALLBACK (channel_invalidated_cb), object, 0);
191 empathy_server_sasl_handler_get_property (GObject *object,
196 EmpathyServerSASLHandlerPriv *priv = EMPATHY_SERVER_SASL_HANDLER (object)->priv;
201 g_value_set_object (value, priv->channel);
204 g_value_set_object (value, priv->account);
207 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
213 empathy_server_sasl_handler_set_property (GObject *object,
218 EmpathyServerSASLHandlerPriv *priv = EMPATHY_SERVER_SASL_HANDLER (object)->priv;
223 priv->channel = g_value_dup_object (value);
226 priv->account = g_value_dup_object (value);
229 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
235 empathy_server_sasl_handler_dispose (GObject *object)
237 EmpathyServerSASLHandlerPriv *priv = EMPATHY_SERVER_SASL_HANDLER (object)->priv;
239 DEBUG ("%p", object);
241 tp_clear_object (&priv->channel);
242 tp_clear_object (&priv->account);
244 G_OBJECT_CLASS (empathy_server_sasl_handler_parent_class)->dispose (object);
248 empathy_server_sasl_handler_finalize (GObject *object)
250 EmpathyServerSASLHandlerPriv *priv = EMPATHY_SERVER_SASL_HANDLER (object)->priv;
252 DEBUG ("%p", object);
254 tp_clear_pointer (&priv->password, g_free);
256 G_OBJECT_CLASS (empathy_server_sasl_handler_parent_class)->finalize (object);
260 empathy_server_sasl_handler_class_init (EmpathyServerSASLHandlerClass *klass)
262 GObjectClass *oclass = G_OBJECT_CLASS (klass);
265 oclass->constructed = empathy_server_sasl_handler_constructed;
266 oclass->get_property = empathy_server_sasl_handler_get_property;
267 oclass->set_property = empathy_server_sasl_handler_set_property;
268 oclass->dispose = empathy_server_sasl_handler_dispose;
269 oclass->finalize = empathy_server_sasl_handler_finalize;
271 g_type_class_add_private (klass, sizeof (EmpathyServerSASLHandlerPriv));
273 pspec = g_param_spec_object ("channel", "The TpChannel",
274 "The TpChannel this handler is supposed to handle.",
276 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS);
277 g_object_class_install_property (oclass, PROP_CHANNEL, pspec);
279 pspec = g_param_spec_object ("account", "The TpAccount",
280 "The TpAccount this channel belongs to.",
282 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS);
283 g_object_class_install_property (oclass, PROP_ACCOUNT, pspec);
285 signals[AUTH_PASSWORD_FAILED] = g_signal_new ("auth-password-failed",
286 G_TYPE_FROM_CLASS (klass),
287 G_SIGNAL_RUN_LAST, 0,
289 g_cclosure_marshal_generic,
290 G_TYPE_NONE, 1, G_TYPE_STRING);
292 signals[INVALIDATED] = g_signal_new ("invalidated",
293 G_TYPE_FROM_CLASS (klass),
294 G_SIGNAL_RUN_LAST, 0,
296 g_cclosure_marshal_generic,
301 empathy_server_sasl_handler_init (EmpathyServerSASLHandler *self)
303 self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
304 EMPATHY_TYPE_SERVER_SASL_HANDLER, EmpathyServerSASLHandlerPriv);
307 EmpathyServerSASLHandler *
308 empathy_server_sasl_handler_new_finish (GAsyncResult *result,
311 GObject *object, *source_object;
313 source_object = g_async_result_get_source_object (result);
315 object = g_async_initable_new_finish (G_ASYNC_INITABLE (source_object),
317 g_object_unref (source_object);
320 return EMPATHY_SERVER_SASL_HANDLER (object);
326 empathy_server_sasl_handler_new_async (TpAccount *account,
328 GAsyncReadyCallback callback,
331 g_return_if_fail (TP_IS_ACCOUNT (account));
332 g_return_if_fail (TP_IS_CHANNEL (channel));
333 g_return_if_fail (callback != NULL);
335 g_async_initable_new_async (EMPATHY_TYPE_SERVER_SASL_HANDLER,
336 G_PRIORITY_DEFAULT, NULL, callback, user_data,
343 auth_cb (GObject *source,
344 GAsyncResult *result,
347 EmpathyServerSASLHandler *self = user_data;
348 EmpathyServerSASLHandlerPriv *priv = self->priv;
349 GError *error = NULL;
351 if (!empathy_sasl_auth_finish (priv->channel, result, &error))
353 if (g_error_matches (error, TP_ERROR, TP_ERROR_AUTHENTICATION_FAILED))
355 g_signal_emit (self, signals[AUTH_PASSWORD_FAILED], 0, priv->password);
357 g_clear_error (&error);
361 DEBUG ("Saving password in keyring");
362 empathy_keyring_set_account_password_async (priv->account,
363 priv->password, priv->save_password,
364 empathy_server_sasl_handler_set_password_cb,
368 tp_channel_close_async (priv->channel, NULL, NULL);
369 g_object_unref (self);
373 channel_has_may_save_response (TpChannel *channel)
375 /* determine if we are permitted to save the password locally */
377 gboolean may_save_response;
379 props = tp_channel_dup_immutable_properties (channel);
381 if (!g_variant_lookup (props,
382 TP_PROP_CHANNEL_INTERFACE_SASL_AUTHENTICATION_MAY_SAVE_RESPONSE,
383 "b", &may_save_response))
385 DEBUG ("MaySaveResponse unknown, assuming TRUE");
386 may_save_response = TRUE;
389 g_variant_unref (props);
390 return may_save_response;
394 empathy_server_sasl_handler_provide_password (
395 EmpathyServerSASLHandler *handler,
396 const gchar *password,
399 EmpathyServerSASLHandlerPriv *priv;
400 gboolean may_save_response;
402 g_return_if_fail (EMPATHY_IS_SERVER_SASL_HANDLER (handler));
404 priv = handler->priv;
406 empathy_sasl_auth_password_async (priv->channel, password,
407 auth_cb, g_object_ref (handler));
409 DEBUG ("%sremembering the password", remember ? "" : "not ");
411 may_save_response = channel_has_may_save_response (priv->channel);
415 if (may_save_response)
417 g_free (priv->password);
419 /* We'll save the password if we manage to connect */
420 priv->password = g_strdup (password);
421 priv->save_password = TRUE;
423 else if (tp_proxy_has_interface_by_id (priv->channel,
424 EMP_IFACE_QUARK_CHANNEL_INTERFACE_CREDENTIALS_STORAGE))
426 DEBUG ("Channel implements Ch.I.CredentialsStorage");
430 DEBUG ("Asked to remember password, but doing so is not permitted");
434 if (!may_save_response)
436 /* delete any password present, it shouldn't be there */
437 empathy_keyring_delete_account_password_async (priv->account, NULL, NULL);
440 /* Additionally, if we implement Ch.I.CredentialsStorage, inform that
441 * whether we want to remember the password */
442 if (tp_proxy_has_interface_by_id (priv->channel,
443 EMP_IFACE_QUARK_CHANNEL_INTERFACE_CREDENTIALS_STORAGE))
445 emp_cli_channel_interface_credentials_storage_call_store_credentials (
446 TP_PROXY (priv->channel), -1, remember, NULL, NULL, NULL, NULL);
451 empathy_server_sasl_handler_cancel (EmpathyServerSASLHandler *handler)
453 EmpathyServerSASLHandlerPriv *priv;
455 g_return_if_fail (EMPATHY_IS_SERVER_SASL_HANDLER (handler));
457 priv = handler->priv;
459 DEBUG ("Cancelling SASL mechanism...");
461 tp_cli_channel_interface_sasl_authentication_call_abort_sasl (
462 priv->channel, -1, TP_SASL_ABORT_REASON_USER_ABORT,
463 "User cancelled the authentication",
464 NULL, NULL, NULL, NULL);
468 empathy_server_sasl_handler_get_account (EmpathyServerSASLHandler *handler)
470 EmpathyServerSASLHandlerPriv *priv;
472 g_return_val_if_fail (EMPATHY_IS_SERVER_SASL_HANDLER (handler), NULL);
474 priv = handler->priv;
476 return priv->account;
480 empathy_server_sasl_handler_get_channel (EmpathyServerSASLHandler *handler)
482 EmpathyServerSASLHandlerPriv *priv;
484 g_return_val_if_fail (EMPATHY_IS_SERVER_SASL_HANDLER (handler), NULL);
486 priv = handler->priv;
488 return priv->channel;
492 empathy_server_sasl_handler_has_password (EmpathyServerSASLHandler *handler)
494 EmpathyServerSASLHandlerPriv *priv;
496 g_return_val_if_fail (EMPATHY_IS_SERVER_SASL_HANDLER (handler), FALSE);
498 priv = handler->priv;
500 return (priv->password != NULL);
504 * empathy_server_sasl_handler_can_save_response_somewhere:
507 * Returns: %TRUE if the response can be saved somewhere, either the keyring
508 * or via Ch.I.CredentialsStorage
511 empathy_server_sasl_handler_can_save_response_somewhere (
512 EmpathyServerSASLHandler *self)
514 EmpathyServerSASLHandlerPriv *priv;
515 gboolean may_save_response;
516 gboolean has_storage_iface;
518 g_return_val_if_fail (EMPATHY_IS_SERVER_SASL_HANDLER (self), FALSE);
522 may_save_response = channel_has_may_save_response (priv->channel);
524 has_storage_iface = tp_proxy_has_interface_by_id (priv->channel,
525 EMP_IFACE_QUARK_CHANNEL_INTERFACE_CREDENTIALS_STORAGE);
527 return may_save_response || has_storage_iface;