2 * empathy-auth-uoa.c - Source for Uoa SASL authentication
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
23 #include <libaccounts-glib/ag-account.h>
24 #include <libaccounts-glib/ag-account-service.h>
25 #include <libaccounts-glib/ag-auth-data.h>
26 #include <libaccounts-glib/ag-manager.h>
27 #include <libaccounts-glib/ag-service.h>
29 #include <libsignon-glib/signon-identity.h>
30 #include <libsignon-glib/signon-auth-session.h>
32 #define DEBUG_FLAG EMPATHY_DEBUG_SASL
33 #include "empathy-debug.h"
34 #include "empathy-utils.h"
35 #include "empathy-uoa-auth-handler.h"
36 #include "empathy-uoa-utils.h"
37 #include "empathy-sasl-mechanisms.h"
39 struct _EmpathyUoaAuthHandlerPriv
44 G_DEFINE_TYPE (EmpathyUoaAuthHandler, empathy_uoa_auth_handler, G_TYPE_OBJECT);
47 empathy_uoa_auth_handler_init (EmpathyUoaAuthHandler *self)
49 self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
50 EMPATHY_TYPE_UOA_AUTH_HANDLER, EmpathyUoaAuthHandlerPriv);
52 self->priv->manager = empathy_uoa_manager_dup ();
56 empathy_uoa_auth_handler_dispose (GObject *object)
58 EmpathyUoaAuthHandler *self = (EmpathyUoaAuthHandler *) object;
60 tp_clear_object (&self->priv->manager);
62 G_OBJECT_CLASS (empathy_uoa_auth_handler_parent_class)->dispose (object);
66 empathy_uoa_auth_handler_class_init (EmpathyUoaAuthHandlerClass *klass)
68 GObjectClass *oclass = G_OBJECT_CLASS (klass);
70 oclass->dispose = empathy_uoa_auth_handler_dispose;
72 g_type_class_add_private (klass, sizeof (EmpathyUoaAuthHandlerPriv));
75 EmpathyUoaAuthHandler *
76 empathy_uoa_auth_handler_new (void)
78 return g_object_new (EMPATHY_TYPE_UOA_AUTH_HANDLER, NULL);
84 AgAuthData *auth_data;
85 SignonAuthSession *session;
86 SignonIdentity *identity;
92 auth_context_new (TpChannel *channel,
93 AgAuthData *auth_data,
94 SignonAuthSession *session,
95 SignonIdentity *identity)
99 ctx = g_slice_new0 (AuthContext);
100 ctx->channel = g_object_ref (channel);
101 ctx->auth_data = ag_auth_data_ref (auth_data);
102 ctx->session = g_object_ref (session);
103 ctx->identity = g_object_ref (identity);
109 auth_context_free (AuthContext *ctx)
111 g_object_unref (ctx->channel);
112 ag_auth_data_unref (ctx->auth_data);
113 g_object_unref (ctx->session);
114 g_object_unref (ctx->identity);
115 g_free (ctx->username);
116 g_slice_free (AuthContext, ctx);
120 auth_context_done (AuthContext *ctx)
122 tp_channel_close_async (ctx->channel, NULL, NULL);
123 auth_context_free (ctx);
127 auth_cb (GObject *source,
128 GAsyncResult *result,
131 TpChannel *channel = (TpChannel *) source;
132 AuthContext *ctx = user_data;
133 GError *error = NULL;
135 if (!empathy_sasl_auth_finish (channel, result, &error))
137 GHashTable *extra_params;
139 DEBUG ("SASL Mechanism error: %s", error->message);
140 g_clear_error (&error);
142 /* Inform SSO that the access token didn't work and it should ask user
143 * to re-grant access. */
144 extra_params = tp_asv_new (
145 SIGNON_SESSION_DATA_UI_POLICY, G_TYPE_INT,
146 SIGNON_POLICY_REQUEST_PASSWORD,
149 ag_auth_data_insert_parameters (ctx->auth_data, extra_params);
151 signon_auth_session_process (ctx->session,
152 ag_auth_data_get_parameters (ctx->auth_data),
153 ag_auth_data_get_mechanism (ctx->auth_data),
156 g_hash_table_unref (extra_params);
160 DEBUG ("Auth on %s suceeded", tp_proxy_get_object_path (channel));
163 auth_context_done (ctx);
167 session_process_cb (SignonAuthSession *session,
168 GHashTable *session_data,
172 AuthContext *ctx = user_data;
173 const gchar *access_token;
174 const gchar *client_id;
178 DEBUG ("Error processing the session: %s", error->message);
179 auth_context_done (ctx);
183 access_token = tp_asv_get_string (session_data, "AccessToken");
184 client_id = tp_asv_get_string (ag_auth_data_get_parameters (ctx->auth_data),
187 switch (empathy_sasl_channel_select_mechanism (ctx->channel))
189 case EMPATHY_SASL_MECHANISM_FACEBOOK:
190 empathy_sasl_auth_facebook_async (ctx->channel,
191 client_id, access_token,
195 case EMPATHY_SASL_MECHANISM_WLM:
196 empathy_sasl_auth_wlm_async (ctx->channel,
201 case EMPATHY_SASL_MECHANISM_GOOGLE:
202 empathy_sasl_auth_google_async (ctx->channel,
203 ctx->username, access_token,
208 g_assert_not_reached ();
213 identity_query_info_cb (SignonIdentity *identity,
214 const SignonIdentityInfo *info,
218 AuthContext *ctx = user_data;
222 DEBUG ("Error querying info from identity: %s", error->message);
223 auth_context_done (ctx);
227 ctx->username = g_strdup (signon_identity_info_get_username (info));
229 signon_auth_session_process (ctx->session,
230 ag_auth_data_get_parameters (ctx->auth_data),
231 ag_auth_data_get_mechanism (ctx->auth_data),
237 empathy_uoa_auth_handler_start (EmpathyUoaAuthHandler *self,
239 TpAccount *tp_account)
241 const GValue *id_value;
245 AgAccountService *service;
246 AgAuthData *auth_data;
248 SignonIdentity *identity;
249 SignonAuthSession *session;
250 GError *error = NULL;
252 g_return_if_fail (TP_IS_CHANNEL (channel));
253 g_return_if_fail (TP_IS_ACCOUNT (tp_account));
254 g_return_if_fail (empathy_uoa_auth_handler_supports (self, channel,
257 DEBUG ("Start UOA auth for account: %s",
258 tp_proxy_get_object_path (tp_account));
260 id_value = tp_account_get_storage_identifier (tp_account);
261 id = g_value_get_uint (id_value);
263 account = ag_manager_get_account (self->priv->manager, id);
265 l = ag_account_list_services_by_type (account, EMPATHY_UOA_SERVICE_TYPE);
268 DEBUG ("Couldn't find IM service for AgAccountId %u", id);
269 g_object_unref (account);
270 tp_channel_close_async (channel, NULL, NULL);
274 /* Assume there is only one IM service */
275 service = ag_account_service_new (account, l->data);
276 ag_service_list_free (l);
277 g_object_unref (account);
279 auth_data = ag_account_service_get_auth_data (service);
280 cred_id = ag_auth_data_get_credentials_id (auth_data);
281 identity = signon_identity_new_from_db (cred_id);
282 session = signon_identity_create_session (identity,
283 ag_auth_data_get_method (auth_data),
287 DEBUG ("Error creating a SignonAuthSession: %s", error->message);
288 tp_channel_close_async (channel, NULL, NULL);
292 /* Query UOA for more info */
293 signon_identity_query_info (identity,
294 identity_query_info_cb,
295 auth_context_new (channel, auth_data, session, identity));
298 ag_auth_data_unref (auth_data);
299 g_object_unref (service);
300 g_object_unref (identity);
301 g_object_unref (session);
305 empathy_uoa_auth_handler_supports (EmpathyUoaAuthHandler *self,
309 const gchar *provider;
310 EmpathySaslMechanism mech;
312 g_return_val_if_fail (TP_IS_CHANNEL (channel), FALSE);
313 g_return_val_if_fail (TP_IS_ACCOUNT (account), FALSE);
315 provider = tp_account_get_storage_provider (account);
317 if (tp_strdiff (provider, EMPATHY_UOA_PROVIDER))
320 mech = empathy_sasl_channel_select_mechanism (channel);
321 return mech == EMPATHY_SASL_MECHANISM_FACEBOOK ||
322 mech == EMPATHY_SASL_MECHANISM_WLM ||
323 mech == EMPATHY_SASL_MECHANISM_GOOGLE;