~mandel/ubuntuone-client/donot-add-watches-parents

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
/*
 * Syncdaemon API
 *
 * Authors: Rodrigo Moya <rodrigo.moya@canonical.com>
 *
 * Copyright 2010 Canonical Ltd.
 *
 * This program is free software: you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 3, as published
 * by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranties of
 * MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
 * PURPOSE.  See the GNU 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 "config.h"
#include <string.h>
#include <glib/gi18n-lib.h>
#include <dbus/dbus-glib.h>
#include "syncdaemon-authentication.h"
#include "syncdaemon-marshal.h"

G_DEFINE_TYPE(SyncdaemonAuthentication, syncdaemon_authentication, G_TYPE_OBJECT)

struct _SyncdaemonAuthenticationPrivate {
	DBusGConnection *bus;
	DBusGProxy *proxy;
	DBusGProxy *newer_proxy;
	gboolean has_credentials;
	SyncdaemonCredentials *credentials;
};

/* Signals */
enum {
	CREDENTIALS_FOUND_SIGNAL,
	AUTHORIZATION_CANCELLED_SIGNAL,
	ERROR_SIGNAL,
	LAST_SIGNAL
};

static guint auth_signals[LAST_SIGNAL] = { 0, };

static void
syncdaemon_authentication_finalize (GObject *object)
{
	SyncdaemonAuthentication *auth = SYNCDAEMON_AUTHENTICATION (object);

	if (auth->priv != NULL) {
		if (auth->priv->credentials != NULL)
			g_object_unref (G_OBJECT (auth->priv->credentials));
		if (auth->priv->proxy != NULL)
			g_object_unref (G_OBJECT (auth->priv->proxy));
		if (auth->priv->newer_proxy != NULL)
			g_object_unref (G_OBJECT (auth->priv->newer_proxy));
		if (auth->priv->bus != NULL)
			dbus_g_connection_unref (auth->priv->bus);

		g_free (auth->priv);
	}

	G_OBJECT_CLASS (syncdaemon_authentication_parent_class)->finalize (object);
}

static void
syncdaemon_authentication_class_init (SyncdaemonAuthenticationClass *klass)
{
	GObjectClass *object_class = G_OBJECT_CLASS (klass);

	object_class->finalize = syncdaemon_authentication_finalize;

	/* Register signals */
	auth_signals[CREDENTIALS_FOUND_SIGNAL] = g_signal_new ("credentials_found",
							       G_TYPE_FROM_CLASS (klass),
							       (GSignalFlags) G_SIGNAL_RUN_LAST,
							       G_STRUCT_OFFSET (SyncdaemonAuthenticationClass, credentials_found),
							       NULL, NULL,
							       g_cclosure_marshal_VOID__OBJECT,
							       G_TYPE_NONE, 1,
							       G_TYPE_OBJECT);
	auth_signals[AUTHORIZATION_CANCELLED_SIGNAL] = g_signal_new ("authorization_cancelled",
								     G_TYPE_FROM_CLASS (klass),
								     (GSignalFlags) G_SIGNAL_RUN_LAST,
								     G_STRUCT_OFFSET (SyncdaemonAuthenticationClass, authorization_cancelled),
								     NULL, NULL,
								     g_cclosure_marshal_VOID__VOID,
								     G_TYPE_NONE, 0);
	auth_signals[ERROR_SIGNAL] = g_signal_new ("error",
						   G_TYPE_FROM_CLASS (klass),
						   (GSignalFlags) G_SIGNAL_RUN_LAST,
						   G_STRUCT_OFFSET (SyncdaemonAuthenticationClass, error),
						   NULL, NULL,
						   g_cclosure_marshal_VOID__STRING,
						   G_TYPE_NONE, 1,
						   G_TYPE_STRING);
}

