Debian Codegraph @

src/Microsoft365/common/e-oauth2-service-microsoft365.c

raw
1 /*
2 * SPDX-FileCopyrightText: (C) 2020 Red Hat (www.redhat.com)
3 * SPDX-License-Identifier: LGPL-2.1-or-later
4 */
5
6 #include "evolution-ews-config.h"
7
8 #include <glib/gi18n-lib.h>
9 #include <libedataserver/libedataserver.h>
10
11 #include "camel-m365-settings.h"
12 #include "e-ms-oapxbc-util.h"
13
14 #include "e-oauth2-service-microsoft365.h"
15
16 /* https://portal.azure.com/
17 https://docs.microsoft.com/en-us/graph/auth/
18
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/permissions-consent-overview
22 https://learn.microsoft.com/en-us/azure/active-directory/develop/v2-oauth2-auth-code-flow
23 */
24
25 #define MICROSOFT365_SCOPE "Calendars.ReadWrite" \
26 " Calendars.ReadWrite.Shared" \
27 " Contacts.ReadWrite" \
28 " Contacts.ReadWrite.Shared" \
29 " Mail.ReadWrite" \
30 " Mail.ReadWrite.Shared" \
31 " Mail.Send" \
32 " Mail.Send.Shared" \
33 " MailboxSettings.Read" \
34 /* " Notes.Create" */ \
35 /* " Notes.ReadWrite.All" */ \
36 " offline_access" \
37 " People.Read" \
38 " Tasks.ReadWrite" \
39 " Tasks.ReadWrite.Shared" \
40 " User.Read" \
41 " User.ReadBasic.All" /* User.Read.All requires admin approval */
42
43 struct _EOAuth2ServiceMicrosoft365Private
44 {
45 GMutex string_cache_lock;
46 GHashTable *string_cache;
47 };
48
49 /* Forward Declarations */
50 static void e_oauth2_service_microsoft365_oauth2_service_init (EOAuth2ServiceInterface *iface);
51
52 G_DEFINE_DYNAMIC_TYPE_EXTENDED (EOAuth2ServiceMicrosoft365, e_oauth2_service_microsoft365, E_TYPE_OAUTH2_SERVICE_BASE, 0,
53 G_IMPLEMENT_INTERFACE_DYNAMIC (E_TYPE_OAUTH2_SERVICE, e_oauth2_service_microsoft365_oauth2_service_init)
54 G_ADD_PRIVATE_DYNAMIC (EOAuth2ServiceMicrosoft365))
55
56 static const gchar *
57 eos_microsoft365_cache_string_take (EOAuth2ServiceMicrosoft365 *oauth2_microsoft365,
58 gchar *str) /* takes ownership of the 'str' */
59 {
60 const gchar *cached_str;
61
62 g_return_val_if_fail (E_IS_OAUTH2_SERVICE_MICROSOFT365 (oauth2_microsoft365), NULL);
63
64 if (!str)
65 return NULL;
66
67 if (!*str) {
68 g_free (str);
69 return "";
70 }
71
72 g_mutex_lock (&oauth2_microsoft365->priv->string_cache_lock);
73
74 cached_str = g_hash_table_lookup (oauth2_microsoft365->priv->string_cache, str);
75 if (cached_str) {
76 g_free (str);
77 } else {
78 g_hash_table_insert (oauth2_microsoft365->priv->string_cache, str, str);
79 cached_str = str;
80 }
81
82 g_mutex_unlock (&oauth2_microsoft365->priv->string_cache_lock);
83
84 return cached_str;
85 }
86
87
88 static const gchar *
89 eos_microsoft365_cache_string (EOAuth2ServiceMicrosoft365 *oauth2_microsoft365,
90 const gchar *str)
91 {
92 gchar *cached_str;
93
94 g_return_val_if_fail (E_IS_OAUTH2_SERVICE_MICROSOFT365 (oauth2_microsoft365), NULL);
95
96 if (!str || !*str)
97 return str;
98
99 g_mutex_lock (&oauth2_microsoft365->priv->string_cache_lock);
100
101 cached_str = g_hash_table_lookup (oauth2_microsoft365->priv->string_cache, str);
102
103 if (!cached_str) {
104 cached_str = g_strdup (str);
105 g_hash_table_insert (oauth2_microsoft365->priv->string_cache, cached_str, cached_str);
106 }
107
108 g_mutex_unlock (&oauth2_microsoft365->priv->string_cache_lock);
109
110 return cached_str;
111 }
112
113 static void
114 eos_microsoft365_get_endpoint_host_and_tenant_locked (CamelM365Settings *m365_settings,
115 const gchar **out_endpoint_host,
116 const gchar **out_tenant)
117 {
118 if (out_endpoint_host) {
119 *out_endpoint_host = camel_m365_settings_get_oauth2_endpoint_host (m365_settings);
120
121 if (e_util_strcmp0 (*out_endpoint_host, NULL) == 0)
122 *out_endpoint_host = MICROSOFT365_ENDPOINT_HOST;
123 }
124
125 if (out_tenant) {
126 *out_tenant = camel_m365_settings_get_oauth2_tenant (m365_settings);
127
128 if (e_util_strcmp0 (*out_tenant, NULL) == 0)
129 *out_tenant = MICROSOFT365_TENANT;
130 }
131 }
132
133 static CamelM365Settings *
134 eos_microsoft365_get_camel_settings (ESource *source)
135 {
136 ESourceCamel *extension;
137
138 if (!source)
139 return NULL;
140
141 g_return_val_if_fail (E_IS_SOURCE (source), NULL);
142
143 extension = e_source_get_extension (source, e_source_camel_get_extension_name ("microsoft365"));
144
145 return CAMEL_M365_SETTINGS (e_source_camel_get_settings (extension));
146 }
147
148 static gboolean
149 eos_microsoft365_guess_can_process (EOAuth2Service *service,
150 const gchar *protocol,
151 const gchar *hostname)
152 {
153 return e_oauth2_services_is_supported () &&
154 protocol && g_ascii_strcasecmp (protocol, "microsoft365") == 0;
155 }
156
157 static const gchar *
158 eos_microsoft365_get_name (EOAuth2Service *service)
159 {
160 return "Microsoft365";
161 }
162
163 static const gchar *
164 eos_microsoft365_get_display_name (EOAuth2Service *service)
165 {
166 /* Translators: This is a user-visible string, display name of an OAuth2 service. */
167 return C_("OAuth2Service", "Microsoft365");
168 }
169
170 static const gchar *
171 eos_microsoft365_get_client_id (EOAuth2Service *service,
172 ESource *source)
173 {
174 EOAuth2ServiceMicrosoft365 *oauth2_microsoft365 = E_OAUTH2_SERVICE_MICROSOFT365 (service);
175 CamelM365Settings *m365_settings;
176
177 m365_settings = eos_microsoft365_get_camel_settings (source);
178
179 if (m365_settings) {
180 const gchar *res = NULL;
181
182 camel_m365_settings_lock (m365_settings);
183
184 if (camel_m365_settings_get_override_oauth2 (m365_settings)) {
185 const gchar *client_id;
186
187 client_id = camel_m365_settings_get_oauth2_client_id (m365_settings);
188
189 if (e_util_strcmp0 (client_id, NULL) != 0)
190 res = eos_microsoft365_cache_string (oauth2_microsoft365, client_id);
191 }
192
193 camel_m365_settings_unlock (m365_settings);
194
195 if (res)
196 return res;
197 }
198
199 return MICROSOFT365_CLIENT_ID;
200 }
201
202 static const gchar *
203 eos_microsoft365_get_client_secret (EOAuth2Service *service,
204 ESource *source)
205 {
206 return NULL;
207 }
208
209 static const gchar *
210 eos_microsoft365_get_authentication_uri (EOAuth2Service *service,
211 ESource *source)
212 {
213 EOAuth2ServiceMicrosoft365 *oauth2_microsoft365 = E_OAUTH2_SERVICE_MICROSOFT365 (service);
214 CamelM365Settings *m365_settings;
215
216 m365_settings = eos_microsoft365_get_camel_settings (source);
217
218 if (m365_settings) {
219 const gchar *res = NULL;
220
221 camel_m365_settings_lock (m365_settings);
222
223 if (camel_m365_settings_get_override_oauth2 (m365_settings)) {
224 const gchar *endpoint_host = NULL;
225 const gchar *tenant = NULL;
226
227 eos_microsoft365_get_endpoint_host_and_tenant_locked (m365_settings, &endpoint_host, &tenant);
228
229 res = eos_microsoft365_cache_string_take (oauth2_microsoft365,
230 g_strdup_printf ("https://%s/%s/oauth2/v2.0/authorize", endpoint_host, tenant));
231 }
232
233 camel_m365_settings_unlock (m365_settings);
234
235 if (res)
236 return res;
237 }
238
239 return "https://" MICROSOFT365_ENDPOINT_HOST "/" MICROSOFT365_TENANT "/oauth2/v2.0/authorize";
240 }
241
242 static const gchar *
243 eos_microsoft365_get_refresh_uri (EOAuth2Service *service,
244 ESource *source)
245 {
246 EOAuth2ServiceMicrosoft365 *oauth2_microsoft365 = E_OAUTH2_SERVICE_MICROSOFT365 (service);
247 CamelM365Settings *m365_settings;
248
249 m365_settings = eos_microsoft365_get_camel_settings (source);
250
251 if (m365_settings) {
252 const gchar *res = NULL;
253
254 camel_m365_settings_lock (m365_settings);
255
256 if (camel_m365_settings_get_override_oauth2 (m365_settings)) {
257 const gchar *endpoint_host = NULL;
258 const gchar *tenant = NULL;
259
260 eos_microsoft365_get_endpoint_host_and_tenant_locked (m365_settings, &endpoint_host, &tenant);
261
262 res = eos_microsoft365_cache_string_take (oauth2_microsoft365,
263 g_strdup_printf ("https://%s/%s/oauth2/v2.0/token", endpoint_host, tenant));
264 }
265
266 camel_m365_settings_unlock (m365_settings);
267
268 if (res)
269 return res;
270 }
271
272 return "https://" MICROSOFT365_ENDPOINT_HOST "/" MICROSOFT365_TENANT "/oauth2/v2.0/token";
273 }
274
275 static const gchar *
276 eos_microsoft365_get_redirect_uri (EOAuth2Service *service,
277 ESource *source)
278 {
279 EOAuth2ServiceMicrosoft365 *oauth2_microsoft365 = E_OAUTH2_SERVICE_MICROSOFT365 (service);
280 CamelM365Settings *m365_settings;
281
282 m365_settings = eos_microsoft365_get_camel_settings (source);
283
284 if (m365_settings) {
285 const gchar *res = NULL;
286
287 camel_m365_settings_lock (m365_settings);
288
289 if (camel_m365_settings_get_override_oauth2 (m365_settings)) {
290 const gchar *redirect_uri;
291
292 redirect_uri = camel_m365_settings_get_oauth2_redirect_uri (m365_settings);
293
294 if (e_util_strcmp0 (redirect_uri, NULL) != 0)
295 res = eos_microsoft365_cache_string (oauth2_microsoft365, redirect_uri);
296
297 if (!res) {
298 const gchar *endpoint_host = NULL;
299
300 eos_microsoft365_get_endpoint_host_and_tenant_locked (m365_settings, &endpoint_host, NULL);
301
302 if (endpoint_host && g_strcmp0 (endpoint_host, MICROSOFT365_ENDPOINT_HOST) != 0) {
303 res = eos_microsoft365_cache_string_take (oauth2_microsoft365,
304 g_strdup_printf ("https://%s/common/oauth2/nativeclient", endpoint_host));
305 }
306 }
307 }
308
309 camel_m365_settings_unlock (m365_settings);
310
311 if (res)
312 return res;
313 }
314
315 if (e_util_strcmp0 (MICROSOFT365_REDIRECT_URI, NULL) != 0)
316 return MICROSOFT365_REDIRECT_URI;
317
318 return "https://" MICROSOFT365_ENDPOINT_HOST "/common/oauth2/nativeclient";
319 }
320
321 static void
322 eos_microsoft365_prepare_authentication_uri_query (EOAuth2Service *service,
323 ESource *source,
324 GHashTable *uri_query)
325 {
326 g_return_if_fail (uri_query != NULL);
327
328 e_oauth2_service_util_set_to_form (uri_query, "response_type", "code");
329 e_oauth2_service_util_set_to_form (uri_query, "scope", MICROSOFT365_SCOPE);
330 e_oauth2_service_util_set_to_form (uri_query, "response_mode", "query");
331 }
332
333 static void
334 eos_microsoft365_prepare_refresh_token_form (EOAuth2Service *service,
335 ESource *source,
336 const gchar *refresh_token,
337 GHashTable *form)
338 {
339 g_return_if_fail (form != NULL);
340
341 e_oauth2_service_util_set_to_form (form, "scope", MICROSOFT365_SCOPE);
342 e_oauth2_service_util_set_to_form (form, "redirect_uri", e_oauth2_service_get_redirect_uri (service, source));
343 }
344
345 static void
346 eos_microsoft365_finalize (GObject *object)
347 {
348 EOAuth2ServiceMicrosoft365 *oauth2_microsoft365 = E_OAUTH2_SERVICE_MICROSOFT365 (object);
349
350 g_mutex_lock (&oauth2_microsoft365->priv->string_cache_lock);
351 g_hash_table_destroy (oauth2_microsoft365->priv->string_cache);
352 g_mutex_unlock (&oauth2_microsoft365->priv->string_cache_lock);
353 g_mutex_clear (&oauth2_microsoft365->priv->string_cache_lock);
354
355 /* Chain up to parent's method. */
356 G_OBJECT_CLASS (e_oauth2_service_microsoft365_parent_class)->finalize (object);
357 }
358
359 static SoupCookie *
360 eos_microsoft365_get_prt_sso_cookie_sync (EOAuth2Service *service,
361 ESource *source,
362 GCancellable *cancellable)
363 {
364 SoupCookie *prtsso_cookie = NULL;
365 const gchar *sso_uri = eos_microsoft365_get_authentication_uri (service, source);
366 const gchar *client_id = eos_microsoft365_get_client_id (service, source);
367 const gchar *redirect_uri = eos_microsoft365_get_redirect_uri (service, source);
368 CamelM365Settings *camel_settings = NULL;
369 gchar *auth_user = NULL;
370 GError *local_error = NULL;
371
372 camel_settings = eos_microsoft365_get_camel_settings (source);
373 auth_user = camel_network_settings_dup_user (CAMEL_NETWORK_SETTINGS (camel_settings));
374
375 prtsso_cookie = e_ms_oapxbc_util_get_prt_sso_cookie_sync (service, source, client_id, sso_uri,
376 redirect_uri, auth_user, cancellable, &local_error);
377 g_free (auth_user);
378 if (!prtsso_cookie) {
379 /* no error reporting yet, infra is missing */
380 g_clear_error (&local_error);
381 }
382
383 return prtsso_cookie;
384 }
385
386 static GSList *
387 eos_microsoft365_dup_credentials_prompter_cookies_sync (EOAuth2Service *service,
388 ESource *source,
389 GCancellable *cancellable)
390 {
391 SoupCookie *prtsso_cookie;
392 GSList *cookies = NULL;
393 prtsso_cookie = eos_microsoft365_get_prt_sso_cookie_sync (service, source, cancellable);
394 if (!prtsso_cookie) {
395 return NULL;
396 }
397 cookies = g_slist_append (cookies, prtsso_cookie);
398 return cookies;
399 }
400
401 static void
402 eos_microsoft365_prepare_refresh_token_message (EOAuth2Service *service,
403 ESource *source,
404 SoupMessage *message)
405 {
406 SoupMessageHeaders *request_headers;
407 SoupCookie *cookie;
408
409 cookie = eos_microsoft365_get_prt_sso_cookie_sync (service, source, NULL);
410 if (!cookie) {
411 return;
412 }
413 request_headers = soup_message_get_request_headers (message);
414 soup_message_headers_append (request_headers,
415 soup_cookie_get_name (cookie),
416 soup_cookie_get_value (cookie));
417 soup_cookie_free (cookie);
418 }
419
420 static void
421 e_oauth2_service_microsoft365_oauth2_service_init (EOAuth2ServiceInterface *iface)
422 {
423 iface->guess_can_process = eos_microsoft365_guess_can_process;
424 iface->get_name = eos_microsoft365_get_name;
425 iface->get_display_name = eos_microsoft365_get_display_name;
426 iface->get_client_id = eos_microsoft365_get_client_id;
427 iface->get_client_secret = eos_microsoft365_get_client_secret;
428 iface->get_authentication_uri = eos_microsoft365_get_authentication_uri;
429 iface->get_refresh_uri = eos_microsoft365_get_refresh_uri;
430 iface->get_redirect_uri = eos_microsoft365_get_redirect_uri;
431 iface->prepare_authentication_uri_query = eos_microsoft365_prepare_authentication_uri_query;
432 iface->prepare_refresh_token_form = eos_microsoft365_prepare_refresh_token_form;
433 iface->prepare_refresh_token_message = eos_microsoft365_prepare_refresh_token_message;
434 iface->dup_credentials_prompter_cookies_sync = eos_microsoft365_dup_credentials_prompter_cookies_sync;
435 }
436
437 static void
438 e_oauth2_service_microsoft365_class_init (EOAuth2ServiceMicrosoft365Class *klass)
439 {
440 GObjectClass *object_class;
441
442 object_class = G_OBJECT_CLASS (klass);
443 object_class->finalize = eos_microsoft365_finalize;
444
445 /* Make sure the ESourceCamel knows about it, even when no Microsoft365 mail account is created */
446 e_source_camel_generate_subtype ("microsoft365", CAMEL_TYPE_M365_SETTINGS);
447 }
448
449 static void
450 e_oauth2_service_microsoft365_class_finalize (EOAuth2ServiceMicrosoft365Class *klass)
451 {
452 }
453
454 static void
455 e_oauth2_service_microsoft365_init (EOAuth2ServiceMicrosoft365 *oauth2_microsoft365)
456 {
457 oauth2_microsoft365->priv = e_oauth2_service_microsoft365_get_instance_private (oauth2_microsoft365);
458
459 g_mutex_init (&oauth2_microsoft365->priv->string_cache_lock);
460 oauth2_microsoft365->priv->string_cache = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
461 }
462
463 void
464 e_oauth2_service_microsoft365_type_register (GTypeModule *type_module)
465 {
466 e_oauth2_service_microsoft365_register_type (type_module);
467 }