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 <extensions/extensions.h>
28 #define DEBUG_FLAG EMPATHY_DEBUG_SASL
29 #include "empathy-debug.h"
30 #include "empathy-keyring.h"
31 #include "empathy-sasl-mechanisms.h"
46 static guint signals[LAST_SIGNAL] = {0};
52 GSimpleAsyncResult *result;
55 gboolean save_password;
57 GSimpleAsyncResult *async_init_res;
58 } EmpathyServerSASLHandlerPriv;
60 static void async_initable_iface_init (GAsyncInitableIface *iface);
62 G_DEFINE_TYPE_WITH_CODE (EmpathyServerSASLHandler, empathy_server_sasl_handler,
64 G_IMPLEMENT_INTERFACE (G_TYPE_ASYNC_INITABLE, async_initable_iface_init));
67 empathy_server_sasl_handler_set_password_cb (GObject *source,
73 if (!empathy_keyring_set_account_password_finish (TP_ACCOUNT (source), result,
76 DEBUG ("Failed to set password: %s", error->message);
77 g_clear_error (&error);
81 DEBUG ("Password set successfully.");
86 empathy_server_sasl_handler_give_password (gpointer data)
88 EmpathyServerSASLHandler *self = data;
89 EmpathyServerSASLHandlerPriv *priv = self->priv;
91 empathy_server_sasl_handler_provide_password (self,
92 priv->password, FALSE);
98 empathy_server_sasl_handler_get_password_async_cb (GObject *source,
102 EmpathyServerSASLHandlerPriv *priv;
103 const gchar *password;
104 GError *error = NULL;
106 priv = EMPATHY_SERVER_SASL_HANDLER (user_data)->priv;
108 password = empathy_keyring_get_account_password_finish (TP_ACCOUNT (source),
111 if (password != NULL)
113 priv->password = g_strdup (password);
115 /* Do this in an idle so the async result will get there
117 g_idle_add (empathy_server_sasl_handler_give_password, user_data);
120 g_simple_async_result_complete (priv->async_init_res);
121 tp_clear_object (&priv->async_init_res);
125 empathy_server_sasl_handler_init_async (GAsyncInitable *initable,
127 GCancellable *cancellable,
128 GAsyncReadyCallback callback,
131 EmpathyServerSASLHandler *self = EMPATHY_SERVER_SASL_HANDLER (initable);
132 EmpathyServerSASLHandlerPriv *priv = self->priv;
134 g_assert (priv->account != NULL);
136 priv->async_init_res = g_simple_async_result_new (G_OBJECT (self),
137 callback, user_data, empathy_server_sasl_handler_new_async);
139 empathy_keyring_get_account_password_async (priv->account,
140 empathy_server_sasl_handler_get_password_async_cb, self);
144 empathy_server_sasl_handler_init_finish (GAsyncInitable *initable,
148 if (g_simple_async_result_propagate_error (G_SIMPLE_ASYNC_RESULT (res),
156 async_initable_iface_init (GAsyncInitableIface *iface)
158 iface->init_async = empathy_server_sasl_handler_init_async;
159 iface->init_finish = empathy_server_sasl_handler_init_finish;
163 channel_invalidated_cb (TpProxy *proxy,
167 EmpathyServerSASLHandler *self)
169 g_signal_emit (self, signals[INVALIDATED], 0);
173 empathy_server_sasl_handler_constructed (GObject *object)
175 EmpathyServerSASLHandlerPriv *priv = EMPATHY_SERVER_SASL_HANDLER (object)->priv;
176 GError *error = NULL;
180 DEBUG ("Failed to connect to SASLStatusChanged: %s", error->message);
181 g_clear_error (&error);
184 tp_g_signal_connect_object (priv->channel, "invalidated",
185 G_CALLBACK (channel_invalidated_cb), object, 0);
189 empathy_server_sasl_handler_get_property (GObject *object,
194 EmpathyServerSASLHandlerPriv *priv = EMPATHY_SERVER_SASL_HANDLER (object)->priv;
199 g_value_set_object (value, priv->channel);
202 g_value_set_object (value, priv->account);
205 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
211 empathy_server_sasl_handler_set_property (GObject *object,
216 EmpathyServerSASLHandlerPriv *priv = EMPATHY_SERVER_SASL_HANDLER (object)->priv;
221 priv->channel = g_value_dup_object (value);
224 priv->account = g_value_dup_object (value);
227 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
233 empathy_server_sasl_handler_dispose (GObject *object)
235 EmpathyServerSASLHandlerPriv *priv = EMPATHY_SERVER_SASL_HANDLER (object)->priv;
237 DEBUG ("%p", object);
239 tp_clear_object (&priv->channel);
240 tp_clear_object (&priv->account);
242 G_OBJECT_CLASS (empathy_server_sasl_handler_parent_class)->dispose (object);
246 empathy_server_sasl_handler_finalize (GObject *object)
248 EmpathyServerSASLHandlerPriv *priv = EMPATHY_SERVER_SASL_HANDLER (object)->priv;
250 DEBUG ("%p", object);
252 tp_clear_pointer (&priv->password, g_free);
254 G_OBJECT_CLASS (empathy_server_sasl_handler_parent_class)->finalize (object);
258 empathy_server_sasl_handler_class_init (EmpathyServerSASLHandlerClass *klass)
260 GObjectClass *oclass = G_OBJECT_CLASS (klass);
263 oclass->constructed = empathy_server_sasl_handler_constructed;
264 oclass->get_property = empathy_server_sasl_handler_get_property;
265 oclass->set_property = empathy_server_sasl_handler_set_property;
266 oclass->dispose = empathy_server_sasl_handler_dispose;
267 oclass->finalize = empathy_server_sasl_handler_finalize;
269 g_type_class_add_private (klass, sizeof (EmpathyServerSASLHandlerPriv));
271 pspec = g_param_spec_object ("channel", "The TpChannel",
272 "The TpChannel this handler is supposed to handle.",
274 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS);
275 g_object_class_install_property (oclass, PROP_CHANNEL, pspec);
277 pspec = g_param_spec_object ("account", "The TpAccount",
278 "The TpAccount this channel belongs to.",
280 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS);
281 g_object_class_install_property (oclass, PROP_ACCOUNT, pspec);
283 signals[AUTH_PASSWORD_FAILED] = g_signal_new ("auth-password-failed",
284 G_TYPE_FROM_CLASS (klass),
285 G_SIGNAL_RUN_LAST, 0,
287 g_cclosure_marshal_generic,
288 G_TYPE_NONE, 1, G_TYPE_STRING);
290 signals[INVALIDATED] = g_signal_new ("invalidated",
291 G_TYPE_FROM_CLASS (klass),
292 G_SIGNAL_RUN_LAST, 0,
294 g_cclosure_marshal_generic,
299 empathy_server_sasl_handler_init (EmpathyServerSASLHandler *self)
301 self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
302 EMPATHY_TYPE_SERVER_SASL_HANDLER, EmpathyServerSASLHandlerPriv);
305 EmpathyServerSASLHandler *
306 empathy_server_sasl_handler_new_finish (GAsyncResult *result,
309 GObject *object, *source_object;
311 source_object = g_async_result_get_source_object (result);
313 object = g_async_initable_new_finish (G_ASYNC_INITABLE (source_object),
315 g_object_unref (source_object);
318 return EMPATHY_SERVER_SASL_HANDLER (object);
324 empathy_server_sasl_handler_new_async (TpAccount *account,
326 GAsyncReadyCallback callback,
329 g_return_if_fail (TP_IS_ACCOUNT (account));
330 g_return_if_fail (TP_IS_CHANNEL (channel));
331 g_return_if_fail (callback != NULL);
333 g_async_initable_new_async (EMPATHY_TYPE_SERVER_SASL_HANDLER,
334 G_PRIORITY_DEFAULT, NULL, callback, user_data,
341 auth_cb (GObject *source,
342 GAsyncResult *result,
345 EmpathyServerSASLHandler *self = user_data;
346 EmpathyServerSASLHandlerPriv *priv = self->priv;
347 GError *error = NULL;
349 if (!empathy_sasl_auth_finish (priv->channel, result, &error))
351 if (g_error_matches (error, TP_ERROR, TP_ERROR_AUTHENTICATION_FAILED))
353 g_signal_emit (self, signals[AUTH_PASSWORD_FAILED], 0, priv->password);
355 g_clear_error (&error);
359 DEBUG ("Saving password in keyring");
360 empathy_keyring_set_account_password_async (priv->account,
361 priv->password, priv->save_password,
362 empathy_server_sasl_handler_set_password_cb,
366 tp_channel_close_async (priv->channel, NULL, NULL);
367 g_object_unref (self);
371 channel_has_may_save_response (TpChannel *channel)
373 /* determine if we are permitted to save the password locally */
375 gboolean may_save_response;
377 props = tp_channel_dup_immutable_properties (channel);
379 if (!g_variant_lookup (props,
380 TP_PROP_CHANNEL_INTERFACE_SASL_AUTHENTICATION_MAY_SAVE_RESPONSE,
381 "b", &may_save_response))
383 DEBUG ("MaySaveResponse unknown, assuming TRUE");
384 may_save_response = TRUE;
387 g_variant_unref (props);
388 return may_save_response;
392 empathy_server_sasl_handler_provide_password (
393 EmpathyServerSASLHandler *handler,
394 const gchar *password,
397 EmpathyServerSASLHandlerPriv *priv;
398 gboolean may_save_response;
400 g_return_if_fail (EMPATHY_IS_SERVER_SASL_HANDLER (handler));
402 priv = handler->priv;
404 empathy_sasl_auth_password_async (priv->channel, password,
405 auth_cb, g_object_ref (handler));
407 DEBUG ("%sremembering the password", remember ? "" : "not ");
409 may_save_response = channel_has_may_save_response (priv->channel);
413 if (may_save_response)
415 g_free (priv->password);
417 /* We'll save the password if we manage to connect */
418 priv->password = g_strdup (password);
419 priv->save_password = TRUE;
421 else if (tp_proxy_has_interface_by_id (priv->channel,
422 EMP_IFACE_QUARK_CHANNEL_INTERFACE_CREDENTIALS_STORAGE))
424 DEBUG ("Channel implements Ch.I.CredentialsStorage");
428 DEBUG ("Asked to remember password, but doing so is not permitted");
432 if (!may_save_response)
434 /* delete any password present, it shouldn't be there */
435 empathy_keyring_delete_account_password_async (priv->account, NULL, NULL);
438 /* Additionally, if we implement Ch.I.CredentialsStorage, inform that
439 * whether we want to remember the password */
440 if (tp_proxy_has_interface_by_id (priv->channel,
441 EMP_IFACE_QUARK_CHANNEL_INTERFACE_CREDENTIALS_STORAGE))
443 emp_cli_channel_interface_credentials_storage_call_store_credentials (
444 TP_PROXY (priv->channel), -1, remember, NULL, NULL, NULL, NULL);
449 empathy_server_sasl_handler_cancel (EmpathyServerSASLHandler *handler)
451 EmpathyServerSASLHandlerPriv *priv;
453 g_return_if_fail (EMPATHY_IS_SERVER_SASL_HANDLER (handler));
455 priv = handler->priv;
457 DEBUG ("Cancelling SASL mechanism...");
459 tp_cli_channel_interface_sasl_authentication_call_abort_sasl (
460 priv->channel, -1, TP_SASL_ABORT_REASON_USER_ABORT,
461 "User cancelled the authentication",
462 NULL, NULL, NULL, NULL);
466 empathy_server_sasl_handler_get_account (EmpathyServerSASLHandler *handler)
468 EmpathyServerSASLHandlerPriv *priv;
470 g_return_val_if_fail (EMPATHY_IS_SERVER_SASL_HANDLER (handler), NULL);
472 priv = handler->priv;
474 return priv->account;
478 empathy_server_sasl_handler_get_channel (EmpathyServerSASLHandler *handler)
480 EmpathyServerSASLHandlerPriv *priv;
482 g_return_val_if_fail (EMPATHY_IS_SERVER_SASL_HANDLER (handler), NULL);
484 priv = handler->priv;
486 return priv->channel;
490 empathy_server_sasl_handler_has_password (EmpathyServerSASLHandler *handler)
492 EmpathyServerSASLHandlerPriv *priv;
494 g_return_val_if_fail (EMPATHY_IS_SERVER_SASL_HANDLER (handler), FALSE);
496 priv = handler->priv;
498 return (priv->password != NULL);
502 * empathy_server_sasl_handler_can_save_response_somewhere:
505 * Returns: %TRUE if the response can be saved somewhere, either the keyring
506 * or via Ch.I.CredentialsStorage
509 empathy_server_sasl_handler_can_save_response_somewhere (
510 EmpathyServerSASLHandler *self)
512 EmpathyServerSASLHandlerPriv *priv;
513 gboolean may_save_response;
514 gboolean has_storage_iface;
516 g_return_val_if_fail (EMPATHY_IS_SERVER_SASL_HANDLER (self), FALSE);
520 may_save_response = channel_has_may_save_response (priv->channel);
522 has_storage_iface = tp_proxy_has_interface_by_id (priv->channel,
523 EMP_IFACE_QUARK_CHANNEL_INTERFACE_CREDENTIALS_STORAGE);
525 return may_save_response || has_storage_iface;