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
|
/*
* empathy-auth-uoa.c - Source for Uoa SASL authentication
* Copyright (C) 2012 Collabora Ltd.
* @author Xavier Claessens <xavier.claessens@collabora.co.uk>
*
* This library 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.1 of the License, or (at your option) any later version.
*
* 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 Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "config.h"
#include <libaccounts-glib/ag-account.h>
#include <libaccounts-glib/ag-account-service.h>
#include <libaccounts-glib/ag-auth-data.h>
#include <libaccounts-glib/ag-manager.h>
#include <libaccounts-glib/ag-service.h>
#include <libsignon-glib/signon-identity.h>
#include <libsignon-glib/signon-auth-session.h>
#define DEBUG_FLAG EMPATHY_DEBUG_SASL
#include "empathy-debug.h"
#include "empathy-utils.h"
#include "empathy-uoa-auth-handler.h"
#include "empathy-sasl-mechanisms.h"
#define SERVICE_TYPE "IM"
struct _EmpathyUoaAuthHandlerPriv
{
AgManager *manager;
};
G_DEFINE_TYPE (EmpathyUoaAuthHandler, empathy_uoa_auth_handler, G_TYPE_OBJECT);
static void
empathy_uoa_auth_handler_init (EmpathyUoaAuthHandler *self)
{
self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
EMPATHY_TYPE_UOA_AUTH_HANDLER, EmpathyUoaAuthHandlerPriv);
self->priv->manager = ag_manager_new_for_service_type (SERVICE_TYPE);
}
static void
empathy_uoa_auth_handler_dispose (GObject *object)
{
EmpathyUoaAuthHandler *self = (EmpathyUoaAuthHandler *) object;
tp_clear_object (&self->priv->manager);
G_OBJECT_CLASS (empathy_uoa_auth_handler_parent_class)->dispose (object);
}
static void
empathy_uoa_auth_handler_class_init (EmpathyUoaAuthHandlerClass *klass)
{
GObjectClass *oclass = G_OBJECT_CLASS (klass);
oclass->dispose = empathy_uoa_auth_handler_dispose;
g_type_class_add_private (klass, sizeof (EmpathyUoaAuthHandlerPriv));
}
EmpathyUoaAuthHandler *
empathy_uoa_auth_handler_new (void)
{
return g_object_new (EMPATHY_TYPE_UOA_AUTH_HANDLER, NULL);
}
typedef struct
{
TpChannel *channel;
AgAuthData *auth_data;
SignonAuthSession *session;
SignonIdentity *identity;
gchar *username;
} QueryInfoData;
static QueryInfoData *
query_info_data_new (TpChannel *channel,
AgAuthData *auth_data,
SignonAuthSession *session,
SignonIdentity *identity)
{
QueryInfoData *data;
data = g_slice_new0 (QueryInfoData);
data->channel = g_object_ref (channel);
data->auth_data = ag_auth_data_ref (auth_data);
data->session = g_object_ref (session);
data->identity = g_object_ref (identity);
return data;
}
static void
query_info_data_free (QueryInfoData *data)
{
g_object_unref (data->channel);
ag_auth_data_unref (data->auth_data);
g_object_unref (data->session);
g_object_unref (data->identity);
g_free (data->username);
g_slice_free (QueryInfoData, data);
}
static void
auth_cb (GObject *source,
GAsyncResult *result,
gpointer user_data)
{
TpChannel *channel = (TpChannel *) source;
QueryInfoData *data = user_data;
GError *error = NULL;
if (!empathy_sasl_auth_finish (channel, result, &error))
{
GHashTable *extra_params;
DEBUG ("SASL Mechanism error: %s", error->message);
g_clear_error (&error);
/* Inform SSO that the access token didn't work and it should ask user
* to re-grant access. */
extra_params = tp_asv_new (
SIGNON_SESSION_DATA_UI_POLICY, G_TYPE_INT,
SIGNON_POLICY_REQUEST_PASSWORD,
NULL);
ag_auth_data_insert_parameters (data->auth_data, extra_params);
signon_auth_session_process (data->session,
ag_auth_data_get_parameters (data->auth_data),
ag_auth_data_get_mechanism (data->auth_data),
NULL, NULL);
g_hash_table_unref (extra_params);
}
else
{
DEBUG ("Auth on %s suceeded", tp_proxy_get_object_path (channel));
}
tp_channel_close_async (channel, NULL, NULL);
query_info_data_free (data);
}
static void
session_process_cb (SignonAuthSession *session,
GHashTable *session_data,
const GError *error,
gpointer user_data)
{
QueryInfoData *data = user_data;
const gchar *access_token;
const gchar *client_id;
if (error != NULL)
{
DEBUG ("Error processing the session: %s", error->message);
tp_channel_close_async (data->channel, NULL, NULL);
query_info_data_free (data);
return;
}
access_token = tp_asv_get_string (session_data, "AccessToken");
client_id = tp_asv_get_string (ag_auth_data_get_parameters (data->auth_data),
"ClientId");
switch (empathy_sasl_channel_select_mechanism (data->channel))
{
case EMPATHY_SASL_MECHANISM_FACEBOOK:
empathy_sasl_auth_facebook_async (data->channel,
client_id, access_token,
auth_cb, data);
break;
case EMPATHY_SASL_MECHANISM_WLM:
empathy_sasl_auth_wlm_async (data->channel,
access_token,
auth_cb, data);
break;
case EMPATHY_SASL_MECHANISM_GOOGLE:
empathy_sasl_auth_google_async (data->channel,
data->username, access_token,
auth_cb, data);
break;
default:
g_assert_not_reached ();
}
}
static void
identity_query_info_cb (SignonIdentity *identity,
const SignonIdentityInfo *info,
const GError *error,
gpointer user_data)
{
QueryInfoData *data = user_data;
if (error != NULL)
{
DEBUG ("Error querying info from identity: %s", error->message);
tp_channel_close_async (data->channel, NULL, NULL);
query_info_data_free (data);
return;
}
data->username = g_strdup (signon_identity_info_get_username (info));
signon_auth_session_process (data->session,
ag_auth_data_get_parameters (data->auth_data),
ag_auth_data_get_mechanism (data->auth_data),
session_process_cb,
data);
}
void
empathy_uoa_auth_handler_start (EmpathyUoaAuthHandler *self,
TpChannel *channel,
TpAccount *tp_account)
{
const GValue *id_value;
AgAccountId id;
AgAccount *account;
GList *l = NULL;
AgAccountService *service;
AgAuthData *auth_data;
guint cred_id;
SignonIdentity *identity;
SignonAuthSession *session;
GError *error = NULL;
g_return_if_fail (TP_IS_CHANNEL (channel));
g_return_if_fail (TP_IS_ACCOUNT (tp_account));
g_return_if_fail (empathy_uoa_auth_handler_supports (self, channel,
tp_account));
DEBUG ("Start UOA auth for account: %s",
tp_proxy_get_object_path (tp_account));
id_value = tp_account_get_storage_identifier (tp_account);
id = g_value_get_uint (id_value);
account = ag_manager_get_account (self->priv->manager, id);
if (account != NULL)
l = ag_account_list_services_by_type (account, SERVICE_TYPE);
if (l == NULL)
{
DEBUG ("Couldn't find IM service for AgAccountId %u", id);
g_object_unref (account);
tp_channel_close_async (channel, NULL, NULL);
return;
}
/* Assume there is only one IM service */
service = ag_account_service_new (account, l->data);
ag_service_list_free (l);
g_object_unref (account);
auth_data = ag_account_service_get_auth_data (service);
cred_id = ag_auth_data_get_credentials_id (auth_data);
identity = signon_identity_new_from_db (cred_id);
session = signon_identity_create_session (identity,
ag_auth_data_get_method (auth_data),
&error);
if (session == NULL)
{
DEBUG ("Error creating a SignonAuthSession: %s", error->message);
tp_channel_close_async (channel, NULL, NULL);
goto cleanup;
}
/* Query UOA for more info */
signon_identity_query_info (identity,
identity_query_info_cb,
query_info_data_new (channel, auth_data, session, identity));
cleanup:
ag_auth_data_unref (auth_data);
g_object_unref (service);
g_object_unref (identity);
g_object_unref (session);
}
gboolean
empathy_uoa_auth_handler_supports (EmpathyUoaAuthHandler *self,
TpChannel *channel,
TpAccount *account)
{
const gchar *provider;
EmpathySaslMechanism mech;
g_return_val_if_fail (TP_IS_CHANNEL (channel), FALSE);
g_return_val_if_fail (TP_IS_ACCOUNT (account), FALSE);
provider = tp_account_get_storage_provider (account);
if (tp_strdiff (provider, EMPATHY_UOA_PROVIDER))
return FALSE;
mech = empathy_sasl_channel_select_mechanism (channel);
return mech == EMPATHY_SASL_MECHANISM_FACEBOOK ||
mech == EMPATHY_SASL_MECHANISM_WLM ||
mech == EMPATHY_SASL_MECHANISM_GOOGLE;
}
|