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
|
/*
* uoa-utils.c
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) version 3.
*
* This program 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 Lesser General Public
* License along with the program; if not, see <http://www.gnu.org/licenses/>
*
*/
#include "uoa-utils.h"
#include <config.h>
#include <glib/gi18n-lib.h>
#include <rest/rest-proxy.h>
#include <json-glib/json-glib.h>
#include <libsignon-glib/signon-glib.h>
/* XXX accounts-glib.h should include this */
#include <libaccounts-glib/ag-auth-data.h>
#define GOOGLE_USERINFO_URI \
"https://www.googleapis.com/oauth2/v2/userinfo"
typedef struct _AsyncContext AsyncContext;
struct _AsyncContext {
GCancellable *cancellable;
gchar *user_identity;
gchar *email_address;
};
static void
async_context_free (AsyncContext *async_context)
{
if (async_context->cancellable != NULL)
g_object_unref (async_context->cancellable);
g_free (async_context->user_identity);
g_free (async_context->email_address);
g_slice_free (AsyncContext, async_context);
}
/****************************** Google Provider ******************************/
static void
e_ag_account_google_got_userinfo_cb (RestProxyCall *call,
const GError *error,
GObject *weak_object,
gpointer user_data)
{
GSimpleAsyncResult *simple;
AsyncContext *async_context;
JsonParser *json_parser;
JsonObject *json_object;
JsonNode *json_node;
const gchar *email;
GError *local_error = NULL;
simple = G_SIMPLE_ASYNC_RESULT (user_data);
async_context = g_simple_async_result_get_op_res_gpointer (simple);
if (error != NULL) {
g_simple_async_result_set_from_error (simple, error);
goto exit;
}
/* This is shamelessly stolen from goagoogleprovider.c */
if (rest_proxy_call_get_status_code (call) != 200) {
g_simple_async_result_set_error (
simple, G_IO_ERROR, G_IO_ERROR_FAILED,
_("Expected status 200 when requesting guid, "
"instead got status %d (%s)"),
rest_proxy_call_get_status_code (call),
rest_proxy_call_get_status_message (call));
goto exit;
}
json_parser = json_parser_new ();
json_parser_load_from_data (
json_parser,
rest_proxy_call_get_payload (call),
rest_proxy_call_get_payload_length (call),
&local_error);
if (local_error != NULL) {
g_prefix_error (
&local_error,
_("Error parsing response as JSON: "));
g_simple_async_result_take_error (simple, local_error);
g_object_unref (json_parser);
goto exit;
}
json_node = json_parser_get_root (json_parser);
json_object = json_node_get_object (json_node);
email = json_object_get_string_member (json_object, "email");
if (email != NULL) {
async_context->user_identity = g_strdup (email);
async_context->email_address = g_strdup (email);
} else {
g_simple_async_result_set_error (
simple, G_IO_ERROR, G_IO_ERROR_FAILED,
_("Didn't find email member in JSON data"));
}
g_object_unref (json_parser);
exit:
g_simple_async_result_complete (simple);
g_object_unref (simple);
}
static void
e_ag_account_google_session_process_cb (GObject *source_object,
GAsyncResult *result,
gpointer user_data)
{
GSimpleAsyncResult *simple;
GVariant *session_data;
GError *error = NULL;
simple = G_SIMPLE_ASYNC_RESULT (user_data);
session_data = signon_auth_session_process_finish (
SIGNON_AUTH_SESSION (source_object), result, &error);
/* Sanity check. */
g_return_if_fail (
((session_data != NULL) && (error == NULL)) ||
((session_data == NULL) && (error != NULL)));
/* Use the access token to obtain the user's email address. */
if (session_data != NULL) {
RestProxy *proxy;
RestProxyCall *call;
gchar *access_token = NULL;
g_variant_lookup (
session_data, "AccessToken", "s", &access_token);
g_variant_unref (session_data);
proxy = rest_proxy_new (GOOGLE_USERINFO_URI, FALSE);
call = rest_proxy_new_call (proxy);
rest_proxy_call_set_method (call, "GET");
/* XXX This should never be NULL, but if it is just let
* the call fail and pick up the resulting GError. */
if (access_token != NULL) {
rest_proxy_call_add_param (
call, "access_token", access_token);
g_free (access_token);
}
/* XXX The 3rd argument is supposed to be a GObject
* that RestProxyCall weakly references such that
* its disposal cancels the call. This obviously
* predates GCancellable. Too bizarre to bother. */
rest_proxy_call_async (
call, e_ag_account_google_got_userinfo_cb,
NULL, g_object_ref (simple), &error);
if (error != NULL) {
/* Undo the reference added to the async call. */
g_object_unref (simple);
}
g_object_unref (proxy);
g_object_unref (call);
}
if (error != NULL) {
g_simple_async_result_take_error (simple, error);
g_simple_async_result_complete (simple);
}
g_object_unref (simple);
}
static void
e_ag_account_collect_google_userinfo (GSimpleAsyncResult *simple,
AgAccount *ag_account,
GCancellable *cancellable)
{
AgAccountService *ag_account_service = NULL;
SignonAuthSession *session;
AgAuthData *ag_auth_data;
GList *list;
GError *error = NULL;
/* First obtain an OAuth 2.0 access token. */
list = ag_account_list_services_by_type (ag_account, "mail");
if (list != NULL) {
ag_account_service = ag_account_service_new (
ag_account, (AgService *) list->data);
ag_service_list_free (list);
}
g_return_if_fail (ag_account_service != NULL);
ag_auth_data = ag_account_service_get_auth_data (ag_account_service);
session = signon_auth_session_new (
ag_auth_data_get_credentials_id (ag_auth_data),
ag_auth_data_get_method (ag_auth_data), &error);
/* Sanity check. */
g_return_if_fail (
((session != NULL) && (error == NULL)) ||
((session == NULL) && (error != NULL)));
if (session != NULL) {
signon_auth_session_process_async (
session,
ag_auth_data_get_login_parameters (ag_auth_data, NULL),
ag_auth_data_get_mechanism (ag_auth_data),
cancellable,
e_ag_account_google_session_process_cb,
g_object_ref (simple));
} else {
g_simple_async_result_take_error (simple, error);
g_simple_async_result_complete_in_idle (simple);
}
ag_auth_data_unref (ag_auth_data);
g_object_unref (ag_account_service);
g_object_unref (simple);
}
/************************ End Provider-Specific Code *************************/
void
e_ag_account_collect_userinfo (AgAccount *ag_account,
GCancellable *cancellable,
GAsyncReadyCallback callback,
gpointer user_data)
{
GSimpleAsyncResult *simple;
AsyncContext *async_context;
const gchar *provider_name;
g_return_if_fail (AG_IS_ACCOUNT (ag_account));
async_context = g_slice_new0 (AsyncContext);
if (G_IS_CANCELLABLE (cancellable))
async_context->cancellable = g_object_ref (cancellable);
simple = g_simple_async_result_new (
G_OBJECT (ag_account), callback,
user_data, e_ag_account_collect_userinfo);
g_simple_async_result_set_check_cancellable (simple, cancellable);
g_simple_async_result_set_op_res_gpointer (
simple, async_context, (GDestroyNotify) async_context_free);
/* XXX This has to be done differently for each provider. */
provider_name = ag_account_get_provider_name (ag_account);
if (g_strcmp0 (provider_name, "google") == 0) {
e_ag_account_collect_google_userinfo (
g_object_ref (simple), ag_account, cancellable);
} else {
g_warn_if_reached ();
g_simple_async_result_complete_in_idle (simple);
}
g_object_unref (simple);
}
gboolean
e_ag_account_collect_userinfo_finish (AgAccount *ag_account,
GAsyncResult *result,
gchar **out_user_identity,
gchar **out_email_address,
GError **error)
{
GSimpleAsyncResult *simple;
AsyncContext *async_context;
g_return_val_if_fail (
g_simple_async_result_is_valid (
result, G_OBJECT (ag_account),
e_ag_account_collect_userinfo), FALSE);
simple = G_SIMPLE_ASYNC_RESULT (result);
async_context = g_simple_async_result_get_op_res_gpointer (simple);
if (g_simple_async_result_propagate_error (simple, error))
return FALSE;
/* The result strings may be NULL without an error. */
if (out_user_identity != NULL) {
*out_user_identity = async_context->user_identity;
async_context->user_identity = NULL;
}
if (out_email_address != NULL) {
*out_email_address = async_context->email_address;
async_context->email_address = NULL;
}
return TRUE;
}
|