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>
26 #define DEBUG_FLAG EMPATHY_DEBUG_SASL
27 #include "empathy-debug.h"
28 #include "empathy-keyring.h"
29 #include "empathy-sasl-mechanisms.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));
65 empathy_server_sasl_handler_set_password_cb (GObject *source,
71 if (!empathy_keyring_set_account_password_finish (TP_ACCOUNT (source), result,
74 DEBUG ("Failed to set password: %s", error->message);
75 g_clear_error (&error);
79 DEBUG ("Password set successfully.");
84 empathy_server_sasl_handler_give_password (gpointer data)
86 EmpathyServerSASLHandler *self = data;
87 EmpathyServerSASLHandlerPriv *priv = self->priv;
89 empathy_server_sasl_handler_provide_password (self,
90 priv->password, FALSE);
96 empathy_server_sasl_handler_get_password_async_cb (GObject *source,
100 EmpathyServerSASLHandlerPriv *priv;
101 const gchar *password;
102 GError *error = NULL;
104 priv = EMPATHY_SERVER_SASL_HANDLER (user_data)->priv;
106 password = empathy_keyring_get_account_password_finish (TP_ACCOUNT (source),
109 if (password != NULL)
111 priv->password = g_strdup (password);
113 /* Do this in an idle so the async result will get there
115 g_idle_add (empathy_server_sasl_handler_give_password, user_data);
118 g_simple_async_result_complete (priv->async_init_res);
119 tp_clear_object (&priv->async_init_res);
123 empathy_server_sasl_handler_init_async (GAsyncInitable *initable,
125 GCancellable *cancellable,
126 GAsyncReadyCallback callback,
129 EmpathyServerSASLHandler *self = EMPATHY_SERVER_SASL_HANDLER (initable);
130 EmpathyServerSASLHandlerPriv *priv = self->priv;
132 g_assert (priv->account != NULL);
134 priv->async_init_res = g_simple_async_result_new (G_OBJECT (self),
135 callback, user_data, empathy_server_sasl_handler_new_async);
137 empathy_keyring_get_account_password_async (priv->account,
138 empathy_server_sasl_handler_get_password_async_cb, self);
142 empathy_server_sasl_handler_init_finish (GAsyncInitable *initable,
146 if (g_simple_async_result_propagate_error (G_SIMPLE_ASYNC_RESULT (res),
154 async_initable_iface_init (GAsyncInitableIface *iface)
156 iface->init_async = empathy_server_sasl_handler_init_async;
157 iface->init_finish = empathy_server_sasl_handler_init_finish;
161 channel_invalidated_cb (TpProxy *proxy,
165 EmpathyServerSASLHandler *self)
167 g_signal_emit (self, signals[INVALIDATED], 0);
171 empathy_server_sasl_handler_constructed (GObject *object)
173 EmpathyServerSASLHandlerPriv *priv = EMPATHY_SERVER_SASL_HANDLER (object)->priv;
174 GError *error = NULL;
178 DEBUG ("Failed to connect to SASLStatusChanged: %s", error->message);
179 g_clear_error (&error);
182 tp_g_signal_connect_object (priv->channel, "invalidated",
183 G_CALLBACK (channel_invalidated_cb), object, 0);
187 empathy_server_sasl_handler_get_property (GObject *object,
192 EmpathyServerSASLHandlerPriv *priv = EMPATHY_SERVER_SASL_HANDLER (object)->priv;
197 g_value_set_object (value, priv->channel);
200 g_value_set_object (value, priv->account);
203 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
209 empathy_server_sasl_handler_set_property (GObject *object,
214 EmpathyServerSASLHandlerPriv *priv = EMPATHY_SERVER_SASL_HANDLER (object)->priv;
219 priv->channel = g_value_dup_object (value);
222 priv->account = g_value_dup_object (value);
225 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
231 empathy_server_sasl_handler_dispose (GObject *object)
233 EmpathyServerSASLHandlerPriv *priv = EMPATHY_SERVER_SASL_HANDLER (object)->priv;
235 DEBUG ("%p", object);
237 tp_clear_object (&priv->channel);
238 tp_clear_object (&priv->account);
240 G_OBJECT_CLASS (empathy_server_sasl_handler_parent_class)->dispose (object);
244 empathy_server_sasl_handler_finalize (GObject *object)
246 EmpathyServerSASLHandlerPriv *priv = EMPATHY_SERVER_SASL_HANDLER (object)->priv;
248 DEBUG ("%p", object);
250 tp_clear_pointer (&priv->password, g_free);
252 G_OBJECT_CLASS (empathy_server_sasl_handler_parent_class)->finalize (object);
256 empathy_server_sasl_handler_class_init (EmpathyServerSASLHandlerClass *klass)
258 GObjectClass *oclass = G_OBJECT_CLASS (klass);
261 oclass->constructed = empathy_server_sasl_handler_constructed;
262 oclass->get_property = empathy_server_sasl_handler_get_property;
263 oclass->set_property = empathy_server_sasl_handler_set_property;
264 oclass->dispose = empathy_server_sasl_handler_dispose;
265 oclass->finalize = empathy_server_sasl_handler_finalize;
267 g_type_class_add_private (klass, sizeof (EmpathyServerSASLHandlerPriv));
269 pspec = g_param_spec_object ("channel", "The TpChannel",
270 "The TpChannel this handler is supposed to handle.",
272 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS);
273 g_object_class_install_property (oclass, PROP_CHANNEL, pspec);
275 pspec = g_param_spec_object ("account", "The TpAccount",
276 "The TpAccount this channel belongs to.",
278 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS);
279 g_object_class_install_property (oclass, PROP_ACCOUNT, pspec);
281 signals[AUTH_PASSWORD_FAILED] = g_signal_new ("auth-password-failed",
282 G_TYPE_FROM_CLASS (klass),
283 G_SIGNAL_RUN_LAST, 0,
285 g_cclosure_marshal_generic,
286 G_TYPE_NONE, 1, G_TYPE_STRING);
288 signals[INVALIDATED] = g_signal_new ("invalidated",
289 G_TYPE_FROM_CLASS (klass),
290 G_SIGNAL_RUN_LAST, 0,
292 g_cclosure_marshal_generic,
297 empathy_server_sasl_handler_init (EmpathyServerSASLHandler *self)
299 self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
300 EMPATHY_TYPE_SERVER_SASL_HANDLER, EmpathyServerSASLHandlerPriv);
303 EmpathyServerSASLHandler *
304 empathy_server_sasl_handler_new_finish (GAsyncResult *result,
307 GObject *object, *source_object;
309 source_object = g_async_result_get_source_object (result);
311 object = g_async_initable_new_finish (G_ASYNC_INITABLE (source_object),
313 g_object_unref (source_object);
316 return EMPATHY_SERVER_SASL_HANDLER (object);
322 empathy_server_sasl_handler_new_async (TpAccount *account,
324 GAsyncReadyCallback callback,
327 g_return_if_fail (TP_IS_ACCOUNT (account));
328 g_return_if_fail (TP_IS_CHANNEL (channel));
329 g_return_if_fail (callback != NULL);
331 g_async_initable_new_async (EMPATHY_TYPE_SERVER_SASL_HANDLER,
332 G_PRIORITY_DEFAULT, NULL, callback, user_data,
339 auth_cb (GObject *source,
340 GAsyncResult *result,
343 EmpathyServerSASLHandler *self = user_data;
344 EmpathyServerSASLHandlerPriv *priv = self->priv;
345 GError *error = NULL;
347 if (!empathy_sasl_auth_finish (priv->channel, result, &error))
349 if (g_error_matches (error, TP_ERROR, TP_ERROR_AUTHENTICATION_FAILED))
351 g_signal_emit (self, signals[AUTH_PASSWORD_FAILED], 0, priv->password);
353 g_clear_error (&error);
357 DEBUG ("Saving password in keyring");
358 empathy_keyring_set_account_password_async (priv->account,
359 priv->password, priv->save_password,
360 empathy_server_sasl_handler_set_password_cb,
364 tp_channel_close_async (priv->channel, NULL, NULL);
365 g_object_unref (self);
369 channel_has_may_save_response (TpChannel *channel)
371 /* determine if we are permitted to save the password locally */
373 gboolean may_save_response;
375 props = tp_channel_dup_immutable_properties (channel);
377 if (!g_variant_lookup (props,
378 TP_PROP_CHANNEL_INTERFACE_SASL_AUTHENTICATION_MAY_SAVE_RESPONSE,
379 "b", &may_save_response))
381 DEBUG ("MaySaveResponse unknown, assuming TRUE");
382 may_save_response = TRUE;
385 g_variant_unref (props);
386 return may_save_response;
390 empathy_server_sasl_handler_provide_password (
391 EmpathyServerSASLHandler *handler,
392 const gchar *password,
395 EmpathyServerSASLHandlerPriv *priv;
396 gboolean may_save_response;
398 g_return_if_fail (EMPATHY_IS_SERVER_SASL_HANDLER (handler));
400 priv = handler->priv;
402 empathy_sasl_auth_password_async (priv->channel, password,
403 auth_cb, g_object_ref (handler));
405 DEBUG ("%sremembering the password", remember ? "" : "not ");
407 may_save_response = channel_has_may_save_response (priv->channel);
411 if (may_save_response)
413 g_free (priv->password);
415 /* We'll save the password if we manage to connect */
416 priv->password = g_strdup (password);
417 priv->save_password = TRUE;
419 else if (tp_proxy_has_interface_by_id (priv->channel,
420 EMP_IFACE_QUARK_CHANNEL_INTERFACE_CREDENTIALS_STORAGE))
422 DEBUG ("Channel implements Ch.I.CredentialsStorage");
426 DEBUG ("Asked to remember password, but doing so is not permitted");
430 if (!may_save_response)
432 /* delete any password present, it shouldn't be there */
433 empathy_keyring_delete_account_password_async (priv->account, NULL, NULL);
436 /* Additionally, if we implement Ch.I.CredentialsStorage, inform that
437 * whether we want to remember the password */
438 if (tp_proxy_has_interface_by_id (priv->channel,
439 EMP_IFACE_QUARK_CHANNEL_INTERFACE_CREDENTIALS_STORAGE))
441 emp_cli_channel_interface_credentials_storage_call_store_credentials (
442 TP_PROXY (priv->channel), -1, remember, NULL, NULL, NULL, NULL);
447 empathy_server_sasl_handler_cancel (EmpathyServerSASLHandler *handler)
449 EmpathyServerSASLHandlerPriv *priv;
451 g_return_if_fail (EMPATHY_IS_SERVER_SASL_HANDLER (handler));
453 priv = handler->priv;
455 DEBUG ("Cancelling SASL mechanism...");
457 tp_cli_channel_interface_sasl_authentication_call_abort_sasl (
458 priv->channel, -1, TP_SASL_ABORT_REASON_USER_ABORT,
459 "User cancelled the authentication",
460 NULL, NULL, NULL, NULL);
464 empathy_server_sasl_handler_get_account (EmpathyServerSASLHandler *handler)
466 EmpathyServerSASLHandlerPriv *priv;
468 g_return_val_if_fail (EMPATHY_IS_SERVER_SASL_HANDLER (handler), NULL);
470 priv = handler->priv;
472 return priv->account;
476 empathy_server_sasl_handler_get_channel (EmpathyServerSASLHandler *handler)
478 EmpathyServerSASLHandlerPriv *priv;
480 g_return_val_if_fail (EMPATHY_IS_SERVER_SASL_HANDLER (handler), NULL);
482 priv = handler->priv;
484 return priv->channel;
488 empathy_server_sasl_handler_has_password (EmpathyServerSASLHandler *handler)
490 EmpathyServerSASLHandlerPriv *priv;
492 g_return_val_if_fail (EMPATHY_IS_SERVER_SASL_HANDLER (handler), FALSE);
494 priv = handler->priv;
496 return (priv->password != NULL);
500 * empathy_server_sasl_handler_can_save_response_somewhere:
503 * Returns: %TRUE if the response can be saved somewhere, either the keyring
504 * or via Ch.I.CredentialsStorage
507 empathy_server_sasl_handler_can_save_response_somewhere (
508 EmpathyServerSASLHandler *self)
510 EmpathyServerSASLHandlerPriv *priv;
511 gboolean may_save_response;
512 gboolean has_storage_iface;
514 g_return_val_if_fail (EMPATHY_IS_SERVER_SASL_HANDLER (self), FALSE);
518 may_save_response = channel_has_may_save_response (priv->channel);
520 has_storage_iface = tp_proxy_has_interface_by_id (priv->channel,
521 EMP_IFACE_QUARK_CHANNEL_INTERFACE_CREDENTIALS_STORAGE);
523 return may_save_response || has_storage_iface;