~online-accounts/webaccounts-browser-extension/13.10

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
/*
 * This file is part of the webaccounts-browser-plugin.
 * Copyright (C) Canonical Ltd. 2012
 *
 * Author: Alberto Mardegan <alberto.mardegan@canonical.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License
 * version 2.1 as published by the Free Software Foundation.
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

#include "login-handler.h"

#include <json-glib/json-glib.h>
#include <gio/gio.h>
#include <libaccounts-glib/ag-account.h>
#include <libaccounts-glib/ag-manager.h>
#include <libaccounts-glib/ag-provider.h>
#include <string.h>

#define WEBCREDENTIALS_CAPTURE_SERVICE "com.canonical.webcredentials.capture"
#define WEBCREDENTIALS_CAPTURE_OBJECT_PATH \
    "/com/canonical/webcredentials/capture"
#define WEBCREDENTIALS_CAPTURE_INTERFACE WEBCREDENTIALS_CAPTURE_SERVICE

#define WEBCREDENTIALS_DCONF_SCHEMA "com.canonical.webcredentials.capture"

/* Instantiating the manager is expensive; so, let's cache it */
static AgManager *manager = NULL;
static GDBusConnection *dbus_connection = NULL;

static AgProvider *
find_provider_for_domain (const gchar *domain)
{
    GList *providers, *list;
    AgProvider *result = NULL;

    g_return_val_if_fail (AG_IS_MANAGER (manager), NULL);

    providers = ag_manager_list_providers (manager);
    for (list = providers; list != NULL; list = list->next)
    {
        AgProvider *provider = list->data;
        const gchar *domains_regex;

        domains_regex = ag_provider_get_domains_regex (provider);
        if (domains_regex == NULL || domains_regex[0] == '\0')
        {
            continue;
        }

        if (g_regex_match_simple (domains_regex, domain, 0, 0))
        {
            /* Found a provider which supports this domain */
            result = ag_provider_ref (provider);
            break;
        }

    }

    ag_provider_list_free (providers);

    return result;
}

static void
blacklist_account (const gchar *domain, const gchar *username)
{
    GSettings *settings;
    gchar **accounts, **accounts_old;
    guint length, i;

    settings = g_settings_new (WEBCREDENTIALS_DCONF_SCHEMA);
    if (G_UNLIKELY (settings == NULL))
    {
        g_warning ("Failed to open DConf database");
        return;
    }

    accounts_old = g_settings_get_strv (settings, "dontask-accounts");
    length = g_strv_length (accounts_old);

    accounts = g_new (gchar *, length + 2);
    for (i = 0; i < length; i++)
    {
        accounts[i] = accounts_old[i];
    }
    accounts[i++] = g_strdup_printf ("%s:%s", domain, username);
    accounts[i] = NULL;

    g_settings_set_strv (settings, "dontask-accounts",
                         (const gchar * const*) accounts);

    g_object_unref (settings);
    g_strfreev (accounts);
    g_free (accounts_old);
}

static gboolean
account_blacklisted (const gchar *domain, const gchar *username)
{
    GSettings *settings;
    gchar **accounts;
    gchar *match;
    gint i;
    gboolean found = FALSE;

    settings = g_settings_new (WEBCREDENTIALS_DCONF_SCHEMA);
    if (G_UNLIKELY (settings == NULL))
    {
        g_warning ("Failed to open DConf database");
        return TRUE;
    }

    accounts = g_settings_get_strv (settings, "dontask-accounts");
    match = g_strdup_printf ("%s:%s", domain, username);

    for (i = 0; accounts[i] != NULL; i++)
    {
        if (strcmp (accounts[i], match) == 0)
        {
            found = TRUE;
            break;
        }
    }

    g_object_unref (settings);
    g_strfreev (accounts);
    g_free (match);
    return found;
}

static gboolean
account_exists (AgProvider *provider, const gchar *username)
{
    GList *accounts, *list;
    gboolean account_exists = FALSE;
    const gchar *provider_name;

    g_return_val_if_fail (AG_IS_MANAGER (manager), FALSE);

    /* An ag_manager_list_by_provider() method would be welcome here.
     * http://code.google.com/p/accounts-sso/issues/detail?id=59
     */
    provider_name = ag_provider_get_name (provider);
    accounts = ag_manager_list (manager);
    for (list = accounts; list != NULL; list = list->next)
    {
        AgAccountId account_id = (AgAccountId)GPOINTER_TO_INT(list->data);
        AgAccount *account;

        account = ag_manager_get_account (manager, account_id);
        if (G_UNLIKELY (account == NULL))
        {
            continue;
        }

        if (g_strcmp0 (ag_account_get_provider_name (account),
                       provider_name) != 0)
        {
            g_object_unref (account);
            continue;
        }

        /* Here we assume that the account username is used as display name; we
         * might need to revisit this later. */
        if (g_strcmp0 (ag_account_get_display_name (account), username) == 0)
        {
            g_object_unref (account);
            account_exists = TRUE;
            break;
        }
        g_object_unref (account);
    }

    ag_manager_list_free (accounts);

    return account_exists;
}

