Blame SOURCES/evolution-ews-3.28.5-oauth2-endpoints.patch

068f62
diff -up evolution-ews-3.28.5/CMakeLists.txt.oauth2-endpoints evolution-ews-3.28.5/CMakeLists.txt
068f62
--- evolution-ews-3.28.5/CMakeLists.txt.oauth2-endpoints	2020-10-07 17:08:15.761562791 +0200
068f62
+++ evolution-ews-3.28.5/CMakeLists.txt	2020-10-07 17:08:15.762562790 +0200
068f62
@@ -195,6 +195,12 @@ if(WITH_OFFICE365_REDIRECT_URI STREQUAL
068f62
 	set(WITH_OFFICE365_REDIRECT_URI "https://login.microsoftonline.com/common/oauth2/nativeclient")
068f62
 endif(WITH_OFFICE365_REDIRECT_URI STREQUAL "")
068f62
 
068f62
+add_printable_variable(WITH_OFFICE365_ENDPOINT_HOST "Office365.com OAuth 2.0 endpoint host" "")
068f62
+
068f62
+if(WITH_OFFICE365_ENDPOINT_HOST STREQUAL "")
068f62
+	set(WITH_OFFICE365_ENDPOINT_HOST "login.microsoftonline.com")
068f62
+endif(WITH_OFFICE365_ENDPOINT_HOST STREQUAL "")
068f62
+
068f62
 # ******************************
068f62
 # Special directories
068f62
 # ******************************
068f62
diff -up evolution-ews-3.28.5/config.h.in.oauth2-endpoints evolution-ews-3.28.5/config.h.in
068f62
--- evolution-ews-3.28.5/config.h.in.oauth2-endpoints	2018-07-30 16:01:00.000000000 +0200
068f62
+++ evolution-ews-3.28.5/config.h.in	2020-10-07 17:08:15.762562790 +0200
068f62
@@ -29,3 +29,6 @@
068f62
 
068f62
 /* Define Office365 OAuth 2.0 default Redirect URI to use */
068f62
 #define OFFICE365_REDIRECT_URI "@WITH_OFFICE365_REDIRECT_URI@"
068f62
+
068f62
+/* Define Office365 OAuth 2.0 default Endpoint Host to use */
068f62
+#define OFFICE365_ENDPOINT_HOST "@WITH_OFFICE365_ENDPOINT_HOST@"
068f62
diff -up evolution-ews-3.28.5/src/configuration/e-mail-config-ews-backend.c.oauth2-endpoints evolution-ews-3.28.5/src/configuration/e-mail-config-ews-backend.c
068f62
--- evolution-ews-3.28.5/src/configuration/e-mail-config-ews-backend.c.oauth2-endpoints	2020-10-07 17:08:15.761562791 +0200
068f62
+++ evolution-ews-3.28.5/src/configuration/e-mail-config-ews-backend.c	2020-10-07 17:08:15.762562790 +0200
068f62
@@ -29,6 +29,7 @@
068f62
 #include <mail/e-mail-config-receiving-page.h>
068f62
 
068f62
 #include "server/camel-ews-settings.h"
068f62
+#include "server/e-oauth2-service-office365.h"
068f62
 
068f62
 #include "e-mail-config-ews-autodiscover.h"
068f62
 #include "e-ews-config-utils.h"
068f62
@@ -50,6 +51,8 @@ struct _EMailConfigEwsBackendPrivate {
068f62
 	GtkWidget *oauth2_tenant_entry;
068f62
 	GtkWidget *oauth2_client_id_entry;
068f62
 	GtkWidget *oauth2_redirect_uri_entry;
068f62
+	GtkWidget *oauth2_resource_uri_entry;
068f62
+	GtkWidget *oauth2_endpoint_host_entry;
068f62
 };
068f62
 
068f62
 G_DEFINE_DYNAMIC_TYPE (
068f62
@@ -156,6 +159,11 @@ mail_config_ews_backend_insert_widgets (
068f62
 	GtkLabel *label;
068f62
 	GtkWidget *widget;
068f62
 	GtkWidget *container;
068f62
+	GtkWidget *expander;
068f62
+	GtkWidget *advanced_help;
068f62
+	GtkWidget *endpoint_host_label;
068f62
+	GtkWidget *redirect_uri_label;
068f62
+	GtkWidget *resource_uri_label;
068f62
 	const gchar *extension_name;
068f62
 	const gchar *text;
068f62
 	gchar *markup;
068f62
@@ -381,11 +389,62 @@ mail_config_ews_backend_insert_widgets (
068f62
 		_("There is not set any default application ID"),
068f62
 		g_strdup_printf (_("Default application ID is “%s”"), OFFICE365_CLIENT_ID));
068f62
 
068f62
-	widget = gtk_label_new_with_mnemonic (_("_Redirect URI:"));
068f62
+	container = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 6);
068f62
+	gtk_grid_attach (priv->oauth2_settings_grid, container, 0, 3, 2, 1);
068f62
+
068f62
+	widget = gtk_expander_new_with_mnemonic (_("_Advanced Settings"));
068f62
+	gtk_widget_set_margin_left (widget, 12);
068f62
+	gtk_box_pack_start (GTK_BOX (container), widget, FALSE, FALSE, 0);
068f62
+	expander = widget;
068f62
+
068f62
+	e_binding_bind_property (
068f62
+		priv->oauth2_override_check, "active",
068f62
+		widget, "sensitive",
068f62
+		G_BINDING_SYNC_CREATE);
068f62
+
068f62
+	markup = g_markup_printf_escaped ("(%s)", _("Help…"));
068f62
+	widget = gtk_label_new (markup);
068f62
+	gtk_label_set_use_markup (GTK_LABEL (widget), TRUE);
068f62
+	gtk_misc_set_alignment (GTK_MISC (widget), 0.0, 0.5);
068f62
+	gtk_box_pack_start (GTK_BOX (container), widget, FALSE, FALSE, 0);
068f62
+	g_free (markup);
068f62
+	advanced_help = widget;
068f62
+
068f62
+	widget = gtk_label_new_with_mnemonic (_("_Endpoint host:"));
068f62
+	gtk_widget_set_margin_left (widget, 12);
068f62
+	gtk_misc_set_alignment (GTK_MISC (widget), 1.0, 0.5);
068f62
+	gtk_grid_attach (priv->oauth2_settings_grid, widget, 0, 4, 1, 1);
068f62
+	label = GTK_LABEL (widget);
068f62
+	endpoint_host_label = widget;
068f62
+
068f62
+	e_binding_bind_property (
068f62
+		priv->oauth2_override_check, "active",
068f62
+		widget, "sensitive",
068f62
+		G_BINDING_SYNC_CREATE);
068f62
+
068f62
+	widget = gtk_entry_new ();
068f62
+	gtk_widget_set_hexpand (widget, TRUE);
068f62
+	gtk_label_set_mnemonic_widget (label, widget);
068f62
+	gtk_grid_attach (priv->oauth2_settings_grid, widget, 1, 4, 1, 1);
068f62
+	priv->oauth2_endpoint_host_entry = widget;
068f62
+
068f62
+	e_binding_bind_property (
068f62
+		priv->oauth2_override_check, "active",
068f62
+		widget, "sensitive",
068f62
+		G_BINDING_SYNC_CREATE);
068f62
+
068f62
+	markup = g_strdup_printf (_("Default endpoint host is “%s”"), "login.microsoftonline.com");
068f62
+	mail_config_ews_backend_set_oauth2_tooltip (widget, OFFICE365_ENDPOINT_HOST,
068f62
+		markup,
068f62
+		g_strdup_printf (_("Default endpoint host is “%s”"), OFFICE365_ENDPOINT_HOST));
068f62
+	g_free (markup);
068f62
+
068f62
+	widget = gtk_label_new_with_mnemonic (_("Red_irect URI:"));
068f62
 	gtk_widget_set_margin_left (widget, 12);
068f62
 	gtk_misc_set_alignment (GTK_MISC (widget), 1.0, 0.5);
068f62
-	gtk_grid_attach (priv->oauth2_settings_grid, widget, 0, 3, 1, 1);
068f62
+	gtk_grid_attach (priv->oauth2_settings_grid, widget, 0, 5, 1, 1);
068f62
 	label = GTK_LABEL (widget);
068f62
+	redirect_uri_label = widget;
068f62
 
068f62
 	e_binding_bind_property (
068f62
 		priv->oauth2_override_check, "active",
068f62
@@ -395,7 +454,7 @@ mail_config_ews_backend_insert_widgets (
068f62
 	widget = gtk_entry_new ();
068f62
 	gtk_widget_set_hexpand (widget, TRUE);
068f62
 	gtk_label_set_mnemonic_widget (label, widget);
068f62
-	gtk_grid_attach (priv->oauth2_settings_grid, widget, 1, 3, 1, 1);
068f62
+	gtk_grid_attach (priv->oauth2_settings_grid, widget, 1, 5, 1, 1);
068f62
 	priv->oauth2_redirect_uri_entry = widget;
068f62
 
068f62
 	e_binding_bind_property (
068f62
@@ -409,8 +468,77 @@ mail_config_ews_backend_insert_widgets (
068f62
 		g_strdup_printf (_("Default redirect URI is “%s”"), OFFICE365_REDIRECT_URI));
068f62
 	g_free (markup);
068f62
 
068f62
+	widget = gtk_label_new_with_mnemonic (_("Re_source URI:"));
068f62
+	gtk_widget_set_margin_left (widget, 12);
068f62
+	gtk_misc_set_alignment (GTK_MISC (widget), 1.0, 0.5);
068f62
+	gtk_grid_attach (priv->oauth2_settings_grid, widget, 0, 6, 1, 1);
068f62
+	label = GTK_LABEL (widget);
068f62
+	resource_uri_label = widget;
068f62
+
068f62
+	e_binding_bind_property (
068f62
+		priv->oauth2_override_check, "active",
068f62
+		widget, "sensitive",
068f62
+		G_BINDING_SYNC_CREATE);
068f62
+
068f62
+	widget = gtk_entry_new ();
068f62
+	gtk_widget_set_hexpand (widget, TRUE);
068f62
+	gtk_label_set_mnemonic_widget (label, widget);
068f62
+	gtk_grid_attach (priv->oauth2_settings_grid, widget, 1, 6, 1, 1);
068f62
+	priv->oauth2_resource_uri_entry = widget;
068f62
+
068f62
+	e_binding_bind_property (
068f62
+		priv->oauth2_override_check, "active",
068f62
+		widget, "sensitive",
068f62
+		G_BINDING_SYNC_CREATE);
068f62
+
068f62
+	markup = g_strdup_printf (_("Default resource URI is derived from the Host URL, or it can eventually fall back to “%s”."), OFFICE365_FALLBACK_RESOURCE_URI);
068f62
+	mail_config_ews_backend_set_oauth2_tooltip (widget, NULL,
068f62
+		markup,
068f62
+		NULL);
068f62
+	g_free (markup);
068f62
+
068f62
 	gtk_widget_show_all (GTK_WIDGET (priv->oauth2_settings_grid));
068f62
 
068f62
+	gtk_expander_set_expanded (GTK_EXPANDER (expander),
068f62
+		e_ews_util_strcmp0 (camel_ews_settings_get_oauth2_endpoint_host (CAMEL_EWS_SETTINGS (settings)), NULL) != 0 ||
068f62
+		e_ews_util_strcmp0 (camel_ews_settings_get_oauth2_redirect_uri (CAMEL_EWS_SETTINGS (settings)), NULL) != 0 ||
068f62
+		e_ews_util_strcmp0 (camel_ews_settings_get_oauth2_resource_uri (CAMEL_EWS_SETTINGS (settings)), NULL) != 0);
068f62
+
068f62
+	e_binding_bind_property (
068f62
+		expander, "expanded",
068f62
+		advanced_help, "visible",
068f62
+		G_BINDING_SYNC_CREATE);
068f62
+
068f62
+	e_binding_bind_property (
068f62
+		expander, "expanded",
068f62
+		endpoint_host_label, "visible",
068f62
+		G_BINDING_SYNC_CREATE);
068f62
+
068f62
+	e_binding_bind_property (
068f62
+		expander, "expanded",
068f62
+		priv->oauth2_endpoint_host_entry, "visible",
068f62
+		G_BINDING_SYNC_CREATE);
068f62
+
068f62
+	e_binding_bind_property (
068f62
+		expander, "expanded",
068f62
+		redirect_uri_label, "visible",
068f62
+		G_BINDING_SYNC_CREATE);
068f62
+
068f62
+	e_binding_bind_property (
068f62
+		expander, "expanded",
068f62
+		priv->oauth2_redirect_uri_entry, "visible",
068f62
+		G_BINDING_SYNC_CREATE);
068f62
+
068f62
+	e_binding_bind_property (
068f62
+		expander, "expanded",
068f62
+		resource_uri_label, "visible",
068f62
+		G_BINDING_SYNC_CREATE);
068f62
+
068f62
+	e_binding_bind_property (
068f62
+		expander, "expanded",
068f62
+		priv->oauth2_resource_uri_entry, "visible",
068f62
+		G_BINDING_SYNC_CREATE);
068f62
+
068f62
 	e_binding_bind_property_full (
068f62
 		priv->auth_check, "active-mechanism",
068f62
 		priv->oauth2_settings_grid, "visible",
068f62
@@ -463,11 +591,23 @@ mail_config_ews_backend_insert_widgets (
068f62
 		G_BINDING_SYNC_CREATE);
068f62
 
068f62
 	e_binding_bind_object_text_property (
068f62
-		settings, "oauth2-redirect_uri",
068f62
+		settings, "oauth2-redirect-uri",
068f62
 		priv->oauth2_redirect_uri_entry, "text",
068f62
 		G_BINDING_BIDIRECTIONAL |
068f62
 		G_BINDING_SYNC_CREATE);
068f62
 
068f62
+	e_binding_bind_object_text_property (
068f62
+		settings, "oauth2-resource-uri",
068f62
+		priv->oauth2_resource_uri_entry, "text",
068f62
+		G_BINDING_BIDIRECTIONAL |
068f62
+		G_BINDING_SYNC_CREATE);
068f62
+
068f62
+	e_binding_bind_object_text_property (
068f62
+		settings, "oauth2-endpoint-host",
068f62
+		priv->oauth2_endpoint_host_entry, "text",
068f62
+		G_BINDING_BIDIRECTIONAL |
068f62
+		G_BINDING_SYNC_CREATE);
068f62
+
068f62
 	extension_name = E_SOURCE_EXTENSION_COLLECTION;
068f62
 	source = e_mail_config_service_backend_get_collection (backend);
068f62
 	extension = e_source_get_extension (source, extension_name);
068f62
diff -up evolution-ews-3.28.5/src/server/camel-ews-settings.c.oauth2-endpoints evolution-ews-3.28.5/src/server/camel-ews-settings.c
068f62
--- evolution-ews-3.28.5/src/server/camel-ews-settings.c.oauth2-endpoints	2018-07-30 16:01:00.000000000 +0200
068f62
+++ evolution-ews-3.28.5/src/server/camel-ews-settings.c	2020-10-07 17:08:15.762562790 +0200
068f62
@@ -47,6 +47,8 @@ struct _CamelEwsSettingsPrivate {
068f62
 	gchar *oauth2_tenant;
068f62
 	gchar *oauth2_client_id;
068f62
 	gchar *oauth2_redirect_uri;
068f62
+	gchar *oauth2_resource_uri;
068f62
+	gchar *oauth2_endpoint_host;
068f62
 };
068f62
 
068f62
 enum {
068f62
@@ -74,7 +76,9 @@ enum {
068f62
 	PROP_OVERRIDE_OAUTH2,
068f62
 	PROP_OAUTH2_TENANT,
068f62
 	PROP_OAUTH2_CLIENT_ID,
068f62
-	PROP_OAUTH2_REDIRECT_URI
068f62
+	PROP_OAUTH2_REDIRECT_URI,
068f62
+	PROP_OAUTH2_RESOURCE_URI,
068f62
+	PROP_OAUTH2_ENDPOINT_HOST
068f62
 };
068f62
 
068f62
 G_DEFINE_TYPE_WITH_CODE (
068f62
@@ -263,6 +267,18 @@ ews_settings_set_property (GObject *obje
068f62
 				CAMEL_EWS_SETTINGS (object),
068f62
 				g_value_get_string (value));
068f62
 			return;
068f62
+
068f62
+		case PROP_OAUTH2_RESOURCE_URI:
068f62
+			camel_ews_settings_set_oauth2_resource_uri (
068f62
+				CAMEL_EWS_SETTINGS (object),
068f62
+				g_value_get_string (value));
068f62
+			return;
068f62
+
068f62
+		case PROP_OAUTH2_ENDPOINT_HOST:
068f62
+			camel_ews_settings_set_oauth2_endpoint_host (
068f62
+				CAMEL_EWS_SETTINGS (object),
068f62
+				g_value_get_string (value));
068f62
+			return;
068f62
 	}
068f62
 
068f62
 	G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
068f62
@@ -442,6 +458,20 @@ ews_settings_get_property (GObject *obje
068f62
 				camel_ews_settings_dup_oauth2_redirect_uri (
068f62
 				CAMEL_EWS_SETTINGS (object)));
068f62
 			return;
068f62
+
068f62
+		case PROP_OAUTH2_RESOURCE_URI:
068f62
+			g_value_take_string (
068f62
+				value,
068f62
+				camel_ews_settings_dup_oauth2_resource_uri (
068f62
+				CAMEL_EWS_SETTINGS (object)));
068f62
+			return;
068f62
+
068f62
+		case PROP_OAUTH2_ENDPOINT_HOST:
068f62
+			g_value_take_string (
068f62
+				value,
068f62
+				camel_ews_settings_dup_oauth2_endpoint_host (
068f62
+				CAMEL_EWS_SETTINGS (object)));
068f62
+			return;
068f62
 	}
068f62
 
068f62
 	G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
068f62
@@ -466,6 +496,8 @@ ews_settings_finalize (GObject *object)
068f62
 	g_free (priv->oauth2_tenant);
068f62
 	g_free (priv->oauth2_client_id);
068f62
 	g_free (priv->oauth2_redirect_uri);
068f62
+	g_free (priv->oauth2_resource_uri);
068f62
+	g_free (priv->oauth2_endpoint_host);
068f62
 
068f62
 	/* Chain up to parent's finalize() method. */
068f62
 	G_OBJECT_CLASS (camel_ews_settings_parent_class)->finalize (object);
068f62
@@ -740,6 +772,30 @@ camel_ews_settings_class_init (CamelEwsS
068f62
 			G_PARAM_READWRITE |
068f62
 			G_PARAM_CONSTRUCT |
068f62
 			G_PARAM_STATIC_STRINGS));
068f62
+
068f62
+	g_object_class_install_property (
068f62
+		object_class,
068f62
+		PROP_OAUTH2_RESOURCE_URI,
068f62
+		g_param_spec_string (
068f62
+			"oauth2-resource-uri",
068f62
+			"OAuth2 Resource URI",
068f62
+			"OAuth2 Resource URI to use, only if override-oauth2 is TRUE, otherwise the compile-time value is used",
068f62
+			NULL,
068f62
+			G_PARAM_READWRITE |
068f62
+			G_PARAM_CONSTRUCT |
068f62
+			G_PARAM_STATIC_STRINGS));
068f62
+
068f62
+	g_object_class_install_property (
068f62
+		object_class,
068f62
+		PROP_OAUTH2_ENDPOINT_HOST,
068f62
+		g_param_spec_string (
068f62
+			"oauth2-endpoint-host",
068f62
+			"OAuth2 Endpoint Host",
068f62
+			"OAuth2 endpoint host to use, only if override-oauth2 is TRUE, otherwise the compile-time value is used",
068f62
+			NULL,
068f62
+			G_PARAM_READWRITE |
068f62
+			G_PARAM_CONSTRUCT |
068f62
+			G_PARAM_STATIC_STRINGS));
068f62
 }
068f62
 
068f62
 static void
068f62
@@ -1543,3 +1599,97 @@ camel_ews_settings_set_oauth2_redirect_u
068f62
 
068f62
 	g_object_notify (G_OBJECT (settings), "oauth2-redirect-uri");
068f62
 }
068f62
+
068f62
+const gchar *
068f62
+camel_ews_settings_get_oauth2_resource_uri (CamelEwsSettings *settings)
068f62
+{
068f62
+	g_return_val_if_fail (CAMEL_IS_EWS_SETTINGS (settings), NULL);
068f62
+
068f62
+	return settings->priv->oauth2_resource_uri;
068f62
+}
068f62
+
068f62
+gchar *
068f62
+camel_ews_settings_dup_oauth2_resource_uri (CamelEwsSettings *settings)
068f62
+{
068f62
+	const gchar *protected;
068f62
+	gchar *duplicate;
068f62
+
068f62
+	g_return_val_if_fail (CAMEL_IS_EWS_SETTINGS (settings), NULL);
068f62
+
068f62
+	g_mutex_lock (&settings->priv->property_lock);
068f62
+
068f62
+	protected = camel_ews_settings_get_oauth2_resource_uri (settings);
068f62
+	duplicate = g_strdup (protected);
068f62
+
068f62
+	g_mutex_unlock (&settings->priv->property_lock);
068f62
+
068f62
+	return duplicate;
068f62
+}
068f62
+
068f62
+void
068f62
+camel_ews_settings_set_oauth2_resource_uri (CamelEwsSettings *settings,
068f62
+					    const gchar *resource_uri)
068f62
+{
068f62
+	g_return_if_fail (CAMEL_IS_EWS_SETTINGS (settings));
068f62
+
068f62
+	g_mutex_lock (&settings->priv->property_lock);
068f62
+
068f62
+	if (g_strcmp0 (settings->priv->oauth2_resource_uri, resource_uri) == 0) {
068f62
+		g_mutex_unlock (&settings->priv->property_lock);
068f62
+		return;
068f62
+	}
068f62
+
068f62
+	g_free (settings->priv->oauth2_resource_uri);
068f62
+	settings->priv->oauth2_resource_uri = e_util_strdup_strip (resource_uri);
068f62
+
068f62
+	g_mutex_unlock (&settings->priv->property_lock);
068f62
+
068f62
+	g_object_notify (G_OBJECT (settings), "oauth2-resource-uri");
068f62
+}
068f62
+
068f62
+const gchar *
068f62
+camel_ews_settings_get_oauth2_endpoint_host (CamelEwsSettings *settings)
068f62
+{
068f62
+	g_return_val_if_fail (CAMEL_IS_EWS_SETTINGS (settings), NULL);
068f62
+
068f62
+	return settings->priv->oauth2_endpoint_host;
068f62
+}
068f62
+
068f62
+gchar *
068f62
+camel_ews_settings_dup_oauth2_endpoint_host (CamelEwsSettings *settings)
068f62
+{
068f62
+	const gchar *protected;
068f62
+	gchar *duplicate;
068f62
+
068f62
+	g_return_val_if_fail (CAMEL_IS_EWS_SETTINGS (settings), NULL);
068f62
+
068f62
+	g_mutex_lock (&settings->priv->property_lock);
068f62
+
068f62
+	protected = camel_ews_settings_get_oauth2_endpoint_host (settings);
068f62
+	duplicate = g_strdup (protected);
068f62
+
068f62
+	g_mutex_unlock (&settings->priv->property_lock);
068f62
+
068f62
+	return duplicate;
068f62
+}
068f62
+
068f62
+void
068f62
+camel_ews_settings_set_oauth2_endpoint_host (CamelEwsSettings *settings,
068f62
+					     const gchar *endpoint_host)
068f62
+{
068f62
+	g_return_if_fail (CAMEL_IS_EWS_SETTINGS (settings));
068f62
+
068f62
+	g_mutex_lock (&settings->priv->property_lock);
068f62
+
068f62
+	if (g_strcmp0 (settings->priv->oauth2_endpoint_host, endpoint_host) == 0) {
068f62
+		g_mutex_unlock (&settings->priv->property_lock);
068f62
+		return;
068f62
+	}
068f62
+
068f62
+	g_free (settings->priv->oauth2_endpoint_host);
068f62
+	settings->priv->oauth2_endpoint_host = e_util_strdup_strip (endpoint_host);
068f62
+
068f62
+	g_mutex_unlock (&settings->priv->property_lock);
068f62
+
068f62
+	g_object_notify (G_OBJECT (settings), "oauth2-endpoint-host");
068f62
+}
068f62
diff -up evolution-ews-3.28.5/src/server/camel-ews-settings.h.oauth2-endpoints evolution-ews-3.28.5/src/server/camel-ews-settings.h
068f62
--- evolution-ews-3.28.5/src/server/camel-ews-settings.h.oauth2-endpoints	2018-07-30 16:01:00.000000000 +0200
068f62
+++ evolution-ews-3.28.5/src/server/camel-ews-settings.h	2020-10-07 17:08:15.763562790 +0200
068f62
@@ -167,6 +167,20 @@ gchar *		camel_ews_settings_dup_oauth2_r
068f62
 void		camel_ews_settings_set_oauth2_redirect_uri
068f62
 						(CamelEwsSettings *settings,
068f62
 						 const gchar *redirect_uri);
068f62
+const gchar *	camel_ews_settings_get_oauth2_resource_uri
068f62
+						(CamelEwsSettings *settings);
068f62
+gchar *		camel_ews_settings_dup_oauth2_resource_uri
068f62
+						(CamelEwsSettings *settings);
068f62
+void		camel_ews_settings_set_oauth2_resource_uri
068f62
+						(CamelEwsSettings *settings,
068f62
+						 const gchar *resource_uri);
068f62
+const gchar *	camel_ews_settings_get_oauth2_endpoint_host
068f62
+						(CamelEwsSettings *settings);
068f62
+gchar *		camel_ews_settings_dup_oauth2_endpoint_host
068f62
+						(CamelEwsSettings *settings);
068f62
+void		camel_ews_settings_set_oauth2_endpoint_host
068f62
+						(CamelEwsSettings *settings,
068f62
+						 const gchar *endpoint_host);
068f62
 
068f62
 G_END_DECLS
068f62
 
068f62
diff -up evolution-ews-3.28.5/src/server/e-ews-connection.c.oauth2-endpoints evolution-ews-3.28.5/src/server/e-ews-connection.c
068f62
--- evolution-ews-3.28.5/src/server/e-ews-connection.c.oauth2-endpoints	2020-10-07 17:08:43.161554484 +0200
068f62
+++ evolution-ews-3.28.5/src/server/e-ews-connection.c	2020-10-07 17:09:20.338543223 +0200
068f62
@@ -9914,13 +9914,7 @@ ews_connection_gather_auth_methods_cb (S
068f62
 
068f62
 	if (!has_bearer) {
068f62
 		/* Special-case Office365 OAuth2, because outlook.office365.com doesn't advertise Bearer */
068f62
-		SoupURI *suri;
068f62
-
068f62
-		suri = soup_message_get_uri (message);
068f62
-		if (suri && soup_uri_get_host (suri) &&
068f62
-		    g_ascii_strcasecmp (soup_uri_get_host (suri), "outlook.office365.com") == 0) {
068f62
-			async_data->items = g_slist_prepend (async_data->items, g_strdup ("Bearer"));
068f62
-		}
068f62
+		async_data->items = g_slist_prepend (async_data->items, g_strdup ("Bearer"));
068f62
 	}
068f62
 
068f62
 	g_object_set_data (G_OBJECT (simple), EWS_OBJECT_KEY_AUTHS_GATHERED, GINT_TO_POINTER (1));
068f62
diff -up evolution-ews-3.28.5/src/server/e-oauth2-service-office365.c.oauth2-endpoints evolution-ews-3.28.5/src/server/e-oauth2-service-office365.c
068f62
--- evolution-ews-3.28.5/src/server/e-oauth2-service-office365.c.oauth2-endpoints	2020-10-07 17:08:15.761562791 +0200
068f62
+++ evolution-ews-3.28.5/src/server/e-oauth2-service-office365.c	2020-10-07 17:09:30.526540133 +0200
068f62
@@ -28,8 +28,6 @@
068f62
    https://tsmatz.wordpress.com/2016/10/07/application-permission-with-v2-endpoint-and-microsoft-graph/
068f62
 */
068f62
 
068f62
-#define OFFICE365_RESOURCE "https://outlook.office.com"
068f62
-
068f62
 struct _EOAuth2ServiceOffice365Private
068f62
 {
068f62
 	GMutex string_cache_lock;
068f62
@@ -53,8 +51,10 @@ eos_office365_cache_string (EOAuth2Servi
068f62
 	if (!str)
068f62
 		return NULL;
068f62
 
068f62
-	if (!*str)
068f62
+	if (!*str) {
068f62
+		g_free (str);
068f62
 		return "";
068f62
+	}
068f62
 
068f62
 	g_mutex_lock (&oauth2_office365->priv->string_cache_lock);
068f62
 
068f62
@@ -71,6 +71,24 @@ eos_office365_cache_string (EOAuth2Servi
068f62
 	return cached_str;
068f62
 }
068f62
 
068f62
+static const gchar *
068f62
+eos_office365_get_endpoint_host (EOAuth2ServiceOffice365 *oauth2_office365,
068f62
+				 CamelEwsSettings *ews_settings)
068f62
+{
068f62
+	if (ews_settings && camel_ews_settings_get_override_oauth2 (ews_settings)) {
068f62
+		gchar *endpoint_host;
068f62
+
068f62
+		endpoint_host = camel_ews_settings_dup_oauth2_endpoint_host (ews_settings);
068f62
+
068f62
+		if (endpoint_host && *endpoint_host)
068f62
+			return eos_office365_cache_string (oauth2_office365, endpoint_host);
068f62
+
068f62
+		g_free (endpoint_host);
068f62
+	}
068f62
+
068f62
+	return OFFICE365_ENDPOINT_HOST;
068f62
+}
068f62
+
068f62
 static CamelEwsSettings *
068f62
 eos_office365_get_camel_settings (ESource *source)
068f62
 {
068f62
@@ -92,8 +110,7 @@ eos_office365_guess_can_process (EOAuth2
068f62
 				 const gchar *hostname)
068f62
 {
068f62
 	return e_oauth2_services_is_supported () &&
068f62
-		protocol && g_ascii_strcasecmp (protocol, "ews") == 0 &&
068f62
-		hostname && e_util_utf8_strstrcase (hostname, "outlook.office365.com");
068f62
+		protocol && g_ascii_strcasecmp (protocol, "ews") == 0;
068f62
 }
068f62
 
068f62
 static const gchar *
068f62
@@ -158,7 +175,8 @@ eos_office365_get_authentication_uri (EO
068f62
 		}
068f62
 
068f62
 		res = eos_office365_cache_string (oauth2_office365,
068f62
-			g_strdup_printf ("https://login.microsoftonline.com/%s/oauth2/authorize",
068f62
+			g_strdup_printf ("https://%s/%s/oauth2/authorize",
068f62
+				eos_office365_get_endpoint_host (oauth2_office365, ews_settings),
068f62
 				tenant ? tenant : OFFICE365_TENANT));
068f62
 
068f62
 		g_free (tenant);
068f62
@@ -166,7 +184,10 @@ eos_office365_get_authentication_uri (EO
068f62
 		return res;
068f62
 	}
068f62
 
068f62
-	return "https://login.microsoftonline.com/" OFFICE365_TENANT "/oauth2/authorize";
068f62
+	return eos_office365_cache_string (oauth2_office365,
068f62
+		g_strdup_printf ("https://%s/%s/oauth2/authorize",
068f62
+			eos_office365_get_endpoint_host (oauth2_office365, ews_settings),
068f62
+			OFFICE365_TENANT));
068f62
 }
068f62
 
068f62
 static const gchar *
068f62
@@ -188,7 +209,8 @@ eos_office365_get_refresh_uri (EOAuth2Se
068f62
 		}
068f62
 
068f62
 		res = eos_office365_cache_string (oauth2_office365,
068f62
-			g_strdup_printf ("https://login.microsoftonline.com/%s/oauth2/token",
068f62
+			g_strdup_printf ("https://%s/%s/oauth2/token",
068f62
+				eos_office365_get_endpoint_host (oauth2_office365, ews_settings),
068f62
 				tenant ? tenant : OFFICE365_TENANT));
068f62
 
068f62
 		g_free (tenant);
068f62
@@ -196,7 +218,10 @@ eos_office365_get_refresh_uri (EOAuth2Se
068f62
 		return res;
068f62
 	}
068f62
 
068f62
-	return "https://login.microsoftonline.com/" OFFICE365_TENANT "/oauth2/token";
068f62
+	return eos_office365_cache_string (oauth2_office365,
068f62
+		g_strdup_printf ("https://%s/%s/oauth2/token",
068f62
+			eos_office365_get_endpoint_host (oauth2_office365, ews_settings),
068f62
+			OFFICE365_TENANT));
068f62
 }
068f62
 
068f62
 static const gchar *
068f62
@@ -220,13 +245,67 @@ eos_office365_get_redirect_uri (EOAuth2S
068f62
 
068f62
 		if (redirect_uri)
068f62
 			return eos_office365_cache_string (oauth2_office365, redirect_uri);
068f62
+
068f62
+		if (e_ews_util_strcmp0 (camel_ews_settings_get_oauth2_endpoint_host (ews_settings), NULL) != 0) {
068f62
+			return eos_office365_cache_string (oauth2_office365,
068f62
+				g_strdup_printf ("https://%s/common/oauth2/nativeclient",
068f62
+					eos_office365_get_endpoint_host (oauth2_office365, ews_settings)));
068f62
+		}
068f62
 	}
068f62
 
068f62
 	res = OFFICE365_REDIRECT_URI;
068f62
 	if (res && *res)
068f62
 		return res;
068f62
 
068f62
-	return "https://login.microsoftonline.com/common/oauth2/nativeclient";
068f62
+	return eos_office365_cache_string (oauth2_office365,
068f62
+		g_strdup_printf ("https://%s/common/oauth2/nativeclient",
068f62
+			eos_office365_get_endpoint_host (oauth2_office365, ews_settings)));
068f62
+}
068f62
+
068f62
+static const gchar *
068f62
+eos_office365_get_resource_uri (EOAuth2Service *service,
068f62
+				ESource *source)
068f62
+{
068f62
+	EOAuth2ServiceOffice365 *oauth2_office365 = E_OAUTH2_SERVICE_OFFICE365 (service);
068f62
+	CamelEwsSettings *ews_settings;
068f62
+
068f62
+	ews_settings = eos_office365_get_camel_settings (source);
068f62
+	if (ews_settings && camel_ews_settings_get_override_oauth2 (ews_settings)) {
068f62
+		gchar *resource_uri;
068f62
+
068f62
+		resource_uri = camel_ews_settings_dup_oauth2_resource_uri (ews_settings);
068f62
+
068f62
+		if (resource_uri && !*resource_uri) {
068f62
+			g_free (resource_uri);
068f62
+			resource_uri = NULL;
068f62
+		}
068f62
+
068f62
+		if (resource_uri)
068f62
+			return eos_office365_cache_string (oauth2_office365, resource_uri);
068f62
+	}
068f62
+
068f62
+	if (ews_settings) {
068f62
+		gchar *host_url;
068f62
+
068f62
+		host_url = camel_ews_settings_dup_hosturl (ews_settings);
068f62
+
068f62
+		if (host_url && *host_url) {
068f62
+			gchar *ptr;
068f62
+
068f62
+			ptr = strstr (host_url, "://");
068f62
+			ptr = ptr ? strchr (ptr + 3, '/') : NULL;
068f62
+
068f62
+			if (ptr) {
068f62
+				*ptr = '\0';
068f62
+
068f62
+				return eos_office365_cache_string (oauth2_office365, host_url);
068f62
+			}
068f62
+		}
068f62
+
068f62
+		g_free (host_url);
068f62
+	}
068f62
+
068f62
+	return OFFICE365_FALLBACK_RESOURCE_URI;
068f62
 }
068f62
 
068f62
 static void
068f62
@@ -238,7 +317,7 @@ eos_office365_prepare_authentication_uri
068f62
 
068f62
 	e_oauth2_service_util_set_to_form (uri_query, "response_mode", "query");
068f62
 	e_oauth2_service_util_set_to_form (uri_query, "prompt", "login");
068f62
-	e_oauth2_service_util_set_to_form (uri_query, "resource", OFFICE365_RESOURCE);
068f62
+	e_oauth2_service_util_set_to_form (uri_query, "resource", eos_office365_get_resource_uri (service, source));
068f62
 }
068f62
 
068f62
 static gboolean
068f62
@@ -305,7 +384,7 @@ eos_office365_prepare_refresh_token_form
068f62
 {
068f62
 	g_return_if_fail (form != NULL);
068f62
 
068f62
-	e_oauth2_service_util_set_to_form (form, "resource", OFFICE365_RESOURCE);
068f62
+	e_oauth2_service_util_set_to_form (form, "resource", eos_office365_get_resource_uri (service, source));
068f62
 	e_oauth2_service_util_set_to_form (form, "redirect_uri", e_oauth2_service_get_redirect_uri (service, source));
068f62
 }
068f62
 
068f62
@@ -369,3 +448,16 @@ e_oauth2_service_office365_type_register
068f62
 {
068f62
 	e_oauth2_service_office365_register_type (type_module);
068f62
 }
068f62
+
068f62
+gint
068f62
+e_ews_util_strcmp0 (const gchar *str1,
068f62
+		    const gchar *str2)
068f62
+{
068f62
+	if (str1 && !*str1)
068f62
+		str1 = NULL;
068f62
+
068f62
+	if (str2 && !*str2)
068f62
+		str2 = NULL;
068f62
+
068f62
+	return g_strcmp0 (str1, str2);
068f62
+}
068f62
diff -up evolution-ews-3.28.5/src/server/e-oauth2-service-office365.h.oauth2-endpoints evolution-ews-3.28.5/src/server/e-oauth2-service-office365.h
068f62
--- evolution-ews-3.28.5/src/server/e-oauth2-service-office365.h.oauth2-endpoints	2018-07-30 16:01:00.000000000 +0200
068f62
+++ evolution-ews-3.28.5/src/server/e-oauth2-service-office365.h	2020-10-07 17:08:15.763562790 +0200
068f62
@@ -39,6 +39,8 @@
068f62
 	(G_TYPE_INSTANCE_GET_CLASS \
068f62
 	((obj), E_TYPE_OAUTH2_SERVICE_OFFICE365, EOAuth2ServiceOffice365Class))
068f62
 
068f62
+#define OFFICE365_FALLBACK_RESOURCE_URI "https://outlook.office365.com"
068f62
+
068f62
 G_BEGIN_DECLS
068f62
 
068f62
 typedef struct _EOAuth2ServiceOffice365 EOAuth2ServiceOffice365;
068f62
@@ -59,6 +61,9 @@ GType		e_oauth2_service_office365_get_ty
068f62
 void		e_oauth2_service_office365_type_register
068f62
 							(GTypeModule *type_module);
068f62
 
068f62
+gint		e_ews_util_strcmp0			(const gchar *str1,
068f62
+							 const gchar *str2);
068f62
+
068f62
 G_END_DECLS
068f62
 
068f62
 #endif /* E_OAUTH2_SERVICE_OFFICE365_H */