summaryrefslogtreecommitdiff
path: root/modules/gnome-online-accounts/e-goa-password-based.c
blob: dd7a75f9fdab3c9391925a1f3c1d292ac3550e15 (plain)
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
/*
 * e-goa-password-based.c
 *
 * 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.
 *
 * 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 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, see <http://www.gnu.org/licenses/>.
 *
 */

#include "e-goa-password-based.h"

/* XXX Yeah, yeah... */
#define GOA_API_IS_SUBJECT_TO_CHANGE

#include <config.h>
#include <goa/goa.h>
#include <glib/gi18n-lib.h>

#define E_GOA_PASSWORD_BASED_GET_PRIVATE(obj) \
	(G_TYPE_INSTANCE_GET_PRIVATE \
	((obj), E_TYPE_GOA_PASSWORD_BASED, EGoaPasswordBasedPrivate))

struct _EGoaPasswordBasedPrivate {
	gint placeholder;
};

G_DEFINE_DYNAMIC_TYPE (
	EGoaPasswordBased,
	e_goa_password_based,
	E_TYPE_AUTHENTICATION_SESSION)

static GoaObject *
e_goa_password_based_ref_account (ESourceRegistryServer *server,
                                  ESource *source,
                                  GoaClient *goa_client)
{
	GoaObject *match = NULL;
	GList *list, *link;
	const gchar *extension_name;
	gchar *account_id = NULL;

	extension_name = E_SOURCE_EXTENSION_GOA;

	source = e_source_registry_server_find_extension (
		server, source, extension_name);

	if (source != NULL) {
		ESourceGoa *extension;

		extension = e_source_get_extension (source, extension_name);
		account_id = e_source_goa_dup_account_id (extension);

		g_object_unref (source);
	}

	if (account_id == NULL)
		return NULL;

	/* FIXME Use goa_client_lookup_by_id() once we require GOA 3.6. */
	list = goa_client_get_accounts (goa_client);

	for (link = list; link != NULL; link = g_list_next (link)) {
		GoaObject *goa_object;
		GoaAccount *goa_account;
		const gchar *candidate_id;

		goa_object = GOA_OBJECT (link->data);
		goa_account = goa_object_get_account (goa_object);
		candidate_id = goa_account_get_id (goa_account);

		if (g_strcmp0 (account_id, candidate_id) == 0)
			match = g_object_ref (goa_object);

		g_object_unref (goa_account);

		if (match != NULL)
			break;
	}

	g_list_free_full (list, (GDestroyNotify) g_object_unref);

	return match;
}

