summaryrefslogtreecommitdiff
path: root/src/libedataserver/e-oauth2-services.c
blob: 22cb20e051447e7d2cabb74bf2b2065d10378ed0 (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
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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
 * Copyright (C) 2018 Red Hat, Inc. (www.redhat.com)
 *
 * 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 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, see <http://www.gnu.org/licenses/>.
 */

/**
 * SECTION: e-oauth2-services
 * @include: libedataserver/libedataserver.h
 * @short_description: An extensible object holding all known OAuth2 services
 *
 * The extensible object, which holds all known OAuth2 services. Each
 * #EOAuth2Service extends this object and adds itself to it with
 * e_oauth2_services_add(). The services can be later searched for
 * with e_oauth2_services_find(), which returns the service suitable
 * for the given protocol and/or host name.
 **/

#include "evolution-data-server-config.h"

#include <stdio.h>

#include "e-extensible.h"
#include "e-oauth2-service.h"

/* Known built-in implementations */
#include "e-oauth2-service-google.h"
#include "e-oauth2-service-outlook.h"
#include "e-oauth2-service-yahoo.h"

#include "e-oauth2-services.h"

struct _EOAuth2ServicesPrivate {
	GMutex property_lock;
	GSList *services; /* EOAuth2Service * */
};

G_DEFINE_TYPE_WITH_CODE (EOAuth2Services, e_oauth2_services, G_TYPE_OBJECT,
	G_ADD_PRIVATE (EOAuth2Services)
	G_IMPLEMENT_INTERFACE (E_TYPE_EXTENSIBLE, NULL))

static GObject *services_singleton = NULL;
G_LOCK_DEFINE_STATIC (services_singleton);

static void
services_singleton_weak_ref_cb (gpointer user_data,
				GObject *object)
{
	G_LOCK (services_singleton);

	g_warn_if_fail (object == services_singleton);
	services_singleton = NULL;

	G_UNLOCK (services_singleton);
}

static GObject *
oauth2_services_constructor (GType type,
			     guint n_construct_params,
			     GObjectConstructParam *construct_params)
{
	GObject *object;

	G_LOCK (services_singleton);

	if (services_singleton) {
		object = g_object_ref (services_singleton);
	} else {
		object = G_OBJECT_CLASS (e_oauth2_services_parent_class)->constructor (type, n_construct_params, construct_params);

		if (object)
			g_object_weak_ref (object, services_singleton_weak_ref_cb, NULL);

		services_singleton = object;
	}

	G_UNLOCK (services_singleton);

	return object;
}

static void
oauth2_services_dispose (GObject *object)
{
	EOAuth2Services *services = E_OAUTH2_SERVICES (object);

	g_mutex_lock (&services->priv->property_lock);
	g_slist_free_full (services->priv->services, g_object_unref);
	g_mutex_unlock (&services->priv->property_lock);

	/* Chain up to parent's method. */
	G_OBJECT_CLASS (e_oauth2_services_parent_class)->dispose (object);
}

static void
oauth2_services_finalize (GObject *object)
{
	EOAuth2Services *services = E_OAUTH2_SERVICES (object);

	g_mutex_clear (&services->priv->property_lock);

	/* Chain up to parent's method. */
	G_OBJECT_CLASS (e_oauth2_services_parent_class)->finalize (object);
}

static void
oauth2_services_constructed (GObject *object)
{
	/* Chain up to parent's method. */
	G_OBJECT_CLASS (e_oauth2_services_parent_class)->constructed (object);

	e_extensible_load_extensions (E_EXTENSIBLE (object));
}

static void
e_oauth2_services_class_init (EOAuth2ServicesClass *klass)
{
	GObjectClass *object_class;

	object_class = G_OBJECT_CLASS (klass);
	object_class->dispose = oauth2_services_dispose;
	object_class->finalize = oauth2_services_finalize;
	object_class->constructed = oauth2_services_constructed;
	object_class->constructor = oauth2_services_constructor;

	/* Ensure built-in service types are registered */
	g_type_ensure (E_TYPE_OAUTH2_SERVICE_GOOGLE);
	g_type_ensure (E_TYPE_OAUTH2_SERVICE_OUTLOOK);
	g_type_ensure (E_TYPE_OAUTH2_SERVICE_YAHOO);
}

static void
e_oauth2_services_init (EOAuth2Services *oauth2_services)
{
	oauth2_services->priv = e_oauth2_services_get_instance_private (oauth2_services);

	g_mutex_init (&oauth2_services->priv->property_lock);
}

/**
 * e_oauth2_services_is_supported:
 *
 * Returns: %TRUE, when evolution-data-server had been compiled
 *    with OAuth2 authentication enabled, %FALSE otherwise.
 *
 * Since: 3.28
 **/
gboolean
e_oauth2_services_is_supported (void)
{
	return TRUE;
}

/**
 * e_oauth2_services_new:
 *
 * Creates a new #EOAuth2Services instance.
 *
 * Returns: (transfer full): an #EOAuth2Services
 *
 * Since: 3.28
 **/
EOAuth2Services *
e_oauth2_services_new (void)
{
	return g_object_new (E_TYPE_OAUTH2_SERVICES, NULL);
}

/**
 * e_oauth2_services_add:
 * @services: an #EOAuth2Services
 * @service: an #EOAuth2Service to add
 *
 * Adds the @service to the list of known OAuth2 services into @services.
 * It also adds a reference to @service.
 *
 * Since: 3.28
 **/
void
e_oauth2_services_add (EOAuth2Services *services,
		       EOAuth2Service *service)
{
	GSList *link;

	g_return_if_fail (E_IS_OAUTH2_SERVICES (services));
	g_return_if_fail (E_IS_OAUTH2_SERVICE (service));

	g_mutex_lock (&services->priv->property_lock);

	for (link = services->priv->services; link; link = g_slist_next (link)) {
		if (link->data == service)
			break;
	}

	if (!link)
		services->priv->services = g_slist_prepend (services->priv->services, g_object_ref (service));

	g_mutex_unlock (&services->priv->property_lock);
}

/**
 * e_oauth2_services_remove:
 * @services: an #EOAuth2Services
 * @service: an #EOAuth2Service to remove
 *
 * Removes the @service from the list of known services in @services.
 * The function does nothing, if the @service had not been added.
 *
 * Since: 3.28
 **/
void
e_oauth2_services_remove (EOAuth2Services *services,
			  EOAuth2Service *service)
{
	GSList *link;

	g_return_if_fail (E_IS_OAUTH2_SERVICES (services));
	g_return_if_fail (E_IS_OAUTH2_SERVICE (service));

	g_mutex_lock (&services->priv->property_lock);

	for (link = services->priv->services; link; link = g_slist_next (link)) {
		if (link->data == service) {
			g_object_unref (service);
			services->priv->services = g_slist_remove (services->priv->services, service);
			break;
		}
	}

	g_mutex_unlock (&services->priv->property_lock);
}

/**
 * e_oauth2_services_list:
 * @services: an #EOAuth2Services
 *
 * Lists all currently known services, which had been added
 * with e_oauth2_services_add(). Free the returned #GSList with
 * g_slist_remove_full (known_services, g_object_unref);
 * when no longer needed.
 *
 * Returns: (transfer full) (element-type EOAuth2Service): a newly allocated #GSList
 *    with all currently known #EOAuth2Service referenced instances
 *
 * Since: 3.28
 **/
GSList *
e_oauth2_services_list (EOAuth2Services *services)
{
	GSList *result;

	g_return_val_if_fail (E_IS_OAUTH2_SERVICES (services), NULL);

	g_mutex_lock (&services->priv->property_lock);

	result = g_slist_copy_deep (services->priv->services, (GCopyFunc) g_object_ref, NULL);

	g_mutex_unlock (&services->priv->property_lock);

	return result;
}

/**
 * e_oauth2_services_find:
 * @services: an #EOAuth2Services
 * @source: an #ESource
 *
 * Searches the list of currently known OAuth2 services for the one which
 * can be used with the given @source.
 *
 * The returned #EOAuth2Service is referenced for thread safety, if found.
 *
 * Returns: (transfer full) (nullable): a referenced #EOAuth2Service, which can be used
 *    with given @source, or %NULL, when none was found.
 *
 * Since: 3.28
 **/
EOAuth2Service *
e_oauth2_services_find (EOAuth2Services *services,
			ESource *source)
{
	GSList *link;
	EOAuth2Service *result = NULL;

	g_return_val_if_fail (E_IS_OAUTH2_SERVICES (services), NULL);
	g_return_val_if_fail (E_IS_SOURCE (source), NULL);

	g_mutex_lock (&services->priv->property_lock);

	for (link = services->priv->services; link; link = g_slist_next (link)) {
		EOAuth2Service *service = link->data;

		if (e_oauth2_service_can_process (service, source)) {
			result = g_object_ref (service);
			break;
		}
	}

	g_mutex_unlock (&services->priv->property_lock);

	return result;
}

/**
 * e_oauth2_services_guess:
 * @services: an #EOAuth2Services
 * @protocol: (nullable): a protocol to search the service for, like "imap", or %NULL
 * @hostname: (nullable): a host name to search the service for, like "server.example.com", or %NULL
 *
 * Searches the list of currently known OAuth2 services for the one which
 * can be used with the given @protocol and/or @hostname.
 * Any of @protocol and @hostname can be %NULL, but not both.
 * It's up to each #EOAuth2Service to decide, which of the arguments
 * are important and whether all or only any of them can be required.
 *
 * The returned #EOAuth2Service is referenced for thread safety, if found.
 *
 * Returns: (transfer full) (nullable): a referenced #EOAuth2Service, which can be used
 *    with given constraints, or %NULL, when none was found.
 *
 * Since: 3.28
 **/
EOAuth2Service *
e_oauth2_services_guess (EOAuth2Services *services,
			 const gchar *protocol,
			 const gchar *hostname)
{
	GSList *link;
	EOAuth2Service *result = NULL;

	g_return_val_if_fail (E_IS_OAUTH2_SERVICES (services), NULL);
	g_return_val_if_fail (protocol || hostname, NULL);

	g_mutex_lock (&services->priv->property_lock);

	for (link = services->priv->services; link; link = g_slist_next (link)) {
		EOAuth2Service *service = link->data;

		if (e_oauth2_service_guess_can_process (service, protocol, hostname)) {
			result = g_object_ref (service);
			break;
		}
	}

	g_mutex_unlock (&services->priv->property_lock);

	return result;
}

static gboolean
e_oauth2_services_can_check_auth_method (const gchar *auth_method)
{
	return auth_method && *auth_method &&
	       e_oauth2_services_is_supported () &&
	       g_strcmp0 (auth_method, "none") != 0 &&
	       g_strcmp0 (auth_method, "plain/password") != 0;
}

/**
 * e_oauth2_services_is_oauth2_alias:
 * @services: an #EOAuth2Services
 * @auth_method: (nullable): an authentication method, or %NULL
 *
 * Returns: whether exists any #EOAuth2Service, with the same name as @auth_method.
 *
 * See: e_oauth2_services_is_oauth2_alias_static()
 *
 * Since: 3.28
 **/
gboolean
e_oauth2_services_is_oauth2_alias (EOAuth2Services *services,
				   const gchar *auth_method)
{
	GSList *link;

	g_return_val_if_fail (E_IS_OAUTH2_SERVICES (services), FALSE);

	if (!e_oauth2_services_can_check_auth_method (auth_method))
		return FALSE;

	g_mutex_lock (&services->priv->property_lock);

	for (link = services->priv->services; link; link = g_slist_next (link)) {
		EOAuth2Service *service = link->data;
		const gchar *name;

		name = e_oauth2_service_get_name (service);

		if (name && g_ascii_strcasecmp (name, auth_method) == 0)
			break;
	}

	g_mutex_unlock (&services->priv->property_lock);

	return link != NULL;
}

/**
 * e_oauth2_services_is_oauth2_alias_static:
 * @auth_method: (nullable): an authentication method, or %NULL
 *
 * This is the same as e_oauth2_services_is_oauth2_alias(), except
 * it creates its own #EOAuth2Services instance and frees it at the end.
 * The #EOAuth2Services is implemented as a singleton, thus it won't be
 * much trouble, as long as there is something else having created one
 * instance.
 *
 * Returns: whether exists any #EOAuth2Service, with the same name as @auth_method.
 *
 * Since: 3.28
 **/
gboolean
e_oauth2_services_is_oauth2_alias_static (const gchar *auth_method)
{
	EOAuth2Services *services;
	gboolean is_alias;

	if (!e_oauth2_services_can_check_auth_method (auth_method))
		return FALSE;

	services = e_oauth2_services_new ();
	is_alias = e_oauth2_services_is_oauth2_alias (services, auth_method);
	g_clear_object (&services);

	return is_alias;
}