static Optional<RegistryAuthenticator> fromAuthenticationMethod(
      String authenticationMethod,
      RegistryEndpointRequestProperties registryEndpointRequestProperties,
      @Nullable String userAgent,
      FailoverHttpClient httpClient)
      throws RegistryAuthenticationFailedException {
    // If the authentication method starts with 'basic' (case insensitive), no registry
    // authentication is needed.
    if (authenticationMethod.matches("^(?i)(basic).*")) {
      return Optional.empty();
    }

    String registryUrl = registryEndpointRequestProperties.getServerUrl();
    String imageName = registryEndpointRequestProperties.getImageName();
    // Checks that the authentication method starts with 'bearer ' (case insensitive).
    if (!authenticationMethod.matches("^(?i)(bearer) .*")) {
      throw newRegistryAuthenticationFailedException(
          registryUrl, imageName, authenticationMethod, "Bearer");
    }

    Pattern realmPattern = Pattern.compile("realm=\"(.*?)\"");
    Matcher realmMatcher = realmPattern.matcher(authenticationMethod);
    if (!realmMatcher.find()) {
      throw newRegistryAuthenticationFailedException(
          registryUrl, imageName, authenticationMethod, "realm");
    }
    String realm = realmMatcher.group(1);

    Pattern servicePattern = Pattern.compile("service=\"(.*?)\"");
    Matcher serviceMatcher = servicePattern.matcher(authenticationMethod);
    // use the provided registry location when missing service (e.g., for OpenShift)
    String service = serviceMatcher.find() ? serviceMatcher.group(1) : registryUrl;

    return Optional.of(
        new RegistryAuthenticator(
            realm, service, registryEndpointRequestProperties, userAgent, httpClient));
  }
@Test
  public void testFromAuthenticationMethod_basic() throws RegistryAuthenticationFailedException {
    assertThat(
            RegistryAuthenticator.fromAuthenticationMethod(
                "Basic", registryEndpointRequestProperties, "user-agent", httpClient))
        .isEmpty();

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

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

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