static EAuthenticationSessionResult
e_goa_password_based_execute_sync (EAuthenticationSession *session,
                                   GCancellable *cancellable,
                                   GError **error)
{
	EAuthenticationSessionResult session_result;
	ESourceAuthenticationResult auth_result;
	ESourceAuthenticator *authenticator;
	ESourceRegistryServer *server;
	ESource *source = NULL;
	GoaClient *goa_client = NULL;
	GoaObject *goa_object = NULL;
	GoaAccount *goa_account = NULL;
	GoaPasswordBased *goa_password_based = NULL;
	GString *password_string;
	const gchar *extension_name;
	const gchar *source_uid;
	gchar *password = NULL;
	gboolean use_imap_password;
	gboolean use_smtp_password;
	gboolean success;

	goa_client = goa_client_new_sync (cancellable, error);
	if (goa_client == NULL) {
		session_result = E_AUTHENTICATION_SESSION_ERROR;
		if (error && *error)
			g_dbus_error_strip_remote_error (*error);
		goto exit;
	}

	server = e_authentication_session_get_server (session);
	source_uid = e_authentication_session_get_source_uid (session);
	source = e_source_registry_server_ref_source (server, source_uid);

	if (source == NULL) {
		g_set_error (
			error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND,
			_("No such data source for UID '%s'"),
			source_uid);
		session_result = E_AUTHENTICATION_SESSION_ERROR;
		goto exit;
	}

	goa_object = e_goa_password_based_ref_account (
		server, source, goa_client);

	if (goa_object == NULL) {
		g_set_error (
			error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND,
			_("Cannot find a corresponding account in "
			"the org.gnome.OnlineAccounts service from "
			"which to obtain a password for '%s'"),
			e_source_get_display_name (source));
		session_result = E_AUTHENTICATION_SESSION_ERROR;
		goto exit;
	}

	goa_account = goa_object_get_account (goa_object);
	goa_password_based = goa_object_get_password_based (goa_object);

	/* XXX We should only be here if the account is password based. */
	g_return_val_if_fail (
		goa_password_based != NULL,
		E_AUTHENTICATION_SESSION_ERROR);

	success = goa_account_call_ensure_credentials_sync (
		goa_account, NULL, cancellable, error);
	if (!success) {
		session_result = E_AUTHENTICATION_SESSION_ERROR;
		if (error && *error)
			g_dbus_error_strip_remote_error (*error);
		goto exit;
	}

	extension_name = E_SOURCE_EXTENSION_MAIL_ACCOUNT;
	use_imap_password = e_source_has_extension (source, extension_name);

	extension_name = E_SOURCE_EXTENSION_MAIL_TRANSPORT;
	use_smtp_password = e_source_has_extension (source, extension_name);

	/* Use a suitable password ID for the ESource. */
	if (use_imap_password) {
		goa_password_based_call_get_password_sync (
			goa_password_based, "imap-password",
			&password, cancellable, error);
	} else if (use_smtp_password) {
		goa_password_based_call_get_password_sync (
			goa_password_based, "smtp-password",
			&password, cancellable, error);
	} else {
		/* Generic fallback - password ID is not used. */
		goa_password_based_call_get_password_sync (
			goa_password_based, "",
			&password, cancellable, error);
	}

	if (password == NULL) {
		session_result = E_AUTHENTICATION_SESSION_ERROR;
		if (error && *error)
			g_dbus_error_strip_remote_error (*error);
		goto exit;
	}

	authenticator = e_authentication_session_get_authenticator (session);
	password_string = g_string_new (password);
	auth_result = e_source_authenticator_try_password_sync (
		authenticator, password_string, cancellable, error);
	g_string_free (password_string, TRUE);

	switch (auth_result) {
		case E_SOURCE_AUTHENTICATION_ERROR:
			session_result = E_AUTHENTICATION_SESSION_ERROR;
			break;

		case E_SOURCE_AUTHENTICATION_ACCEPTED:
			session_result = E_AUTHENTICATION_SESSION_SUCCESS;
			break;

		case E_SOURCE_AUTHENTICATION_REJECTED:
			/* FIXME Apparently applications are expected to post
			 *       a desktop-wide notification about the failed
			 *       authentication attempt. */
			g_set_error (
				error, G_IO_ERROR,
				G_IO_ERROR_PERMISSION_DENIED,
				_("Invalid password for '%s'"),
				e_source_get_display_name (source));
			session_result = E_AUTHENTICATION_SESSION_ERROR;
			break;

		default:
			g_warn_if_reached ();
			session_result = E_AUTHENTICATION_SESSION_DISMISSED;
			break;
	}

exit:
	g_clear_object (&source);
	g_clear_object (&goa_client);
	g_clear_object (&goa_object);
	g_clear_object (&goa_account);
	g_clear_object (&goa_password_based);

	g_free (password);

	return session_result;
}

static void
e_goa_password_based_class_init (EGoaPasswordBasedClass *class)
{
	EAuthenticationSessionClass *authentication_session_class;

	g_type_class_add_private (class, sizeof (EGoaPasswordBasedPrivate));

	authentication_session_class =
		E_AUTHENTICATION_SESSION_CLASS (class);
	authentication_session_class->execute_sync =
		e_goa_password_based_execute_sync;
}

static void
e_goa_password_based_class_finalize (EGoaPasswordBasedClass *class)
{
}

static void
e_goa_password_based_init (EGoaPasswordBased *session)
{
	session->priv = E_GOA_PASSWORD_BASED_GET_PRIVATE (session);
}

void
e_goa_password_based_type_register (GTypeModule *type_module)
{
	/* XXX G_DEFINE_DYNAMIC_TYPE declares a static type registration
	 *     function, so we have to wrap it with a public function in
	 *     order to register types from a separate compilation unit. */
	e_goa_password_based_register_type (type_module);
}