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

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