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
21 #include "empathy-server-sasl-handler.h"
23 #include "empathy-keyring.h"
24 #include "empathy-sasl-mechanisms.h"
25 #include "extensions.h"
27 #define DEBUG_FLAG EMPATHY_DEBUG_SASL
28 #include "empathy-debug.h"
43 static guint signals[LAST_SIGNAL] = {0};
49 GSimpleAsyncResult *result;
52 gboolean save_password;
54 GSimpleAsyncResult *async_init_res;
55 } EmpathyServerSASLHandlerPriv;
57 static void async_initable_iface_init (GAsyncInitableIface *iface);
59 G_DEFINE_TYPE_WITH_CODE (EmpathyServerSASLHandler, empathy_server_sasl_handler,
61 G_IMPLEMENT_INTERFACE (G_TYPE_ASYNC_INITABLE, async_initable_iface_init));
64 empathy_server_sasl_handler_set_password_cb (GObject *source,
70 if (!empathy_keyring_set_account_password_finish (TP_ACCOUNT (source), result,
73 DEBUG ("Failed to set password: %s", error->message);
74 g_clear_error (&error);
78 DEBUG ("Password set successfully.");
83 empathy_server_sasl_handler_give_password (gpointer data)
85 EmpathyServerSASLHandler *self = data;
86 EmpathyServerSASLHandlerPriv *priv = self->priv;
88 empathy_server_sasl_handler_provide_password (self,
89 priv->password, FALSE);
95 empathy_server_sasl_handler_get_password_async_cb (GObject *source,
99 EmpathyServerSASLHandlerPriv *priv;
100 const gchar *password;
101 GError *error = NULL;
103 priv = EMPATHY_SERVER_SASL_HANDLER (user_data)->priv;
105 password = empathy_keyring_get_account_password_finish (TP_ACCOUNT (source),
108 if (password != NULL)
110 priv->password = g_strdup (password);
112 /* Do this in an idle so the async result will get there
114 g_idle_add (empathy_server_sasl_handler_give_password, user_data);
117 g_simple_async_result_complete (priv->async_init_res);
118 tp_clear_object (&priv->async_init_res);
122 empathy_server_sasl_handler_init_async (GAsyncInitable *initable,
124 GCancellable *cancellable,
125 GAsyncReadyCallback callback,
128 EmpathyServerSASLHandler *self = EMPATHY_SERVER_SASL_HANDLER (initable);
129 EmpathyServerSASLHandlerPriv *priv = self->priv;
131 g_assert (priv->account != NULL);
133 priv->async_init_res = g_simple_async_result_new (G_OBJECT (self),
134 callback, user_data, empathy_server_sasl_handler_new_async);
136 empathy_keyring_get_account_password_async (priv->account,
137 empathy_server_sasl_handler_get_password_async_cb, self);
141 empathy_server_sasl_handler_init_finish (GAsyncInitable *initable,
145 if (g_simple_async_result_propagate_error (G_SIMPLE_ASYNC_RESULT (res),
153 async_initable_iface_init (GAsyncInitableIface *iface)
155 iface->init_async = empathy_server_sasl_handler_init_async;
156 iface->init_finish = empathy_server_sasl_handler_init_finish;
160 channel_invalidated_cb (TpProxy *proxy,
164 EmpathyServerSASLHandler *self)
166 g_signal_emit (self, signals[INVALIDATED], 0);
170 empathy_server_sasl_handler_constructed (GObject *object)
172 EmpathyServerSASLHandlerPriv *priv = EMPATHY_SERVER_SASL_HANDLER (object)->priv;
173 GError *error = NULL;
177 DEBUG ("Failed to connect to SASLStatusChanged: %s", error->message);
178 g_clear_error (&error);
181 tp_g_signal_connect_object (priv->channel, "invalidated",
182 G_CALLBACK (channel_invalidated_cb), object, 0);
186 empathy_server_sasl_handler_get_property (GObject *object,
191 EmpathyServerSASLHandlerPriv *priv = EMPATHY_SERVER_SASL_HANDLER (object)->priv;
196 g_value_set_object (value, priv->channel);
199 g_value_set_object (value, priv->account);
202 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
208 empathy_server_sasl_handler_set_property (GObject *object,
213 EmpathyServerSASLHandlerPriv *priv = EMPATHY_SERVER_SASL_HANDLER (object)->priv;
218 priv->channel = g_value_dup_object (value);
221 priv->account = g_value_dup_object (value);
224 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
230 empathy_server_sasl_handler_dispose (GObject *object)
232 EmpathyServerSASLHandlerPriv *priv = EMPATHY_SERVER_SASL_HANDLER (object)->priv;
234 DEBUG ("%p", object);
236 tp_clear_object (&priv->channel);
237 tp_clear_object (&priv->account);
239 G_OBJECT_CLASS (empathy_server_sasl_handler_parent_class)->dispose (object);
243 empathy_server_sasl_handler_finalize (GObject *object)
245 EmpathyServerSASLHandlerPriv *priv = EMPATHY_SERVER_SASL_HANDLER (object)->priv;
247 DEBUG ("%p", object);
249 tp_clear_pointer (&priv->password, g_free);
251 G_OBJECT_CLASS (empathy_server_sasl_handler_parent_class)->finalize (object);
255 empathy_server_sasl_handler_class_init (EmpathyServerSASLHandlerClass *klass)
257 GObjectClass *oclass = G_OBJECT_CLASS (klass);
260 oclass->constructed = empathy_server_sasl_handler_constructed;
261 oclass->get_property = empathy_server_sasl_handler_get_property;
262 oclass->set_property = empathy_server_sasl_handler_set_property;
263 oclass->dispose = empathy_server_sasl_handler_dispose;
264 oclass->finalize = empathy_server_sasl_handler_finalize;
266 g_type_class_add_private (klass, sizeof (EmpathyServerSASLHandlerPriv));
268 pspec = g_param_spec_object ("channel", "The TpChannel",
269 "The TpChannel this handler is supposed to handle.",
271 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS);
272 g_object_class_install_property (oclass, PROP_CHANNEL, pspec);
274 pspec = g_param_spec_object ("account", "The TpAccount",
275 "The TpAccount this channel belongs to.",
277 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS);
278 g_object_class_install_property (oclass, PROP_ACCOUNT, pspec);
280 signals[AUTH_PASSWORD_FAILED] = g_signal_new ("auth-password-failed",
281 G_TYPE_FROM_CLASS (klass),
282 G_SIGNAL_RUN_LAST, 0,
284 g_cclosure_marshal_generic,
285 G_TYPE_NONE, 1, G_TYPE_STRING);
287 signals[INVALIDATED] = g_signal_new ("invalidated",
288 G_TYPE_FROM_CLASS (klass),
289 G_SIGNAL_RUN_LAST, 0,
291 g_cclosure_marshal_generic,
296 empathy_server_sasl_handler_init (EmpathyServerSASLHandler *self)
298 self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
299 EMPATHY_TYPE_SERVER_SASL_HANDLER, EmpathyServerSASLHandlerPriv);
302 EmpathyServerSASLHandler *
303 empathy_server_sasl_handler_new_finish (GAsyncResult *result,
306 GObject *object, *source_object;
308 source_object = g_async_result_get_source_object (result);
310 object = g_async_initable_new_finish (G_ASYNC_INITABLE (source_object),
312 g_object_unref (source_object);
315 return EMPATHY_SERVER_SASL_HANDLER (object);
321 empathy_server_sasl_handler_new_async (TpAccount *account,
323 GAsyncReadyCallback callback,
326 g_return_if_fail (TP_IS_ACCOUNT (account));
327 g_return_if_fail (TP_IS_CHANNEL (channel));
328 g_return_if_fail (callback != NULL);
330 g_async_initable_new_async (EMPATHY_TYPE_SERVER_SASL_HANDLER,
331 G_PRIORITY_DEFAULT, NULL, callback, user_data,
338 auth_cb (GObject *source,
339 GAsyncResult *result,
342 EmpathyServerSASLHandler *self = user_data;
343 EmpathyServerSASLHandlerPriv *priv = self->priv;
344 GError *error = NULL;
346 if (!empathy_sasl_auth_finish (priv->channel, result, &error))
348 if (g_error_matches (error, TP_ERROR, TP_ERROR_AUTHENTICATION_FAILED))
350 g_signal_emit (self, signals[AUTH_PASSWORD_FAILED], 0, priv->password);
352 g_clear_error (&error);
356 DEBUG ("Saving password in keyring");
357 empathy_keyring_set_account_password_async (priv->account,
358 priv->password, priv->save_password,
359 empathy_server_sasl_handler_set_password_cb,
363 tp_channel_close_async (priv->channel, NULL, NULL);
364 g_object_unref (self);
368 channel_has_may_save_response (TpChannel *channel)
370 /* determine if we are permitted to save the password locally */
372 gboolean may_save_response;
374 props = tp_channel_dup_immutable_properties (channel);
376 if (!g_variant_lookup (props,
377 TP_PROP_CHANNEL_INTERFACE_SASL_AUTHENTICATION_MAY_SAVE_RESPONSE,
378 "b", &may_save_response))
380 DEBUG ("MaySaveResponse unknown, assuming TRUE");
381 may_save_response = TRUE;
384 g_variant_unref (props);
385 return may_save_response;
389 empathy_server_sasl_handler_provide_password (
390 EmpathyServerSASLHandler *handler,
391 const gchar *password,
394 EmpathyServerSASLHandlerPriv *priv;
395 gboolean may_save_response;
397 g_return_if_fail (EMPATHY_IS_SERVER_SASL_HANDLER (handler));
399 priv = handler->priv;
401 empathy_sasl_auth_password_async (priv->channel, password,
402 auth_cb, g_object_ref (handler));
404 DEBUG ("%sremembering the password", remember ? "" : "not ");
406 may_save_response = channel_has_may_save_response (priv->channel);
410 if (may_save_response)
412 g_free (priv->password);
414 /* We'll save the password if we manage to connect */
415 priv->password = g_strdup (password);
416 priv->save_password = TRUE;
418 else if (tp_proxy_has_interface_by_id (priv->channel,
419 EMP_IFACE_QUARK_CHANNEL_INTERFACE_CREDENTIALS_STORAGE))
421 DEBUG ("Channel implements Ch.I.CredentialsStorage");
425 DEBUG ("Asked to remember password, but doing so is not permitted");
429 if (!may_save_response)
431 /* delete any password present, it shouldn't be there */
432 empathy_keyring_delete_account_password_async (priv->account, NULL, NULL);
435 /* Additionally, if we implement Ch.I.CredentialsStorage, inform that
436 * whether we want to remember the password */
437 if (tp_proxy_has_interface_by_id (priv->channel,
438 EMP_IFACE_QUARK_CHANNEL_INTERFACE_CREDENTIALS_STORAGE))
440 emp_cli_channel_interface_credentials_storage_call_store_credentials (
441 TP_PROXY (priv->channel), -1, remember, NULL, NULL, NULL, NULL);
446 empathy_server_sasl_handler_cancel (EmpathyServerSASLHandler *handler)
448 EmpathyServerSASLHandlerPriv *priv;
450 g_return_if_fail (EMPATHY_IS_SERVER_SASL_HANDLER (handler));
452 priv = handler->priv;
454 DEBUG ("Cancelling SASL mechanism...");
456 tp_cli_channel_interface_sasl_authentication_call_abort_sasl (
457 priv->channel, -1, TP_SASL_ABORT_REASON_USER_ABORT,
458 "User cancelled the authentication",
459 NULL, NULL, NULL, NULL);
463 empathy_server_sasl_handler_get_account (EmpathyServerSASLHandler *handler)
465 EmpathyServerSASLHandlerPriv *priv;
467 g_return_val_if_fail (EMPATHY_IS_SERVER_SASL_HANDLER (handler), NULL);
469 priv = handler->priv;
471 return priv->account;
475 empathy_server_sasl_handler_get_channel (EmpathyServerSASLHandler *handler)
477 EmpathyServerSASLHandlerPriv *priv;
479 g_return_val_if_fail (EMPATHY_IS_SERVER_SASL_HANDLER (handler), NULL);
481 priv = handler->priv;
483 return priv->channel;
487 empathy_server_sasl_handler_has_password (EmpathyServerSASLHandler *handler)
489 EmpathyServerSASLHandlerPriv *priv;
491 g_return_val_if_fail (EMPATHY_IS_SERVER_SASL_HANDLER (handler), FALSE);
493 priv = handler->priv;
495 return (priv->password != NULL);
499 * empathy_server_sasl_handler_can_save_response_somewhere:
502 * Returns: %TRUE if the response can be saved somewhere, either the keyring
503 * or via Ch.I.CredentialsStorage
506 empathy_server_sasl_handler_can_save_response_somewhere (
507 EmpathyServerSASLHandler *self)
509 EmpathyServerSASLHandlerPriv *priv;
510 gboolean may_save_response;
511 gboolean has_storage_iface;
513 g_return_val_if_fail (EMPATHY_IS_SERVER_SASL_HANDLER (self), FALSE);
517 may_save_response = channel_has_may_save_response (priv->channel);
519 has_storage_iface = tp_proxy_has_interface_by_id (priv->channel,
520 EMP_IFACE_QUARK_CHANNEL_INTERFACE_CREDENTIALS_STORAGE);
522 return may_save_response || has_storage_iface;