Debian Codegraph @

src/EWS/common/e-oauth2-service-office365.c

raw
1 /*
2 * SPDX-FileCopyrightText: (C) 2018 Red Hat, Inc. (www.redhat.com)
3 * SPDX-FileCopyrightText: (C) 2024 Siemens AG
4 * SPDX-License-Identifier: LGPL-2.1-or-later
5 */
6
7 #include "evolution-ews-config.h"
8
9 #include <glib/gi18n-lib.h>
10 #include <libedataserver/libedataserver.h>
11
12 #include "camel-ews-settings.h"
13 #include "e-ews-debug.h"
14 #include "e-ms-oapxbc-util.h"
15
16 #include "e-oauth2-service-office365.h"
17
18 /* https://portal.azure.com/
19 https://docs.microsoft.com/en-us/azure/active-directory/develop/active-directory-developers-guide
20 https://tsmatz.wordpress.com/2016/10/07/application-permission-with-v2-endpoint-and-microsoft-graph/
21 https://learn.microsoft.com/en-us/azure/active-directory/develop/authentication-flows-app-scenarios
22 */
23
24 #define AUTHENTICATION_PATH_V1 "oauth2/authorize"
25 #define AUTHENTICATION_PATH_V2 "oauth2/v2.0/authorize"
26
27 #define REFRESH_PATH_V1 "oauth2/token"
28 #define REFRESH_PATH_V2 "oauth2/v2.0/token"
29
30 #define OFFICE365_SCOPE "offline_access" \
31 " openid"
32 #define OFFICE365_HOST_SCOPE_SUFFIX "EWS.AccessAsUser.All"
33
34 struct _EOAuth2ServiceOffice365Private
35 {
36 GMutex string_cache_lock;
37 GHashTable *string_cache;
38 };
39
40 /* Forward Declarations */
41 static void e_oauth2_service_office365_oauth2_service_init (EOAuth2ServiceInterface *iface);
42
43 G_DEFINE_DYNAMIC_TYPE_EXTENDED (EOAuth2ServiceOffice365, e_oauth2_service_office365, E_TYPE_OAUTH2_SERVICE_BASE, 0,
44 G_ADD_PRIVATE_DYNAMIC (EOAuth2ServiceOffice365)
45 G_IMPLEMENT_INTERFACE_DYNAMIC (E_TYPE_OAUTH2_SERVICE, e_oauth2_service_office365_oauth2_service_init))
46
47 static const gchar *
48 eos_office365_cache_string_take (EOAuth2ServiceOffice365 *oauth2_office365,
49 gchar *str) /* takes ownership of the 'str' */
50 {
51 const gchar *cached_str;
52
53 g_return_val_if_fail (E_IS_OAUTH2_SERVICE_OFFICE365 (oauth2_office365), NULL);
54
55 if (!str)
56 return NULL;
57
58 if (!*str) {
59 g_free (str);
60 return "";
61 }
62
63 g_mutex_lock (&oauth2_office365->priv->string_cache_lock);
64
65 cached_str = g_hash_table_lookup (oauth2_office365->priv->string_cache, str);
66 if (cached_str) {
67 g_free (str);
68 } else {
69 g_hash_table_insert (oauth2_office365->priv->string_cache, str, str);
70 cached_str = str;
71 }
72
73 g_mutex_unlock (&oauth2_office365->priv->string_cache_lock);
74
75 return cached_str;
76 }
77
78 static const gchar *
79 eos_office365_cache_string (EOAuth2ServiceOffice365 *oauth2_office365,
80 const gchar *str)
81 {
82 gchar *cached_str;
83
84 g_return_val_if_fail (E_IS_OAUTH2_SERVICE_OFFICE365 (oauth2_office365), NULL);
85
86 if (!str || !*str)
87 return str;
88
89 g_mutex_lock (&oauth2_office365->priv->string_cache_lock);
90
91 cached_str = g_hash_table_lookup (oauth2_office365->priv->string_cache, str);
92
93 if (!cached_str) {
94 cached_str = g_strdup (str);
95 g_hash_table_insert (oauth2_office365->priv->string_cache, cached_str, cached_str);
96 }
97
98 g_mutex_unlock (&oauth2_office365->priv->string_cache_lock);
99
100 return cached_str;
101 }
102
103 static void
104 eos_office365_get_endpoint_host_and_tenant_locked (CamelEwsSettings *ews_settings,
105 const gchar **out_endpoint_host,
106 const gchar **out_tenant)
107 {
108 if (out_endpoint_host) {
109 *out_endpoint_host = camel_ews_settings_get_oauth2_endpoint_host (ews_settings);
110
111 if (e_util_strcmp0 (*out_endpoint_host, NULL) == 0)
112 *out_endpoint_host = OFFICE365_ENDPOINT_HOST;
113 }
114
115 if (out_tenant) {
116 *out_tenant = camel_ews_settings_get_oauth2_tenant (ews_settings);
117
118 if (e_util_strcmp0 (*out_tenant, NULL) == 0)
119 *out_tenant = OFFICE365_TENANT;
120 }
121 }
122
123 static CamelEwsSettings *
124 eos_office365_get_camel_settings (ESource *source)
125 {
126 ESourceCamel *extension;
127
128 if (!source)
129 return NULL;
130
131 g_return_val_if_fail (E_IS_SOURCE (source), NULL);
132
133 extension = e_source_get_extension (source, e_source_camel_get_extension_name ("ews"));
134
135 return CAMEL_EWS_SETTINGS (e_source_camel_get_settings (extension));
136 }
137
138 static gboolean
139 eos_office365_guess_can_process (EOAuth2Service *service,
140 const gchar *protocol,
141 const gchar *hostname)
142 {
143 return e_oauth2_services_is_supported () &&
144 protocol && g_ascii_strcasecmp (protocol, "ews") == 0;
145 }
146
147 static const gchar *
148 eos_office365_get_name (EOAuth2Service *service)
149 {
150 return "Office365";
151 }
152
153 static const gchar *
154 eos_office365_get_display_name (EOAuth2Service *service)
155 {
156 /* Translators: This is a user-visible string, display name of an OAuth2 service. */
157 return C_("OAuth2Service", "Office365");
158 }
159
160 static const gchar *
161 eos_office365_get_client_id (EOAuth2Service *service,
162 ESource *source)
163 {
164 EOAuth2ServiceOffice365 *oauth2_office365 = E_OAUTH2_SERVICE_OFFICE365 (service);
165 CamelEwsSettings *ews_settings;
166
167 ews_settings = eos_office365_get_camel_settings (source);
168
169 if (ews_settings) {
170 const gchar *res = NULL;
171
172 camel_ews_settings_lock (ews_settings);
173
174 if (camel_ews_settings_get_override_oauth2 (ews_settings)) {
175 const gchar *client_id;
176
177 client_id = camel_ews_settings_get_oauth2_client_id (ews_settings);
178
179 if (e_util_strcmp0 (client_id, NULL) != 0)
180 res = eos_office365_cache_string (oauth2_office365, client_id);
181 }
182
183 camel_ews_settings_unlock (ews_settings);
184
185 if (res)
186 return res;
187 }
188
189 return OFFICE365_CLIENT_ID;
190 }
191
192 static const gchar *
193 eos_office365_get_client_secret (EOAuth2Service *service,
194 ESource *source)
195 {
196 return NULL;
197 }
198
199 static const gchar *
200 eos_office365_get_authentication_uri (EOAuth2Service *service,
201 ESource *source)
202 {
203 EOAuth2ServiceOffice365 *oauth2_office365 = E_OAUTH2_SERVICE_OFFICE365 (service);
204 CamelEwsSettings *ews_settings;
205 gboolean use_oauth2_v2 = FALSE;
206
207 ews_settings = eos_office365_get_camel_settings (source);
208
209 if (ews_settings) {
210 const gchar *res = NULL;
211
212 camel_ews_settings_lock (ews_settings);
213
214 use_oauth2_v2 = camel_ews_settings_get_use_oauth2_v2 (ews_settings);
215
216 if (camel_ews_settings_get_override_oauth2 (ews_settings)) {
217 const gchar *endpoint_host = NULL;
218 const gchar *tenant = NULL;
219 const gchar *uri_path;
220
221 eos_office365_get_endpoint_host_and_tenant_locked (ews_settings, &endpoint_host, &tenant);
222
223 if (use_oauth2_v2)
224 uri_path = AUTHENTICATION_PATH_V2;
225 else
226 uri_path = AUTHENTICATION_PATH_V1;
227
228 res = eos_office365_cache_string_take (oauth2_office365,
229 g_strdup_printf ("https://%s/%s/%s", endpoint_host, tenant, uri_path));
230 }
231
232 camel_ews_settings_unlock (ews_settings);
233
234 if (res)
235 return res;
236 }
237
238 if (use_oauth2_v2)
239 return "https://" OFFICE365_ENDPOINT_HOST "/" OFFICE365_TENANT "/" AUTHENTICATION_PATH_V2;
240
241 return "https://" OFFICE365_ENDPOINT_HOST "/" OFFICE365_TENANT "/" AUTHENTICATION_PATH_V1;
242 }
243
244 static const gchar *
245 eos_office365_get_refresh_uri (EOAuth2Service *service,
246 ESource *source)
247 {
248 EOAuth2ServiceOffice365 *oauth2_office365 = E_OAUTH2_SERVICE_OFFICE365 (service);
249 CamelEwsSettings *ews_settings;
250 gboolean use_oauth2_v2 = FALSE;
251
252 ews_settings = eos_office365_get_camel_settings (source);
253
254 if (ews_settings) {
255 const gchar *res = NULL;
256
257 camel_ews_settings_lock (ews_settings);
258
259 use_oauth2_v2 = camel_ews_settings_get_use_oauth2_v2 (ews_settings);
260
261 if (camel_ews_settings_get_override_oauth2 (ews_settings)) {
262 const gchar *endpoint_host = NULL;
263 const gchar *tenant = NULL;
264 const gchar *uri_path;
265
266 eos_office365_get_endpoint_host_and_tenant_locked (ews_settings, &endpoint_host, &tenant);
267
268 if (use_oauth2_v2)
269 uri_path = REFRESH_PATH_V2;
270 else
271 uri_path = REFRESH_PATH_V1;
272
273 res = eos_office365_cache_string_take (oauth2_office365,
274 g_strdup_printf ("https://%s/%s/%s", endpoint_host, tenant, uri_path));
275 }
276
277 camel_ews_settings_unlock (ews_settings);
278
279 if (res)
280 return res;
281 }
282
283 if (use_oauth2_v2)
284 return "https://" OFFICE365_ENDPOINT_HOST "/" OFFICE365_TENANT "/" REFRESH_PATH_V2;
285
286 return "https://" OFFICE365_ENDPOINT_HOST "/" OFFICE365_TENANT "/" REFRESH_PATH_V1;
287 }
288
289 static const gchar *
290 eos_office365_get_redirect_uri (EOAuth2Service *service,
291 ESource *source)
292 {
293 EOAuth2ServiceOffice365 *oauth2_office365 = E_OAUTH2_SERVICE_OFFICE365 (service);
294 CamelEwsSettings *ews_settings;
295
296 ews_settings = eos_office365_get_camel_settings (source);
297
298 if (ews_settings) {
299 const gchar *res = NULL;
300
301 camel_ews_settings_lock (ews_settings);
302
303 if (camel_ews_settings_get_override_oauth2 (ews_settings)) {
304 const gchar *redirect_uri;
305
306 redirect_uri = camel_ews_settings_get_oauth2_redirect_uri (ews_settings);
307
308 if (e_util_strcmp0 (redirect_uri, NULL) != 0)
309 res = eos_office365_cache_string (oauth2_office365, redirect_uri);
310
311 if (!res) {
312 const gchar *endpoint_host = NULL;
313
314 eos_office365_get_endpoint_host_and_tenant_locked (ews_settings, &endpoint_host, NULL);
315
316 if (endpoint_host && g_strcmp0 (endpoint_host, OFFICE365_ENDPOINT_HOST) != 0) {
317 res = eos_office365_cache_string_take (oauth2_office365,
318 g_strdup_printf ("https://%s/common/oauth2/nativeclient", endpoint_host));
319 }
320 }
321 }
322
323 camel_ews_settings_unlock (ews_settings);
324
325 if (res)
326 return res;
327 }
328
329 if (e_util_strcmp0 (OFFICE365_REDIRECT_URI, NULL) != 0)
330 return OFFICE365_REDIRECT_URI;
331
332 return "https://" OFFICE365_ENDPOINT_HOST "/common/oauth2/nativeclient";
333 }
334
335 static gboolean
336 eos_office365_matches_fallback_resource_uri (const gchar *uri,
337 guint len)
338 {
339 return uri && strlen (OFFICE365_FALLBACK_RESOURCE_URI) == len &&
340 strncmp (uri, OFFICE365_FALLBACK_RESOURCE_URI, len) == 0;
341 }
342
343 static const gchar *
344 eos_office365_get_resource_uri (EOAuth2Service *service,
345 ESource *source)
346 {
347 EOAuth2ServiceOffice365 *oauth2_office365 = E_OAUTH2_SERVICE_OFFICE365 (service);
348 CamelEwsSettings *ews_settings;
349
350 ews_settings = eos_office365_get_camel_settings (source);
351
352 if (ews_settings) {
353 const gchar *res = NULL;
354
355 camel_ews_settings_lock (ews_settings);
356
357 if (camel_ews_settings_get_override_oauth2 (ews_settings)) {
358 const gchar *resource_uri;
359
360 resource_uri = camel_ews_settings_get_oauth2_resource_uri (ews_settings);
361
362 if (e_util_strcmp0 (resource_uri, NULL) != 0)
363 res = eos_office365_cache_string (oauth2_office365, resource_uri);
364
365 }
366
367 if (!res) {
368 const gchar *host_url;
369
370 host_url = camel_ews_settings_get_hosturl (ews_settings);
371
372 if (host_url && *host_url) {
373 const gchar *ptr;
374
375 ptr = strstr (host_url, "://");
376 ptr = ptr ? strchr (ptr + 3, '/') : NULL;
377
378 if (ptr && !eos_office365_matches_fallback_resource_uri (host_url, ptr - host_url)) {
379 gchar *resource_uri;
380
381 resource_uri = g_strndup (host_url, ptr - host_url);
382
383 res = eos_office365_cache_string_take (oauth2_office365, resource_uri);
384 }
385 }
386 }
387
388 camel_ews_settings_unlock (ews_settings);
389
390 if (res)
391 return res;
392 }
393
394 return OFFICE365_FALLBACK_RESOURCE_URI;
395 }
396
397 static const gchar*
398 eos_office365_get_oauth2_v2_scope (EOAuth2Service *service,
399 ESource *source)
400 {
401 EOAuth2ServiceOffice365 *oauth2_office365 = E_OAUTH2_SERVICE_OFFICE365 (service);
402
403 return eos_office365_cache_string_take (oauth2_office365,
404 g_strdup_printf ("%s %s/%s", OFFICE365_SCOPE, eos_office365_get_resource_uri (service, source), OFFICE365_HOST_SCOPE_SUFFIX));
405 }
406
407 static void
408 eos_office365_prepare_authentication_uri_query (EOAuth2Service *service,
409 ESource *source,
410 GHashTable *uri_query)
411 {
412 CamelEwsSettings *ews_settings;
413
414 g_return_if_fail (uri_query != NULL);
415
416 e_oauth2_service_util_set_to_form (uri_query, "response_mode", "query");
417
418 ews_settings = eos_office365_get_camel_settings (source);
419
420 if (ews_settings && camel_ews_settings_get_use_oauth2_v2 (ews_settings))
421 e_oauth2_service_util_set_to_form (uri_query, "scope", eos_office365_get_oauth2_v2_scope (service, source));
422 else
423 e_oauth2_service_util_set_to_form (uri_query, "resource", eos_office365_get_resource_uri (service, source));
424 }
425
426 static void
427 eos_office365_prepare_refresh_token_form (EOAuth2Service *service,
428 ESource *source,
429 const gchar *refresh_token,
430 GHashTable *form)
431 {
432 CamelEwsSettings *ews_settings;
433
434 g_return_if_fail (form != NULL);
435
436 ews_settings = eos_office365_get_camel_settings (source);
437
438 e_oauth2_service_util_set_to_form (form, "redirect_uri", e_oauth2_service_get_redirect_uri (service, source));
439
440 if (ews_settings && camel_ews_settings_get_use_oauth2_v2 (ews_settings))
441 e_oauth2_service_util_set_to_form (form, "scope", eos_office365_get_oauth2_v2_scope (service, source));
442 else
443 e_oauth2_service_util_set_to_form (form, "resource", eos_office365_get_resource_uri (service, source));
444 }
445
446 static void
447 eos_office365_finalize (GObject *object)
448 {
449 EOAuth2ServiceOffice365 *oauth2_office365 = E_OAUTH2_SERVICE_OFFICE365 (object);
450
451 g_mutex_lock (&oauth2_office365->priv->string_cache_lock);
452 g_hash_table_destroy (oauth2_office365->priv->string_cache);
453 g_mutex_unlock (&oauth2_office365->priv->string_cache_lock);
454 g_mutex_clear (&oauth2_office365->priv->string_cache_lock);
455
456 /* Chain up to parent's method. */
457 G_OBJECT_CLASS (e_oauth2_service_office365_parent_class)->finalize (object);
458 }
459
460 static SoupCookie *
461 eos_office365_get_prt_sso_cookie_sync (EOAuth2Service *service,
462 ESource *source,
463 GCancellable *cancellable)
464 {
465 SoupCookie *prtsso_cookie = NULL;
466 const gchar *sso_uri = eos_office365_get_authentication_uri (service, source);
467 const gchar *client_id = eos_office365_get_client_id (service, source);
468 const gchar *redirect_uri = eos_office365_get_redirect_uri (service, source);
469 CamelEwsSettings *ews_settings = NULL;
470 gchar *auth_user = NULL;
471 GError *local_error = NULL;
472
473 ews_settings = eos_office365_get_camel_settings (source);
474 auth_user = camel_network_settings_dup_user (CAMEL_NETWORK_SETTINGS (ews_settings));
475
476 prtsso_cookie = e_ms_oapxbc_util_get_prt_sso_cookie_sync (service, source, client_id, sso_uri,
477 redirect_uri, auth_user, cancellable, &local_error);
478 g_free (auth_user);
479 if (!prtsso_cookie) {
480 e_ews_debug_print (_("Failed to acquire PRT SSO cookie: %s"), local_error ? local_error->message : "Unknown error");
481 g_clear_error (&local_error);
482 }
483
484 return prtsso_cookie;
485 }
486
487 static GSList *
488 eos_office365_dup_credentials_prompter_cookies_sync (EOAuth2Service *service,
489 ESource *source,
490 GCancellable *cancellable)
491 {
492 SoupCookie *prtsso_cookie;
493 GSList *cookies = NULL;
494 prtsso_cookie = eos_office365_get_prt_sso_cookie_sync (service, source, cancellable);
495 if (!prtsso_cookie) {
496 return NULL;
497 }
498 cookies = g_slist_append (cookies, prtsso_cookie);
499 return cookies;
500 }
501
502 static void
503 eos_office365_prepare_refresh_token_message (EOAuth2Service *service,
504 ESource *source,
505 SoupMessage *message)
506 {
507 SoupMessageHeaders *request_headers;
508 SoupCookie *cookie;
509
510 cookie = eos_office365_get_prt_sso_cookie_sync (service, source, NULL);
511 if (!cookie) {
512 return;
513 }
514 request_headers = soup_message_get_request_headers (message);
515 soup_message_headers_append (request_headers,
516 soup_cookie_get_name (cookie),
517 soup_cookie_get_value (cookie));
518 soup_cookie_free (cookie);
519 }
520
521 static void
522 e_oauth2_service_office365_oauth2_service_init (EOAuth2ServiceInterface *iface)
523 {
524 iface->guess_can_process = eos_office365_guess_can_process;
525 iface->get_name = eos_office365_get_name;
526 iface->get_display_name = eos_office365_get_display_name;
527 iface->get_client_id = eos_office365_get_client_id;
528 iface->get_client_secret = eos_office365_get_client_secret;
529 iface->get_authentication_uri = eos_office365_get_authentication_uri;
530 iface->get_refresh_uri = eos_office365_get_refresh_uri;
531 iface->get_redirect_uri = eos_office365_get_redirect_uri;
532 iface->prepare_authentication_uri_query = eos_office365_prepare_authentication_uri_query;
533 iface->prepare_refresh_token_form = eos_office365_prepare_refresh_token_form;
534 iface->prepare_refresh_token_message = eos_office365_prepare_refresh_token_message;
535 iface->dup_credentials_prompter_cookies_sync = eos_office365_dup_credentials_prompter_cookies_sync;
536 }
537
538 static void
539 e_oauth2_service_office365_class_init (EOAuth2ServiceOffice365Class *klass)
540 {
541 GObjectClass *object_class;
542
543 object_class = G_OBJECT_CLASS (klass);
544 object_class->finalize = eos_office365_finalize;
545 }
546
547 static void
548 e_oauth2_service_office365_class_finalize (EOAuth2ServiceOffice365Class *klass)
549 {
550 }
551
552 static void
553 e_oauth2_service_office365_init (EOAuth2ServiceOffice365 *oauth2_office365)
554 {
555 oauth2_office365->priv = e_oauth2_service_office365_get_instance_private (oauth2_office365);
556
557 g_mutex_init (&oauth2_office365->priv->string_cache_lock);
558 oauth2_office365->priv->string_cache = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
559 }
560
561 void
562 e_oauth2_service_office365_type_register (GTypeModule *type_module)
563 {
564 e_oauth2_service_office365_register_type (type_module);
565 }