]> git.0d.be Git - empathy.git/blob - libempathy/empathy-uoa-auth-handler.c
keyring: move from Empathy to tp-account-widgets
[empathy.git] / libempathy / empathy-uoa-auth-handler.c
1 /*
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>
5  *
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.
10  *
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.
15  *
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
19  */
20
21 #include "config.h"
22 #include "empathy-uoa-auth-handler.h"
23
24 #include <libaccounts-glib/ag-account.h>
25 #include <libaccounts-glib/ag-account-service.h>
26 #include <libaccounts-glib/ag-auth-data.h>
27 #include <libaccounts-glib/ag-manager.h>
28 #include <libaccounts-glib/ag-service.h>
29
30 #include <libsignon-glib/signon-identity.h>
31 #include <libsignon-glib/signon-auth-session.h>
32
33 #include <tp-account-widgets/tpaw-keyring.h>
34
35 #include "empathy-utils.h"
36 #include "empathy-uoa-utils.h"
37 #include "empathy-sasl-mechanisms.h"
38
39 #define DEBUG_FLAG EMPATHY_DEBUG_SASL
40 #include "empathy-debug.h"
41
42 struct _EmpathyUoaAuthHandlerPriv
43 {
44   AgManager *manager;
45 };
46
47 G_DEFINE_TYPE (EmpathyUoaAuthHandler, empathy_uoa_auth_handler, G_TYPE_OBJECT);
48
49 static void
50 empathy_uoa_auth_handler_init (EmpathyUoaAuthHandler *self)
51 {
52   self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
53       EMPATHY_TYPE_UOA_AUTH_HANDLER, EmpathyUoaAuthHandlerPriv);
54
55   self->priv->manager = empathy_uoa_manager_dup ();
56 }
57
58 static void
59 empathy_uoa_auth_handler_dispose (GObject *object)
60 {
61   EmpathyUoaAuthHandler *self = (EmpathyUoaAuthHandler *) object;
62
63   tp_clear_object (&self->priv->manager);
64
65   G_OBJECT_CLASS (empathy_uoa_auth_handler_parent_class)->dispose (object);
66 }
67
68 static void
69 empathy_uoa_auth_handler_class_init (EmpathyUoaAuthHandlerClass *klass)
70 {
71   GObjectClass *oclass = G_OBJECT_CLASS (klass);
72
73   oclass->dispose = empathy_uoa_auth_handler_dispose;
74
75   g_type_class_add_private (klass, sizeof (EmpathyUoaAuthHandlerPriv));
76 }
77
78 EmpathyUoaAuthHandler *
79 empathy_uoa_auth_handler_new (void)
80 {
81   return g_object_new (EMPATHY_TYPE_UOA_AUTH_HANDLER, NULL);
82 }
83
84 typedef struct
85 {
86   TpChannel *channel;
87   AgAccountService *service;
88   AgAuthData *auth_data;
89   SignonIdentity *identity;
90   SignonAuthSession *session;
91
92   gchar *username;
93 } AuthContext;
94
95 static AuthContext *
96 auth_context_new (TpChannel *channel,
97     AgAccountService *service)
98 {
99   AuthContext *ctx;
100   guint cred_id;
101
102   ctx = g_slice_new0 (AuthContext);
103   ctx->channel = g_object_ref (channel);
104   ctx->service = g_object_ref (service);
105
106   ctx->auth_data = ag_account_service_get_auth_data (service);
107   if (ctx->auth_data == NULL)
108     goto out;
109
110   cred_id = ag_auth_data_get_credentials_id (ctx->auth_data);
111   if (cred_id == 0)
112     goto out;
113
114   ctx->identity = signon_identity_new_from_db (cred_id);
115   if (ctx->identity == NULL)
116     goto out;
117
118   ctx->session = signon_identity_create_session (ctx->identity,
119       ag_auth_data_get_method (ctx->auth_data), NULL);
120   if (ctx->session == NULL)
121     goto out;
122
123 out:
124   return ctx;
125 }
126
127 static void
128 auth_context_free (AuthContext *ctx)
129 {
130   g_clear_object (&ctx->channel);
131   g_clear_object (&ctx->service);
132   tp_clear_pointer (&ctx->auth_data, ag_auth_data_unref);
133   g_clear_object (&ctx->session);
134   g_clear_object (&ctx->identity);
135   g_free (ctx->username);
136
137   g_slice_free (AuthContext, ctx);
138 }
139
140 static void
141 auth_context_done (AuthContext *ctx)
142 {
143   tp_channel_close_async (ctx->channel, NULL, NULL);
144   auth_context_free (ctx);
145 }
146
147 static void
148 request_password_session_process_cb (SignonAuthSession *session,
149     GHashTable *session_data,
150     const GError *error,
151     gpointer user_data)
152 {
153   AuthContext *ctx = user_data;
154
155   if (error != NULL)
156     {
157       DEBUG ("Error processing the session to request user's attention: %s",
158           error->message);
159     }
160
161   auth_context_done (ctx);
162 }
163
164 static void
165 request_password (AuthContext *ctx)
166 {
167   GHashTable *extra_params;
168
169   DEBUG ("Invalid credentials, request user action");
170
171   /* Inform SSO that the access token (or password) didn't work and it should
172    * ask user to re-grant access (or retype password). */
173   extra_params = tp_asv_new (
174       SIGNON_SESSION_DATA_UI_POLICY, G_TYPE_INT,
175           SIGNON_POLICY_REQUEST_PASSWORD,
176       NULL);
177
178   ag_auth_data_insert_parameters (ctx->auth_data, extra_params);
179
180   signon_auth_session_process (ctx->session,
181       ag_auth_data_get_parameters (ctx->auth_data),
182       ag_auth_data_get_mechanism (ctx->auth_data),
183       request_password_session_process_cb, ctx);
184
185   g_hash_table_unref (extra_params);
186 }
187
188 static void
189 auth_cb (GObject *source,
190     GAsyncResult *result,
191     gpointer user_data)
192 {
193   TpChannel *channel = (TpChannel *) source;
194   AuthContext *ctx = user_data;
195   GError *error = NULL;
196
197   if (!empathy_sasl_auth_finish (channel, result, &error))
198     {
199       DEBUG ("SASL Mechanism error: %s", error->message);
200       g_clear_error (&error);
201
202       request_password (ctx);
203     }
204   else
205     {
206       DEBUG ("Auth on %s suceeded", tp_proxy_get_object_path (channel));
207       auth_context_done (ctx);
208     }
209 }
210
211 static void
212 session_process_cb (SignonAuthSession *session,
213     GHashTable *session_data,
214     const GError *error,
215     gpointer user_data)
216 {
217   AuthContext *ctx = user_data;
218   const gchar *access_token;
219   const gchar *client_id;
220
221   if (error != NULL)
222     {
223       DEBUG ("Error processing the session: %s", error->message);
224       auth_context_done (ctx);
225       return;
226     }
227
228   access_token = tp_asv_get_string (session_data, "AccessToken");
229   client_id = tp_asv_get_string (ag_auth_data_get_parameters (ctx->auth_data),
230       "ClientId");
231
232   switch (empathy_sasl_channel_select_mechanism (ctx->channel))
233     {
234       case EMPATHY_SASL_MECHANISM_FACEBOOK:
235         empathy_sasl_auth_facebook_async (ctx->channel,
236             client_id, access_token,
237             auth_cb, ctx);
238         break;
239
240       case EMPATHY_SASL_MECHANISM_WLM:
241         empathy_sasl_auth_wlm_async (ctx->channel,
242             access_token,
243             auth_cb, ctx);
244         break;
245
246       case EMPATHY_SASL_MECHANISM_GOOGLE:
247         empathy_sasl_auth_google_async (ctx->channel,
248             ctx->username, access_token,
249             auth_cb, ctx);
250         break;
251
252       case EMPATHY_SASL_MECHANISM_PASSWORD:
253         empathy_sasl_auth_password_async (ctx->channel,
254             tp_asv_get_string (session_data, "Secret"),
255             auth_cb, ctx);
256         break;
257
258       default:
259         g_assert_not_reached ();
260     }
261 }
262
263 static void
264 identity_query_info_cb (SignonIdentity *identity,
265     const SignonIdentityInfo *info,
266     const GError *error,
267     gpointer user_data)
268 {
269   AuthContext *ctx = user_data;
270
271   if (error != NULL)
272     {
273       DEBUG ("Error querying info from identity: %s", error->message);
274       auth_context_done (ctx);
275       return;
276     }
277
278   ctx->username = g_strdup (signon_identity_info_get_username (info));
279
280   signon_auth_session_process (ctx->session,
281       ag_auth_data_get_parameters (ctx->auth_data),
282       ag_auth_data_get_mechanism (ctx->auth_data),
283       session_process_cb,
284       ctx);
285 }
286
287 static void
288 set_account_password_cb (GObject *source,
289     GAsyncResult *result,
290     gpointer user_data)
291 {
292   TpAccount *tp_account = (TpAccount *) source;
293   AuthContext *ctx = user_data;
294   AuthContext *new_ctx;
295   GError *error = NULL;
296
297   if (!tpaw_keyring_set_account_password_finish (tp_account, result, &error))
298     {
299       DEBUG ("Failed to set empty password on UOA account: %s", error->message);
300       auth_context_done (ctx);
301       return;
302     }
303
304   new_ctx = auth_context_new (ctx->channel, ctx->service);
305   auth_context_free (ctx);
306
307   if (new_ctx->session != NULL)
308     {
309       /* The trick worked! */
310       request_password (new_ctx);
311       return;
312     }
313
314   DEBUG ("Still can't get a signon session, even after setting empty pwd");
315   auth_context_done (new_ctx);
316 }
317
318 void
319 empathy_uoa_auth_handler_start (EmpathyUoaAuthHandler *self,
320     TpChannel *channel,
321     TpAccount *tp_account)
322 {
323   const GValue *id_value;
324   AgAccountId id;
325   AgAccount *account;
326   GList *l = NULL;
327   AgAccountService *service;
328   AuthContext *ctx;
329
330   g_return_if_fail (TP_IS_CHANNEL (channel));
331   g_return_if_fail (TP_IS_ACCOUNT (tp_account));
332   g_return_if_fail (empathy_uoa_auth_handler_supports (self, channel,
333       tp_account));
334
335   DEBUG ("Start UOA auth for account: %s",
336       tp_proxy_get_object_path (tp_account));
337
338   id_value = tp_account_get_storage_identifier (tp_account);
339   id = g_value_get_uint (id_value);
340
341   account = ag_manager_get_account (self->priv->manager, id);
342   if (account != NULL)
343     l = ag_account_list_services_by_type (account, EMPATHY_UOA_SERVICE_TYPE);
344   if (l == NULL)
345     {
346       DEBUG ("Couldn't find IM service for AgAccountId %u", id);
347       g_object_unref (account);
348       tp_channel_close_async (channel, NULL, NULL);
349       return;
350     }
351
352   /* Assume there is only one IM service */
353   service = ag_account_service_new (account, l->data);
354   ag_service_list_free (l);
355   g_object_unref (account);
356
357   ctx = auth_context_new (channel, service);
358   if (ctx->session == NULL)
359     {
360       /* This (usually?) means we never stored credentials for this account.
361        * To ask user to type his password SSO needs a SignonIdentity bound to
362        * our account. Let's store an empty password. */
363       DEBUG ("Couldn't create a signon session");
364       tpaw_keyring_set_account_password_async (tp_account, "", FALSE,
365           set_account_password_cb, ctx);
366     }
367   else
368     {
369       /* All is fine! Query UOA for more info */
370       signon_identity_query_info (ctx->identity,
371           identity_query_info_cb, ctx);
372     }
373
374   g_object_unref (service);
375 }
376
377 gboolean
378 empathy_uoa_auth_handler_supports (EmpathyUoaAuthHandler *self,
379     TpChannel *channel,
380     TpAccount *account)
381 {
382   const gchar *provider;
383   EmpathySaslMechanism mech;
384
385   g_return_val_if_fail (TP_IS_CHANNEL (channel), FALSE);
386   g_return_val_if_fail (TP_IS_ACCOUNT (account), FALSE);
387
388   provider = tp_account_get_storage_provider (account);
389
390   if (tp_strdiff (provider, EMPATHY_UOA_PROVIDER))
391     return FALSE;
392
393   mech = empathy_sasl_channel_select_mechanism (channel);
394   return mech == EMPATHY_SASL_MECHANISM_FACEBOOK ||
395       mech == EMPATHY_SASL_MECHANISM_WLM ||
396       mech == EMPATHY_SASL_MECHANISM_GOOGLE ||
397       mech == EMPATHY_SASL_MECHANISM_PASSWORD;
398 }