[evolution-ews/wip/mcrha/soup3] Ensure correct authentication method is used on the ESource-s



commit 49bcdc372d2b4c0af9400bb0ba4cddc5d66bdf15
Author: Milan Crha <mcrha redhat com>
Date:   Tue May 24 12:51:54 2022 +0200

    Ensure correct authentication method is used on the ESource-s

 src/EWS/common/e-ews-connection-utils.c    | 19 ---------
 src/EWS/common/e-ews-connection-utils.h    |  3 --
 src/EWS/common/e-ews-connection.c          |  3 --
 src/EWS/common/e-ews-notification.c        | 62 ------------------------------
 src/EWS/registry/e-ews-backend.c           | 46 ++++++++++++----------
 src/Microsoft365/registry/e-m365-backend.c | 21 ++++++----
 6 files changed, 40 insertions(+), 114 deletions(-)
---
diff --git a/src/EWS/common/e-ews-connection-utils.c b/src/EWS/common/e-ews-connection-utils.c
index e8fad0da..e3fc8d4d 100644
--- a/src/EWS/common/e-ews-connection-utils.c
+++ b/src/EWS/common/e-ews-connection-utils.c
@@ -257,22 +257,3 @@ e_ews_connection_utils_check_x_ms_credential_headers (SoupMessage *message,
 
        return any_found;
 }
-
-void
-e_ews_connection_utils_prepare_auth_method (SoupSession *soup_session,
-                                           EwsAuthType auth_method)
-{
-       /* We used to disable Basic auth to avoid it getting in the way of
-        * our GSSAPI hacks. But leave it enabled in the case where NTLM is
-        * enabled, which is the default configuration. It's a useful fallback
-        * which people may be relying on. */
-       if (auth_method == EWS_AUTH_TYPE_GSSAPI) {
-               soup_session_add_feature_by_type (soup_session, SOUP_TYPE_AUTH_NEGOTIATE);
-               soup_session_remove_feature_by_type (soup_session, SOUP_TYPE_AUTH_BASIC);
-       } else if (auth_method == EWS_AUTH_TYPE_OAUTH2) {
-               soup_session_add_feature_by_type (soup_session, E_TYPE_SOUP_AUTH_BEARER);
-               soup_session_remove_feature_by_type (soup_session, SOUP_TYPE_AUTH_BASIC);
-       } else if (auth_method == EWS_AUTH_TYPE_NTLM) {
-               soup_session_add_feature_by_type (soup_session, SOUP_TYPE_AUTH_NTLM);
-       }
-}
diff --git a/src/EWS/common/e-ews-connection-utils.h b/src/EWS/common/e-ews-connection-utils.h
index d2365d14..211c0d8c 100644
--- a/src/EWS/common/e-ews-connection-utils.h
+++ b/src/EWS/common/e-ews-connection-utils.h
@@ -35,9 +35,6 @@ gboolean      e_ews_connection_utils_check_x_ms_credential_headers
                                                         gint *out_expire_in_days,
                                                         gboolean *out_expired,
                                                         gchar **out_service_url);
-void           e_ews_connection_utils_prepare_auth_method
-                                                       (SoupSession *soup_session,
-                                                        EwsAuthType auth_method);
 
 G_END_DECLS
 
diff --git a/src/EWS/common/e-ews-connection.c b/src/EWS/common/e-ews-connection.c
index 11675039..cb68d600 100644
--- a/src/EWS/common/e-ews-connection.c
+++ b/src/EWS/common/e-ews-connection.c
@@ -228,9 +228,6 @@ e_ews_soup_worker_thread (gpointer user_data)
        g_mutex_lock (&cnc->priv->soup.mutex);
        cnc->priv->soup.session = SOUP_SESSION (e_ews_connection_create_soup_session (cnc));
 
-       e_ews_connection_utils_prepare_auth_method (cnc->priv->soup.session,
-               camel_ews_settings_get_auth_mechanism (settings));
-
        g_cond_broadcast (&cnc->priv->soup.cond);
        g_mutex_unlock (&cnc->priv->soup.mutex);
 
