private boolean doBearerAuth(boolean readOnlyBearerAuth) throws IOException, RegistryException {
    String registry = registryEndpointRequestProperties.getServerUrl();
    String repository = registryEndpointRequestProperties.getImageName();
    String image = registry + "/" + repository;
    eventHandlers.dispatch(LogEvent.debug("attempting bearer auth for " + image + "..."));

    Optional<RegistryAuthenticator> authenticator =
        callRegistryEndpoint(
            new AuthenticationMethodRetriever(
                registryEndpointRequestProperties, getUserAgent(), httpClient));
    if (!authenticator.isPresent()) {
      eventHandlers.dispatch(LogEvent.debug("server requires basic auth for " + image));
      return false; // server returned "WWW-Authenticate: Basic ..."
    }

    doBearerAuth(readOnlyBearerAuth, authenticator.get());
    return true;
  }
Attempts bearer authentication for pull and push.

@return true if bearer authentication succeeded; false if the server expects basic
    authentication (and thus bearer authentication was not attempted)
@throws IOException if communicating with the endpoint fails
@throws RegistryException if communicating with the endpoint fails
@throws RegistryAuthenticationFailedException if authentication fails
@throws RegistryCredentialsNotSentException if authentication failed and credentials were not
    sent over plain HTTP
jib-core/src/main/java/com/google/cloud/tools/jib/registry/RegistryClient.java