Blame SOURCES/flatpak-1.6.2-oci-fixes.patch

ad2398
diff -urN flatpak-1.6.2/common/flatpak-oci-registry.c flatpak-1.6.2.new/common/flatpak-oci-registry.c
ad2398
--- flatpak-1.6.2/common/flatpak-oci-registry.c	2019-12-20 09:52:17.000000000 +0000
ad2398
+++ flatpak-1.6.2.new/common/flatpak-oci-registry.c	2020-03-20 12:01:39.923000000 +0000
ad2398
@@ -901,6 +901,7 @@
ad2398
 
ad2398
 static char *
ad2398
 get_token_for_www_auth (FlatpakOciRegistry *self,
ad2398
+                        const char    *repository,
ad2398
                         const char    *www_authenticate,
ad2398
                         const char    *auth,
ad2398
                         GCancellable  *cancellable,
ad2398
@@ -911,6 +912,7 @@
ad2398
   g_autoptr(GHashTable) params = NULL;
ad2398
   g_autoptr(GHashTable) args = NULL;
ad2398
   const char *realm, *service, *scope, *token;
ad2398
+  g_autofree char *default_scope = NULL;
ad2398
   g_autoptr(SoupURI) auth_uri = NULL;
ad2398
   g_autoptr(GBytes) body = NULL;
ad2398
   g_autoptr(JsonNode) json = NULL;
ad2398
@@ -941,16 +943,21 @@
ad2398
   service = g_hash_table_lookup (params, "service");
ad2398
   if (service)
ad2398
     g_hash_table_insert (args, "service", (char *)service);
ad2398
+
ad2398
   scope = g_hash_table_lookup (params, "scope");
ad2398
-  if (scope)
ad2398
-    g_hash_table_insert (args, "scope", (char *)scope);
ad2398
+  if (scope == NULL)
ad2398
+    scope = default_scope = g_strdup_printf("repository:%s:pull", repository);
ad2398
+  g_hash_table_insert (args, "scope", (char *)scope);
ad2398
 
ad2398
   soup_uri_set_query_from_form (auth_uri, args);
ad2398
 
ad2398
   auth_msg = soup_message_new_from_uri ("GET", auth_uri);
ad2398
 
ad2398
-  g_autofree char *basic_auth = g_strdup_printf ("Basic %s", auth);
ad2398
-  soup_message_headers_replace (auth_msg->request_headers, "Authorization", basic_auth);
ad2398
+  if (auth)
ad2398
+    {
ad2398
+      g_autofree char *basic_auth = g_strdup_printf ("Basic %s", auth);
ad2398
+      soup_message_headers_replace (auth_msg->request_headers, "Authorization", basic_auth);
ad2398
+    }
ad2398
 
ad2398
   auth_stream = soup_session_send (self->soup_session, auth_msg, NULL, error);
ad2398
   if (auth_stream == NULL)
ad2398
@@ -1030,7 +1037,7 @@
ad2398
       return NULL;
ad2398
     }
ad2398
 
ad2398
-  token = get_token_for_www_auth (self, www_authenticate, basic_auth, cancellable, error);
ad2398
+  token = get_token_for_www_auth (self, repository, www_authenticate, basic_auth, cancellable, error);
ad2398
   if (token == NULL)
ad2398
     return NULL;
ad2398
 
ad2398
diff -urN flatpak-1.6.2/oci-authenticator/flatpak-oci-authenticator.c flatpak-1.6.2.new/oci-authenticator/flatpak-oci-authenticator.c
ad2398
--- flatpak-1.6.2/oci-authenticator/flatpak-oci-authenticator.c	2019-12-19 09:33:40.000000000 +0000
ad2398
+++ flatpak-1.6.2.new/oci-authenticator/flatpak-oci-authenticator.c	2020-03-20 12:01:39.936000000 +0000
ad2398
@@ -428,10 +428,12 @@
ad2398
   g_autoptr(GError) error = NULL;
ad2398
   g_autoptr(AutoFlatpakAuthenticatorRequest) request = NULL;
ad2398
   const char *auth = NULL;
ad2398
+  gboolean have_auth;
ad2398
   const char *oci_registry_uri = NULL;
ad2398
   gsize n_refs, i;
ad2398
   gboolean no_interaction = FALSE;
ad2398
   g_autoptr(FlatpakOciRegistry) registry = NULL;
ad2398
+  g_autofree char *first_token = NULL;
ad2398
   GVariantBuilder tokens;
ad2398
   GVariantBuilder results;