static void
credentials_found_cb (DBusGProxy *proxy, const gchar *app_name, GHashTable *result, gpointer user_data)
{
	SyncdaemonAuthentication *auth = SYNCDAEMON_AUTHENTICATION (user_data);

	if (g_strcmp0 (app_name, SSO_APP_NAME) != 0) {
		g_debug ("Credentials are not for '" SSO_APP_NAME "', ignoring");
		return;
	}

	if (auth->priv->credentials != NULL)
		g_object_unref (G_OBJECT (auth->priv->credentials));

	auth->priv->credentials = syncdaemon_credentials_new_from_hash_table (result);
	auth->priv->has_credentials = TRUE;

	g_signal_emit (auth, auth_signals[CREDENTIALS_FOUND_SIGNAL], 0, auth->priv->credentials);
}

static void
authorization_denied_cb (DBusGProxy *proxy, const gchar *app_name, gpointer user_data)
{
	g_debug ("Authorization denied");
	if (g_strcmp0 (app_name, SSO_APP_NAME) != 0) {
		g_debug ("Credentials are not for '" SSO_APP_NAME "', ignoring");
		return;
	}

	g_signal_emit (user_data, auth_signals[AUTHORIZATION_CANCELLED_SIGNAL], 0);
}

static void
credentials_error_cb (DBusGProxy *proxy,
		      const gchar *app_name,
		      const gchar *error_message,
		      const gchar *detailed_error,
		      gpointer user_data)
{
	g_debug ("Authorization error");
	if (g_strcmp0 (app_name, SSO_APP_NAME) != 0) {
		g_debug ("Credentials are not for '" SSO_APP_NAME "', ignoring");
		return;
	}

	g_signal_emit (user_data, auth_signals[ERROR_SIGNAL], 0, error_message);
}

static void
syncdaemon_authentication_init (SyncdaemonAuthentication *auth)
{
	GError *error = NULL;

	auth->priv = g_new0 (SyncdaemonAuthenticationPrivate, 1);
	auth->priv->has_credentials = FALSE;

	/* Initialize DBus */
	auth->priv->bus = dbus_g_bus_get (DBUS_BUS_SESSION, &error);
	if (error != NULL) {
		g_warning ("Couldn't get session bus: %s", error->message);
		g_error_free (error);
	} else {
		auth->priv->proxy = dbus_g_proxy_new_for_name (auth->priv->bus, "com.ubuntu.sso",
							       "/credentials", "com.ubuntu.sso.ApplicationCredentials");
		if (auth->priv->proxy != NULL) {
			dbus_g_object_register_marshaller (_syncdaemon_marshal_VOID__STRING_POINTER,
							   G_TYPE_NONE,
							   G_TYPE_STRING,
							   dbus_g_type_get_map ("GHashTable",
										G_TYPE_STRING,
										G_TYPE_STRING),
							   G_TYPE_INVALID);

			dbus_g_proxy_add_signal (auth->priv->proxy, "CredentialsFound",
						 G_TYPE_STRING,
						 dbus_g_type_get_map ("GHashTable",
								      G_TYPE_STRING,
								      G_TYPE_STRING),
						 G_TYPE_INVALID);
			dbus_g_proxy_connect_signal (auth->priv->proxy, "CredentialsFound",
						     G_CALLBACK (credentials_found_cb), auth, NULL);

			dbus_g_proxy_add_signal (auth->priv->proxy, "AuthorizationDenied",
						 G_TYPE_STRING,
						 G_TYPE_INVALID);
			dbus_g_proxy_connect_signal (auth->priv->proxy, "AuthorizationDenied",
						     G_CALLBACK (authorization_denied_cb), auth, NULL);

			dbus_g_proxy_add_signal (auth->priv->proxy, "CredentialsError",
						 G_TYPE_STRING,
						 G_TYPE_STRING,
						 G_TYPE_STRING,
						 G_TYPE_INVALID);
			dbus_g_proxy_connect_signal (auth->priv->proxy, "CredentialsError",
						     G_CALLBACK (credentials_error_cb), auth, NULL);
		} else
			g_warning ("Couldn't get proxy for com.ubuntu.sso.ApplicationCredentials");

		auth->priv->newer_proxy = dbus_g_proxy_new_for_name (auth->priv->bus, "com.ubuntu.sso",
							       "/com/ubuntu/sso/credentials", "com.ubuntu.sso.CredentialsManagement");
		if (auth->priv->newer_proxy != NULL) {
			dbus_g_object_register_marshaller (_syncdaemon_marshal_VOID__STRING_POINTER,
							   G_TYPE_NONE,
							   G_TYPE_STRING,
							   dbus_g_type_get_map ("GHashTable",
										G_TYPE_STRING,
										G_TYPE_STRING),
							   G_TYPE_INVALID);

			dbus_g_proxy_add_signal (auth->priv->newer_proxy, "CredentialsFound",
						 G_TYPE_STRING,
						 dbus_g_type_get_map ("GHashTable",
								      G_TYPE_STRING,
								      G_TYPE_STRING),
						 G_TYPE_INVALID);
			dbus_g_proxy_connect_signal (auth->priv->newer_proxy, "CredentialsFound",
						     G_CALLBACK (credentials_found_cb), auth, NULL);
		} else
			g_warning ("Couldn't get proxy for com.ubuntu.sso.CredentialsManagement");

	}
}

