[evolution-ews] Simplify code around OAuth2 processing



commit 089854bc9437fb6693e0228a0e254453984a4736
Author: Milan Crha <mcrha redhat com>
Date:   Tue Oct 13 12:34:56 2020 +0200

    Simplify code around OAuth2 processing

 src/EWS/common/camel-ews-settings.c                | 136 ++++++-----
 src/EWS/common/camel-ews-settings.h                |   2 +
 src/EWS/common/e-oauth2-service-office365.c        | 250 +++++++++++++--------
 src/EWS/evolution/e-mail-config-ews-backend.c      |  36 ++-
 src/Microsoft365/common/camel-m365-settings.c      |  76 ++++---
 src/Microsoft365/common/camel-m365-settings.h      |   2 +
 .../common/e-oauth2-service-microsoft365.c         | 195 +++++++++-------
 .../evolution/e-mail-config-m365-backend.c         |  33 ++-
 8 files changed, 453 insertions(+), 277 deletions(-)
---
diff --git a/src/EWS/common/camel-ews-settings.c b/src/EWS/common/camel-ews-settings.c
index 956bb456..35b7626a 100644
--- a/src/EWS/common/camel-ews-settings.c
+++ b/src/EWS/common/camel-ews-settings.c
@@ -858,6 +858,22 @@ camel_ews_settings_init (CamelEwsSettings *settings)
                                     NULL);
 }
 
