[evolution-data-server/tintou/external-auth] ESourceAuthentication: Add is-external property and use it instead of checking UOA or GOA extensions



commit 9d831e2ee78389cb236e33ac7c0d3795d2ff6a08
Author: Corentin Noël <corentin noel collabora com>
Date:   Tue Aug 20 16:04:43 2019 +0200

    ESourceAuthentication: Add is-external property and use it instead of checking UOA or GOA extensions

 src/libedataserver/e-oauth2-service.c              | 14 ++--
 src/libedataserver/e-source-authentication.c       | 83 +++++++++++++++++++++-
 src/libedataserver/e-source-authentication.h       |  5 ++
 src/libedataserverui/e-trust-prompt.c              | 11 ++-
 .../module-gnome-online-accounts.c                 |  6 ++
 src/modules/google-backend/module-google-backend.c | 25 ++++---
 .../oauth2-services/module-oauth2-services.c       |  7 +-
 7 files changed, 123 insertions(+), 28 deletions(-)
---
diff --git a/src/libedataserver/e-oauth2-service.c b/src/libedataserver/e-oauth2-service.c
index c4d295baf..979095b74 100644
--- a/src/libedataserver/e-oauth2-service.c
+++ b/src/libedataserver/e-oauth2-service.c
@@ -38,8 +38,6 @@
 #include "e-secret-store.h"
 #include "e-soup-ssl-trust.h"
 #include "e-source-authentication.h"
-#include "e-source-goa.h"
-#include "e-source-uoa.h"
 
 #include "e-oauth2-service.h"
 
