]> git.0d.be Git - empathy.git/blob - libempathy/empathy-keyring.c
Reorder header inclusions accordingly to the Telepathy coding style
[empathy.git] / libempathy / empathy-keyring.c
1 /*
2  * Copyright (C) 2010 Collabora Ltd.
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17  */
18
19 #include "config.h"
20 #include "empathy-keyring.h"
21
22 #include <glib/gi18n-lib.h>
23 #include <libsecret/secret.h>
24
25 #ifdef HAVE_UOA
26 #include <libaccounts-glib/ag-account.h>
27 #include <libaccounts-glib/ag-account-service.h>
28 #include <libaccounts-glib/ag-auth-data.h>
29 #include <libaccounts-glib/ag-manager.h>
30 #include <libaccounts-glib/ag-service.h>
31 #include <libsignon-glib/signon-identity.h>
32 #include "empathy-uoa-utils.h"
33 #endif
34
35 #include "empathy-utils.h"
36
37 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
38 #include "empathy-debug.h"
39
40 static const SecretSchema account_keyring_schema =
41   { "org.gnome.Empathy.Account", SECRET_SCHEMA_DONT_MATCH_NAME,
42     { { "account-id", SECRET_SCHEMA_ATTRIBUTE_STRING },
43       { "param-name", SECRET_SCHEMA_ATTRIBUTE_STRING },
44       { NULL } } };
45
46 static const SecretSchema room_keyring_schema =
47   { "org.gnome.Empathy.Room", SECRET_SCHEMA_DONT_MATCH_NAME,
48     { { "account-id", SECRET_SCHEMA_ATTRIBUTE_STRING },
49       { "room-id", SECRET_SCHEMA_ATTRIBUTE_STRING },
50       { NULL } } };
51
52 gboolean
53 empathy_keyring_is_available (void)
54 {
55   return TRUE;
56 }
57
58 /* get */
59
60 static void
61 lookup_item_cb (GObject *source,
62     GAsyncResult *result,
63     gpointer user_data)
64 {
65   GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (user_data);
66   GError *error = NULL;
67   gchar *password;
68
69   password = secret_password_lookup_finish (result, &error);
70   if (error != NULL)
71     {
72       g_simple_async_result_set_error (simple, TP_ERROR,
73           TP_ERROR_DOES_NOT_EXIST, "%s", error->message);
74       g_clear_error (&error);
75       goto out;
76     }
77
78   if (password == NULL)
79     {
80       g_simple_async_result_set_error (simple, TP_ERROR,
81           TP_ERROR_DOES_NOT_EXIST, _("Password not found"));
82       goto out;
83     }
84
85   g_simple_async_result_set_op_res_gpointer (simple, password,
86       (GDestroyNotify) secret_password_free);
87
88 out:
89   g_simple_async_result_complete (simple);
90   g_object_unref (simple);
91 }
92
93 #ifdef HAVE_UOA
94 static AgAccountService *
95 uoa_password_common (TpAccount *tp_account,
96     GSimpleAsyncResult *result,
97     AgAuthData **ret_auth_data)
98 {
99   const GValue *storage_id;
100   AgAccountId account_id;
101   AgManager *manager = NULL;
102   AgAccount *account = NULL;
103   GList *l;
104   AgAccountService *service = NULL;
105   AgAuthData *auth_data = NULL;
106
107   g_assert (ret_auth_data != NULL);
108   *ret_auth_data = NULL;
109
110   storage_id = tp_account_get_storage_identifier (tp_account);
111   account_id = g_value_get_uint (storage_id);
112   if (account_id == 0)
113     {
114       g_simple_async_result_set_error (result,
115           TP_ERROR, TP_ERROR_INVALID_ARGUMENT,
116           "StorageId is invalid, cannot get the AgAccount for this TpAccount");
117       g_simple_async_result_complete_in_idle (result);
118       goto error;
119     }
120
121   manager = empathy_uoa_manager_dup ();
122   account = ag_manager_get_account (manager, account_id);
123
124   /* Assuming there is only one IM service */
125   l = ag_account_list_services_by_type (account, EMPATHY_UOA_SERVICE_TYPE);
126   if (l == NULL)
127     {
128       g_simple_async_result_set_error (result,
129           TP_ERROR, TP_ERROR_INVALID_ARGUMENT,
130           "AgAccount has no IM service");
131       g_simple_async_result_complete_in_idle (result);
132       goto error;
133     }
134   service = ag_account_service_new (account, l->data);
135   ag_service_list_free (l);
136
137   auth_data = ag_account_service_get_auth_data (service);
138   if (auth_data == NULL)
139     {
140       g_simple_async_result_set_error (result,
141           TP_ERROR, TP_ERROR_INVALID_ARGUMENT,
142           "Service has no AgAuthData");
143       g_simple_async_result_complete_in_idle (result);
144       goto error;
145     }
146
147   if (tp_strdiff (ag_auth_data_get_mechanism (auth_data), "password") ||
148       tp_strdiff (ag_auth_data_get_method (auth_data), "password"))
149     {
150       g_simple_async_result_set_error (result,
151           TP_ERROR, TP_ERROR_INVALID_ARGUMENT,
152           "Service does not use password authentication");
153       g_simple_async_result_complete_in_idle (result);
154       goto error;
155     }
156
157   g_object_unref (manager);
158   g_object_unref (account);
159
160   *ret_auth_data = auth_data;
161   return service;
162
163 error:
164   g_clear_object (&manager);
165   g_clear_object (&account);
166   g_clear_object (&service);
167   tp_clear_pointer (&auth_data, ag_auth_data_unref);
168   return NULL;
169 }
170
171 static void
172 uoa_session_process_cb (SignonAuthSession *session,
173     GHashTable *session_data,
174     const GError *error,
175     gpointer user_data)
176 {
177   GSimpleAsyncResult *result = user_data;
178   const gchar *password;
179
180   if (error != NULL)
181     {
182       g_simple_async_result_set_from_error (result, error);
183       goto out;
184     }
185
186   password = tp_asv_get_string (session_data, "Secret");
187   if (tp_str_empty (password))
188     {
189       g_simple_async_result_set_error (result, TP_ERROR,
190           TP_ERROR_DOES_NOT_EXIST, _("Password not found"));
191       goto out;
192     }
193
194   g_simple_async_result_set_op_res_gpointer (result, g_strdup (password),
195       g_free);
196
197 out:
198   /* libaccounts-glib API does not guarantee the callback happens after
199    * reentering mainloop */
200   g_simple_async_result_complete_in_idle (result);
201   g_object_unref (result);
202   g_object_unref (session);
203 }
204
205 static void
206 uoa_get_account_password (TpAccount *tp_account,
207     GSimpleAsyncResult *result)
208 {
209   AgAccountService *service;
210   AgAuthData *auth_data;
211   guint cred_id;
212   SignonIdentity *identity;
213   SignonAuthSession *session;
214   GError *error = NULL;
215
216   DEBUG ("Store password for %s in signond",
217       tp_account_get_path_suffix (tp_account));
218
219   service = uoa_password_common (tp_account, result, &auth_data);
220   if (service == NULL)
221     return;
222
223   cred_id = ag_auth_data_get_credentials_id (auth_data);
224   if (cred_id == 0)
225     {
226       g_simple_async_result_set_error (result,
227           TP_ERROR, TP_ERROR_INVALID_ARGUMENT,
228           "AgAccount has no CredentialsId");
229       g_simple_async_result_complete_in_idle (result);
230       goto out;
231     }
232
233   identity = signon_identity_new_from_db (cred_id);
234   session = signon_identity_create_session (identity,
235       ag_auth_data_get_method (auth_data), &error);
236   g_object_unref (identity);
237
238   if (session == NULL)
239     {
240       g_simple_async_result_set_from_error (result, error);
241       g_simple_async_result_complete_in_idle (result);
242       goto out;
243     }
244
245   signon_auth_session_process (session,
246       ag_auth_data_get_parameters (auth_data),
247       ag_auth_data_get_mechanism (auth_data),
248       uoa_session_process_cb,
249       g_object_ref (result));
250
251 out:
252   ag_auth_data_unref (auth_data);
253   g_object_unref (service);
254 }
255 #endif
256
257 void
258 empathy_keyring_get_account_password_async (TpAccount *account,
259     GAsyncReadyCallback callback,
260     gpointer user_data)
261 {
262   GSimpleAsyncResult *simple;
263   const gchar *account_id;
264
265   g_return_if_fail (TP_IS_ACCOUNT (account));
266   g_return_if_fail (callback != NULL);
267
268   simple = g_simple_async_result_new (G_OBJECT (account), callback,
269       user_data, empathy_keyring_get_account_password_async);
270
271   account_id = tp_proxy_get_object_path (account) +
272     strlen (TP_ACCOUNT_OBJECT_PATH_BASE);
273
274   DEBUG ("Trying to get password for: %s", account_id);
275
276 #ifdef HAVE_UOA
277     {
278       const gchar *provider;
279
280       provider = tp_account_get_storage_provider (account);
281       if (!tp_strdiff (provider, EMPATHY_UOA_PROVIDER))
282         {
283           uoa_get_account_password (account, simple);
284           g_object_unref (simple);
285           return;
286         }
287     }
288 #endif
289
290   secret_password_lookup (&account_keyring_schema, NULL,
291           lookup_item_cb, simple,
292           "account-id", account_id,
293           "param-name", "password",
294           NULL);
295 }
296
297 void
298 empathy_keyring_get_room_password_async (TpAccount *account,
299     const gchar *id,
300     GAsyncReadyCallback callback,
301     gpointer user_data)
302 {
303   GSimpleAsyncResult *simple;
304   const gchar *account_id;
305
306   g_return_if_fail (TP_IS_ACCOUNT (account));
307   g_return_if_fail (id != NULL);
308   g_return_if_fail (callback != NULL);
309
310   simple = g_simple_async_result_new (G_OBJECT (account), callback,
311       user_data, empathy_keyring_get_room_password_async);
312
313   account_id = tp_proxy_get_object_path (account) +
314     strlen (TP_ACCOUNT_OBJECT_PATH_BASE);
315
316   DEBUG ("Trying to get password for room '%s' on account '%s'",
317       id, account_id);
318
319   secret_password_lookup (&room_keyring_schema, NULL,
320           lookup_item_cb, simple,
321           "account-id", account_id,
322           "room-id", id,
323           NULL);
324 }
325
326 const gchar *
327 empathy_keyring_get_account_password_finish (TpAccount *account,
328     GAsyncResult *result,
329     GError **error)
330 {
331   empathy_implement_finish_return_pointer (account,
332       empathy_keyring_get_account_password_async);
333 }
334
335 const gchar *
336 empathy_keyring_get_room_password_finish (TpAccount *account,
337     GAsyncResult *result,
338     GError **error)
339 {
340   empathy_implement_finish_return_pointer (account,
341       empathy_keyring_get_room_password_async);
342 }
343
344 /* set */
345
346 static void
347 store_password_cb (GObject *source,
348     GAsyncResult *result,
349     gpointer user_data)
350 {
351   GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (user_data);
352   GError *error = NULL;
353
354   if (!secret_password_store_finish (result, &error))
355     {
356       g_simple_async_result_set_error (simple, TP_ERROR,
357           TP_ERROR_DOES_NOT_EXIST, "%s", error->message);
358       g_error_free (error);
359     }
360
361   g_simple_async_result_complete (simple);
362   g_object_unref (simple);
363 }
364
365 #ifdef HAVE_UOA
366 typedef struct
367 {
368   AgAccountService *service;
369   gchar *password;
370   gboolean remember;
371   GSimpleAsyncResult *result;
372 } UoaChangePasswordData;
373
374 static UoaChangePasswordData *
375 uoa_change_password_data_new (AgAccountService *service,
376     const gchar *password,
377     gboolean remember,
378     GSimpleAsyncResult *result)
379 {
380   UoaChangePasswordData *data;
381
382   data = g_slice_new0 (UoaChangePasswordData);
383   data->service = g_object_ref (service);
384   data->password = g_strdup (password);
385   data->remember = remember;
386   data->result = g_object_ref (result);
387
388   return data;
389 }
390
391 static void
392 uoa_change_password_data_free (UoaChangePasswordData *data)
393 {
394   g_object_unref (data->service);
395   g_free (data->password);
396   g_object_unref (data->result);
397   g_slice_free (UoaChangePasswordData, data);
398 }
399
400 static void
401 uoa_identity_store_cb (SignonIdentity *identity,
402     guint32 id,
403     const GError *error,
404     gpointer user_data)
405 {
406   UoaChangePasswordData *data = user_data;
407
408   if (error != NULL)
409     g_simple_async_result_set_from_error (data->result, error);
410
411   g_simple_async_result_complete (data->result);
412   uoa_change_password_data_free (data);
413   g_object_unref (identity);
414 }
415
416 static void
417 uoa_identity_query_info_cb (SignonIdentity *identity,
418     const SignonIdentityInfo *info,
419     const GError *error,
420     gpointer user_data)
421 {
422   UoaChangePasswordData *data = user_data;
423
424   if (error != NULL)
425     {
426       g_simple_async_result_set_from_error (data->result, error);
427       /* libaccounts-glib API does not guarantee the callback happens after
428        * reentering mainloop */
429       g_simple_async_result_complete_in_idle (data->result);
430       uoa_change_password_data_free (data);
431       g_object_unref (identity);
432       return;
433     }
434
435   /* const SignonIdentityInfo is a lie, cast it! - Mardy */
436   signon_identity_info_set_secret ((SignonIdentityInfo *) info,
437       data->password, data->remember);
438
439   signon_identity_store_credentials_with_info (identity, info,
440       uoa_identity_store_cb, data);
441 }
442
443 static void
444 uoa_initial_account_store_cb (AgAccount *account,
445     const GError *error,
446     gpointer user_data)
447 {
448   UoaChangePasswordData *data = user_data;
449
450   if (error != NULL)
451     g_simple_async_result_set_from_error (data->result, error);
452
453       /* libaccounts-glib API does not guarantee the callback happens after
454        * reentering mainloop */
455   g_simple_async_result_complete_in_idle (data->result);
456   uoa_change_password_data_free (data);
457 }
458
459 static void
460 uoa_initial_identity_store_cb (SignonIdentity *identity,
461     guint32 id,
462     const GError *error,
463     gpointer user_data)
464 {
465   UoaChangePasswordData *data = user_data;
466   AgAccount *account = ag_account_service_get_account (data->service);
467   GValue value = G_VALUE_INIT;
468
469   if (error != NULL)
470     {
471       g_simple_async_result_set_from_error (data->result, error);
472       /* libaccounts-glib API does not guarantee the callback happens after
473        * reentering mainloop */
474       g_simple_async_result_complete_in_idle (data->result);
475       uoa_change_password_data_free (data);
476       g_object_unref (identity);
477       return;
478     }
479
480   g_value_init (&value, G_TYPE_UINT);
481   g_value_set_uint (&value, id);
482   ag_account_select_service (account, NULL);
483   ag_account_set_value (account, "CredentialsId", &value);
484   g_value_unset (&value);
485
486   ag_account_store (account, uoa_initial_account_store_cb, data);
487
488   g_object_unref (identity);
489 }
490
491 static void
492 uoa_set_account_password (TpAccount *tp_account,
493     const gchar *password,
494     gboolean remember,
495     GSimpleAsyncResult *result)
496 {
497   AgAccountService *service;
498   AgAuthData *auth_data;
499   guint cred_id;
500   UoaChangePasswordData *data;
501   SignonIdentity *identity;
502
503   DEBUG ("Store password for %s in signond",
504       tp_account_get_path_suffix (tp_account));
505
506   service = uoa_password_common (tp_account, result, &auth_data);
507   if (service == NULL)
508     return;
509
510   data = uoa_change_password_data_new (service, password, remember, result);
511
512   cred_id = ag_auth_data_get_credentials_id (auth_data);
513   if (cred_id == 0)
514     {
515       SignonIdentityInfo *info;
516       const GHashTable *params;
517       const gchar *username;
518       const gchar *acl_all[] = { "*", NULL };
519
520       /* This is the first time we store password for this account.
521        * First check if we have an 'username' param as this is more accurate
522        * in the tp-idle case. */
523       params = tp_account_get_parameters (tp_account);
524       username = tp_asv_get_string (params, "username");
525       if (username == NULL)
526         username = tp_asv_get_string (params, "account");
527
528       identity = signon_identity_new ();
529       info = signon_identity_info_new ();
530       signon_identity_info_set_username (info, username);
531       signon_identity_info_set_secret (info, password, remember);
532       signon_identity_info_set_access_control_list (info, acl_all);
533
534       /* Give identity and data ownership to the callback */
535       signon_identity_store_credentials_with_info (identity, info,
536           uoa_initial_identity_store_cb, data);
537
538       signon_identity_info_free (info);
539     }
540   else
541     {
542       /* There is already a password stored, query info to update it.
543        * Give identity and data ownership to the callback */
544       identity = signon_identity_new_from_db (cred_id);
545       signon_identity_query_info (identity,
546           uoa_identity_query_info_cb, data);
547     }
548
549   g_object_unref (service);
550   ag_auth_data_unref (auth_data);
551 }
552 #endif
553
554 void
555 empathy_keyring_set_account_password_async (TpAccount *account,
556     const gchar *password,
557     gboolean remember,
558     GAsyncReadyCallback callback,
559     gpointer user_data)
560 {
561   GSimpleAsyncResult *simple;
562   const gchar *account_id;
563   gchar *name;
564
565   g_return_if_fail (TP_IS_ACCOUNT (account));
566   g_return_if_fail (password != NULL);
567
568   simple = g_simple_async_result_new (G_OBJECT (account), callback,
569       user_data, empathy_keyring_set_account_password_async);
570
571   account_id = tp_proxy_get_object_path (account) +
572     strlen (TP_ACCOUNT_OBJECT_PATH_BASE);
573
574   DEBUG ("Remembering password for %s", account_id);
575
576 #ifdef HAVE_UOA
577     {
578       const gchar *provider;
579
580       provider = tp_account_get_storage_provider (account);
581       if (!tp_strdiff (provider, EMPATHY_UOA_PROVIDER))
582         {
583           uoa_set_account_password (account, password, remember, simple);
584           g_object_unref (simple);
585           return;
586         }
587     }
588 #endif
589
590   name = g_strdup_printf (_("IM account password for %s (%s)"),
591       tp_account_get_display_name (account), account_id);
592
593   secret_password_store (&account_keyring_schema,
594       remember ? NULL : SECRET_COLLECTION_SESSION,
595       name, password,
596       NULL, store_password_cb, simple,
597       "account-id", account_id,
598       "param-name", "password",
599       NULL);
600
601   g_free (name);
602 }
603
604 void
605 empathy_keyring_set_room_password_async (TpAccount *account,
606     const gchar *id,
607     const gchar *password,
608     GAsyncReadyCallback callback,
609     gpointer user_data)
610 {
611   GSimpleAsyncResult *simple;
612   const gchar *account_id;
613   gchar *name;
614
615   g_return_if_fail (TP_IS_ACCOUNT (account));
616   g_return_if_fail (id != NULL);
617   g_return_if_fail (password != NULL);
618
619   simple = g_simple_async_result_new (G_OBJECT (account), callback,
620       user_data, empathy_keyring_set_room_password_async);
621
622   account_id = tp_proxy_get_object_path (account) +
623     strlen (TP_ACCOUNT_OBJECT_PATH_BASE);
624
625   DEBUG ("Remembering password for room '%s' on account '%s'", id, account_id);
626
627   name = g_strdup_printf (_("Password for chatroom '%s' on account %s (%s)"),
628       id, tp_account_get_display_name (account), account_id);
629
630   secret_password_store (&room_keyring_schema, NULL, name, password,
631       NULL, store_password_cb, simple,
632       "account-id", account_id,
633       "room-id", id,
634       NULL);
635
636   g_free (name);
637 }
638
639 gboolean
640 empathy_keyring_set_account_password_finish (TpAccount *account,
641     GAsyncResult *result,
642     GError **error)
643 {
644   empathy_implement_finish_void (account, empathy_keyring_set_account_password_async);
645 }
646
647 gboolean
648 empathy_keyring_set_room_password_finish (TpAccount *account,
649     GAsyncResult *result,
650     GError **error)
651 {
652   empathy_implement_finish_void (account, empathy_keyring_set_room_password_async);
653 }
654
655 /* delete */
656
657 static void
658 items_delete_cb (GObject *source,
659     GAsyncResult *result,
660     gpointer user_data)
661 {
662   GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (user_data);
663   GError *error = NULL;
664
665   if (!secret_password_clear_finish (result, &error))
666     {
667       g_simple_async_result_set_error (simple, TP_ERROR,
668               TP_ERROR_DOES_NOT_EXIST, "%s", error->message);
669       g_error_free (error);
670     }
671
672   g_simple_async_result_complete (simple);
673   g_object_unref (simple);
674 }
675
676 void
677 empathy_keyring_delete_account_password_async (TpAccount *account,
678     GAsyncReadyCallback callback,
679     gpointer user_data)
680 {
681   GSimpleAsyncResult *simple;
682   const gchar *account_id;
683
684   g_return_if_fail (TP_IS_ACCOUNT (account));
685
686   simple = g_simple_async_result_new (G_OBJECT (account), callback,
687       user_data, empathy_keyring_delete_account_password_async);
688
689   account_id = tp_proxy_get_object_path (account) +
690     strlen (TP_ACCOUNT_OBJECT_PATH_BASE);
691
692   DEBUG ("Deleting password for %s", account_id);
693
694 #ifdef HAVE_UOA
695     {
696       const gchar *provider;
697
698       provider = tp_account_get_storage_provider (account);
699       if (!tp_strdiff (provider, EMPATHY_UOA_PROVIDER))
700         {
701           /* I see no other way to forget the stored password than overwriting
702            * with an empty one. */
703           uoa_set_account_password (account, "", FALSE, simple);
704           g_object_unref (simple);
705           return;
706         }
707     }
708 #endif
709
710   secret_password_clear (&account_keyring_schema, NULL,
711           items_delete_cb, simple,
712           "account-id", account_id,
713           "param-name", "password",
714           NULL);
715 }
716
717 gboolean
718 empathy_keyring_delete_account_password_finish (TpAccount *account,
719     GAsyncResult *result,
720     GError **error)
721 {
722   empathy_implement_finish_void (account, empathy_keyring_delete_account_password_async);
723 }