/**
 * syncdaemon_authentication_find_credentials:
 */
SyncdaemonCredentials *
syncdaemon_authentication_find_credentials (SyncdaemonAuthentication *auth)
{
	g_return_val_if_fail (SYNCDAEMON_IS_AUTHENTICATION (auth), NULL);

	if (!auth->priv->has_credentials) {
		GHashTable *credentials;
		GError *error = NULL;

		if (dbus_g_proxy_call (auth->priv->proxy, "find_credentials", &error,
				       G_TYPE_STRING, SSO_APP_NAME,
				       G_TYPE_INVALID,
				       dbus_g_type_get_map ("GHashTable",
                                                            G_TYPE_STRING,
                                                            G_TYPE_STRING), &credentials,
				       G_TYPE_INVALID)) {
			if (g_hash_table_size (credentials) >= 4) {
				auth->priv->credentials = syncdaemon_credentials_new_from_hash_table (credentials);
				auth->priv->has_credentials = TRUE;
			} else
				g_warning ("Got less number of items in credentials hash table than expected!");

			g_hash_table_destroy (credentials);
		} else {
			g_warning ("Could not get credentials for '" SSO_APP_NAME "': %s", error->message);
			g_error_free (error);

			return NULL;
		}
	}

	return auth->priv->credentials;
}

/**
 * syncdaemon_authentication_has_credentials:
 */
gboolean
syncdaemon_authentication_has_credentials (SyncdaemonAuthentication *auth)
{
	g_return_val_if_fail (SYNCDAEMON_IS_AUTHENTICATION (auth), FALSE);

	if (auth->priv->has_credentials)
		return TRUE;

	return syncdaemon_authentication_find_credentials (auth) != NULL;
}

/**
 * syncdaemon_authentication_login_or_register:
 */
void
syncdaemon_authentication_login_or_register (SyncdaemonAuthentication *auth)
{
	g_return_if_fail (SYNCDAEMON_IS_AUTHENTICATION (auth));

	dbus_g_proxy_call_no_reply (auth->priv->proxy, "login_or_register_to_get_credentials",
				    G_TYPE_STRING, SSO_APP_NAME,
				    G_TYPE_STRING, SSO_TC_URL,
				    G_TYPE_STRING, Q_("Ubuntu One requires an Ubuntu Single Sign On (SSO) account. This process will allow you to create a new account, if you do not yet have one."),
				    G_TYPE_INT64, 0, /* FIXME */
				    G_TYPE_INVALID,
				    G_TYPE_INVALID);
}

/**
 * syncdaemon_authentication_clear_token:
 */
void
syncdaemon_authentication_clear_token (SyncdaemonAuthentication *auth)
{
	GError *error = NULL;

	g_return_if_fail (SYNCDAEMON_IS_AUTHENTICATION (auth));

	if (!dbus_g_proxy_call (auth->priv->proxy, "clear_token", &error,
				G_TYPE_STRING, SSO_APP_NAME,
				G_TYPE_INVALID,
				G_TYPE_INVALID)) {
		g_warning ("Error calling 'clear_token': %s", error->message);
		g_error_free (error);
	}
}