ad2398
   g_autofree char *sender = g_strdup (g_dbus_method_invocation_get_sender (invocation));
ad2398
@@ -439,6 +441,7 @@
ad2398
   g_debug ("handling Authenticator.RequestRefTokens");
ad2398
 
ad2398
   g_variant_lookup (arg_authenticator_options, "auth", "&s", &auth);
ad2398
+  have_auth = auth != NULL;
ad2398
 
ad2398
   if (!g_variant_lookup (arg_options, "xa.oci-registry-uri", "&s", &oci_registry_uri))
ad2398
     {
ad2398
@@ -476,18 +479,33 @@
ad2398
     return error_request (request, sender, error->message);
ad2398
 
ad2398
 
ad2398
-  if (auth == NULL)
ad2398
+  /* Look up credentials in config files */
ad2398
+  if (!have_auth)
ad2398
     {
ad2398
       g_debug ("Looking for %s in auth info", oci_registry_uri);
ad2398
       auth = lookup_auth_from_config (oci_registry_uri);
ad2398
+      have_auth = auth != NULL;
ad2398
     }
ad2398
 
ad2398
+  /* Try to see if we can get a token without presenting credentials */
ad2398
   n_refs = g_variant_n_children (arg_refs);
ad2398
-  if (auth == NULL && n_refs > 0 &&
ad2398
+  if (!have_auth && n_refs > 0)
ad2398
+    {
ad2398
+      g_autoptr(GVariant) ref_data = g_variant_get_child_value (arg_refs, 0);
ad2398
+
ad2398
+      first_token = get_token_for_ref (registry, ref_data, NULL, &error);
ad2398
+      if (first_token != NULL)
ad2398
+        have_auth = TRUE;
ad2398
+      else
ad2398
+        g_clear_error (&error);
ad2398
+    }
ad2398
+
ad2398
+  /* Prompt the user for credentials */
ad2398
+  n_refs = g_variant_n_children (arg_refs);
ad2398
+  if (!have_auth && n_refs > 0 &&
ad2398
       !no_interaction)
ad2398
     {
ad2398
       g_autoptr(GVariant) ref_data = g_variant_get_child_value (arg_refs, 0);
ad2398
-      g_autofree char *token = NULL;
ad2398
 
ad2398
       while (auth == NULL)
ad2398
         {
ad2398
@@ -498,13 +516,21 @@
ad2398
           if (test_auth == NULL)
ad2398
             return cancel_request (request, sender);
ad2398
 
ad2398
-          token = get_token_for_ref (registry, ref_data, test_auth, &error);
ad2398
-          if (token != NULL)
ad2398
-            auth = g_steal_pointer (&test_auth);
ad2398
+          first_token = get_token_for_ref (registry, ref_data, test_auth, &error);
ad2398
+          if (first_token != NULL)
ad2398
+            {
ad2398
+              auth = g_steal_pointer (&test_auth);
ad2398
+              have_auth = TRUE;
ad2398
+            }
ad2398
+          else
ad2398
+            {
ad2398
+              g_debug ("Failed to get token: %s", error->message);
ad2398
+              g_clear_error (&error);
ad2398
+            }
ad2398
         }
ad2398
     }
ad2398
 
ad2398
-  if (auth == NULL)
ad2398
+  if (!have_auth)
ad2398
     return error_request (request, sender, "No authentication information available");
ad2398
 
ad2398
   g_variant_builder_init (&tokens, G_VARIANT_TYPE ("a{sas}"));
ad2398
@@ -515,9 +541,16 @@
ad2398
       char *for_refs_strv[2] = { NULL, NULL};
ad2398
       g_autofree char *token = NULL;
ad2398
 
ad2398
-      token = get_token_for_ref (registry, ref_data, auth, &error);
ad2398
-      if (token == NULL)
ad2398
-        return error_request (request, sender, error->message);
ad2398
+      if (i == 0 && first_token != NULL)
ad2398
+        {
ad2398
+          token = g_steal_pointer (&first_token);
ad2398
+        }
ad2398
+      else
ad2398
+        {
ad2398
+          token = get_token_for_ref (registry, ref_data, auth, &error);
ad2398
+          if (token == NULL)
ad2398
+            return error_request (request, sender, error->message);
ad2398
+        }
ad2398
 
ad2398
       g_variant_get_child (ref_data, 0, "&s", &for_refs_strv[0]);
ad2398
       g_variant_builder_add (&tokens, "{s^as}", token, for_refs_strv);