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>
24 #define DEBUG_FLAG EMPATHY_DEBUG_SASL
25 #include "empathy-debug.h"
26 #include "empathy-utils.h"
40 static guint signals[LAST_SIGNAL] = {0};
46 GSimpleAsyncResult *result;
47 } EmpathyServerSASLHandlerPriv;
49 G_DEFINE_TYPE (EmpathyServerSASLHandler, empathy_server_sasl_handler,
52 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyServerSASLHandler);
54 static const gchar *sasl_statuses[] = {
65 sasl_status_changed_cb (TpChannel *channel,
72 EmpathyServerSASLHandlerPriv *priv = GET_PRIV (weak_object);
74 DEBUG ("SASL status changed to '%s'", sasl_statuses[status]);
76 if (status == TP_SASL_STATUS_SERVER_SUCCEEDED)
78 tp_cli_channel_interface_sasl_authentication_call_accept_sasl (
79 priv->channel, -1, NULL, NULL, NULL, NULL);
81 tp_cli_channel_call_close (priv->channel, -1,
82 NULL, NULL, NULL, NULL);
87 channel_invalidated_cb (TpProxy *proxy,
91 EmpathyServerSASLHandler *self)
93 g_signal_emit (self, signals[INVALIDATED], 0);
97 empathy_server_sasl_handler_constructed (GObject *object)
99 EmpathyServerSASLHandlerPriv *priv;
100 GError *error = NULL;
102 priv = GET_PRIV (object);
104 tp_cli_channel_interface_sasl_authentication_connect_to_sasl_status_changed (priv->channel,
105 sasl_status_changed_cb, NULL, NULL, object, &error);
109 DEBUG ("Failed to connect to SASLStatusChanged: %s", error->message);
110 g_clear_error (&error);
113 tp_g_signal_connect_object (priv->channel, "invalidated",
114 G_CALLBACK (channel_invalidated_cb), object, 0);
118 empathy_server_sasl_handler_get_property (GObject *object,
123 EmpathyServerSASLHandlerPriv *priv = GET_PRIV (object);
128 g_value_set_object (value, priv->channel);
131 g_value_set_object (value, priv->account);
134 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
140 empathy_server_sasl_handler_set_property (GObject *object,
145 EmpathyServerSASLHandlerPriv *priv = GET_PRIV (object);
150 priv->channel = g_value_dup_object (value);
153 priv->account = g_value_dup_object (value);
156 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
162 empathy_server_sasl_handler_dispose (GObject *object)
164 EmpathyServerSASLHandlerPriv *priv = GET_PRIV (object);
166 DEBUG ("%p", object);
168 tp_clear_object (&priv->channel);
169 tp_clear_object (&priv->account);
171 G_OBJECT_CLASS (empathy_server_sasl_handler_parent_class)->dispose (object);
175 empathy_server_sasl_handler_class_init (EmpathyServerSASLHandlerClass *klass)
177 GObjectClass *oclass = G_OBJECT_CLASS (klass);
180 oclass->constructed = empathy_server_sasl_handler_constructed;
181 oclass->get_property = empathy_server_sasl_handler_get_property;
182 oclass->set_property = empathy_server_sasl_handler_set_property;
183 oclass->dispose = empathy_server_sasl_handler_dispose;
185 g_type_class_add_private (klass, sizeof (EmpathyServerSASLHandlerPriv));
187 pspec = g_param_spec_object ("channel", "The TpChannel",
188 "The TpChannel this handler is supposed to handle.",
190 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS);
191 g_object_class_install_property (oclass, PROP_CHANNEL, pspec);
193 pspec = g_param_spec_object ("account", "The TpAccount",
194 "The TpAccount this channel belongs to.",
196 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS);
197 g_object_class_install_property (oclass, PROP_ACCOUNT, pspec);
199 signals[INVALIDATED] = g_signal_new ("invalidated",
200 G_TYPE_FROM_CLASS (klass),
201 G_SIGNAL_RUN_LAST, 0,
203 g_cclosure_marshal_VOID__VOID,
208 empathy_server_sasl_handler_init (EmpathyServerSASLHandler *self)
210 self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
211 EMPATHY_TYPE_SERVER_SASL_HANDLER, EmpathyServerSASLHandlerPriv);
214 EmpathyServerSASLHandler *
215 empathy_server_sasl_handler_new (TpAccount *account,
218 g_return_val_if_fail (TP_IS_CHANNEL (channel), NULL);
220 return g_object_new (EMPATHY_TYPE_SERVER_SASL_HANDLER,
227 start_mechanism_with_data_cb (TpChannel *proxy,
230 GObject *weak_object)
234 DEBUG ("Failed to start mechanism: %s", error->message);
238 DEBUG ("Started mechanism successfully");
242 empathy_server_sasl_handler_provide_password (
243 EmpathyServerSASLHandler *handler,
244 const gchar *password,
247 EmpathyServerSASLHandlerPriv *priv;
250 g_return_if_fail (EMPATHY_IS_SERVER_SASL_HANDLER (handler));
252 priv = GET_PRIV (handler);
254 array = g_array_sized_new (TRUE, FALSE,
255 sizeof (gchar), strlen (password));
257 g_array_append_vals (array, password, strlen (password));
259 DEBUG ("Calling StartMechanismWithData with our password");
261 tp_cli_channel_interface_sasl_authentication_call_start_mechanism_with_data (
262 priv->channel, -1, "X-TELEPATHY-PASSWORD", array, start_mechanism_with_data_cb,
263 NULL, NULL, G_OBJECT (handler));
265 g_array_unref (array);
267 DEBUG ("%sremembering the password", remember ? "" : "not ");
276 empathy_server_sasl_handler_cancel (EmpathyServerSASLHandler *handler)
278 EmpathyServerSASLHandlerPriv *priv;
280 g_return_if_fail (EMPATHY_IS_SERVER_SASL_HANDLER (handler));
282 priv = GET_PRIV (handler);
284 DEBUG ("Cancelling SASL mechanism...");
286 tp_cli_channel_interface_sasl_authentication_call_abort_sasl (
287 priv->channel, -1, TP_SASL_ABORT_REASON_USER_ABORT,
288 "User cancelled the authentication",
289 NULL, NULL, NULL, NULL);
293 empathy_server_sasl_handler_get_account (EmpathyServerSASLHandler *handler)
295 EmpathyServerSASLHandlerPriv *priv;
297 g_return_val_if_fail (EMPATHY_IS_SERVER_SASL_HANDLER (handler), NULL);
299 priv = GET_PRIV (handler);
301 return priv->account;