Blame SOURCES/evolution-data-server-3.40.4-google-oauth2.patch

1a484a
From 08ec37272bb945625daed7e6ae7ed2bd663cdabd Mon Sep 17 00:00:00 2001
1a484a
From: Milan Crha <mcrha@redhat.com>
1a484a
Date: Wed, 4 May 2022 15:30:49 +0200
1a484a
Subject: [PATCH] I#388 - Google OAuth out-of-band (oob) flow will be
1a484a
 deprecated
1a484a
1a484a
Closes https://gitlab.gnome.org/GNOME/evolution-data-server/-/issues/388
1a484a
---
1a484a
 src/libedataserver/e-oauth2-service-google.c | 62 +++++++++++++++++---
1a484a
 1 file changed, 55 insertions(+), 7 deletions(-)
1a484a
1a484a
diff --git a/src/libedataserver/e-oauth2-service-google.c b/src/libedataserver/e-oauth2-service-google.c
1a484a
index 4d262d32f..93af1cb0b 100644
1a484a
--- a/src/libedataserver/e-oauth2-service-google.c
1a484a
+++ b/src/libedataserver/e-oauth2-service-google.c
1a484a
@@ -24,6 +24,7 @@
1a484a
 #include "e-oauth2-service-google.h"
1a484a
 
1a484a
 /* https://developers.google.com/identity/protocols/OAuth2InstalledApp */
1a484a
+/* https://developers.google.com/identity/protocols/oauth2/native-app */
1a484a
 
1a484a
 /* Forward Declarations */
1a484a
 static void e_oauth2_service_google_oauth2_service_init (EOAuth2ServiceInterface *iface);
1a484a
@@ -122,14 +123,60 @@ static const gchar *
1a484a
 eos_google_get_authentication_uri (EOAuth2Service *service,
1a484a
 				   ESource *source)
1a484a
 {
1a484a
-	return "https://accounts.google.com/o/oauth2/auth";
1a484a
+	return "https://accounts.google.com/o/oauth2/v2/auth";
1a484a
 }
1a484a
 
1a484a
 static const gchar *
1a484a
 eos_google_get_refresh_uri (EOAuth2Service *service,
1a484a
 			    ESource *source)
1a484a
 {
1a484a
-	return "https://www.googleapis.com/oauth2/v3/token";
1a484a
+	return "https://oauth2.googleapis.com/token";
1a484a
+}
1a484a
+
1a484a
+static const gchar *
1a484a
+eos_google_get_redirect_uri (EOAuth2Service *service,
1a484a
+			     ESource *source)
1a484a
+{
1a484a
+	G_LOCK_DEFINE_STATIC (redirect_uri);
1a484a
+	const gchar *key_name = "oauth2-google-redirect-uri";
1a484a
+	gchar *value;
1a484a
+
1a484a
+	G_LOCK (redirect_uri);
1a484a
+
1a484a
+	value = g_object_get_data (G_OBJECT (service), key_name);
1a484a
+	if (!value) {
1a484a
+		const gchar *client_id = eos_google_get_client_id (service, source);
1a484a
+
1a484a
+		if (client_id) {
1a484a
+			GPtrArray *array;
1a484a
+			gchar **strv;
1a484a
+			gchar *joinstr;
1a484a
+			guint ii;
1a484a
+
1a484a
+			strv = g_strsplit (client_id, ".", -1);
1a484a
+			array = g_ptr_array_new ();
1a484a
+
1a484a
+			for (ii = 0; strv[ii]; ii++) {
1a484a
+				g_ptr_array_insert (array, 0, strv[ii]);
1a484a
+			}
1a484a
+
1a484a
+			g_ptr_array_add (array, NULL);
1a484a
+
1a484a
+			joinstr = g_strjoinv (".", (gchar **) array->pdata);
1a484a
+			/* Use reverse-DNS of the client ID with the below path */
1a484a
+			value = g_strconcat (joinstr, ":/oauth2redirect", NULL);
1a484a
+
1a484a
+			g_ptr_array_free (array, TRUE);
1a484a
+			g_strfreev (strv);
1a484a
+			g_free (joinstr);
1a484a
+
1a484a
+			g_object_set_data_full (G_OBJECT (service), key_name, value, g_free);
1a484a
+		}
1a484a
+	}
1a484a
+
1a484a
+	G_UNLOCK (redirect_uri);
1a484a
+
1a484a
+	return value;
1a484a
 }
1a484a
 
1a484a
 static void
1a484a
@@ -191,13 +238,13 @@ eos_google_extract_authorization_code (EOAuth2Service *service,
1a484a
 
1a484a
 				params = soup_form_decode (query);
1a484a
 				if (params) {
1a484a
-					const gchar *response;
1a484a
+					const gchar *code;
1a484a
 
1a484a
-					response = g_hash_table_lookup (params, "response");
1a484a
-					if (response && g_ascii_strncasecmp (response, "code=", 5) == 0) {
1a484a
-						*out_authorization_code = g_strdup (response + 5);
1a484a
+					code = g_hash_table_lookup (params, "code");
1a484a
+					if (code && *code) {
1a484a
+						*out_authorization_code = g_strdup (code);
1a484a
 						known = TRUE;
1a484a
-					} else if (response && g_ascii_strncasecmp (response, "error", 5) == 0) {
1a484a
+					} else if (g_hash_table_lookup (params, "error")) {
1a484a
 						known = TRUE;
1a484a
 					}
1a484a
 
1a484a
@@ -225,6 +272,7 @@ e_oauth2_service_google_oauth2_service_init (EOAuth2ServiceInterface *iface)
1a484a
 	iface->get_client_secret = eos_google_get_client_secret;
1a484a
 	iface->get_authentication_uri = eos_google_get_authentication_uri;
1a484a
 	iface->get_refresh_uri = eos_google_get_refresh_uri;
1a484a
+	iface->get_redirect_uri = eos_google_get_redirect_uri;
1a484a
 	iface->prepare_authentication_uri_query = eos_google_prepare_authentication_uri_query;
1a484a
 	iface->extract_authorization_code = eos_google_extract_authorization_code;
1a484a
 }
1a484a
-- 
1a484a
2.35.1
1a484a