2 * empathy-goa-auth-handler.c - Source for Goa SASL authentication
3 * Copyright (C) 2011 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
23 #define GOA_API_IS_SUBJECT_TO_CHANGE /* awesome! */
26 #define DEBUG_FLAG EMPATHY_DEBUG_SASL
27 #include "empathy-debug.h"
28 #include "empathy-utils.h"
29 #include "empathy-goa-auth-handler.h"
30 #include "empathy-sasl-mechanisms.h"
32 struct _EmpathyGoaAuthHandlerPriv
35 gboolean client_preparing;
37 /* List of AuthData waiting for client to be created */
41 G_DEFINE_TYPE (EmpathyGoaAuthHandler, empathy_goa_auth_handler, G_TYPE_OBJECT);
44 empathy_goa_auth_handler_init (EmpathyGoaAuthHandler *self)
46 self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
47 EMPATHY_TYPE_GOA_AUTH_HANDLER, EmpathyGoaAuthHandlerPriv);
51 empathy_goa_auth_handler_dispose (GObject *object)
53 EmpathyGoaAuthHandler *self = (EmpathyGoaAuthHandler *) object;
55 /* AuthData keeps a ref on self */
56 g_assert (self->priv->auth_queue == NULL);
58 tp_clear_object (&self->priv->client);
60 G_OBJECT_CLASS (empathy_goa_auth_handler_parent_class)->dispose (object);
64 empathy_goa_auth_handler_class_init (EmpathyGoaAuthHandlerClass *klass)
66 GObjectClass *oclass = G_OBJECT_CLASS (klass);
68 oclass->dispose = empathy_goa_auth_handler_dispose;
70 g_type_class_add_private (klass, sizeof (EmpathyGoaAuthHandlerPriv));
73 EmpathyGoaAuthHandler *
74 empathy_goa_auth_handler_new (void)
76 return g_object_new (EMPATHY_TYPE_GOA_AUTH_HANDLER, NULL);
81 EmpathyGoaAuthHandler *self;
85 GoaObject *goa_object;
90 auth_data_new (EmpathyGoaAuthHandler *self,
96 data = g_slice_new0 (AuthData);
97 data->self = g_object_ref (self);
98 data->channel = g_object_ref (channel);
99 data->account = g_object_ref (account);
105 auth_data_free (AuthData *data)
107 tp_clear_object (&data->self);
108 tp_clear_object (&data->channel);
109 tp_clear_object (&data->account);
110 tp_clear_object (&data->goa_object);
111 g_free (data->access_token);
112 g_slice_free (AuthData, data);
116 fail_auth (AuthData *data)
118 DEBUG ("Auth failed for account %s",
119 tp_proxy_get_object_path (data->account));
121 tp_channel_close_async (data->channel, NULL, NULL);
122 auth_data_free (data);
126 auth_cb (GObject *source,
127 GAsyncResult *result,
130 TpChannel *channel = (TpChannel *) source;
131 AuthData *data = user_data;
132 GError *error = NULL;
134 if (!empathy_sasl_auth_finish (channel, result, &error))
136 DEBUG ("SASL Mechanism error: %s", error->message);
138 g_clear_error (&error);
143 tp_channel_close_async (channel, NULL, NULL);
144 auth_data_free (data);
148 got_oauth2_access_token_cb (GObject *source,
149 GAsyncResult *result,
152 GoaOAuth2Based *oauth2 = (GoaOAuth2Based *) source;
153 AuthData *data = user_data;
156 GError *error = NULL;
158 if (!goa_oauth2_based_call_get_access_token_finish (oauth2,
159 &access_token, &expires_in, result, &error))
161 DEBUG ("Failed to get access token: %s", error->message);
163 g_clear_error (&error);
167 DEBUG ("Got access token for %s:\n%s",
168 tp_proxy_get_object_path (data->account),
171 switch (empathy_sasl_channel_select_mechanism (data->channel))
173 case EMPATHY_SASL_MECHANISM_FACEBOOK:
174 empathy_sasl_auth_facebook_async (data->channel,
175 goa_oauth2_based_get_client_id (oauth2), access_token,
179 case EMPATHY_SASL_MECHANISM_WLM:
180 empathy_sasl_auth_wlm_async (data->channel,
186 g_assert_not_reached ();
189 g_free (access_token);
193 ensure_credentials_cb (GObject *source,
194 GAsyncResult *result,
197 AuthData *data = user_data;
198 GoaAccount *goa_account = (GoaAccount *) source;
199 GoaOAuth2Based *oauth2;
201 GError *error = NULL;
203 if (!goa_account_call_ensure_credentials_finish (goa_account, &expires_in,
206 DEBUG ("Failed to EnsureCredentials: %s", error->message);
208 g_clear_error (&error);
212 /* We support only oaut2 */
213 oauth2 = goa_object_get_oauth2_based (data->goa_object);
216 DEBUG ("GoaObject does not implement oauth2");
221 DEBUG ("Goa daemon has credentials for %s, get the access token",
222 tp_proxy_get_object_path (data->account));
224 goa_oauth2_based_call_get_access_token (oauth2, NULL,
225 got_oauth2_access_token_cb, data);
227 g_object_unref (oauth2);
231 start_auth (AuthData *data)
233 EmpathyGoaAuthHandler *self = data->self;
234 const GValue *id_value;
236 GList *goa_accounts, *l;
237 gboolean found = FALSE;
239 id_value = tp_account_get_storage_identifier (data->account);
240 id = g_value_get_string (id_value);
242 goa_accounts = goa_client_get_accounts (self->priv->client);
243 for (l = goa_accounts; l != NULL && !found; l = l->next)
245 GoaObject *goa_object = l->data;
246 GoaAccount *goa_account;
248 goa_account = goa_object_get_account (goa_object);
249 if (!tp_strdiff (goa_account_get_id (goa_account), id))
251 data->goa_object = g_object_ref (goa_object);
253 DEBUG ("Found the GoaAccount for %s, ensure credentials",
254 tp_proxy_get_object_path (data->account));
256 goa_account_call_ensure_credentials (goa_account, NULL,
257 ensure_credentials_cb, data);
262 g_object_unref (goa_account);
264 g_list_free_full (goa_accounts, g_object_unref);
268 DEBUG ("Cannot find GoaAccount");
274 client_new_cb (GObject *source,
275 GAsyncResult *result,
278 EmpathyGoaAuthHandler *self = user_data;
280 GError *error = NULL;
282 self->priv->client_preparing = FALSE;
283 self->priv->client = goa_client_new_finish (result, &error);
284 if (self->priv->client == NULL)
286 DEBUG ("Error getting GoaClient: %s", error->message);
287 g_clear_error (&error);
290 /* process queued data */
291 for (l = self->priv->auth_queue; l != NULL; l = l->next)
293 AuthData *data = l->data;
295 if (self->priv->client != NULL)
301 tp_clear_pointer (&self->priv->auth_queue, g_list_free);
305 empathy_goa_auth_handler_start (EmpathyGoaAuthHandler *self,
311 g_return_if_fail (TP_IS_CHANNEL (channel));
312 g_return_if_fail (TP_IS_ACCOUNT (account));
313 g_return_if_fail (empathy_goa_auth_handler_supports (self, channel, account));
315 DEBUG ("Start Goa auth for account: %s",
316 tp_proxy_get_object_path (account));
318 data = auth_data_new (self, channel, account);
320 if (self->priv->client == NULL)
322 /* GOA client not ready yet, queue data */
323 if (!self->priv->client_preparing)
325 goa_client_new (NULL, client_new_cb, self);
326 self->priv->client_preparing = TRUE;
329 self->priv->auth_queue = g_list_prepend (self->priv->auth_queue, data);
338 empathy_goa_auth_handler_supports (EmpathyGoaAuthHandler *self,
342 const gchar *provider;
343 EmpathySaslMechanism mech;
345 g_return_val_if_fail (TP_IS_CHANNEL (channel), FALSE);
346 g_return_val_if_fail (TP_IS_ACCOUNT (account), FALSE);
348 provider = tp_account_get_storage_provider (account);
349 if (tp_strdiff (provider, EMPATHY_GOA_PROVIDER))
352 mech = empathy_sasl_channel_select_mechanism (channel);
353 return mech == EMPATHY_SASL_MECHANISM_FACEBOOK ||
354 mech == EMPATHY_SASL_MECHANISM_WLM;