[evolution] Adapt to changes in built-in OAuth2 implementation in evolution-data-server



commit c1a673bdc27c2975b3c954f1e3424ba4b9809f05
Author: Milan Crha <mcrha redhat com>
Date:   Mon Jan 22 13:33:52 2018 +0100

    Adapt to changes in built-in OAuth2 implementation in evolution-data-server

 .../evolution-mail-engine-docs.sgml.in             |    2 -
 po/POTFILES.in                                     |    2 -
 src/e-util/e-collection-account-wizard.c           |    2 +-
 src/libemail-engine/CMakeLists.txt                 |    4 -
 src/libemail-engine/camel-sasl-oauth2-google.c     |  116 -----------------
 src/libemail-engine/camel-sasl-oauth2-google.h     |   65 ----------
 src/libemail-engine/camel-sasl-xoauth2.c           |  132 --------------------
 src/libemail-engine/camel-sasl-xoauth2.h           |   66 ----------
 src/libemail-engine/e-mail-session.c               |   98 ++++++++++-----
 src/libemail-engine/libemail-engine.h              |    1 -
 .../cal-config-google/e-cal-config-google.c        |    2 +-
 .../cal-config-google/e-cal-config-gtasks.c        |    2 +-
 .../cal-config-google/e-google-chooser-button.c    |    3 +-
 .../cal-config-google/module-cal-config-google.c   |   13 ++
 .../cal-config-google/module-cal-config-google.h   |    7 +-
 .../mail-config/e-mail-config-google-summary.c     |   30 ++++-
 16 files changed, 112 insertions(+), 433 deletions(-)
---
diff --git a/docs/reference/evolution-mail-engine/evolution-mail-engine-docs.sgml.in 
b/docs/reference/evolution-mail-engine/evolution-mail-engine-docs.sgml.in
index 2dc1c7d..d440c06 100644
--- a/docs/reference/evolution-mail-engine/evolution-mail-engine-docs.sgml.in
+++ b/docs/reference/evolution-mail-engine/evolution-mail-engine-docs.sgml.in
@@ -16,8 +16,6 @@
   <chapter>
     <title>Classes</title>
     <xi:include href="xml/camel-null-store.xml"/>
-    <xi:include href="xml/camel-sasl-xoauth2.xml"/>
-    <xi:include href="xml/camel-sasl-oauth2-google.xml"/>
     <xi:include href="xml/e-mail-junk-filter.xml"/>
     <xi:include href="xml/e-mail-session.xml"/>
     <xi:include href="xml/em-filter-folder-element.xml"/>
diff --git a/po/POTFILES.in b/po/POTFILES.in
index 750961e..c2dbc29 100644
--- a/po/POTFILES.in
+++ b/po/POTFILES.in
@@ -304,8 +304,6 @@ src/e-util/gal-view-instance-save-as-dialog.c
 [type: gettext/glade]src/e-util/gal-view-instance-save-as-dialog.ui
 src/e-util/widgets.error.xml
 src/libemail-engine/camel-null-store.c
-src/libemail-engine/camel-sasl-oauth2-google.c
-src/libemail-engine/camel-sasl-xoauth2.c
 src/libemail-engine/e-mail-folder-utils.c
 src/libemail-engine/e-mail-session.c
 src/libemail-engine/e-mail-session-utils.c