@@ -49,20 +47,16 @@ static gboolean
 eos_default_can_process (EOAuth2Service *service,
                         ESource *source)
 {
-       gboolean can = FALSE;
-
        g_return_val_if_fail (E_IS_SOURCE (source), FALSE);
 
-       if (e_source_has_extension (source, E_SOURCE_EXTENSION_GOA) ||
-           e_source_has_extension (source, E_SOURCE_EXTENSION_UOA)) {
-               return FALSE;
-       }
-
        if (e_source_has_extension (source, E_SOURCE_EXTENSION_AUTHENTICATION)) {
                ESourceAuthentication *auth_extension;
                gchar *method;
 
                auth_extension = e_source_get_extension (source, E_SOURCE_EXTENSION_AUTHENTICATION);
+               if (e_source_authentication_get_is_external (auth_extension))
+                       return FALSE;
+
                method = e_source_authentication_dup_method (auth_extension);
 
                if (g_strcmp0 (method, e_oauth2_service_get_name (service)) == 0) {
@@ -73,7 +67,7 @@ eos_default_can_process (EOAuth2Service *service,
                g_free (method);
        }
 
-       return can;
+       return FALSE;
 }
 
 static gboolean
diff --git a/src/libedataserver/e-source-authentication.c b/src/libedataserver/e-source-authentication.c
index bf2514ce2..cba4d2ec6 100644
--- a/src/libedataserver/e-source-authentication.c
+++ b/src/libedataserver/e-source-authentication.c
@@ -50,6 +50,7 @@ struct _ESourceAuthenticationPrivate {
        gboolean remember_password;
        gchar *user;
        gchar *credential_name;
+       gboolean is_external;
 
        /* GNetworkAddress caches data internally, so we maintain the
         * instance to preserve the cache as opposed to just creating
@@ -66,7 +67,8 @@ enum {
        PROP_PROXY_UID,
        PROP_REMEMBER_PASSWORD,
        PROP_USER,
-       PROP_CREDENTIAL_NAME
+       PROP_CREDENTIAL_NAME,
+       PROP_IS_EXTERNAL
 };
 
 G_DEFINE_TYPE (
@@ -142,6 +144,12 @@ source_authentication_set_property (GObject *object,
                                E_SOURCE_AUTHENTICATION (object),
                                g_value_get_string (value));
                        return;
+
+               case PROP_IS_EXTERNAL:
+                       e_source_authentication_set_is_external (
+                               E_SOURCE_AUTHENTICATION (object),
+                               g_value_get_boolean (value));
+                       return;
        }
 
        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
@@ -209,6 +217,13 @@ source_authentication_get_property (GObject *object,
                                e_source_authentication_dup_credential_name (
                                E_SOURCE_AUTHENTICATION (object)));
                        return;
+
+               case PROP_IS_EXTERNAL:
+                       g_value_set_boolean (
+                               value,
+                               e_source_authentication_get_is_external (
+                               E_SOURCE_AUTHENTICATION (object)));
+                       return;
        }
 
        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
@@ -372,6 +387,21 @@ e_source_authentication_class_init (ESourceAuthenticationClass *class)
                        G_PARAM_EXPLICIT_NOTIFY |
                        G_PARAM_STATIC_STRINGS |
                        E_SOURCE_PARAM_SETTING));
+
+
+       g_object_class_install_property (
+               object_class,
+               PROP_IS_EXTERNAL,
+               g_param_spec_boolean (
+                       "is-external",
+                       "Is External",
+                       "Whether the authentication is done by another authentication manager (like any 
Single Sign On daemon)",
+                       FALSE,
+                       G_PARAM_READWRITE |
+                       G_PARAM_CONSTRUCT |
+                       G_PARAM_EXPLICIT_NOTIFY |
+                       G_PARAM_STATIC_STRINGS |
+                       E_SOURCE_PARAM_SETTING));
 }
 
 static void
@@ -966,3 +996,54 @@ e_source_authentication_set_credential_name (ESourceAuthentication *extension,
 
        g_object_notify (G_OBJECT (extension), "credential-name");
 }
+
+/**
+ * e_source_authentication_get_is_external:
+ * @extension: an #ESourceAuthentication
+ *
+ * Get if the authentication is done by and external application such as a
+ * Single Sign On application (e.g. GNOME Online Accounts)
+ *
+ * Returns: %TRUE if the authentication is done by an external application,
+ * %FALSE otherwise
+ *
+ * Since: 3.34
+ **/
+gboolean
+e_source_authentication_get_is_external (ESourceAuthentication *extension)
+{
+       g_return_val_if_fail (E_IS_SOURCE_AUTHENTICATION (extension), FALSE);
+
+       return extension->priv->is_external;
+}
+
+/**
+ * e_source_authentication_set_is_external:
+ * @extension: an #ESourceAuthentication
+ * @is_external: %TRUE if the authentication is done using an external
+ * application, %FALSE otherwise
+ *
+ * Set if the authentication is done by and external application such as a
+ * Single Sign On application (e.g. GNOME Online Accounts)
+ *
+ * Since: 3.34
+ **/
+void
+e_source_authentication_set_is_external (ESourceAuthentication *extension,
+                                         gboolean is_external)
+{
+       g_return_if_fail (E_IS_SOURCE_AUTHENTICATION (extension));
+
+       e_source_extension_property_lock (E_SOURCE_EXTENSION (extension));
+
+       if (extension->priv->is_external == is_external) {
+               e_source_extension_property_unlock (E_SOURCE_EXTENSION (extension));
+               return;
+       }
+
+       extension->priv->is_external = is_external;
+
+       e_source_extension_property_unlock (E_SOURCE_EXTENSION (extension));
+
+       g_object_notify (G_OBJECT (extension), "is-external");
+}
diff --git a/src/libedataserver/e-source-authentication.h b/src/libedataserver/e-source-authentication.h
index ee29798c3..3c8d1204c 100644
--- a/src/libedataserver/e-source-authentication.h
+++ b/src/libedataserver/e-source-authentication.h
@@ -129,6 +129,11 @@ gchar *            e_source_authentication_dup_credential_name
 void           e_source_authentication_set_credential_name
                                        (ESourceAuthentication *extension,
                                         const gchar *credential_name);
+gboolean       e_source_authentication_get_is_external
+                                       (ESourceAuthentication *extension);
+void           e_source_authentication_set_is_external
+                                       (ESourceAuthentication *extension,
+                                        gboolean is_external);
 
 G_END_DECLS
 
diff --git a/src/libedataserverui/e-trust-prompt.c b/src/libedataserverui/e-trust-prompt.c
index af8b596d5..cdc052420 100644
--- a/src/libedataserverui/e-trust-prompt.c
+++ b/src/libedataserverui/e-trust-prompt.c
@@ -558,18 +558,17 @@ e_trust_prompt_run_for_source (GtkWindow *parent,
        g_return_if_fail (E_IS_SOURCE (source));
        g_return_if_fail (certificate_pem != NULL);
 
-       if (e_source_has_extension (source, E_SOURCE_EXTENSION_GOA) ||
-           e_source_has_extension (source, E_SOURCE_EXTENSION_UOA)) {
-               /* Make sure that GOA/UOA collection sources contain these extensions too */
-               g_warn_if_fail (e_source_get_extension (source, E_SOURCE_EXTENSION_AUTHENTICATION));
+       if (e_source_has_extension (source, E_SOURCE_EXTENSION_AUTHENTICATION))
+               extension_authentication = e_source_get_extension (source, E_SOURCE_EXTENSION_AUTHENTICATION);
+
+       if (extension_authentication && e_source_authentication_get_is_external (extension_authentication)) {
                g_warn_if_fail (e_source_get_extension (source, E_SOURCE_EXTENSION_WEBDAV_BACKEND));
        }
 
-       if (e_source_has_extension (source, E_SOURCE_EXTENSION_AUTHENTICATION))
-               extension_authentication = e_source_get_extension (source, E_SOURCE_EXTENSION_AUTHENTICATION);
        if (e_source_has_extension (source, E_SOURCE_EXTENSION_WEBDAV_BACKEND))
                extension_webdav = e_source_get_extension (source, E_SOURCE_EXTENSION_WEBDAV_BACKEND);
 
+
        save_data = g_new0 (SaveSourceData, 1);
        save_data->response = E_TRUST_PROMPT_RESPONSE_UNKNOWN;
        save_data->call_save = FALSE;
diff --git a/src/modules/gnome-online-accounts/module-gnome-online-accounts.c 
b/src/modules/gnome-online-accounts/module-gnome-online-accounts.c
index 1fd2a6ec8..0c1105813 100644
--- a/src/modules/gnome-online-accounts/module-gnome-online-accounts.c
+++ b/src/modules/gnome-online-accounts/module-gnome-online-accounts.c
@@ -539,6 +539,9 @@ gnome_online_accounts_config_oauth (EGnomeOnlineAccounts *extension,
        extension_name = E_SOURCE_EXTENSION_AUTHENTICATION;
        source_extension = e_source_get_extension (source, extension_name);
 
+       e_source_authentication_set_is_external (
+               E_SOURCE_AUTHENTICATION (source_extension),
+               TRUE);
        e_source_authentication_set_method (
                E_SOURCE_AUTHENTICATION (source_extension),
                CAMEL_OAUTH_MECHANISM_NAME);
@@ -558,6 +561,9 @@ gnome_online_accounts_config_oauth2 (EGnomeOnlineAccounts *extension,
        extension_name = E_SOURCE_EXTENSION_AUTHENTICATION;
        source_extension = e_source_get_extension (source, extension_name);
 
+       e_source_authentication_set_is_external (
+               E_SOURCE_AUTHENTICATION (source_extension),
+               TRUE);
        e_source_authentication_set_method (
                E_SOURCE_AUTHENTICATION (source_extension),
                CAMEL_OAUTH2_MECHANISM_NAME);
diff --git a/src/modules/google-backend/module-google-backend.c 
b/src/modules/google-backend/module-google-backend.c
index 29586337f..f241be230 100644
--- a/src/modules/google-backend/module-google-backend.c
+++ b/src/modules/google-backend/module-google-backend.c
@@ -102,6 +102,7 @@ static gboolean
 google_backend_can_use_google_auth (ESource *source)
 {
        ESourceRegistryServer *registry;
+       ESourceAuthentication *auth_extension;
        gboolean res;
 
        g_return_val_if_fail (E_IS_SERVER_SIDE_SOURCE (source), FALSE);
@@ -124,8 +125,8 @@ google_backend_can_use_google_auth (ESource *source)
                }
        }
 
-       res = !e_source_has_extension (source, E_SOURCE_EXTENSION_GOA) &&
-             !e_source_has_extension (source, E_SOURCE_EXTENSION_UOA);
+       auth_extension = e_source_get_extension (source, E_SOURCE_EXTENSION_AUTHENTICATION);
+       res = !e_source_authentication_get_is_external (auth_extension);
 
        g_object_unref (source);
 
@@ -707,8 +708,7 @@ google_backend_populate (ECollectionBackend *backend)
 
        /* Force OAuth2 for GOA/UOA Google accounts and do it before calling parent method,
           thus it knows what authentication method it's supposed to use */
-       if (e_source_has_extension (source, E_SOURCE_EXTENSION_GOA) ||
-           e_source_has_extension (source, E_SOURCE_EXTENSION_UOA))
+       if (e_source_authentication_get_is_external (authentication_extension))
                e_source_authentication_set_method (authentication_extension, "OAuth2");
 
        /* Chain up to parent's method. */
@@ -747,6 +747,7 @@ google_backend_child_added (ECollectionBackend *backend,
        ESource *collection_source;
        const gchar *extension_name;
        gboolean is_mail = FALSE;
+       gboolean has_external_auth = FALSE;
 
        /* Chain up to parent's child_added() method. */
        E_COLLECTION_BACKEND_CLASS (e_google_backend_parent_class)->
@@ -782,6 +783,8 @@ google_backend_child_added (ECollectionBackend *backend,
                        child_source, extension_name);
                auth_child_user = e_source_authentication_get_user (
                        auth_child_extension);
+               has_external_auth = e_source_authentication_get_is_external (
+                       auth_child_extension);
 
                /* XXX Do not override an existing user name setting.
                 *     The IMAP or (especially) SMTP configuration may
@@ -842,8 +845,7 @@ google_backend_child_added (ECollectionBackend *backend,
                        G_CALLBACK (google_backend_contacts_update_auth_method_cb),
                        backend);
 
-               if (!e_source_has_extension (collection_source, E_SOURCE_EXTENSION_GOA) &&
-                   !e_source_has_extension (collection_source, E_SOURCE_EXTENSION_UOA)) {
+               if (!has_external_auth) {
                        /* Even the book is part of the collection it can be removed
                           separately, if not configured through GOA or UOA. */
                        e_server_side_source_set_removable (E_SERVER_SIDE_SOURCE (child_source), TRUE);
@@ -856,16 +858,23 @@ google_backend_child_removed (ECollectionBackend *backend,
                              ESource *child_source)
 {
        ESource *collection_source;
+       gboolean has_external_auth = FALSE;
 
        /* Chain up to parent's method. */
        E_COLLECTION_BACKEND_CLASS (e_google_backend_parent_class)->child_removed (backend, child_source);
 
        collection_source = e_backend_get_source (E_BACKEND (backend));
 
+       if (e_source_has_extension (child_source, E_SOURCE_EXTENSION_AUTHENTICATION)) {
+               ESourceAuthentication *auth_child_extension;
+               auth_child_extension = e_source_get_extension (
+                       child_source, E_SOURCE_EXTENSION_AUTHENTICATION);
+               has_external_auth = e_source_authentication_get_is_external (
+                       auth_child_extension);
+       }
        if (e_source_has_extension (child_source, E_SOURCE_EXTENSION_ADDRESS_BOOK) &&
            e_source_has_extension (collection_source, E_SOURCE_EXTENSION_COLLECTION) &&
-           !e_source_has_extension (collection_source, E_SOURCE_EXTENSION_GOA) &&
-           !e_source_has_extension (collection_source, E_SOURCE_EXTENSION_UOA)) {
+           !has_external_auth) {
                ESourceCollection *collection_extension;
 
                collection_extension = e_source_get_extension (collection_source, 
E_SOURCE_EXTENSION_COLLECTION);
diff --git a/src/modules/oauth2-services/module-oauth2-services.c 
b/src/modules/oauth2-services/module-oauth2-services.c
index 31540f78d..488295438 100644
--- a/src/modules/oauth2-services/module-oauth2-services.c
+++ b/src/modules/oauth2-services/module-oauth2-services.c
@@ -124,14 +124,15 @@ oauth2_source_monitor_update_source (EOAuth2SourceMonitor *extension,
        g_return_if_fail (E_IS_SERVER_SIDE_SOURCE (source));
 
        if (!extension->oauth2_services ||
-           !e_source_has_extension (source, E_SOURCE_EXTENSION_AUTHENTICATION) ||
-           e_source_has_extension (source, E_SOURCE_EXTENSION_GOA) ||
-           e_source_has_extension (source, E_SOURCE_EXTENSION_UOA))
+           !e_source_has_extension (source, E_SOURCE_EXTENSION_AUTHENTICATION))
                return;
 
        server_source = E_SERVER_SIDE_SOURCE (source);
        authentication_extension = e_source_get_extension (source, E_SOURCE_EXTENSION_AUTHENTICATION);
 
+       if (e_source_authentication_get_is_external (authentication_extension))
+               return;
+
        auth_method = e_source_authentication_dup_method (authentication_extension);
 
        if (e_oauth2_services_is_oauth2_alias (extension->oauth2_services, auth_method)) {


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