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

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