/*
 * Check whether the given account is relevant for the WebCredentials
 * implementationand, if it is, forward the information to the system settings
 * applet.
 */
void
webaccounts_store_login (const gchar *domain,
                         const gchar *username,
                         const gchar *password,
                         GVariant *cookies)
{
    AgProvider *provider;

    g_debug ("%s; domain %s, username %s, password %s", G_STRFUNC,
               domain, username, password);
    if (manager == NULL)
    {
        manager = ag_manager_new ();
    }

    if (domain == NULL || domain[0] == '\0' ||
        username == NULL || username[0] == '\0')
    {
        g_warning ("Insufficient informations (domain = '%s', username = '%s')",
                   domain, username);
        return;
    }

    provider = find_provider_for_domain (domain);
    if (provider == NULL)
    {
        g_debug ("Provider not found");
        /* The domain is not bound to any account provider; nothing to do here */
        goto nothing_to_do;
    }

    if (account_blacklisted (domain, username))
    {
        g_debug ("Account %s on %s is blacklisted", username, domain);
        goto nothing_to_do;
    }

    /* If an account for the same provider with the same username already
     * exists, stop processing here. */
    if (account_exists (provider, username))
    {
        g_debug ("Account for %s exists", username);
        goto nothing_to_do;
    }

    /* Cannot pass NULL strings to D-Bus */
    if (password == NULL)
    {
        password = "";
    }

    if (dbus_connection == NULL)
    {
        GError *error = NULL;

        dbus_connection = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, &error);
        if (error != NULL)
        {
            g_critical ("Failed to connect to session bus: %s", error->message);
            g_error_free (error);
            return;
        }
    }

    /* Asynchronous call, with no callback because we don't care about the
     * outcome of this call. */
    g_dbus_connection_call (dbus_connection,
                            WEBCREDENTIALS_CAPTURE_SERVICE,
                            WEBCREDENTIALS_CAPTURE_OBJECT_PATH,
                            WEBCREDENTIALS_CAPTURE_INTERFACE,
                            "LoginCaptured",
                            g_variant_new ("(sss@a{ss})",
                                           ag_provider_get_name (provider),
                                           username,
                                           password,
                                           cookies),
                            NULL,
                            G_DBUS_CALL_FLAGS_NONE,
                            -1,
                            NULL,
                            NULL,
                            NULL);

    /* Don't propose to create this account ever again */
    blacklist_account (domain, username);

nothing_to_do:
    if (provider != NULL)
    {
        ag_provider_unref (provider);
        provider = NULL;
    }
}

void
webaccounts_login_handler_set_json (const gchar *login_info_json)
{
    GVariant *login_info;
    GVariant *cookies_in;
    GVariant *cookies = NULL;
    GVariantBuilder builder;
    GError *error = NULL;
    gchar *username = NULL;
    gchar *password = NULL;
    gchar *domain = NULL;

    g_return_if_fail (login_info_json != NULL);

    login_info = json_gvariant_deserialize_data (login_info_json, -1, NULL,
                                                 &error);
    if (G_UNLIKELY (error != NULL))
    {
        g_warning ("Couldn't deserialize JSON object: %s", error->message);
        g_clear_error (&error);
        return;
    }
    g_return_if_fail (login_info != NULL);

    if (!g_variant_lookup (login_info, "domain", "s", &domain))
    {
        g_warning ("Domain is missing");
        goto error;
    }

    if (!g_variant_lookup (login_info, "login", "s", &username))
    {
        g_warning ("Username is missing");
        goto error;
    }

    g_variant_lookup (login_info, "password", "s", &password);

    g_debug ("Got username %s, domain %s", username, domain);

    cookies_in = g_variant_lookup_value (login_info,
                                         "cookies", G_VARIANT_TYPE_DICTIONARY);
    g_variant_builder_init (&builder, (GVariantType *) "a{ss}");
    if (cookies_in)
    {
        GVariantIter iter;
        GVariant *value;
        gchar *key;

        g_debug ("Got cookies");

        g_variant_iter_init (&iter, cookies_in);
        while (g_variant_iter_loop (&iter, "{sv}", &key, &value))
        {
            g_debug ("domain %s, cookies: %s",
                     key, g_variant_get_string (value, NULL));

            g_variant_builder_add (&builder, "{ss}",
                                   key, g_variant_get_string (value, NULL));
        }
        g_variant_unref (cookies_in);
    }

    cookies = g_variant_builder_end (&builder);

    webaccounts_store_login (domain, username, password, cookies);

error:
    g_free (password);
    g_free (username);
    g_free (domain);
    g_variant_unref (login_info);
}