Skip to content

Commit

Permalink
fix: fix WWW-Authenticate header parsing for Basic authentication (Go…
Browse files Browse the repository at this point in the history
  • Loading branch information
chanseokoh authored Jun 8, 2023
1 parent 22d5378 commit 8c35a15
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,9 @@ static Optional<RegistryAuthenticator> fromAuthenticationMethod(
@Nullable String userAgent,
FailoverHttpClient httpClient)
throws RegistryAuthenticationFailedException {
// If the authentication method starts with 'basic ' (case insensitive), no registry
// If the authentication method starts with 'basic' (case insensitive), no registry
// authentication is needed.
if (authenticationMethod.matches("^(?i)(basic) .*")) {
if (authenticationMethod.matches("^(?i)(basic).*")) {
return Optional.empty();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@

package com.google.cloud.tools.jib.registry;

import static com.google.common.truth.Truth.assertThat;
import static com.google.common.truth.Truth8.assertThat;

import com.google.cloud.tools.jib.api.Credential;
import com.google.cloud.tools.jib.api.RegistryAuthenticationFailedException;
import com.google.cloud.tools.jib.http.FailoverHttpClient;
Expand Down Expand Up @@ -83,10 +86,11 @@ public void testFromAuthenticationMethod_bearer()
"user-agent",
httpClient)
.get();
Assert.assertEquals(
new URL("https://somerealm?service=someservice&scope=repository:someimage:scope"),
registryAuthenticator.getAuthenticationUrl(
null, Collections.singletonMap("someimage", "scope")));
assertThat(
registryAuthenticator.getAuthenticationUrl(
null, Collections.singletonMap("someimage", "scope")))
.isEqualTo(
new URL("https://somerealm?service=someservice&scope=repository:someimage:scope"));

registryAuthenticator =
RegistryAuthenticator.fromAuthenticationMethod(
Expand All @@ -95,10 +99,11 @@ public void testFromAuthenticationMethod_bearer()
"user-agent",
httpClient)
.get();
Assert.assertEquals(
new URL("https://somerealm?service=someservice&scope=repository:someimage:scope"),
registryAuthenticator.getAuthenticationUrl(
null, Collections.singletonMap("someimage", "scope")));
assertThat(
registryAuthenticator.getAuthenticationUrl(
null, Collections.singletonMap("someimage", "scope")))
.isEqualTo(
new URL("https://somerealm?service=someservice&scope=repository:someimage:scope"));
}

@Test
Expand Down Expand Up @@ -155,29 +160,34 @@ public void istAuthenticationUrl_oauth2() throws MalformedURLException {

@Test
public void testFromAuthenticationMethod_basic() throws RegistryAuthenticationFailedException {
Assert.assertFalse(
RegistryAuthenticator.fromAuthenticationMethod(
assertThat(
RegistryAuthenticator.fromAuthenticationMethod(
"Basic", registryEndpointRequestProperties, "user-agent", httpClient))
.isEmpty();

assertThat(
RegistryAuthenticator.fromAuthenticationMethod(
"Basic realm=\"https://somerealm\",service=\"someservice\",scope=\"somescope\"",
registryEndpointRequestProperties,
"user-agent",
httpClient)
.isPresent());
httpClient))
.isEmpty();

Assert.assertFalse(
RegistryAuthenticator.fromAuthenticationMethod(
assertThat(
RegistryAuthenticator.fromAuthenticationMethod(
"BASIC realm=\"https://somerealm\",service=\"someservice\",scope=\"somescope\"",
registryEndpointRequestProperties,
"user-agent",
httpClient)
.isPresent());
httpClient))
.isEmpty();

Assert.assertFalse(
RegistryAuthenticator.fromAuthenticationMethod(
assertThat(
RegistryAuthenticator.fromAuthenticationMethod(
"bASIC realm=\"https://somerealm\",service=\"someservice\",scope=\"somescope\"",
registryEndpointRequestProperties,
"user-agent",
httpClient)
.isPresent());
httpClient))
.isEmpty();
}

@Test
Expand Down

0 comments on commit 8c35a15

Please sign in to comment.