diff --git a/src/e-util/e-collection-account-wizard.c b/src/e-util/e-collection-account-wizard.c
index d17023d..c3cd507 100644
--- a/src/e-util/e-collection-account-wizard.c
+++ b/src/e-util/e-collection-account-wizard.c
@@ -951,7 +951,7 @@ collection_account_wizard_write_changes_thread (ESimpleAsyncResult *result,
        if (!text || !*text)
                e_source_backend_set_backend_name (E_SOURCE_BACKEND (collection_extension), "none");
 
-       google_supported = e_source_credentials_google_is_supported ();
+       google_supported = e_oauth2_services_is_oauth2_alias (e_source_registry_get_oauth2_services 
(wizard->priv->registry), "Google");
 
        for (ii = 0; ii <= E_CONFIG_LOOKUP_RESULT_LAST_KIND; ii++) {
                source = wizard->priv->sources[ii];
diff --git a/src/libemail-engine/CMakeLists.txt b/src/libemail-engine/CMakeLists.txt
index 2946599..9029cc7 100644
--- a/src/libemail-engine/CMakeLists.txt
+++ b/src/libemail-engine/CMakeLists.txt
@@ -7,8 +7,6 @@ set(DEPENDENCIES
 
 set(SOURCES
        camel-null-store.c
-       camel-sasl-oauth2-google.c
-       camel-sasl-xoauth2.c
        e-mail-folder-utils.c
        e-mail-junk-filter.c
        e-mail-session-utils.c
@@ -30,8 +28,6 @@ set(SOURCES
 set(HEADERS
        libemail-engine.h
        camel-null-store.h
-       camel-sasl-oauth2-google.h
-       camel-sasl-xoauth2.h
        e-mail-engine-enums.h
        e-mail-folder-utils.h
        e-mail-junk-filter.h
diff --git a/src/libemail-engine/e-mail-session.c b/src/libemail-engine/e-mail-session.c
index 4dd3eb9..be0bd92 100644
--- a/src/libemail-engine/e-mail-session.c
+++ b/src/libemail-engine/e-mail-session.c
@@ -47,10 +47,6 @@
 /* This is our hack, not part of libcamel. */
 #include "camel-null-store.h"
 
-/* These too, though it's less of a hack. */
-#include "camel-sasl-xoauth2.h"
-#include "camel-sasl-oauth2-google.h"
-
 #include "e-mail-session.h"
 #include "e-mail-folder-utils.h"
 #include "e-mail-utils.h"
@@ -1198,7 +1194,7 @@ mail_session_add_service (CamelSession *session,
        if (CAMEL_IS_SERVICE (service)) {
                ESource *source;
                ESource *tmp_source;
-               gboolean is_google = FALSE;
+               EOAuth2Service *oauth2_service;
 
                /* Each CamelService has a corresponding ESource. */
                source = e_source_registry_ref_source (registry, uid);
@@ -1227,32 +1223,17 @@ mail_session_add_service (CamelSession *session,
                 * if necessary. */
                camel_service_migrate_files (service);
 
-               if (e_source_has_extension (source, E_SOURCE_EXTENSION_AUTHENTICATION)) {
-                       ESourceAuthentication *auth_extension;
-                       const gchar *host;
-
-                       auth_extension = e_source_get_extension (source, E_SOURCE_EXTENSION_AUTHENTICATION);
-                       host = e_source_authentication_get_host (auth_extension);
-
-                       is_google = host && (
-                               e_util_utf8_strstrcase (host, "gmail.com") != NULL ||
-                               e_util_utf8_strstrcase (host, "googlemail.com") != NULL);
-               }
-
-               g_object_unref (source);
-
-               /* Kind of hack for the custom SASL authentication unknown to Camel. */
-               if (is_google) {
-                       CamelProvider *provider;
-                       CamelSaslOAuth2GoogleClass *oauth2_google_class;
-
-                       oauth2_google_class = g_type_class_ref (CAMEL_TYPE_SASL_OAUTH2_GOOGLE);
-                       provider = camel_service_get_provider (service);
+               /* Kind of hack, to add also correct OAuth2 SASL implementation */
+               oauth2_service = e_oauth2_services_find (e_source_registry_get_oauth2_services (registry), 
source);
+               if (oauth2_service) {
+                       CamelServiceAuthType *auth_type;
 
-                       if (provider && oauth2_google_class) {
-                               CamelServiceAuthType *auth_type = oauth2_google_class->parent_class.auth_type;
+                       auth_type = camel_sasl_authtype (e_oauth2_service_get_name (oauth2_service));
+                       if (auth_type) {
+                               CamelProvider *provider;
 
-                               if (!g_list_find (provider->authtypes, auth_type))
+                               provider = camel_service_get_provider (service);
+                               if (provider && !g_list_find (provider->authtypes, auth_type))
                                        provider->authtypes = g_list_append (provider->authtypes, auth_type);
                        }
                }
@@ -1531,6 +1512,60 @@ mail_session_forward_to_sync (CamelSession *session,
        return success;
 }
 
+static gboolean
+mail_session_get_oauth2_access_token_sync (CamelSession *session,
+                                          CamelService *service,
+                                          gchar **out_access_token,
+                                          gint *out_expires_in,
+                                          GCancellable *cancellable,
+                                          GError **error)
+{
+       EMailSession *mail_session;
+       ESource *source, *cred_source;
+       GError *local_error = NULL;
+       gboolean success;
+
+       g_return_val_if_fail (E_IS_MAIL_SESSION (session), FALSE);
+       g_return_val_if_fail (CAMEL_IS_SERVICE (service), FALSE);
+
+       mail_session = E_MAIL_SESSION (session);
+       source = e_source_registry_ref_source (mail_session->priv->registry, camel_service_get_uid (service));
+       if (!source) {
+               g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND,
+                       _("Corresponding source for service with UID ā€œ%sā€ not found"),
+                       camel_service_get_uid (service));
+
+               return FALSE;
+       }
+
+       cred_source = e_source_registry_find_extension (mail_session->priv->registry, source, 
E_SOURCE_EXTENSION_COLLECTION);
+       if (cred_source && !e_util_can_use_collection_as_credential_source (cred_source, source)) {
+               g_clear_object (&cred_source);
+       }
+
+       success = e_source_get_oauth2_access_token_sync (cred_source ? cred_source : source, cancellable, 
out_access_token, out_expires_in, &local_error);
+
+       /* The Connection Refused error can be returned when the OAuth2 token is expired or
+          when its refresh failed for some reason. In that case change the error domain/code,
+          thus the other Camel/mail code understands it. */
+       if (g_error_matches (local_error, G_IO_ERROR, G_IO_ERROR_CONNECTION_REFUSED) ||
+           g_error_matches (local_error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND)) {
+               local_error->domain = CAMEL_SERVICE_ERROR;
+               local_error->code = CAMEL_SERVICE_ERROR_CANT_AUTHENTICATE;
+
+               e_source_invoke_credentials_required_sync (cred_source ? cred_source : source,
+                       E_SOURCE_CREDENTIALS_REASON_REJECTED, NULL, 0, local_error, cancellable, NULL);
+       }
+
+       if (local_error)
+               g_propagate_error (error, local_error);
+
+       g_clear_object (&cred_source);
+       g_object_unref (source);
+
+       return success;
+}
+
 static EMVFolderContext *
 mail_session_create_vfolder_context (EMailSession *session)
 {
@@ -1557,6 +1592,7 @@ e_mail_session_class_init (EMailSessionClass *class)
        session_class->get_password = mail_session_get_password;
        session_class->forget_password = mail_session_forget_password;
        session_class->forward_to_sync = mail_session_forward_to_sync;
+       session_class->get_oauth2_access_token_sync = mail_session_get_oauth2_access_token_sync;
 
        class->create_vfolder_context = mail_session_create_vfolder_context;
 
@@ -1695,10 +1731,6 @@ e_mail_session_class_init (EMailSessionClass *class)
 
        /* Make sure ESourceCamel picks up the "none" provider. */
        e_source_camel_generate_subtype ("none", CAMEL_TYPE_SETTINGS);
-
-       /* Make sure CamelSasl picks up our mechanisms. */
-       g_type_ensure (CAMEL_TYPE_SASL_XOAUTH2);
-       g_type_ensure (CAMEL_TYPE_SASL_OAUTH2_GOOGLE);
 }
 
 static void
diff --git a/src/libemail-engine/libemail-engine.h b/src/libemail-engine/libemail-engine.h
index 6e0b2be..d811478 100644
--- a/src/libemail-engine/libemail-engine.h
+++ b/src/libemail-engine/libemail-engine.h
@@ -21,7 +21,6 @@
 #define __LIBEMAIL_ENGINE_H_INSIDE__
 
 #include <libemail-engine/camel-null-store.h>
-#include <libemail-engine/camel-sasl-xoauth2.h>
 #include <libemail-engine/e-mail-engine-enums.h>
 #include <libemail-engine/e-mail-engine-enumtypes.h>
 #include <libemail-engine/e-mail-folder-utils.h>
diff --git a/src/modules/cal-config-google/e-cal-config-google.c 
b/src/modules/cal-config-google/e-cal-config-google.c
index 419de98..70781a7 100644
--- a/src/modules/cal-config-google/e-cal-config-google.c
+++ b/src/modules/cal-config-google/e-cal-config-google.c
@@ -132,7 +132,7 @@ cal_config_google_commit_changes (ESourceConfigBackend *backend,
        authentication_extension = e_source_get_extension (
                scratch_source, E_SOURCE_EXTENSION_AUTHENTICATION);
 
-       can_google_auth = e_source_credentials_google_is_supported () &&
+       can_google_auth = e_module_cal_config_google_is_supported (backend, NULL) &&
                          g_strcmp0 (e_source_authentication_get_method (authentication_extension), "OAuth2") 
!= 0;
 
        /* The backend name is actually "caldav" even though the
diff --git a/src/modules/cal-config-google/e-cal-config-gtasks.c 
b/src/modules/cal-config-google/e-cal-config-gtasks.c
index d25bb7d..99bdc27 100644
--- a/src/modules/cal-config-google/e-cal-config-gtasks.c
+++ b/src/modules/cal-config-google/e-cal-config-gtasks.c
@@ -64,7 +64,7 @@ cal_config_gtasks_allow_creation (ESourceConfigBackend *backend)
                return FALSE;
 
        source = e_source_config_get_original_source (config);
-       if (!source && e_source_credentials_google_is_supported ())
+       if (!source && e_module_cal_config_google_is_supported (backend, NULL))
                return TRUE;
 
        if (!e_source_has_extension (source, E_SOURCE_EXTENSION_TASK_LIST))
diff --git a/src/modules/cal-config-google/e-google-chooser-button.c 
b/src/modules/cal-config-google/e-google-chooser-button.c
index cdd5369..7c2852b 100644
--- a/src/modules/cal-config-google/e-google-chooser-button.c
+++ b/src/modules/cal-config-google/e-google-chooser-button.c
@@ -21,6 +21,7 @@
 
 #include <libedataserverui/libedataserverui.h>
 
+#include "module-cal-config-google.h"
 #include "e-google-chooser-button.h"
 
 #define E_GOOGLE_CHOOSER_BUTTON_GET_PRIVATE(obj) \
@@ -196,7 +197,7 @@ google_chooser_button_clicked (GtkButton *button)
        webdav_extension = e_source_get_extension (priv->source, E_SOURCE_EXTENSION_WEBDAV_BACKEND);
 
        uri = e_source_webdav_dup_soup_uri (webdav_extension);
-       can_google_auth = e_source_credentials_google_is_supported () &&
+       can_google_auth = e_module_cal_config_google_is_supported (NULL, registry) &&
                          g_strcmp0 (e_source_authentication_get_method (authentication_extension), "OAuth2") 
!= 0;
 
        e_google_chooser_button_construct_default_uri (uri, e_source_authentication_get_user 
(authentication_extension));
diff --git a/src/modules/cal-config-google/module-cal-config-google.c 
b/src/modules/cal-config-google/module-cal-config-google.c
index 79f6020..63ae476 100644
--- a/src/modules/cal-config-google/module-cal-config-google.c
+++ b/src/modules/cal-config-google/module-cal-config-google.c
@@ -20,6 +20,19 @@
 #include "e-google-chooser-button.h"
 #include "module-cal-config-google.h"
 
+gboolean
+e_module_cal_config_google_is_supported (ESourceConfigBackend *backend,
+                                        ESourceRegistry *registry)
+{
+       if (!backend && !registry)
+               return FALSE;
+
+       if (!registry)
+               registry = e_source_config_get_registry (e_source_config_backend_get_config (backend));
+
+       return registry && e_oauth2_services_is_oauth2_alias (e_source_registry_get_oauth2_services 
(registry), "Google");
+}
+
 /* Module Entry Points */
 void e_module_load (GTypeModule *type_module);
 void e_module_unload (GTypeModule *type_module);
diff --git a/src/modules/cal-config-google/module-cal-config-google.h 
b/src/modules/cal-config-google/module-cal-config-google.h
index 89dc1c1..7aca7f1 100644
--- a/src/modules/cal-config-google/module-cal-config-google.h
+++ b/src/modules/cal-config-google/module-cal-config-google.h
@@ -18,8 +18,11 @@
 
 #include <glib.h>
 #include <libebackend/libebackend.h>
+#include "e-util/e-util.h"
 
-void   e_cal_config_google_type_register       (GTypeModule *type_module);
-void   e_cal_config_gtasks_type_register       (GTypeModule *type_module);
+gboolean       e_module_cal_config_google_is_supported (ESourceConfigBackend *backend,
+                                                        ESourceRegistry *registry);
+void           e_cal_config_google_type_register       (GTypeModule *type_module);
+void           e_cal_config_gtasks_type_register       (GTypeModule *type_module);
 
 #endif /* MODULE_CAL_CONFIG_GOOGLE_H */
diff --git a/src/modules/mail-config/e-mail-config-google-summary.c 
b/src/modules/mail-config/e-mail-config-google-summary.c
index e7a0dfd..e86a7e9 100644
--- a/src/modules/mail-config/e-mail-config-google-summary.c
+++ b/src/modules/mail-config/e-mail-config-google-summary.c
@@ -21,6 +21,7 @@
 
 #include <libedataserver/libedataserver.h>
 #include <mail/e-mail-config-summary-page.h>
+#include "shell/e-shell.h"
 
 #include "e-mail-config-google-summary.h"
 
@@ -98,6 +99,23 @@ mail_config_google_summary_is_applicable (EMailConfigSummaryPage *page)
        return FALSE;
 }
 
+static gboolean
+mail_config_google_summary_is_oauth2_supported (void)
+{
+       EShell *shell;
+       ESourceRegistry *registry;
+
+       shell = e_shell_get_default ();
+       if (!shell)
+               return FALSE;
+
+       registry = e_shell_get_registry (shell);
+       if (!registry)
+               return FALSE;
+
+       return e_oauth2_services_is_oauth2_alias (e_source_registry_get_oauth2_services (registry), "Google");
+}
+
 static void
 mail_config_google_summary_refresh_cb (EMailConfigSummaryPage *page,
                                        EMailConfigGoogleSummary *extension)
@@ -132,7 +150,7 @@ mail_config_google_summary_commit_changes_cb (EMailConfigSummaryPage *page,
        toggle_button = GTK_TOGGLE_BUTTON (extension->priv->calendar_toggle);
        calendar_active = gtk_toggle_button_get_active (toggle_button);
 
-       if (e_source_credentials_google_is_supported ()) {
+       if (mail_config_google_summary_is_oauth2_supported ()) {
                toggle_button = GTK_TOGGLE_BUTTON (extension->priv->contacts_toggle);
                contacts_active = gtk_toggle_button_get_active (toggle_button);
        } else {
@@ -142,7 +160,7 @@ mail_config_google_summary_commit_changes_cb (EMailConfigSummaryPage *page,
        /* If the user declined both Calendar and Contacts, do nothing,
           but set the Google/OAuth2 authentication for the sources. */
        if (!calendar_active && !contacts_active) {
-               if (e_source_credentials_google_is_supported ()) {
+               if (mail_config_google_summary_is_oauth2_supported ()) {
                        source = e_mail_config_summary_page_get_account_source (page);
                        auth_extension = e_source_get_extension (source, E_SOURCE_EXTENSION_AUTHENTICATION);
                        e_source_authentication_set_method (auth_extension, "Google");
@@ -180,7 +198,7 @@ mail_config_google_summary_commit_changes_cb (EMailConfigSummaryPage *page,
        auth_extension = e_source_get_extension (source, E_SOURCE_EXTENSION_AUTHENTICATION);
        e_source_authentication_set_host (auth_extension, "");
 
-       if (e_source_credentials_google_is_supported ()) {
+       if (mail_config_google_summary_is_oauth2_supported ()) {
                e_source_authentication_set_user (auth_extension, user);
                e_source_authentication_set_method (auth_extension, "Google");
        }
@@ -297,7 +315,7 @@ mail_config_google_summary_constructed (GObject *object)
        extension->priv->calendar_toggle = widget;  /* not referenced */
        gtk_widget_show (widget);
 
-       if (e_source_credentials_google_is_supported ()) {
+       if (mail_config_google_summary_is_oauth2_supported ()) {
                text = _("Add Google Con_tacts to this account");
                widget = gtk_check_button_new_with_mnemonic (text);
                gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (widget), TRUE);
@@ -321,7 +339,7 @@ mail_config_google_summary_constructed (GObject *object)
        gtk_label_set_use_markup (GTK_LABEL (widget), TRUE);
        gtk_widget_set_margin_left (widget, 12);
        gtk_misc_set_alignment (GTK_MISC (widget), 0.0, 0.5);
-       if (e_source_credentials_google_is_supported ())
+       if (mail_config_google_summary_is_oauth2_supported ())
                gtk_grid_attach (GTK_GRID (container), widget, 0, 3, 1, 1);
        else
                gtk_grid_attach (GTK_GRID (container), widget, 0, 2, 1, 1);
@@ -344,7 +362,7 @@ mail_config_google_summary_constructed (GObject *object)
                collection_extension, "calendar-enabled",
                G_BINDING_SYNC_CREATE);
 
-       if (e_source_credentials_google_is_supported ())
+       if (mail_config_google_summary_is_oauth2_supported ())
                e_binding_bind_property (
                        extension->priv->contacts_toggle, "active",
                        collection_extension, "contacts-enabled",


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