diff --git a/src/EWS/common/e-ews-notification.c b/src/EWS/common/e-ews-notification.c
index 198e1eba..57b58bee 100644
--- a/src/EWS/common/e-ews-notification.c
+++ b/src/EWS/common/e-ews-notification.c
@@ -14,7 +14,6 @@
 
 struct _EEwsNotificationPrivate {
        GMutex thread_lock;
-       SoupSession *soup_session;
        GWeakRef connection_wk;
        GCancellable *cancellable;
        gchar *last_subscription_id; /* guarded by the caller, because it can be set only after construct */
@@ -129,30 +128,6 @@ ews_notification_get_property (GObject *object,
        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
 }
 
-static void
-ews_notification_constructed (GObject *object)
-{
-       EEwsNotification *notif;
-       EEwsConnection *cnc;
-       CamelEwsSettings *ews_settings;
-
-       /* Chain up to parent's method. */
-       G_OBJECT_CLASS (e_ews_notification_parent_class)->constructed (object);
-
-       notif = E_EWS_NOTIFICATION (object);
-       cnc = e_ews_notification_ref_connection (notif);
-
-       if (cnc) {
-               ews_settings = e_ews_connection_ref_settings (cnc);
-
-               e_ews_connection_utils_prepare_auth_method (notif->priv->soup_session,
-                       camel_ews_settings_get_auth_mechanism (ews_settings));
-
-               g_object_unref (ews_settings);
-               g_object_unref (cnc);
-       }
-}
-
 static void
 ews_notification_dispose (GObject *object)
 {
@@ -161,7 +136,6 @@ ews_notification_dispose (GObject *object)
        if (notif->priv->cancellable != NULL)
                g_cancellable_cancel (notif->priv->cancellable);
 
-       g_clear_object (&notif->priv->soup_session);
        g_clear_object (&notif->priv->cancellable);
        g_weak_ref_set (&notif->priv->connection_wk, NULL);
 
@@ -192,7 +166,6 @@ e_ews_notification_class_init (EEwsNotificationClass *class)
        object_class = G_OBJECT_CLASS (class);
        object_class->set_property = ews_notification_set_property;
        object_class->get_property = ews_notification_get_property;
-       object_class->constructed = ews_notification_constructed;
        object_class->dispose = ews_notification_dispose;
        object_class->finalize = ews_notification_finalize;
 
@@ -221,30 +194,10 @@ e_ews_notification_class_init (EEwsNotificationClass *class)
 static void
 e_ews_notification_init (EEwsNotification *notification)
 {
-       gint log_level;
-
        notification->priv = e_ews_notification_get_instance_private (notification);
 
        g_weak_ref_init (&notification->priv->connection_wk, NULL);
        g_mutex_init (&notification->priv->thread_lock);
-
-       notification->priv->soup_session = soup_session_new ();
-
-       soup_session_add_feature_by_type (notification->priv->soup_session,
-                                         SOUP_TYPE_COOKIE_JAR);
-
-       log_level = e_ews_debug_get_log_level ();
-       if (log_level > 0) {
-               SoupLogger *logger;
-               logger = soup_logger_new (SOUP_LOGGER_LOG_BODY);
-
-               if (log_level == 2)
-                       soup_logger_set_printer (logger, e_ews_debug_soup_log_printer_stdout, NULL, NULL);
-
-               soup_session_add_feature (notification->priv->soup_session, SOUP_SESSION_FEATURE (logger));
-
-               g_object_unref (logger);
-       }
 }
 
 static gboolean
@@ -621,7 +574,6 @@ static gpointer
 e_ews_notification_get_events_thread (gpointer user_data)
 {
        EEwsNotificationThreadData *td = user_data;
-       EEwsConnection *cnc;
        gchar *subscription_id = NULL;
        gboolean ret, fatal_error = FALSE;
 
@@ -631,20 +583,6 @@ e_ews_notification_get_events_thread (gpointer user_data)
 
        g_mutex_lock (&td->notification->priv->thread_lock);
 
-       cnc = e_ews_notification_ref_connection (td->notification);
-
-       if (cnc) {
-               GProxyResolver *proxy_resolver = NULL;
-
-               /* Skip GBinding here, due to the notification object can be freed in this thread,
-                  which can cause a crash in the GBinding code. */
-               g_object_get (cnc, "proxy-resolver", &proxy_resolver, NULL);
-               g_object_set (td->notification->priv->soup_session, "proxy-resolver", proxy_resolver, NULL);
-
-               g_clear_object (&proxy_resolver);
-               g_object_unref (cnc);
-       }
-
        if (td->notification->priv->last_subscription_id) {
                e_ews_notification_unsubscribe_folder_sync (td->notification, 
td->notification->priv->last_subscription_id, td->cancellable);
                g_clear_pointer (&td->notification->priv->last_subscription_id, g_free);
diff --git a/src/EWS/registry/e-ews-backend.c b/src/EWS/registry/e-ews-backend.c
index 3a89e930..b30ac156 100644
--- a/src/EWS/registry/e-ews-backend.c
+++ b/src/EWS/registry/e-ews-backend.c
@@ -189,6 +189,9 @@ ews_backend_sync_authentication (EEwsBackend *ews_backend,
 
        e_source_authentication_set_user (child_authentication_extension,
                e_source_authentication_get_user (coll_authentication_extension));
+
+       e_source_authentication_set_method (child_authentication_extension,
+               e_source_authentication_get_method (coll_authentication_extension));
 }
 
 static ESource *
@@ -711,6 +714,7 @@ ews_backend_constructed (GObject *object)
        EBackend *backend;
        ESource *source;
        ESourceAuthentication *auth_extension;
+       CamelEwsSettings *settings;
        const gchar *extension_name;
        gchar *host = NULL;
        guint16 port = 0;
@@ -739,6 +743,14 @@ ews_backend_constructed (GObject *object)
 
        g_free (host);
 
+       settings = ews_backend_get_settings (E_EWS_BACKEND (backend));
+
+       /* NTLM is a fallback, for any unknown value, but ESoupSession requires it
+          explicitly set, to use it for authentication, thus make sure it is set */
+       if (camel_ews_settings_get_auth_mechanism (settings) == EWS_AUTH_TYPE_NTLM &&
+           g_strcmp0 (e_source_authentication_get_method (auth_extension), "NTLM") != 0)
+               e_source_authentication_set_method (auth_extension, "NTLM");
+
        /* Reset the connectable, it steals data from Authentication extension,
           where is written incorrect address */
        e_backend_set_connectable (backend, NULL);
@@ -864,37 +876,31 @@ ews_backend_child_added (ECollectionBackend *backend,
 {
        ESource *collection_source;
        const gchar *extension_name;
-       gboolean is_mail = FALSE;
 
        collection_source = e_backend_get_source (E_BACKEND (backend));
 
-       extension_name = E_SOURCE_EXTENSION_MAIL_ACCOUNT;
-       is_mail |= e_source_has_extension (child_source, extension_name);
-
-       extension_name = E_SOURCE_EXTENSION_MAIL_IDENTITY;
-       is_mail |= e_source_has_extension (child_source, extension_name);
-
-       extension_name = E_SOURCE_EXTENSION_MAIL_TRANSPORT;
-       is_mail |= e_source_has_extension (child_source, extension_name);
-
-       /* Synchronize mail-related user with the collection identity. */
        extension_name = E_SOURCE_EXTENSION_AUTHENTICATION;
-       if (is_mail && e_source_has_extension (child_source, extension_name)) {
+       if (e_source_has_extension (child_source, extension_name)) {
                ESourceAuthentication *auth_child_extension;
-               ESourceCollection *collection_extension;
+               ESourceAuthentication *auth_collection_extension;
 
-               extension_name = E_SOURCE_EXTENSION_COLLECTION;
-               collection_extension = e_source_get_extension (
-                       collection_source, extension_name);
+               auth_child_extension = e_source_get_extension (child_source, extension_name);
+               auth_collection_extension = e_source_get_extension (collection_source, extension_name);
 
-               extension_name = E_SOURCE_EXTENSION_AUTHENTICATION;
-               auth_child_extension = e_source_get_extension (
-                       child_source, extension_name);
+               e_binding_bind_property (
+                       auth_collection_extension, "host",
+                       auth_child_extension, "host",
+                       G_BINDING_SYNC_CREATE);
 
                e_binding_bind_property (
-                       collection_extension, "identity",
+                       auth_collection_extension, "user",
                        auth_child_extension, "user",
                        G_BINDING_SYNC_CREATE);
+
+               e_binding_bind_property (
+                       auth_collection_extension, "method",
+                       auth_child_extension, "method",
+                       G_BINDING_SYNC_CREATE);
        }
 
        /* We track EWS folders in a hash table by folder ID. */
diff --git a/src/Microsoft365/registry/e-m365-backend.c b/src/Microsoft365/registry/e-m365-backend.c
index e8bb3580..261d5cf7 100644
--- a/src/Microsoft365/registry/e-m365-backend.c
+++ b/src/Microsoft365/registry/e-m365-backend.c
@@ -569,20 +569,27 @@ m365_backend_child_added (ECollectionBackend *backend,
 
        collection_source = e_backend_get_source (E_BACKEND (backend));
 
-       if (e_source_has_extension (child_source, E_SOURCE_EXTENSION_AUTHENTICATION) && (
-           e_source_has_extension (child_source, E_SOURCE_EXTENSION_MAIL_ACCOUNT) ||
-           e_source_has_extension (child_source, E_SOURCE_EXTENSION_MAIL_IDENTITY) ||
-           e_source_has_extension (child_source, E_SOURCE_EXTENSION_MAIL_TRANSPORT))) {
+       if (e_source_has_extension (child_source, E_SOURCE_EXTENSION_AUTHENTICATION)) {
                ESourceAuthentication *auth_child_extension;
-               ESourceCollection *collection_extension;
+               ESourceAuthentication *auth_collection_extension;
 
-               collection_extension = e_source_get_extension (collection_source, 
E_SOURCE_EXTENSION_COLLECTION);
                auth_child_extension = e_source_get_extension (child_source, 
E_SOURCE_EXTENSION_AUTHENTICATION);
+               auth_collection_extension = e_source_get_extension (collection_source, 
E_SOURCE_EXTENSION_AUTHENTICATION);
+
+               e_binding_bind_property (
+                       auth_collection_extension, "host",
+                       auth_child_extension, "host",
+                       G_BINDING_SYNC_CREATE);
 
                e_binding_bind_property (
-                       collection_extension, "identity",
+                       auth_collection_extension, "user",
                        auth_child_extension, "user",
                        G_BINDING_SYNC_CREATE);
+
+               e_binding_bind_property (
+                       auth_collection_extension, "method",
+                       auth_child_extension, "method",
+                       G_BINDING_SYNC_CREATE);
        }
 
        /* We track M365 folders in a hash table by folder ID. */


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