2 * empathy-sasl-mechanisms.h - Header for SASL authentication mechanisms
3 * Copyright (C) 2012 Collabora Ltd.
4 * @author Xavier Claessens <xavier.claessens@collabora.co.uk>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 #include "empathy-sasl-mechanisms.h"
24 #include <libsoup/soup.h>
25 #include <tp-account-widgets/tpaw-utils.h>
27 #define DEBUG_FLAG EMPATHY_DEBUG_SASL
28 #include "empathy-debug.h"
29 #include "empathy-utils.h"
31 #define MECH_FACEBOOK "X-FACEBOOK-PLATFORM"
32 #define MECH_WLM "X-MESSENGER-OAUTH2"
33 #define MECH_GOOGLE "X-OAUTH2"
34 #define MECH_PASSWORD "X-TELEPATHY-PASSWORD"
38 EmpathySaslMechanism id;
42 static SupportedMech supported_mechanisms[] = {
43 { EMPATHY_SASL_MECHANISM_FACEBOOK, MECH_FACEBOOK },
44 { EMPATHY_SASL_MECHANISM_WLM, MECH_WLM },
45 { EMPATHY_SASL_MECHANISM_GOOGLE, MECH_GOOGLE },
47 /* Must be the last one, otherwise empathy_sasl_channel_select_mechanism()
48 * will prefer password over web auth for servers supporting both. */
49 { EMPATHY_SASL_MECHANISM_PASSWORD, MECH_PASSWORD }
53 generic_cb (TpChannel *proxy,
58 GSimpleAsyncResult *result = user_data;
62 g_simple_async_result_set_from_error (result, error);
63 g_simple_async_result_complete (result);
68 sasl_status_changed_cb (TpChannel *channel,
70 const gchar *dbus_error,
75 GSimpleAsyncResult *result = user_data;
79 case TP_SASL_STATUS_SERVER_SUCCEEDED:
80 tp_cli_channel_interface_sasl_authentication_call_accept_sasl (channel,
81 -1, generic_cb, g_object_ref (result), g_object_unref, NULL);
84 case TP_SASL_STATUS_SERVER_FAILED:
85 case TP_SASL_STATUS_CLIENT_FAILED:
89 tp_proxy_dbus_error_to_gerror (channel, dbus_error,
90 tp_asv_get_string (details, "debug-message"), &error);
92 DEBUG ("SASL failed: %s", error->message);
94 g_simple_async_result_take_error (result, error);
95 g_simple_async_result_complete (result);
99 case TP_SASL_STATUS_SUCCEEDED:
100 DEBUG ("SASL succeeded");
102 g_simple_async_result_complete (result);
110 static GSimpleAsyncResult *
111 empathy_sasl_auth_common_async (TpChannel *channel,
112 GAsyncReadyCallback callback,
115 GSimpleAsyncResult *result;
116 GError *error = NULL;
118 g_return_val_if_fail (TP_IS_CHANNEL (channel), NULL);
119 g_return_val_if_fail (tp_proxy_has_interface_by_id (channel,
120 TP_IFACE_QUARK_CHANNEL_INTERFACE_SASL_AUTHENTICATION), NULL);
122 result = g_simple_async_result_new ((GObject *) channel,
123 callback, user_data, empathy_sasl_auth_common_async);
125 tp_cli_channel_interface_sasl_authentication_connect_to_sasl_status_changed (
126 channel, sasl_status_changed_cb,
127 g_object_ref (result), g_object_unref, NULL, &error);
128 g_assert_no_error (error);
141 facebook_data_free (FacebookData *data)
143 g_object_unref (data->channel);
144 g_free (data->client_id);
145 g_free (data->access_token);
146 g_slice_free (FacebookData, data);
150 facebook_new_challenge_cb (TpChannel *channel,
151 const GArray *challenge,
153 GObject *weak_object)
155 GSimpleAsyncResult *result = user_data;
160 GArray *response_array;
162 DEBUG ("new challenge: %s", challenge->data);
164 data = g_simple_async_result_get_op_res_gpointer (result);
166 h = soup_form_decode (challenge->data);
168 /* See https://developers.facebook.com/docs/chat/#platauth */
169 params = g_hash_table_new (g_str_hash, g_str_equal);
170 g_hash_table_insert (params, "method", g_hash_table_lookup (h, "method"));
171 g_hash_table_insert (params, "nonce", g_hash_table_lookup (h, "nonce"));
172 g_hash_table_insert (params, "access_token", data->access_token);
173 g_hash_table_insert (params, "api_key", data->client_id);
174 g_hash_table_insert (params, "call_id", "0");
175 g_hash_table_insert (params, "v", "1.0");
177 response = soup_form_encode_hash (params);
178 DEBUG ("Response: %s", response);
180 response_array = g_array_new (FALSE, FALSE, sizeof (gchar));
181 g_array_append_vals (response_array, response, strlen (response));
183 tp_cli_channel_interface_sasl_authentication_call_respond (data->channel, -1,
184 response_array, generic_cb, g_object_ref (result), g_object_unref, NULL);
186 g_hash_table_unref (h);
187 g_hash_table_unref (params);
189 g_array_unref (response_array);
193 empathy_sasl_auth_facebook_async (TpChannel *channel,
194 const gchar *client_id,
195 const gchar *access_token,
196 GAsyncReadyCallback callback,
199 GSimpleAsyncResult *result;
201 GError *error = NULL;
203 result = empathy_sasl_auth_common_async (channel, callback, user_data);
205 g_return_if_fail (result != NULL);
206 g_return_if_fail (empathy_sasl_channel_supports_mechanism (channel,
208 g_return_if_fail (!tp_str_empty (client_id));
209 g_return_if_fail (!tp_str_empty (access_token));
211 DEBUG ("Start %s mechanism", MECH_FACEBOOK);
213 data = g_slice_new0 (FacebookData);
214 data->channel = g_object_ref (channel);
215 data->client_id = g_strdup (client_id);
216 data->access_token = g_strdup (access_token);
218 g_simple_async_result_set_op_res_gpointer (result, data,
219 (GDestroyNotify) facebook_data_free);
221 tp_cli_channel_interface_sasl_authentication_connect_to_new_challenge (
222 channel, facebook_new_challenge_cb,
223 g_object_ref (result), g_object_unref,
225 g_assert_no_error (error);
227 tp_cli_channel_interface_sasl_authentication_call_start_mechanism (
228 channel, -1, MECH_FACEBOOK, generic_cb,
229 g_object_ref (result), g_object_unref, NULL);
231 g_object_unref (result);
235 empathy_sasl_auth_wlm_async (TpChannel *channel,
236 const gchar *access_token,
237 GAsyncReadyCallback callback,
240 GSimpleAsyncResult *result;
241 guchar *token_decoded;
242 gsize token_decoded_len;
243 GArray *token_decoded_array;
245 result = empathy_sasl_auth_common_async (channel, callback, user_data);
247 g_return_if_fail (result != NULL);
248 g_return_if_fail (empathy_sasl_channel_supports_mechanism (channel,
250 g_return_if_fail (!tp_str_empty (access_token));
252 DEBUG ("Start %s mechanism", MECH_WLM);
254 /* Wocky will base64 encode, but token actually already is base64, so we
255 * decode now and it will be re-encoded. */
256 token_decoded = g_base64_decode (access_token, &token_decoded_len);
257 token_decoded_array = g_array_new (FALSE, FALSE, sizeof (guchar));
258 g_array_append_vals (token_decoded_array, token_decoded, token_decoded_len);
260 tp_cli_channel_interface_sasl_authentication_call_start_mechanism_with_data (
261 channel, -1, MECH_WLM, token_decoded_array,
262 generic_cb, g_object_ref (result), g_object_unref, NULL);
264 g_array_unref (token_decoded_array);
265 g_free (token_decoded);
266 g_object_unref (result);
270 empathy_sasl_auth_google_async (TpChannel *channel,
271 const gchar *username,
272 const gchar *access_token,
273 GAsyncReadyCallback callback,
276 GSimpleAsyncResult *result;
279 result = empathy_sasl_auth_common_async (channel, callback, user_data);
281 g_return_if_fail (result != NULL);
282 g_return_if_fail (empathy_sasl_channel_supports_mechanism (channel,
284 g_return_if_fail (!tp_str_empty (username));
285 g_return_if_fail (!tp_str_empty (access_token));
287 DEBUG ("Start %s mechanism", MECH_GOOGLE);
289 credential = g_array_sized_new (FALSE, FALSE, sizeof (gchar),
290 strlen (access_token) + strlen (username) + 2);
292 g_array_append_val (credential, "\0");
293 g_array_append_vals (credential, username, strlen (username));
294 g_array_append_val (credential, "\0");
295 g_array_append_vals (credential, access_token, strlen (access_token));
297 tp_cli_channel_interface_sasl_authentication_call_start_mechanism_with_data (
298 channel, -1, MECH_GOOGLE, credential,
299 generic_cb, g_object_ref (result), g_object_unref, NULL);
301 g_array_unref (credential);
302 g_object_unref (result);
306 empathy_sasl_auth_password_async (TpChannel *channel,
307 const gchar *password,
308 GAsyncReadyCallback callback,
311 GSimpleAsyncResult *result;
312 GArray *password_array;
314 result = empathy_sasl_auth_common_async (channel, callback, user_data);
316 g_return_if_fail (result != NULL);
317 g_return_if_fail (empathy_sasl_channel_supports_mechanism (channel,
319 g_return_if_fail (!tp_str_empty (password));
321 DEBUG ("Start %s mechanism", MECH_PASSWORD);
323 password_array = g_array_sized_new (FALSE, FALSE, sizeof (gchar),
325 g_array_append_vals (password_array, password, strlen (password));
327 tp_cli_channel_interface_sasl_authentication_call_start_mechanism_with_data (
328 channel, -1, MECH_PASSWORD, password_array,
329 generic_cb, g_object_ref (result), g_object_unref, NULL);
331 g_array_unref (password_array);
332 g_object_unref (result);
336 empathy_sasl_auth_finish (TpChannel *channel,
337 GAsyncResult *result,
340 tpaw_implement_finish_void (channel, empathy_sasl_auth_common_async);
344 empathy_sasl_channel_supports_mechanism (TpChannel *channel,
345 const gchar *mechanism)
348 GStrv available_mechanisms;
351 props = tp_channel_dup_immutable_properties (channel);
353 g_variant_lookup (props,
354 TP_PROP_CHANNEL_INTERFACE_SASL_AUTHENTICATION_AVAILABLE_MECHANISMS,
355 "^as", &available_mechanisms);
357 result = tp_strv_contains ((const gchar * const *) available_mechanisms,
360 g_variant_unref (props);
361 g_strfreev (available_mechanisms);
366 empathy_sasl_channel_select_mechanism (TpChannel *channel)
370 for (i = 0; i < G_N_ELEMENTS (supported_mechanisms); i++)
372 if (empathy_sasl_channel_supports_mechanism (channel,
373 supported_mechanisms[i].name))
374 return supported_mechanisms[i].id;
377 return EMPATHY_SASL_MECHANISM_UNSUPPORTED;