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/util.h>
26 #define DEBUG_FLAG EMPATHY_DEBUG_SASL
27 #include "empathy-debug.h"
28 #include "empathy-keyring.h"
42 static guint signals[LAST_SIGNAL] = {0};
48 GSimpleAsyncResult *result;
52 GSimpleAsyncResult *async_init_res;
53 } EmpathyServerSASLHandlerPriv;
55 static void async_initable_iface_init (GAsyncInitableIface *iface);
57 G_DEFINE_TYPE_WITH_CODE (EmpathyServerSASLHandler, empathy_server_sasl_handler,
59 G_IMPLEMENT_INTERFACE (G_TYPE_ASYNC_INITABLE, async_initable_iface_init));
61 static const gchar *sasl_statuses[] = {
72 sasl_status_changed_cb (TpChannel *channel,
79 EmpathyServerSASLHandlerPriv *priv = EMPATHY_SERVER_SASL_HANDLER (weak_object)->priv;
82 if (status >= G_N_ELEMENTS (sasl_statuses))
84 DEBUG ("SASL status changed to unknown status");
88 DEBUG ("SASL status changed to '%s'", sasl_statuses[status]);
90 if (status == TP_SASL_STATUS_SERVER_SUCCEEDED)
92 tp_cli_channel_interface_sasl_authentication_call_accept_sasl (
93 priv->channel, -1, NULL, NULL, NULL, NULL);
95 tp_cli_channel_call_close (priv->channel, -1,
96 NULL, NULL, NULL, NULL);
101 empathy_server_sasl_handler_give_password (gpointer data)
103 EmpathyServerSASLHandler *self = data;
104 EmpathyServerSASLHandlerPriv *priv = self->priv;
106 empathy_server_sasl_handler_provide_password (self,
107 priv->password, FALSE);
113 empathy_server_sasl_handler_get_password_async_cb (GObject *source,
114 GAsyncResult *result,
117 EmpathyServerSASLHandlerPriv *priv;
118 const gchar *password;
119 GError *error = NULL;
121 priv = EMPATHY_SERVER_SASL_HANDLER (user_data)->priv;
123 password = empathy_keyring_get_password_finish (TP_ACCOUNT (source),
126 if (password != NULL)
128 priv->password = g_strdup (password);
130 /* Do this in an idle so the async result will get there
132 g_idle_add (empathy_server_sasl_handler_give_password, user_data);
135 g_simple_async_result_complete (priv->async_init_res);
136 tp_clear_object (&priv->async_init_res);
140 empathy_server_sasl_handler_init_async (GAsyncInitable *initable,
142 GCancellable *cancellable,
143 GAsyncReadyCallback callback,
146 EmpathyServerSASLHandler *self = EMPATHY_SERVER_SASL_HANDLER (initable);
147 EmpathyServerSASLHandlerPriv *priv = self->priv;
149 g_assert (priv->account != NULL);
151 priv->async_init_res = g_simple_async_result_new (G_OBJECT (self),
152 callback, user_data, empathy_server_sasl_handler_new_async);
154 empathy_keyring_get_password_async (priv->account,
155 empathy_server_sasl_handler_get_password_async_cb, self);
159 empathy_server_sasl_handler_init_finish (GAsyncInitable *initable,
163 if (g_simple_async_result_propagate_error (G_SIMPLE_ASYNC_RESULT (res),
171 async_initable_iface_init (GAsyncInitableIface *iface)
173 iface->init_async = empathy_server_sasl_handler_init_async;
174 iface->init_finish = empathy_server_sasl_handler_init_finish;
178 channel_invalidated_cb (TpProxy *proxy,
182 EmpathyServerSASLHandler *self)
184 g_signal_emit (self, signals[INVALIDATED], 0);
188 empathy_server_sasl_handler_constructed (GObject *object)
190 EmpathyServerSASLHandlerPriv *priv = EMPATHY_SERVER_SASL_HANDLER (object)->priv;
191 GError *error = NULL;
193 tp_cli_channel_interface_sasl_authentication_connect_to_sasl_status_changed (
194 priv->channel, sasl_status_changed_cb, NULL, NULL, object, &error);
198 DEBUG ("Failed to connect to SASLStatusChanged: %s", error->message);
199 g_clear_error (&error);
202 tp_g_signal_connect_object (priv->channel, "invalidated",
203 G_CALLBACK (channel_invalidated_cb), object, 0);
207 empathy_server_sasl_handler_get_property (GObject *object,
212 EmpathyServerSASLHandlerPriv *priv = EMPATHY_SERVER_SASL_HANDLER (object)->priv;
217 g_value_set_object (value, priv->channel);
220 g_value_set_object (value, priv->account);
223 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
229 empathy_server_sasl_handler_set_property (GObject *object,
234 EmpathyServerSASLHandlerPriv *priv = EMPATHY_SERVER_SASL_HANDLER (object)->priv;
239 priv->channel = g_value_dup_object (value);
242 priv->account = g_value_dup_object (value);
245 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
251 empathy_server_sasl_handler_dispose (GObject *object)
253 EmpathyServerSASLHandlerPriv *priv = EMPATHY_SERVER_SASL_HANDLER (object)->priv;
255 DEBUG ("%p", object);
257 tp_clear_object (&priv->channel);
258 tp_clear_object (&priv->account);
260 G_OBJECT_CLASS (empathy_server_sasl_handler_parent_class)->dispose (object);
264 empathy_server_sasl_handler_finalize (GObject *object)
266 EmpathyServerSASLHandlerPriv *priv = EMPATHY_SERVER_SASL_HANDLER (object)->priv;
268 DEBUG ("%p", object);
270 tp_clear_pointer (&priv->password, g_free);
272 G_OBJECT_CLASS (empathy_server_sasl_handler_parent_class)->finalize (object);
276 empathy_server_sasl_handler_class_init (EmpathyServerSASLHandlerClass *klass)
278 GObjectClass *oclass = G_OBJECT_CLASS (klass);
281 oclass->constructed = empathy_server_sasl_handler_constructed;
282 oclass->get_property = empathy_server_sasl_handler_get_property;
283 oclass->set_property = empathy_server_sasl_handler_set_property;
284 oclass->dispose = empathy_server_sasl_handler_dispose;
285 oclass->dispose = empathy_server_sasl_handler_finalize;
287 g_type_class_add_private (klass, sizeof (EmpathyServerSASLHandlerPriv));
289 pspec = g_param_spec_object ("channel", "The TpChannel",
290 "The TpChannel this handler is supposed to handle.",
292 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS);
293 g_object_class_install_property (oclass, PROP_CHANNEL, pspec);
295 pspec = g_param_spec_object ("account", "The TpAccount",
296 "The TpAccount this channel belongs to.",
298 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS);
299 g_object_class_install_property (oclass, PROP_ACCOUNT, pspec);
301 signals[INVALIDATED] = g_signal_new ("invalidated",
302 G_TYPE_FROM_CLASS (klass),
303 G_SIGNAL_RUN_LAST, 0,
305 g_cclosure_marshal_VOID__VOID,
310 empathy_server_sasl_handler_init (EmpathyServerSASLHandler *self)
312 self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
313 EMPATHY_TYPE_SERVER_SASL_HANDLER, EmpathyServerSASLHandlerPriv);
316 EmpathyServerSASLHandler *
317 empathy_server_sasl_handler_new_finish (GAsyncResult *result,
320 GObject *object, *source_object;
322 source_object = g_async_result_get_source_object (result);
324 object = g_async_initable_new_finish (G_ASYNC_INITABLE (source_object),
326 g_object_unref (source_object);
329 return EMPATHY_SERVER_SASL_HANDLER (object);
335 empathy_server_sasl_handler_new_async (TpAccount *account,
337 GAsyncReadyCallback callback,
340 g_return_if_fail (TP_IS_ACCOUNT (account));
341 g_return_if_fail (TP_IS_CHANNEL (channel));
342 g_return_if_fail (callback != NULL);
344 g_async_initable_new_async (EMPATHY_TYPE_SERVER_SASL_HANDLER,
345 G_PRIORITY_DEFAULT, NULL, callback, user_data,
352 start_mechanism_with_data_cb (TpChannel *proxy,
355 GObject *weak_object)
359 DEBUG ("Failed to start mechanism: %s", error->message);
363 DEBUG ("Started mechanism successfully");
367 empathy_server_sasl_handler_set_password_cb (GObject *source,
368 GAsyncResult *result,
371 GError *error = NULL;
373 if (!empathy_keyring_set_password_finish (TP_ACCOUNT (source), result,
376 DEBUG ("Failed to set password: %s", error->message);
377 g_clear_error (&error);
381 DEBUG ("Password set successfully.");
386 empathy_server_sasl_handler_provide_password (
387 EmpathyServerSASLHandler *handler,
388 const gchar *password,
391 EmpathyServerSASLHandlerPriv *priv;
394 g_return_if_fail (EMPATHY_IS_SERVER_SASL_HANDLER (handler));
396 priv = handler->priv;
398 array = g_array_sized_new (TRUE, FALSE,
399 sizeof (gchar), strlen (password));
401 g_array_append_vals (array, password, strlen (password));
403 DEBUG ("Calling StartMechanismWithData with our password");
405 tp_cli_channel_interface_sasl_authentication_call_start_mechanism_with_data (
406 priv->channel, -1, "X-TELEPATHY-PASSWORD", array,
407 start_mechanism_with_data_cb, NULL, NULL, G_OBJECT (handler));
409 g_array_unref (array);
411 DEBUG ("%sremembering the password", remember ? "" : "not ");
415 empathy_keyring_set_password_async (priv->account, password,
416 empathy_server_sasl_handler_set_password_cb, NULL);
421 empathy_server_sasl_handler_cancel (EmpathyServerSASLHandler *handler)
423 EmpathyServerSASLHandlerPriv *priv;
425 g_return_if_fail (EMPATHY_IS_SERVER_SASL_HANDLER (handler));
427 priv = handler->priv;
429 DEBUG ("Cancelling SASL mechanism...");
431 tp_cli_channel_interface_sasl_authentication_call_abort_sasl (
432 priv->channel, -1, TP_SASL_ABORT_REASON_USER_ABORT,
433 "User cancelled the authentication",
434 NULL, NULL, NULL, NULL);
438 empathy_server_sasl_handler_get_account (EmpathyServerSASLHandler *handler)
440 EmpathyServerSASLHandlerPriv *priv;
442 g_return_val_if_fail (EMPATHY_IS_SERVER_SASL_HANDLER (handler), NULL);
444 priv = handler->priv;
446 return priv->account;
450 empathy_server_sasl_handler_has_password (EmpathyServerSASLHandler *handler)
452 EmpathyServerSASLHandlerPriv *priv;
454 g_return_val_if_fail (EMPATHY_IS_SERVER_SASL_HANDLER (handler), FALSE);
456 priv = handler->priv;
458 return (priv->password != NULL);