+void
+camel_ews_settings_lock (CamelEwsSettings *settings)
+{
+       g_return_if_fail (CAMEL_IS_EWS_SETTINGS (settings));
+
+       g_mutex_lock (&settings->priv->property_lock);
+}
+
+void
+camel_ews_settings_unlock (CamelEwsSettings *settings)
+{
+       g_return_if_fail (CAMEL_IS_EWS_SETTINGS (settings));
+
+       g_mutex_unlock (&settings->priv->property_lock);
+}
+
 /**
  * camel_ews_settings_get_auth_mechanism:
  * @settings: a #CamelEwsSettings
@@ -995,12 +1011,12 @@ camel_ews_settings_dup_email (CamelEwsSettings *settings)
 
        g_return_val_if_fail (CAMEL_IS_EWS_SETTINGS (settings), NULL);
 
-       g_mutex_lock (&settings->priv->property_lock);
+       camel_ews_settings_lock (settings);
 
        protected = camel_ews_settings_get_email (settings);
        duplicate = g_strdup (protected);
 
-       g_mutex_unlock (&settings->priv->property_lock);
+       camel_ews_settings_unlock (settings);
 
        return duplicate;
 }
@@ -1011,17 +1027,17 @@ camel_ews_settings_set_email (CamelEwsSettings *settings,
 {
        g_return_if_fail (CAMEL_IS_EWS_SETTINGS (settings));
 
-       g_mutex_lock (&settings->priv->property_lock);
+       camel_ews_settings_lock (settings);
 
        if (g_strcmp0 (settings->priv->email, email) == 0) {
-               g_mutex_unlock (&settings->priv->property_lock);
+               camel_ews_settings_unlock (settings);
                return;
        }
 
        g_free (settings->priv->email);
        settings->priv->email = e_util_strdup_strip (email);
 
-       g_mutex_unlock (&settings->priv->property_lock);
+       camel_ews_settings_unlock (settings);
 
        g_object_notify (G_OBJECT (settings), "email");
 }
@@ -1128,12 +1144,12 @@ camel_ews_settings_dup_gal_uid (CamelEwsSettings *settings)
 
        g_return_val_if_fail (CAMEL_IS_EWS_SETTINGS (settings), NULL);
 
-       g_mutex_lock (&settings->priv->property_lock);
+       camel_ews_settings_lock (settings);
 
        protected = camel_ews_settings_get_gal_uid (settings);
        duplicate = g_strdup (protected);
 
-       g_mutex_unlock (&settings->priv->property_lock);
+       camel_ews_settings_unlock (settings);
 
        return duplicate;
 }
@@ -1144,17 +1160,17 @@ camel_ews_settings_set_gal_uid (CamelEwsSettings *settings,
 {
        g_return_if_fail (CAMEL_IS_EWS_SETTINGS (settings));
 
-       g_mutex_lock (&settings->priv->property_lock);
+       camel_ews_settings_lock (settings);
 
        if (g_strcmp0 (settings->priv->gal_uid, gal_uid) == 0) {
-               g_mutex_unlock (&settings->priv->property_lock);
+               camel_ews_settings_unlock (settings);
                return;
        }
 
        g_free (settings->priv->gal_uid);
        settings->priv->gal_uid = e_util_strdup_strip (gal_uid);
 
-       g_mutex_unlock (&settings->priv->property_lock);
+       camel_ews_settings_unlock (settings);
 
        g_object_notify (G_OBJECT (settings), "gal-uid");
 }
@@ -1175,12 +1191,12 @@ camel_ews_settings_dup_hosturl (CamelEwsSettings *settings)
 
        g_return_val_if_fail (CAMEL_IS_EWS_SETTINGS (settings), NULL);
 
-       g_mutex_lock (&settings->priv->property_lock);
+       camel_ews_settings_lock (settings);
 
        protected = camel_ews_settings_get_hosturl (settings);
        duplicate = g_strdup (protected);
 
-       g_mutex_unlock (&settings->priv->property_lock);
+       camel_ews_settings_unlock (settings);
 
        return duplicate;
 }
@@ -1191,17 +1207,17 @@ camel_ews_settings_set_hosturl (CamelEwsSettings *settings,
 {
        g_return_if_fail (CAMEL_IS_EWS_SETTINGS (settings));
 
-       g_mutex_lock (&settings->priv->property_lock);
+       camel_ews_settings_lock (settings);
 
        if (g_strcmp0 (settings->priv->hosturl, hosturl) == 0) {
-               g_mutex_unlock (&settings->priv->property_lock);
+               camel_ews_settings_unlock (settings);
                return;
        }
 
        g_free (settings->priv->hosturl);
        settings->priv->hosturl = e_util_strdup_strip (hosturl);
 
-       g_mutex_unlock (&settings->priv->property_lock);
+       camel_ews_settings_unlock (settings);
 
        g_object_notify (G_OBJECT (settings), "hosturl");
 }
@@ -1222,12 +1238,12 @@ camel_ews_settings_dup_oaburl (CamelEwsSettings *settings)
 
        g_return_val_if_fail (CAMEL_IS_EWS_SETTINGS (settings), NULL);
 
-       g_mutex_lock (&settings->priv->property_lock);
+       camel_ews_settings_lock (settings);
 
        protected = camel_ews_settings_get_oaburl (settings);
        duplicate = g_strdup (protected);
 
-       g_mutex_unlock (&settings->priv->property_lock);
+       camel_ews_settings_unlock (settings);
 
        return duplicate;
 }
@@ -1238,17 +1254,17 @@ camel_ews_settings_set_oaburl (CamelEwsSettings *settings,
 {
        g_return_if_fail (CAMEL_IS_EWS_SETTINGS (settings));
 
-       g_mutex_lock (&settings->priv->property_lock);
+       camel_ews_settings_lock (settings);
 
        if (g_strcmp0 (settings->priv->oaburl, oaburl) == 0) {
-               g_mutex_unlock (&settings->priv->property_lock);
+               camel_ews_settings_unlock (settings);
                return;
        }
 
        g_free (settings->priv->oaburl);
        settings->priv->oaburl = e_util_strdup_strip (oaburl);
 
-       g_mutex_unlock (&settings->priv->property_lock);
+       camel_ews_settings_unlock (settings);
 
        g_object_notify (G_OBJECT (settings), "oaburl");
 }
@@ -1291,12 +1307,12 @@ camel_ews_settings_dup_oal_selected (CamelEwsSettings *settings)
 
        g_return_val_if_fail (CAMEL_IS_EWS_SETTINGS (settings), NULL);
 
-       g_mutex_lock (&settings->priv->property_lock);
+       camel_ews_settings_lock (settings);
 
        protected = camel_ews_settings_get_oal_selected (settings);
        duplicate = g_strdup (protected);
 
-       g_mutex_unlock (&settings->priv->property_lock);
+       camel_ews_settings_unlock (settings);
 
        return duplicate;
 }
@@ -1307,17 +1323,17 @@ camel_ews_settings_set_oal_selected (CamelEwsSettings *settings,
 {
        g_return_if_fail (CAMEL_IS_EWS_SETTINGS (settings));
 
-       g_mutex_lock (&settings->priv->property_lock);
+       camel_ews_settings_lock (settings);
 
        if (g_strcmp0 (settings->priv->oal_selected, oal_selected) == 0) {
-               g_mutex_unlock (&settings->priv->property_lock);
+               camel_ews_settings_unlock (settings);
                return;
        }
 
        g_free (settings->priv->oal_selected);
        settings->priv->oal_selected = e_util_strdup_strip (oal_selected);
 
-       g_mutex_unlock (&settings->priv->property_lock);
+       camel_ews_settings_unlock (settings);
 
        g_object_notify (G_OBJECT (settings), "oal-selected");
 }
@@ -1382,12 +1398,12 @@ camel_ews_settings_dup_impersonate_user (CamelEwsSettings *settings)
 
        g_return_val_if_fail (CAMEL_IS_EWS_SETTINGS (settings), NULL);
 
-       g_mutex_lock (&settings->priv->property_lock);
+       camel_ews_settings_lock (settings);
 
        protected = camel_ews_settings_get_impersonate_user (settings);
        duplicate = g_strdup (protected);
 
-       g_mutex_unlock (&settings->priv->property_lock);
+       camel_ews_settings_unlock (settings);
 
        return duplicate;
 }
@@ -1398,17 +1414,17 @@ camel_ews_settings_set_impersonate_user (CamelEwsSettings *settings,
 {
        g_return_if_fail (CAMEL_IS_EWS_SETTINGS (settings));
 
-       g_mutex_lock (&settings->priv->property_lock);
+       camel_ews_settings_lock (settings);
 
        if (g_strcmp0 (settings->priv->impersonate_user, impersonate_user) == 0) {
-               g_mutex_unlock (&settings->priv->property_lock);
+               camel_ews_settings_unlock (settings);
                return;
        }
 
        g_free (settings->priv->impersonate_user);
        settings->priv->impersonate_user = e_util_strdup_strip (impersonate_user);
 
-       g_mutex_unlock (&settings->priv->property_lock);
+       camel_ews_settings_unlock (settings);
 
        g_object_notify (G_OBJECT (settings), "impersonate-user");
 }
@@ -1451,12 +1467,12 @@ camel_ews_settings_dup_user_agent (CamelEwsSettings *settings)
 
        g_return_val_if_fail (CAMEL_IS_EWS_SETTINGS (settings), NULL);
 
-       g_mutex_lock (&settings->priv->property_lock);
+       camel_ews_settings_lock (settings);
 
        protected = camel_ews_settings_get_user_agent (settings);
        duplicate = g_strdup (protected);
 
-       g_mutex_unlock (&settings->priv->property_lock);
+       camel_ews_settings_unlock (settings);
 
        return duplicate;
 }
@@ -1467,17 +1483,17 @@ camel_ews_settings_set_user_agent (CamelEwsSettings *settings,
 {
        g_return_if_fail (CAMEL_IS_EWS_SETTINGS (settings));
 
-       g_mutex_lock (&settings->priv->property_lock);
+       camel_ews_settings_lock (settings);
 
        if (g_strcmp0 (settings->priv->user_agent, user_agent) == 0) {
-               g_mutex_unlock (&settings->priv->property_lock);
+               camel_ews_settings_unlock (settings);
                return;
        }
 
        g_free (settings->priv->user_agent);
        settings->priv->user_agent = e_util_strdup_strip (user_agent);
 
-       g_mutex_unlock (&settings->priv->property_lock);
+       camel_ews_settings_unlock (settings);
 
        g_object_notify (G_OBJECT (settings), "user-agent");
 }
@@ -1520,12 +1536,12 @@ camel_ews_settings_dup_oauth2_tenant (CamelEwsSettings *settings)
 
        g_return_val_if_fail (CAMEL_IS_EWS_SETTINGS (settings), NULL);
 
-       g_mutex_lock (&settings->priv->property_lock);
+       camel_ews_settings_lock (settings);
 
        protected = camel_ews_settings_get_oauth2_tenant (settings);
        duplicate = g_strdup (protected);
 
-       g_mutex_unlock (&settings->priv->property_lock);
+       camel_ews_settings_unlock (settings);
 
        return duplicate;
 }
@@ -1536,17 +1552,17 @@ camel_ews_settings_set_oauth2_tenant (CamelEwsSettings *settings,
 {
        g_return_if_fail (CAMEL_IS_EWS_SETTINGS (settings));
 
-       g_mutex_lock (&settings->priv->property_lock);
+       camel_ews_settings_lock (settings);
 
        if (g_strcmp0 (settings->priv->oauth2_tenant, tenant) == 0) {
-               g_mutex_unlock (&settings->priv->property_lock);
+               camel_ews_settings_unlock (settings);
                return;
        }
 
        g_free (settings->priv->oauth2_tenant);
        settings->priv->oauth2_tenant = e_util_strdup_strip (tenant);
 
-       g_mutex_unlock (&settings->priv->property_lock);
+       camel_ews_settings_unlock (settings);
 
        g_object_notify (G_OBJECT (settings), "oauth2-tenant");
 }
@@ -1567,12 +1583,12 @@ camel_ews_settings_dup_oauth2_client_id (CamelEwsSettings *settings)
 
        g_return_val_if_fail (CAMEL_IS_EWS_SETTINGS (settings), NULL);
 
-       g_mutex_lock (&settings->priv->property_lock);
+       camel_ews_settings_lock (settings);
 
        protected = camel_ews_settings_get_oauth2_client_id (settings);
        duplicate = g_strdup (protected);
 
-       g_mutex_unlock (&settings->priv->property_lock);
+       camel_ews_settings_unlock (settings);
 
        return duplicate;
 }
@@ -1583,17 +1599,17 @@ camel_ews_settings_set_oauth2_client_id (CamelEwsSettings *settings,
 {
        g_return_if_fail (CAMEL_IS_EWS_SETTINGS (settings));
 
-       g_mutex_lock (&settings->priv->property_lock);
+       camel_ews_settings_lock (settings);
 
        if (g_strcmp0 (settings->priv->oauth2_client_id, client_id) == 0) {
-               g_mutex_unlock (&settings->priv->property_lock);
+               camel_ews_settings_unlock (settings);
                return;
        }
 
        g_free (settings->priv->oauth2_client_id);
        settings->priv->oauth2_client_id = e_util_strdup_strip (client_id);
 
-       g_mutex_unlock (&settings->priv->property_lock);
+       camel_ews_settings_unlock (settings);
 
        g_object_notify (G_OBJECT (settings), "oauth2-client-id");
 }
@@ -1614,12 +1630,12 @@ camel_ews_settings_dup_oauth2_redirect_uri (CamelEwsSettings *settings)
 
        g_return_val_if_fail (CAMEL_IS_EWS_SETTINGS (settings), NULL);
 
-       g_mutex_lock (&settings->priv->property_lock);
+       camel_ews_settings_lock (settings);
 
        protected = camel_ews_settings_get_oauth2_redirect_uri (settings);
        duplicate = g_strdup (protected);
 
-       g_mutex_unlock (&settings->priv->property_lock);
+       camel_ews_settings_unlock (settings);
 
        return duplicate;
 }
@@ -1630,17 +1646,17 @@ camel_ews_settings_set_oauth2_redirect_uri (CamelEwsSettings *settings,
 {
        g_return_if_fail (CAMEL_IS_EWS_SETTINGS (settings));
 
-       g_mutex_lock (&settings->priv->property_lock);
+       camel_ews_settings_lock (settings);
 
        if (g_strcmp0 (settings->priv->oauth2_redirect_uri, redirect_uri) == 0) {
-               g_mutex_unlock (&settings->priv->property_lock);
+               camel_ews_settings_unlock (settings);
                return;
        }
 
        g_free (settings->priv->oauth2_redirect_uri);
        settings->priv->oauth2_redirect_uri = e_util_strdup_strip (redirect_uri);
 
-       g_mutex_unlock (&settings->priv->property_lock);
+       camel_ews_settings_unlock (settings);
 
        g_object_notify (G_OBJECT (settings), "oauth2-redirect-uri");
 }
@@ -1661,12 +1677,12 @@ camel_ews_settings_dup_oauth2_resource_uri (CamelEwsSettings *settings)
 
        g_return_val_if_fail (CAMEL_IS_EWS_SETTINGS (settings), NULL);
 
-       g_mutex_lock (&settings->priv->property_lock);
+       camel_ews_settings_lock (settings);
 
        protected = camel_ews_settings_get_oauth2_resource_uri (settings);
        duplicate = g_strdup (protected);
 
-       g_mutex_unlock (&settings->priv->property_lock);
+       camel_ews_settings_unlock (settings);
 
        return duplicate;
 }
@@ -1677,17 +1693,17 @@ camel_ews_settings_set_oauth2_resource_uri (CamelEwsSettings *settings,
 {
        g_return_if_fail (CAMEL_IS_EWS_SETTINGS (settings));
 
-       g_mutex_lock (&settings->priv->property_lock);
+       camel_ews_settings_lock (settings);
 
        if (g_strcmp0 (settings->priv->oauth2_resource_uri, resource_uri) == 0) {
-               g_mutex_unlock (&settings->priv->property_lock);
+               camel_ews_settings_unlock (settings);
                return;
        }
 
        g_free (settings->priv->oauth2_resource_uri);
        settings->priv->oauth2_resource_uri = e_util_strdup_strip (resource_uri);
 
-       g_mutex_unlock (&settings->priv->property_lock);
+       camel_ews_settings_unlock (settings);
 
        g_object_notify (G_OBJECT (settings), "oauth2-resource-uri");
 }
@@ -1708,12 +1724,12 @@ camel_ews_settings_dup_oauth2_endpoint_host (CamelEwsSettings *settings)
 
        g_return_val_if_fail (CAMEL_IS_EWS_SETTINGS (settings), NULL);
 
-       g_mutex_lock (&settings->priv->property_lock);
+       camel_ews_settings_lock (settings);
 
        protected = camel_ews_settings_get_oauth2_endpoint_host (settings);
        duplicate = g_strdup (protected);
 
-       g_mutex_unlock (&settings->priv->property_lock);
+       camel_ews_settings_unlock (settings);
 
        return duplicate;
 }
@@ -1724,17 +1740,17 @@ camel_ews_settings_set_oauth2_endpoint_host (CamelEwsSettings *settings,
 {
        g_return_if_fail (CAMEL_IS_EWS_SETTINGS (settings));
 
-       g_mutex_lock (&settings->priv->property_lock);
+       camel_ews_settings_lock (settings);
 
        if (g_strcmp0 (settings->priv->oauth2_endpoint_host, endpoint_host) == 0) {
-               g_mutex_unlock (&settings->priv->property_lock);
+               camel_ews_settings_unlock (settings);
                return;
        }
 
        g_free (settings->priv->oauth2_endpoint_host);
        settings->priv->oauth2_endpoint_host = e_util_strdup_strip (endpoint_host);
 
-       g_mutex_unlock (&settings->priv->property_lock);
+       camel_ews_settings_unlock (settings);
 
        g_object_notify (G_OBJECT (settings), "oauth2-endpoint-host");
 }
diff --git a/src/EWS/common/camel-ews-settings.h b/src/EWS/common/camel-ews-settings.h
index e0423264..fa884ea5 100644
--- a/src/EWS/common/camel-ews-settings.h
+++ b/src/EWS/common/camel-ews-settings.h
@@ -55,6 +55,8 @@ typedef enum {
 
 
 GType          camel_ews_settings_get_type     (void) G_GNUC_CONST;
+void           camel_ews_settings_lock         (CamelEwsSettings *settings);
+void           camel_ews_settings_unlock       (CamelEwsSettings *settings);
 EwsAuthType    camel_ews_settings_get_auth_mechanism
                                                (CamelEwsSettings *settings);
 gboolean       camel_ews_settings_get_check_all
diff --git a/src/EWS/common/e-oauth2-service-office365.c b/src/EWS/common/e-oauth2-service-office365.c
index ab25d158..72e9b9cd 100644
--- a/src/EWS/common/e-oauth2-service-office365.c
+++ b/src/EWS/common/e-oauth2-service-office365.c
@@ -31,8 +31,8 @@ G_DEFINE_DYNAMIC_TYPE_EXTENDED (EOAuth2ServiceOffice365, e_oauth2_service_office
        G_IMPLEMENT_INTERFACE_DYNAMIC (E_TYPE_OAUTH2_SERVICE, e_oauth2_service_office365_oauth2_service_init))
 
 static const gchar *
-eos_office365_cache_string (EOAuth2ServiceOffice365 *oauth2_office365,
-                           gchar *str) /* takes ownership of the 'str' */
+eos_office365_cache_string_take (EOAuth2ServiceOffice365 *oauth2_office365,
+                                gchar *str) /* takes ownership of the 'str' */
 {
        const gchar *cached_str;
 
@@ -62,21 +62,48 @@ eos_office365_cache_string (EOAuth2ServiceOffice365 *oauth2_office365,
 }
 
 static const gchar *
-eos_office365_get_endpoint_host (EOAuth2ServiceOffice365 *oauth2_office365,
-                                CamelEwsSettings *ews_settings)
+eos_office365_cache_string (EOAuth2ServiceOffice365 *oauth2_office365,
+                           const gchar *str)
 {
-       if (ews_settings && camel_ews_settings_get_override_oauth2 (ews_settings)) {
-               gchar *endpoint_host;
+       gchar *cached_str;
+
+       g_return_val_if_fail (E_IS_OAUTH2_SERVICE_OFFICE365 (oauth2_office365), NULL);
 
-               endpoint_host = camel_ews_settings_dup_oauth2_endpoint_host (ews_settings);
+       if (!str || !*str)
+               return str;
 
-               if (endpoint_host && *endpoint_host)
-                       return eos_office365_cache_string (oauth2_office365, endpoint_host);
+       g_mutex_lock (&oauth2_office365->priv->string_cache_lock);
+
+       cached_str = g_hash_table_lookup (oauth2_office365->priv->string_cache, str);
 
-               g_free (endpoint_host);
+       if (!cached_str) {
+               cached_str = g_strdup (str);
+               g_hash_table_insert (oauth2_office365->priv->string_cache, cached_str, cached_str);
        }
 
-       return OFFICE365_ENDPOINT_HOST;
+       g_mutex_unlock (&oauth2_office365->priv->string_cache_lock);
+
+       return cached_str;
+}
+
+static void
+eos_office365_get_endpoint_host_and_tenant_locked (CamelEwsSettings *ews_settings,
+                                                  const gchar **out_endpoint_host,
+                                                  const gchar **out_tenant)
+{
+       if (out_endpoint_host) {
+               *out_endpoint_host = camel_ews_settings_get_oauth2_endpoint_host (ews_settings);
+
+               if (e_util_strcmp0 (*out_endpoint_host, NULL) == 0)
+                       *out_endpoint_host = OFFICE365_ENDPOINT_HOST;
+       }
+
+       if (out_tenant) {
+               *out_tenant = camel_ews_settings_get_oauth2_tenant (ews_settings);
+
+               if (e_util_strcmp0 (*out_tenant, NULL) == 0)
+                       *out_tenant = OFFICE365_TENANT;
+       }
 }
 
 static CamelEwsSettings *
@@ -124,16 +151,25 @@ eos_office365_get_client_id (EOAuth2Service *service,
        CamelEwsSettings *ews_settings;
 
        ews_settings = eos_office365_get_camel_settings (source);
-       if (ews_settings && camel_ews_settings_get_override_oauth2 (ews_settings)) {
-               gchar *client_id = camel_ews_settings_dup_oauth2_client_id (ews_settings);
 
-               if (client_id && !*client_id) {
-                       g_free (client_id);
-                       client_id = NULL;
+       if (ews_settings) {
+               const gchar *res = NULL;
+
+               camel_ews_settings_lock (ews_settings);
+
+               if (camel_ews_settings_get_override_oauth2 (ews_settings)) {
+                       const gchar *client_id;
+
+                       client_id = camel_ews_settings_get_oauth2_client_id (ews_settings);
+
+                       if (e_util_strcmp0 (client_id, NULL) != 0)
+                               res = eos_office365_cache_string (oauth2_office365, client_id);
                }
 
-               if (client_id)
-                       return eos_office365_cache_string (oauth2_office365, client_id);
+               camel_ews_settings_unlock (ews_settings);
+
+               if (res)
+                       return res;
        }
 
        return OFFICE365_CLIENT_ID;
@@ -154,30 +190,29 @@ eos_office365_get_authentication_uri (EOAuth2Service *service,
        CamelEwsSettings *ews_settings;
 
        ews_settings = eos_office365_get_camel_settings (source);
-       if (ews_settings && camel_ews_settings_get_override_oauth2 (ews_settings)) {
-               gchar *tenant;
-               const gchar *res;
-
-               tenant = camel_ews_settings_dup_oauth2_tenant (ews_settings);
-               if (tenant && !*tenant) {
-                       g_free (tenant);
-                       tenant = NULL;
-               }
 
-               res = eos_office365_cache_string (oauth2_office365,
-                       g_strdup_printf ("https://%s/%s/oauth2/authorize";,
-                               eos_office365_get_endpoint_host (oauth2_office365, ews_settings),
-                               tenant ? tenant : OFFICE365_TENANT));
+       if (ews_settings) {
+               const gchar *res = NULL;
+
+               camel_ews_settings_lock (ews_settings);
+
+               if (camel_ews_settings_get_override_oauth2 (ews_settings)) {
+                       const gchar *endpoint_host = NULL;
+                       const gchar *tenant = NULL;
+
+                       eos_office365_get_endpoint_host_and_tenant_locked (ews_settings, &endpoint_host, 
&tenant);
+
+                       res = eos_office365_cache_string_take (oauth2_office365,
+                               g_strdup_printf ("https://%s/%s/oauth2/authorize";, endpoint_host, tenant));
+               }
 
-               g_free (tenant);
+               camel_ews_settings_unlock (ews_settings);
 
-               return res;
+               if (res)
+                       return res;
        }
 
-       return eos_office365_cache_string (oauth2_office365,
-               g_strdup_printf ("https://%s/%s/oauth2/authorize";,
-                       eos_office365_get_endpoint_host (oauth2_office365, ews_settings),
-                       OFFICE365_TENANT));
+       return "https://"; OFFICE365_ENDPOINT_HOST "/" OFFICE365_TENANT "/oauth2/authorize";
 }
 
 static const gchar *
@@ -188,30 +223,29 @@ eos_office365_get_refresh_uri (EOAuth2Service *service,
        CamelEwsSettings *ews_settings;
 
        ews_settings = eos_office365_get_camel_settings (source);
-       if (ews_settings && camel_ews_settings_get_override_oauth2 (ews_settings)) {
-               gchar *tenant;
-               const gchar *res;
-
-               tenant = camel_ews_settings_dup_oauth2_tenant (ews_settings);
-               if (tenant && !*tenant) {
-                       g_free (tenant);
-                       tenant = NULL;
-               }
 
-               res = eos_office365_cache_string (oauth2_office365,
-                       g_strdup_printf ("https://%s/%s/oauth2/token";,
-                               eos_office365_get_endpoint_host (oauth2_office365, ews_settings),
-                               tenant ? tenant : OFFICE365_TENANT));
+       if (ews_settings) {
+               const gchar *res = NULL;
+
+               camel_ews_settings_lock (ews_settings);
+
+               if (camel_ews_settings_get_override_oauth2 (ews_settings)) {
+                       const gchar *endpoint_host = NULL;
+                       const gchar *tenant = NULL;
+
+                       eos_office365_get_endpoint_host_and_tenant_locked (ews_settings, &endpoint_host, 
&tenant);
+
+                       res = eos_office365_cache_string_take (oauth2_office365,
+                               g_strdup_printf ("https://%s/%s/oauth2/token";, endpoint_host, tenant));
+               }
 
-               g_free (tenant);
+               camel_ews_settings_unlock (ews_settings);
 
-               return res;
+               if (res)
+                       return res;
        }
 
-       return eos_office365_cache_string (oauth2_office365,
-               g_strdup_printf ("https://%s/%s/oauth2/token";,
-                       eos_office365_get_endpoint_host (oauth2_office365, ews_settings),
-                       OFFICE365_TENANT));
+       return "https://"; OFFICE365_ENDPOINT_HOST "/" OFFICE365_TENANT "/oauth2/token";
 }
 
 static const gchar *
@@ -220,36 +254,52 @@ eos_office365_get_redirect_uri (EOAuth2Service *service,
 {
        EOAuth2ServiceOffice365 *oauth2_office365 = E_OAUTH2_SERVICE_OFFICE365 (service);
        CamelEwsSettings *ews_settings;
-       const gchar *res;
 
        ews_settings = eos_office365_get_camel_settings (source);
-       if (ews_settings && camel_ews_settings_get_override_oauth2 (ews_settings)) {
-               gchar *redirect_uri;
 
-               redirect_uri = camel_ews_settings_dup_oauth2_redirect_uri (ews_settings);
+       if (ews_settings) {
+               const gchar *res = NULL;
 
-               if (redirect_uri && !*redirect_uri) {
-                       g_free (redirect_uri);
-                       redirect_uri = NULL;
-               }
+               camel_ews_settings_lock (ews_settings);
+
+               if (camel_ews_settings_get_override_oauth2 (ews_settings)) {
+                       const gchar *redirect_uri;
+
+                       redirect_uri = camel_ews_settings_get_oauth2_redirect_uri (ews_settings);
 
-               if (redirect_uri)
-                       return eos_office365_cache_string (oauth2_office365, redirect_uri);
+                       if (e_util_strcmp0 (redirect_uri, NULL) != 0)
+                               res = eos_office365_cache_string (oauth2_office365, redirect_uri);
 
-               if (e_util_strcmp0 (camel_ews_settings_get_oauth2_endpoint_host (ews_settings), NULL) != 0) {
-                       return eos_office365_cache_string (oauth2_office365,
-                               g_strdup_printf ("https://%s/common/oauth2/nativeclient";,
-                                       eos_office365_get_endpoint_host (oauth2_office365, ews_settings)));
+                       if (!res) {
+                               const gchar *endpoint_host = NULL;
+
+                               eos_office365_get_endpoint_host_and_tenant_locked (ews_settings, 
&endpoint_host, NULL);
+
+                               if (endpoint_host && g_strcmp0 (endpoint_host, OFFICE365_ENDPOINT_HOST) != 0) 
{
+                                       res = eos_office365_cache_string_take (oauth2_office365,
+                                               g_strdup_printf ("https://%s/common/oauth2/nativeclient";, 
endpoint_host));
+                               }
+                       }
                }
+
+               camel_ews_settings_unlock (ews_settings);
+
+               if (res)
+                       return res;
        }
 
-       res = OFFICE365_REDIRECT_URI;
-       if (res && *res)
-               return res;
+       if (e_util_strcmp0 (OFFICE365_REDIRECT_URI, NULL) != 0)
+               return OFFICE365_REDIRECT_URI;
+
+       return "https://"; OFFICE365_ENDPOINT_HOST "/common/oauth2/nativeclient";
+}
 
-       return eos_office365_cache_string (oauth2_office365,
-               g_strdup_printf ("https://%s/common/oauth2/nativeclient";,
-                       eos_office365_get_endpoint_host (oauth2_office365, ews_settings)));
+static gboolean
+eos_office365_matches_fallback_resource_uri (const gchar *uri,
+                                            guint len)
+{
+       return uri && strlen (OFFICE365_FALLBACK_RESOURCE_URI) == len &&
+               strncmp (uri, OFFICE365_FALLBACK_RESOURCE_URI, len) == 0;
 }
 
 static const gchar *
@@ -260,39 +310,47 @@ eos_office365_get_resource_uri (EOAuth2Service *service,
        CamelEwsSettings *ews_settings;
 
        ews_settings = eos_office365_get_camel_settings (source);
-       if (ews_settings && camel_ews_settings_get_override_oauth2 (ews_settings)) {
-               gchar *resource_uri;
 
-               resource_uri = camel_ews_settings_dup_oauth2_resource_uri (ews_settings);
+       if (ews_settings) {
+               const gchar *res = NULL;
+
+               camel_ews_settings_lock (ews_settings);
+
+               if (camel_ews_settings_get_override_oauth2 (ews_settings)) {
+                       const gchar *resource_uri;
+
+                       resource_uri = camel_ews_settings_get_oauth2_resource_uri (ews_settings);
+
+                       if (e_util_strcmp0 (resource_uri, NULL) != 0)
+                               res = eos_office365_cache_string (oauth2_office365, resource_uri);
 
-               if (resource_uri && !*resource_uri) {
-                       g_free (resource_uri);
-                       resource_uri = NULL;
                }
 
-               if (resource_uri)
-                       return eos_office365_cache_string (oauth2_office365, resource_uri);
-       }
+               if (!res) {
+                       const gchar *host_url;
 
-       if (ews_settings) {
-               gchar *host_url;
+                       host_url = camel_ews_settings_get_hosturl (ews_settings);
 
-               host_url = camel_ews_settings_dup_hosturl (ews_settings);
+                       if (host_url && *host_url) {
+                               const gchar *ptr;
 
-               if (host_url && *host_url) {
-                       gchar *ptr;
+                               ptr = strstr (host_url, "://");
+                               ptr = ptr ? strchr (ptr + 3, '/') : NULL;
 
-                       ptr = strstr (host_url, "://");
-                       ptr = ptr ? strchr (ptr + 3, '/') : NULL;
+                               if (ptr && !eos_office365_matches_fallback_resource_uri (host_url, ptr - 
host_url)) {
+                                       gchar *resource_uri;
 
-                       if (ptr) {
-                               *ptr = '\0';
+                                       resource_uri = g_strndup (host_url, ptr - host_url);
 
-                               return eos_office365_cache_string (oauth2_office365, host_url);
+                                       res = eos_office365_cache_string_take (oauth2_office365, 
resource_uri);
+                               }
                        }
                }
 
-               g_free (host_url);
+               camel_ews_settings_unlock (ews_settings);
+
+               if (res)
+                       return res;
        }
 
        return OFFICE365_FALLBACK_RESOURCE_URI;
diff --git a/src/EWS/evolution/e-mail-config-ews-backend.c b/src/EWS/evolution/e-mail-config-ews-backend.c
index 328845ac..821a9764 100644
--- a/src/EWS/evolution/e-mail-config-ews-backend.c
+++ b/src/EWS/evolution/e-mail-config-ews-backend.c
@@ -144,6 +144,7 @@ mail_config_ews_backend_insert_widgets (EMailConfigServiceBackend *backend,
        ESource *source;
        ESourceExtension *extension;
        CamelSettings *settings;
+       CamelEwsSettings *ews_settings;
        GtkLabel *label;
        GtkWidget *widget;
        GtkWidget *container;
@@ -240,15 +241,25 @@ mail_config_ews_backend_insert_widgets (EMailConfigServiceBackend *backend,
        gtk_grid_attach (GTK_GRID (container), widget, 1, 3, 1, 1);
        gtk_widget_show (widget);
 
-       if (camel_ews_settings_get_use_impersonation (CAMEL_EWS_SETTINGS (settings))) {
-               const gchar *impersonate_user = camel_ews_settings_get_impersonate_user (CAMEL_EWS_SETTINGS 
(settings));
+       ews_settings = CAMEL_EWS_SETTINGS (settings);
+
+       camel_ews_settings_lock (ews_settings);
+
+       if (camel_ews_settings_get_use_impersonation (ews_settings)) {
+               const gchar *impersonate_user = camel_ews_settings_get_impersonate_user (ews_settings);
 
                if (impersonate_user && !*impersonate_user) {
-                       camel_ews_settings_set_impersonate_user (CAMEL_EWS_SETTINGS (settings), NULL);
-                       camel_ews_settings_set_use_impersonation (CAMEL_EWS_SETTINGS (settings), FALSE);
+                       camel_ews_settings_unlock (ews_settings);
+
+                       camel_ews_settings_set_impersonate_user (ews_settings, NULL);
+                       camel_ews_settings_set_use_impersonation (ews_settings, FALSE);
+
+                       camel_ews_settings_lock (ews_settings);
                }
        }
 
+       camel_ews_settings_unlock (ews_settings);
+
        e_binding_bind_property (
                settings, "use-impersonation",
                widget, "active",
@@ -487,10 +498,14 @@ mail_config_ews_backend_insert_widgets (EMailConfigServiceBackend *backend,
 
        gtk_widget_show_all (GTK_WIDGET (priv->oauth2_settings_grid));
 
+       camel_ews_settings_lock (ews_settings);
+
        gtk_expander_set_expanded (GTK_EXPANDER (expander),
-               e_util_strcmp0 (camel_ews_settings_get_oauth2_endpoint_host (CAMEL_EWS_SETTINGS (settings)), 
NULL) != 0 ||
-               e_util_strcmp0 (camel_ews_settings_get_oauth2_redirect_uri (CAMEL_EWS_SETTINGS (settings)), 
NULL) != 0 ||
-               e_util_strcmp0 (camel_ews_settings_get_oauth2_resource_uri (CAMEL_EWS_SETTINGS (settings)), 
NULL) != 0);
+               e_util_strcmp0 (camel_ews_settings_get_oauth2_endpoint_host (ews_settings), NULL) != 0 ||
+               e_util_strcmp0 (camel_ews_settings_get_oauth2_redirect_uri (ews_settings), NULL) != 0 ||
+               e_util_strcmp0 (camel_ews_settings_get_oauth2_resource_uri (ews_settings), NULL) != 0);
+
+       camel_ews_settings_unlock (ews_settings);
 
        e_binding_bind_property (
                expander, "expanded",
@@ -700,6 +715,9 @@ mail_config_ews_backend_check_complete (EMailConfigServiceBackend *backend)
        settings = e_mail_config_service_backend_get_settings (backend);
 
        ews_settings = CAMEL_EWS_SETTINGS (settings);
+
+       camel_ews_settings_lock (ews_settings);
+
        hosturl = camel_ews_settings_get_hosturl (ews_settings);
        oaburl = camel_ews_settings_get_oaburl (ews_settings);
 
@@ -753,12 +771,14 @@ mail_config_ews_backend_check_complete (EMailConfigServiceBackend *backend)
                        client_id = OFFICE365_CLIENT_ID;
                }
 
-               correct = client_id && *client_id;
+               correct = e_util_strcmp0 (client_id, NULL) != 0;
                complete = complete && correct;
 
                e_util_set_entry_issue_hint (priv->oauth2_client_id_entry, correct ? NULL : _("Application ID 
cannot be empty"));
        }
 
+       camel_ews_settings_unlock (ews_settings);
+
        return complete;
 }
 
diff --git a/src/Microsoft365/common/camel-m365-settings.c b/src/Microsoft365/common/camel-m365-settings.c
index 9e5d45cd..039b1fab 100644
--- a/src/Microsoft365/common/camel-m365-settings.c
+++ b/src/Microsoft365/common/camel-m365-settings.c
@@ -571,6 +571,22 @@ camel_m365_settings_get_from_backend (struct _EBackend *backend,
        return CAMEL_M365_SETTINGS (settings);
 }
 
+void
+camel_m365_settings_lock (CamelM365Settings *settings)
+{
+       g_return_if_fail (CAMEL_IS_M365_SETTINGS (settings));
+
+       g_mutex_lock (&settings->priv->property_lock);
+}
+
+void
+camel_m365_settings_unlock (CamelM365Settings *settings)
+{
+       g_return_if_fail (CAMEL_IS_M365_SETTINGS (settings));
+
+       g_mutex_unlock (&settings->priv->property_lock);
+}
+
 gboolean
 camel_m365_settings_get_use_impersonation (CamelM365Settings *settings)
 {
@@ -609,12 +625,12 @@ camel_m365_settings_dup_impersonate_user (CamelM365Settings *settings)
 
        g_return_val_if_fail (CAMEL_IS_M365_SETTINGS (settings), NULL);
 
-       g_mutex_lock (&settings->priv->property_lock);
+       camel_m365_settings_lock (settings);
 
        protected = camel_m365_settings_get_impersonate_user (settings);
        duplicate = g_strdup (protected);
 
-       g_mutex_unlock (&settings->priv->property_lock);
+       camel_m365_settings_unlock (settings);
 
        return duplicate;
 }
@@ -625,17 +641,17 @@ camel_m365_settings_set_impersonate_user (CamelM365Settings *settings,
 {
        g_return_if_fail (CAMEL_IS_M365_SETTINGS (settings));
 
-       g_mutex_lock (&settings->priv->property_lock);
+       camel_m365_settings_lock (settings);
 
        if (g_strcmp0 (settings->priv->impersonate_user, impersonate_user) == 0) {
-               g_mutex_unlock (&settings->priv->property_lock);
+               camel_m365_settings_unlock (settings);
                return;
        }
 
        g_free (settings->priv->impersonate_user);
        settings->priv->impersonate_user = e_util_strdup_strip (impersonate_user);
 
-       g_mutex_unlock (&settings->priv->property_lock);
+       camel_m365_settings_unlock (settings);
 
        g_object_notify (G_OBJECT (settings), "impersonate-user");
 }
@@ -678,12 +694,12 @@ camel_m365_settings_dup_email (CamelM365Settings *settings)
 
        g_return_val_if_fail (CAMEL_IS_M365_SETTINGS (settings), NULL);
 
-       g_mutex_lock (&settings->priv->property_lock);
+       camel_m365_settings_lock (settings);
 
        protected = camel_m365_settings_get_email (settings);
        duplicate = g_strdup (protected);
 
-       g_mutex_unlock (&settings->priv->property_lock);
+       camel_m365_settings_unlock (settings);
 
        return duplicate;
 }
@@ -694,17 +710,17 @@ camel_m365_settings_set_email (CamelM365Settings *settings,
 {
        g_return_if_fail (CAMEL_IS_M365_SETTINGS (settings));
 
-       g_mutex_lock (&settings->priv->property_lock);
+       camel_m365_settings_lock (settings);
 
        if (g_strcmp0 (settings->priv->email, email) == 0) {
-               g_mutex_unlock (&settings->priv->property_lock);
+               camel_m365_settings_unlock (settings);
                return;
        }
 
        g_free (settings->priv->email);
        settings->priv->email = e_util_strdup_strip (email);
 
-       g_mutex_unlock (&settings->priv->property_lock);
+       camel_m365_settings_unlock (settings);
 
        g_object_notify (G_OBJECT (settings), "email");
 }
@@ -813,12 +829,12 @@ camel_m365_settings_dup_oauth2_tenant (CamelM365Settings *settings)
 
        g_return_val_if_fail (CAMEL_IS_M365_SETTINGS (settings), NULL);
 
-       g_mutex_lock (&settings->priv->property_lock);
+       camel_m365_settings_lock (settings);
 
        protected = camel_m365_settings_get_oauth2_tenant (settings);
        duplicate = g_strdup (protected);
 
-       g_mutex_unlock (&settings->priv->property_lock);
+       camel_m365_settings_unlock (settings);
 
        return duplicate;
 }
@@ -829,17 +845,17 @@ camel_m365_settings_set_oauth2_tenant (CamelM365Settings *settings,
 {
        g_return_if_fail (CAMEL_IS_M365_SETTINGS (settings));
 
-       g_mutex_lock (&settings->priv->property_lock);
+       camel_m365_settings_lock (settings);
 
        if (g_strcmp0 (settings->priv->oauth2_tenant, tenant) == 0) {
-               g_mutex_unlock (&settings->priv->property_lock);
+               camel_m365_settings_unlock (settings);
                return;
        }
 
        g_free (settings->priv->oauth2_tenant);
        settings->priv->oauth2_tenant = e_util_strdup_strip (tenant);
 
-       g_mutex_unlock (&settings->priv->property_lock);
+       camel_m365_settings_unlock (settings);
 
        g_object_notify (G_OBJECT (settings), "oauth2-tenant");
 }
@@ -860,12 +876,12 @@ camel_m365_settings_dup_oauth2_client_id (CamelM365Settings *settings)
 
        g_return_val_if_fail (CAMEL_IS_M365_SETTINGS (settings), NULL);
 
-       g_mutex_lock (&settings->priv->property_lock);
+       camel_m365_settings_lock (settings);
 
        protected = camel_m365_settings_get_oauth2_client_id (settings);
        duplicate = g_strdup (protected);
 
-       g_mutex_unlock (&settings->priv->property_lock);
+       camel_m365_settings_unlock (settings);
 
        return duplicate;
 }
@@ -876,17 +892,17 @@ camel_m365_settings_set_oauth2_client_id (CamelM365Settings *settings,
 {
        g_return_if_fail (CAMEL_IS_M365_SETTINGS (settings));
 
-       g_mutex_lock (&settings->priv->property_lock);
+       camel_m365_settings_lock (settings);
 
        if (g_strcmp0 (settings->priv->oauth2_client_id, client_id) == 0) {
-               g_mutex_unlock (&settings->priv->property_lock);
+               camel_m365_settings_unlock (settings);
                return;
        }
 
        g_free (settings->priv->oauth2_client_id);
        settings->priv->oauth2_client_id = e_util_strdup_strip (client_id);
 
-       g_mutex_unlock (&settings->priv->property_lock);
+       camel_m365_settings_unlock (settings);
 
        g_object_notify (G_OBJECT (settings), "oauth2-client-id");
 }
@@ -907,12 +923,12 @@ camel_m365_settings_dup_oauth2_redirect_uri (CamelM365Settings *settings)
 
        g_return_val_if_fail (CAMEL_IS_M365_SETTINGS (settings), NULL);
 
-       g_mutex_lock (&settings->priv->property_lock);
+       camel_m365_settings_lock (settings);
 
        protected = camel_m365_settings_get_oauth2_redirect_uri (settings);
        duplicate = g_strdup (protected);
 
-       g_mutex_unlock (&settings->priv->property_lock);
+       camel_m365_settings_unlock (settings);
 
        return duplicate;
 }
@@ -923,17 +939,17 @@ camel_m365_settings_set_oauth2_redirect_uri (CamelM365Settings *settings,
 {
        g_return_if_fail (CAMEL_IS_M365_SETTINGS (settings));
 
-       g_mutex_lock (&settings->priv->property_lock);
+       camel_m365_settings_lock (settings);
 
        if (g_strcmp0 (settings->priv->oauth2_redirect_uri, redirect_uri) == 0) {
-               g_mutex_unlock (&settings->priv->property_lock);
+               camel_m365_settings_unlock (settings);
                return;
        }
 
        g_free (settings->priv->oauth2_redirect_uri);
        settings->priv->oauth2_redirect_uri = e_util_strdup_strip (redirect_uri);
 
-       g_mutex_unlock (&settings->priv->property_lock);
+       camel_m365_settings_unlock (settings);
 
        g_object_notify (G_OBJECT (settings), "oauth2-redirect-uri");
 }
@@ -954,12 +970,12 @@ camel_m365_settings_dup_oauth2_endpoint_host (CamelM365Settings *settings)
 
        g_return_val_if_fail (CAMEL_IS_M365_SETTINGS (settings), NULL);
 
-       g_mutex_lock (&settings->priv->property_lock);
+       camel_m365_settings_lock (settings);
 
        protected = camel_m365_settings_get_oauth2_endpoint_host (settings);
        duplicate = g_strdup (protected);
 
-       g_mutex_unlock (&settings->priv->property_lock);
+       camel_m365_settings_unlock (settings);
 
        return duplicate;
 }
@@ -970,17 +986,17 @@ camel_m365_settings_set_oauth2_endpoint_host (CamelM365Settings *settings,
 {
        g_return_if_fail (CAMEL_IS_M365_SETTINGS (settings));
 
-       g_mutex_lock (&settings->priv->property_lock);
+       camel_m365_settings_lock (settings);
 
        if (g_strcmp0 (settings->priv->oauth2_endpoint_host, endpoint_host) == 0) {
-               g_mutex_unlock (&settings->priv->property_lock);
+               camel_m365_settings_unlock (settings);
                return;
        }
 
        g_free (settings->priv->oauth2_endpoint_host);
        settings->priv->oauth2_endpoint_host = e_util_strdup_strip (endpoint_host);
 
-       g_mutex_unlock (&settings->priv->property_lock);
+       camel_m365_settings_unlock (settings);
 
        g_object_notify (G_OBJECT (settings), "oauth2-endpoint-host");
 }
diff --git a/src/Microsoft365/common/camel-m365-settings.h b/src/Microsoft365/common/camel-m365-settings.h
index 7e88963e..f99f5b5c 100644
--- a/src/Microsoft365/common/camel-m365-settings.h
+++ b/src/Microsoft365/common/camel-m365-settings.h
@@ -55,6 +55,8 @@ CamelM365Settings *
                camel_m365_settings_get_from_backend
                                                (struct _EBackend *backend,
                                                 struct _ESourceRegistry *registry);
+void           camel_m365_settings_lock        (CamelM365Settings *settings);
+void           camel_m365_settings_unlock      (CamelM365Settings *settings);
 gboolean       camel_m365_settings_get_use_impersonation
                                                (CamelM365Settings *settings);
 void           camel_m365_settings_set_use_impersonation
diff --git a/src/Microsoft365/common/e-oauth2-service-microsoft365.c 
b/src/Microsoft365/common/e-oauth2-service-microsoft365.c
index d456f79a..5440dd24 100644
--- a/src/Microsoft365/common/e-oauth2-service-microsoft365.c
+++ b/src/Microsoft365/common/e-oauth2-service-microsoft365.c
@@ -49,8 +49,8 @@ G_DEFINE_DYNAMIC_TYPE_EXTENDED (EOAuth2ServiceMicrosoft365, e_oauth2_service_mic
        G_ADD_PRIVATE_DYNAMIC (EOAuth2ServiceMicrosoft365))
 
 static const gchar *
-eos_microsoft365_cache_string (EOAuth2ServiceMicrosoft365 *oauth2_microsoft365,
-                              gchar *str) /* takes ownership of the 'str' */
+eos_microsoft365_cache_string_take (EOAuth2ServiceMicrosoft365 *oauth2_microsoft365,
+                                   gchar *str) /* takes ownership of the 'str' */
 {
        const gchar *cached_str;
 
@@ -79,22 +79,50 @@ eos_microsoft365_cache_string (EOAuth2ServiceMicrosoft365 *oauth2_microsoft365,
        return cached_str;
 }
 
+
 static const gchar *
-eos_microsoft365_get_endpoint_host (EOAuth2ServiceMicrosoft365 *oauth2_microsoft365,
-                                   CamelM365Settings *m365_settings)
+eos_microsoft365_cache_string (EOAuth2ServiceMicrosoft365 *oauth2_microsoft365,
+                              const gchar *str)
 {
-       if (m365_settings && camel_m365_settings_get_override_oauth2 (m365_settings)) {
-               gchar *endpoint_host;
+       gchar *cached_str;
+
+       g_return_val_if_fail (E_IS_OAUTH2_SERVICE_MICROSOFT365 (oauth2_microsoft365), NULL);
+
+       if (!str || !*str)
+               return str;
 
-               endpoint_host = camel_m365_settings_dup_oauth2_endpoint_host (m365_settings);
+       g_mutex_lock (&oauth2_microsoft365->priv->string_cache_lock);
 
-               if (endpoint_host && *endpoint_host)
-                       return eos_microsoft365_cache_string (oauth2_microsoft365, endpoint_host);
+       cached_str = g_hash_table_lookup (oauth2_microsoft365->priv->string_cache, str);
 
-               g_free (endpoint_host);
+       if (!cached_str) {
+               cached_str = g_strdup (str);
+               g_hash_table_insert (oauth2_microsoft365->priv->string_cache, cached_str, cached_str);
        }
 
-       return MICROSOFT365_ENDPOINT_HOST;
+       g_mutex_unlock (&oauth2_microsoft365->priv->string_cache_lock);
+
+       return cached_str;
+}
+
+static void
+eos_microsoft365_get_endpoint_host_and_tenant_locked (CamelM365Settings *m365_settings,
+                                                     const gchar **out_endpoint_host,
+                                                     const gchar **out_tenant)
+{
+       if (out_endpoint_host) {
+               *out_endpoint_host = camel_m365_settings_get_oauth2_endpoint_host (m365_settings);
+
+               if (e_util_strcmp0 (*out_endpoint_host, NULL) == 0)
+                       *out_endpoint_host = MICROSOFT365_ENDPOINT_HOST;
+       }
+
+       if (out_tenant) {
+               *out_tenant = camel_m365_settings_get_oauth2_tenant (m365_settings);
+
+               if (e_util_strcmp0 (*out_tenant, NULL) == 0)
+                       *out_tenant = MICROSOFT365_TENANT;
+       }
 }
 
 static CamelM365Settings *
@@ -142,16 +170,25 @@ eos_microsoft365_get_client_id (EOAuth2Service *service,
        CamelM365Settings *m365_settings;
 
        m365_settings = eos_microsoft365_get_camel_settings (source);
-       if (m365_settings && camel_m365_settings_get_override_oauth2 (m365_settings)) {
-               gchar *client_id = camel_m365_settings_dup_oauth2_client_id (m365_settings);
 
-               if (client_id && !*client_id) {
-                       g_free (client_id);
-                       client_id = NULL;
+       if (m365_settings) {
+               const gchar *res = NULL;
+
+               camel_m365_settings_lock (m365_settings);
+
+               if (camel_m365_settings_get_override_oauth2 (m365_settings)) {
+                       const gchar *client_id;
+
+                       client_id = camel_m365_settings_get_oauth2_client_id (m365_settings);
+
+                       if (e_util_strcmp0 (client_id, NULL) != 0)
+                               res = eos_microsoft365_cache_string (oauth2_microsoft365, client_id);
                }
 
-               if (client_id)
-                       return eos_microsoft365_cache_string (oauth2_microsoft365, client_id);
+               camel_m365_settings_unlock (m365_settings);
+
+               if (res)
+                       return res;
        }
 
        return MICROSOFT365_CLIENT_ID;
@@ -172,30 +209,29 @@ eos_microsoft365_get_authentication_uri (EOAuth2Service *service,
        CamelM365Settings *m365_settings;
 
        m365_settings = eos_microsoft365_get_camel_settings (source);
-       if (m365_settings && camel_m365_settings_get_override_oauth2 (m365_settings)) {
-               gchar *tenant;
-               const gchar *res;
-
-               tenant = camel_m365_settings_dup_oauth2_tenant (m365_settings);
-               if (tenant && !*tenant) {
-                       g_free (tenant);
-                       tenant = NULL;
-               }
 
-               res = eos_microsoft365_cache_string (oauth2_microsoft365,
-                       g_strdup_printf ("https://%s/%s/oauth2/v2.0/authorize";,
-                               eos_microsoft365_get_endpoint_host (oauth2_microsoft365, m365_settings),
-                               tenant ? tenant : MICROSOFT365_TENANT));
+       if (m365_settings) {
+               const gchar *res = NULL;
+
+               camel_m365_settings_lock (m365_settings);
+
+               if (camel_m365_settings_get_override_oauth2 (m365_settings)) {
+                       const gchar *endpoint_host = NULL;
+                       const gchar *tenant = NULL;
+
+                       eos_microsoft365_get_endpoint_host_and_tenant_locked (m365_settings, &endpoint_host, 
&tenant);
 
-               g_free (tenant);
+                       res = eos_microsoft365_cache_string_take (oauth2_microsoft365,
+                               g_strdup_printf ("https://%s/%s/oauth2/v2.0/authorize";, endpoint_host, 
tenant));
+               }
+
+               camel_m365_settings_unlock (m365_settings);
 
-               return res;
+               if (res)
+                       return res;
        }
 
-       return eos_microsoft365_cache_string (oauth2_microsoft365,
-               g_strdup_printf ("https://%s/%s/oauth2/v2.0/authorize";,
-                       eos_microsoft365_get_endpoint_host (oauth2_microsoft365, m365_settings),
-                       MICROSOFT365_TENANT));
+       return "https://"; MICROSOFT365_ENDPOINT_HOST "/" MICROSOFT365_TENANT "/oauth2/v2.0/authorize";
 }
 
 static const gchar *
@@ -206,30 +242,29 @@ eos_microsoft365_get_refresh_uri (EOAuth2Service *service,
        CamelM365Settings *m365_settings;
 
        m365_settings = eos_microsoft365_get_camel_settings (source);
-       if (m365_settings && camel_m365_settings_get_override_oauth2 (m365_settings)) {
-               gchar *tenant;
-               const gchar *res;
-
-               tenant = camel_m365_settings_dup_oauth2_tenant (m365_settings);
-               if (tenant && !*tenant) {
-                       g_free (tenant);
-                       tenant = NULL;
-               }
 
-               res = eos_microsoft365_cache_string (oauth2_microsoft365,
-                       g_strdup_printf ("https://%s/%s/oauth2/v2.0/token";,
-                               eos_microsoft365_get_endpoint_host (oauth2_microsoft365, m365_settings),
-                               tenant ? tenant : MICROSOFT365_TENANT));
+       if (m365_settings) {
+               const gchar *res = NULL;
 
-               g_free (tenant);
+               camel_m365_settings_lock (m365_settings);
 
-               return res;
+               if (camel_m365_settings_get_override_oauth2 (m365_settings)) {
+                       const gchar *endpoint_host = NULL;
+                       const gchar *tenant = NULL;
+
+                       eos_microsoft365_get_endpoint_host_and_tenant_locked (m365_settings, &endpoint_host, 
&tenant);
+
+                       res = eos_microsoft365_cache_string_take (oauth2_microsoft365,
+                               g_strdup_printf ("https://%s/%s/oauth2/v2.0/token";, endpoint_host, tenant));
+               }
+
+               camel_m365_settings_unlock (m365_settings);
+
+               if (res)
+                       return res;
        }
 
-       return eos_microsoft365_cache_string (oauth2_microsoft365,
-               g_strdup_printf ("https://%s/%s/oauth2/v2.0/token";,
-                       eos_microsoft365_get_endpoint_host (oauth2_microsoft365, m365_settings),
-                       MICROSOFT365_TENANT));
+       return "https://"; MICROSOFT365_ENDPOINT_HOST "/" MICROSOFT365_TENANT "/oauth2/v2.0/token";
 }
 
 static const gchar *
@@ -238,36 +273,44 @@ eos_microsoft365_get_redirect_uri (EOAuth2Service *service,
 {
        EOAuth2ServiceMicrosoft365 *oauth2_microsoft365 = E_OAUTH2_SERVICE_MICROSOFT365 (service);
        CamelM365Settings *m365_settings;
-       const gchar *res;
 
        m365_settings = eos_microsoft365_get_camel_settings (source);
-       if (m365_settings && camel_m365_settings_get_override_oauth2 (m365_settings)) {
-               gchar *redirect_uri;
 
-               redirect_uri = camel_m365_settings_dup_oauth2_redirect_uri (m365_settings);
+       if (m365_settings) {
+               const gchar *res = NULL;
 
-               if (redirect_uri && !*redirect_uri) {
-                       g_free (redirect_uri);
-                       redirect_uri = NULL;
-               }
+               camel_m365_settings_lock (m365_settings);
+
+               if (camel_m365_settings_get_override_oauth2 (m365_settings)) {
+                       const gchar *redirect_uri;
+
+                       redirect_uri = camel_m365_settings_get_oauth2_redirect_uri (m365_settings);
+
+                       if (e_util_strcmp0 (redirect_uri, NULL) != 0)
+                               res = eos_microsoft365_cache_string (oauth2_microsoft365, redirect_uri);
 
-               if (redirect_uri)
-                       return eos_microsoft365_cache_string (oauth2_microsoft365, redirect_uri);
+                       if (!res) {
+                               const gchar *endpoint_host = NULL;
 
-               if (e_util_strcmp0 (camel_m365_settings_get_oauth2_endpoint_host (m365_settings), NULL) != 0) 
{
-                       return eos_microsoft365_cache_string (oauth2_microsoft365,
-                               g_strdup_printf ("https://%s/common/oauth2/nativeclient";,
-                                       eos_microsoft365_get_endpoint_host (oauth2_microsoft365, 
m365_settings)));
+                               eos_microsoft365_get_endpoint_host_and_tenant_locked (m365_settings, 
&endpoint_host, NULL);
+
+                               if (endpoint_host && g_strcmp0 (endpoint_host, MICROSOFT365_ENDPOINT_HOST) != 
0) {
+                                       res = eos_microsoft365_cache_string_take (oauth2_microsoft365,
+                                               g_strdup_printf ("https://%s/common/oauth2/nativeclient";, 
endpoint_host));
+                               }
+                       }
                }
+
+               camel_m365_settings_unlock (m365_settings);
+
+               if (res)
+                       return res;
        }
 
-       res = MICROSOFT365_REDIRECT_URI;
-       if (res && *res)
-               return res;
+       if (e_util_strcmp0 (MICROSOFT365_REDIRECT_URI, NULL) != 0)
+               return MICROSOFT365_REDIRECT_URI;
 
-       return eos_microsoft365_cache_string (oauth2_microsoft365,
-               g_strdup_printf ("https://%s/common/oauth2/nativeclient";,
-                       eos_microsoft365_get_endpoint_host (oauth2_microsoft365, m365_settings)));
+       return "https://"; MICROSOFT365_ENDPOINT_HOST "/common/oauth2/nativeclient";
 }
 
 static void
diff --git a/src/Microsoft365/evolution/e-mail-config-m365-backend.c 
b/src/Microsoft365/evolution/e-mail-config-m365-backend.c
index 72bee447..36b22f36 100644
--- a/src/Microsoft365/evolution/e-mail-config-m365-backend.c
+++ b/src/Microsoft365/evolution/e-mail-config-m365-backend.c
@@ -83,6 +83,7 @@ mail_config_m365_backend_insert_widgets (EMailConfigServiceBackend *backend,
        ESourceExtension *extension;
        ESourceAuthentication *auth_extension;
        CamelSettings *settings;
+       CamelM365Settings *m365_settings;
        GtkLabel *label;
        GtkWidget *widget;
        GtkWidget *container;
@@ -145,15 +146,25 @@ mail_config_m365_backend_insert_widgets (EMailConfigServiceBackend *backend,
        gtk_grid_attach (GTK_GRID (container), widget, 1, 1, 1, 1);
        gtk_widget_show (widget);
 
-       if (camel_m365_settings_get_use_impersonation (CAMEL_M365_SETTINGS (settings))) {
-               const gchar *impersonate_user = camel_m365_settings_get_impersonate_user (CAMEL_M365_SETTINGS 
(settings));
+       m365_settings = CAMEL_M365_SETTINGS (settings);
+
+       camel_m365_settings_lock (m365_settings);
+
+       if (camel_m365_settings_get_use_impersonation (m365_settings)) {
+               const gchar *impersonate_user = camel_m365_settings_get_impersonate_user (m365_settings);
 
                if (impersonate_user && !*impersonate_user) {
-                       camel_m365_settings_set_impersonate_user (CAMEL_M365_SETTINGS (settings), NULL);
-                       camel_m365_settings_set_use_impersonation (CAMEL_M365_SETTINGS (settings), FALSE);
+                       camel_m365_settings_unlock (m365_settings);
+
+                       camel_m365_settings_set_impersonate_user (m365_settings, NULL);
+                       camel_m365_settings_set_use_impersonation (m365_settings, FALSE);
+
+                       camel_m365_settings_lock (m365_settings);
                }
        }
 
+       camel_m365_settings_unlock (m365_settings);
+
        e_binding_bind_property (
                settings, "use-impersonation",
                widget, "active",
@@ -346,9 +357,13 @@ mail_config_m365_backend_insert_widgets (EMailConfigServiceBackend *backend,
 
        gtk_widget_show_all (GTK_WIDGET (m365_backend->priv->oauth2_settings_grid));
 
+       camel_m365_settings_lock (m365_settings);
+
        gtk_expander_set_expanded (GTK_EXPANDER (expander),
-               e_util_strcmp0 (camel_m365_settings_get_oauth2_endpoint_host (CAMEL_M365_SETTINGS 
(settings)), NULL) != 0 ||
-               e_util_strcmp0 (camel_m365_settings_get_oauth2_redirect_uri (CAMEL_M365_SETTINGS (settings)), 
NULL) != 0);
+               e_util_strcmp0 (camel_m365_settings_get_oauth2_endpoint_host (m365_settings), NULL) != 0 ||
+               e_util_strcmp0 (camel_m365_settings_get_oauth2_redirect_uri (m365_settings), NULL) != 0);
+
+       camel_m365_settings_unlock (m365_settings);
 
        e_binding_bind_property (
                expander, "expanded",
@@ -503,15 +518,19 @@ mail_config_m365_backend_check_complete (EMailConfigServiceBackend *backend)
                CamelM365Settings *m365_settings = CAMEL_M365_SETTINGS (settings);
                const gchar *client_id;
 
+               camel_m365_settings_lock (m365_settings);
+
                if (camel_m365_settings_get_override_oauth2 (m365_settings)) {
                        client_id = camel_m365_settings_get_oauth2_client_id (m365_settings);
                } else {
                        client_id = MICROSOFT365_CLIENT_ID;
                }
 
-               correct = client_id && *client_id;
+               correct = e_util_strcmp0 (client_id, NULL) != 0;
                complete = complete && correct;
 
+               camel_m365_settings_unlock (m365_settings);
+
                e_util_set_entry_issue_hint (m365_backend->priv->oauth2_client_id_entry, correct ? NULL : 
_("Application ID cannot be empty"));
        }
 


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]