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 DEBUG ("Calling AcceptSASL");
93 tp_cli_channel_interface_sasl_authentication_call_accept_sasl (
94 priv->channel, -1, NULL, NULL, NULL, NULL);
96 else if (status == TP_SASL_STATUS_SUCCEEDED)
98 DEBUG ("SASL succeeded, calling Close");
99 tp_cli_channel_call_close (priv->channel, -1,
100 NULL, NULL, NULL, NULL);
105 empathy_server_sasl_handler_give_password (gpointer data)
107 EmpathyServerSASLHandler *self = data;
108 EmpathyServerSASLHandlerPriv *priv = self->priv;
110 empathy_server_sasl_handler_provide_password (self,
111 priv->password, FALSE);
117 empathy_server_sasl_handler_get_password_async_cb (GObject *source,
118 GAsyncResult *result,
121 EmpathyServerSASLHandlerPriv *priv;
122 const gchar *password;
123 GError *error = NULL;
125 priv = EMPATHY_SERVER_SASL_HANDLER (user_data)->priv;
127 password = empathy_keyring_get_password_finish (TP_ACCOUNT (source),
130 if (password != NULL)
132 priv->password = g_strdup (password);
134 /* Do this in an idle so the async result will get there
136 g_idle_add (empathy_server_sasl_handler_give_password, user_data);
139 g_simple_async_result_complete (priv->async_init_res);
140 tp_clear_object (&priv->async_init_res);
144 empathy_server_sasl_handler_init_async (GAsyncInitable *initable,
146 GCancellable *cancellable,
147 GAsyncReadyCallback callback,
150 EmpathyServerSASLHandler *self = EMPATHY_SERVER_SASL_HANDLER (initable);
151 EmpathyServerSASLHandlerPriv *priv = self->priv;
153 g_assert (priv->account != NULL);
155 priv->async_init_res = g_simple_async_result_new (G_OBJECT (self),
156 callback, user_data, empathy_server_sasl_handler_new_async);
158 empathy_keyring_get_password_async (priv->account,
159 empathy_server_sasl_handler_get_password_async_cb, self);
163 empathy_server_sasl_handler_init_finish (GAsyncInitable *initable,
167 if (g_simple_async_result_propagate_error (G_SIMPLE_ASYNC_RESULT (res),
175 async_initable_iface_init (GAsyncInitableIface *iface)
177 iface->init_async = empathy_server_sasl_handler_init_async;
178 iface->init_finish = empathy_server_sasl_handler_init_finish;
182 channel_invalidated_cb (TpProxy *proxy,
186 EmpathyServerSASLHandler *self)
188 g_signal_emit (self, signals[INVALIDATED], 0);
192 empathy_server_sasl_handler_constructed (GObject *object)
194 EmpathyServerSASLHandlerPriv *priv = EMPATHY_SERVER_SASL_HANDLER (object)->priv;
195 GError *error = NULL;
197 tp_cli_channel_interface_sasl_authentication_connect_to_sasl_status_changed (
198 priv->channel, sasl_status_changed_cb, NULL, NULL, object, &error);
202 DEBUG ("Failed to connect to SASLStatusChanged: %s", error->message);
203 g_clear_error (&error);
206 tp_g_signal_connect_object (priv->channel, "invalidated",
207 G_CALLBACK (channel_invalidated_cb), object, 0);
211 empathy_server_sasl_handler_get_property (GObject *object,
216 EmpathyServerSASLHandlerPriv *priv = EMPATHY_SERVER_SASL_HANDLER (object)->priv;
221 g_value_set_object (value, priv->channel);
224 g_value_set_object (value, priv->account);
227 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
233 empathy_server_sasl_handler_set_property (GObject *object,
238 EmpathyServerSASLHandlerPriv *priv = EMPATHY_SERVER_SASL_HANDLER (object)->priv;
243 priv->channel = g_value_dup_object (value);
246 priv->account = g_value_dup_object (value);
249 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
255 empathy_server_sasl_handler_dispose (GObject *object)
257 EmpathyServerSASLHandlerPriv *priv = EMPATHY_SERVER_SASL_HANDLER (object)->priv;
259 DEBUG ("%p", object);
261 tp_clear_object (&priv->channel);
262 tp_clear_object (&priv->account);
264 G_OBJECT_CLASS (empathy_server_sasl_handler_parent_class)->dispose (object);
268 empathy_server_sasl_handler_finalize (GObject *object)
270 EmpathyServerSASLHandlerPriv *priv = EMPATHY_SERVER_SASL_HANDLER (object)->priv;
272 DEBUG ("%p", object);
274 tp_clear_pointer (&priv->password, g_free);
276 G_OBJECT_CLASS (empathy_server_sasl_handler_parent_class)->finalize (object);
280 empathy_server_sasl_handler_class_init (EmpathyServerSASLHandlerClass *klass)
282 GObjectClass *oclass = G_OBJECT_CLASS (klass);
285 oclass->constructed = empathy_server_sasl_handler_constructed;
286 oclass->get_property = empathy_server_sasl_handler_get_property;
287 oclass->set_property = empathy_server_sasl_handler_set_property;
288 oclass->dispose = empathy_server_sasl_handler_dispose;
289 oclass->dispose = empathy_server_sasl_handler_finalize;
291 g_type_class_add_private (klass, sizeof (EmpathyServerSASLHandlerPriv));
293 pspec = g_param_spec_object ("channel", "The TpChannel",
294 "The TpChannel this handler is supposed to handle.",
296 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS);
297 g_object_class_install_property (oclass, PROP_CHANNEL, pspec);
299 pspec = g_param_spec_object ("account", "The TpAccount",
300 "The TpAccount this channel belongs to.",
302 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS);
303 g_object_class_install_property (oclass, PROP_ACCOUNT, pspec);
305 signals[INVALIDATED] = g_signal_new ("invalidated",
306 G_TYPE_FROM_CLASS (klass),
307 G_SIGNAL_RUN_LAST, 0,
309 g_cclosure_marshal_VOID__VOID,
314 empathy_server_sasl_handler_init (EmpathyServerSASLHandler *self)
316 self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
317 EMPATHY_TYPE_SERVER_SASL_HANDLER, EmpathyServerSASLHandlerPriv);
320 EmpathyServerSASLHandler *
321 empathy_server_sasl_handler_new_finish (GAsyncResult *result,
324 GObject *object, *source_object;
326 source_object = g_async_result_get_source_object (result);
328 object = g_async_initable_new_finish (G_ASYNC_INITABLE (source_object),
330 g_object_unref (source_object);
333 return EMPATHY_SERVER_SASL_HANDLER (object);
339 empathy_server_sasl_handler_new_async (TpAccount *account,
341 GAsyncReadyCallback callback,
344 g_return_if_fail (TP_IS_ACCOUNT (account));
345 g_return_if_fail (TP_IS_CHANNEL (channel));
346 g_return_if_fail (callback != NULL);
348 g_async_initable_new_async (EMPATHY_TYPE_SERVER_SASL_HANDLER,
349 G_PRIORITY_DEFAULT, NULL, callback, user_data,
356 start_mechanism_with_data_cb (TpChannel *proxy,
359 GObject *weak_object)
363 DEBUG ("Failed to start mechanism: %s", error->message);
367 DEBUG ("Started mechanism successfully");
371 empathy_server_sasl_handler_set_password_cb (GObject *source,
372 GAsyncResult *result,
375 GError *error = NULL;
377 if (!empathy_keyring_set_password_finish (TP_ACCOUNT (source), result,
380 DEBUG ("Failed to set password: %s", error->message);
381 g_clear_error (&error);
385 DEBUG ("Password set successfully.");
390 empathy_server_sasl_handler_provide_password (
391 EmpathyServerSASLHandler *handler,
392 const gchar *password,
395 EmpathyServerSASLHandlerPriv *priv;
398 g_return_if_fail (EMPATHY_IS_SERVER_SASL_HANDLER (handler));
400 priv = handler->priv;
402 array = g_array_sized_new (TRUE, FALSE,
403 sizeof (gchar), strlen (password));
405 g_array_append_vals (array, password, strlen (password));
407 DEBUG ("Calling StartMechanismWithData with our password");
409 tp_cli_channel_interface_sasl_authentication_call_start_mechanism_with_data (
410 priv->channel, -1, "X-TELEPATHY-PASSWORD", array,
411 start_mechanism_with_data_cb, NULL, NULL, G_OBJECT (handler));
413 g_array_unref (array);
415 DEBUG ("%sremembering the password", remember ? "" : "not ");
419 empathy_keyring_set_password_async (priv->account, password,
420 empathy_server_sasl_handler_set_password_cb, NULL);
425 empathy_server_sasl_handler_cancel (EmpathyServerSASLHandler *handler)
427 EmpathyServerSASLHandlerPriv *priv;
429 g_return_if_fail (EMPATHY_IS_SERVER_SASL_HANDLER (handler));
431 priv = handler->priv;
433 DEBUG ("Cancelling SASL mechanism...");
435 tp_cli_channel_interface_sasl_authentication_call_abort_sasl (
436 priv->channel, -1, TP_SASL_ABORT_REASON_USER_ABORT,
437 "User cancelled the authentication",
438 NULL, NULL, NULL, NULL);
442 empathy_server_sasl_handler_get_account (EmpathyServerSASLHandler *handler)
444 EmpathyServerSASLHandlerPriv *priv;
446 g_return_val_if_fail (EMPATHY_IS_SERVER_SASL_HANDLER (handler), NULL);
448 priv = handler->priv;
450 return priv->account;
454 empathy_server_sasl_handler_has_password (EmpathyServerSASLHandler *handler)
456 EmpathyServerSASLHandlerPriv *priv;
458 g_return_val_if_fail (EMPATHY_IS_SERVER_SASL_HANDLER (handler), FALSE);
460 priv = handler->priv;
462 return (priv->password != NULL);