@Override
  public RegistryClient call() throws CredentialRetrievalException, IOException, RegistryException {
    String registry = buildContext.getTargetImageConfiguration().getImageRegistry();
    try (ProgressEventDispatcher progressDispatcher =
            progressEventDispatcherFactory.create("authenticating push to " + registry, 2);
        TimerEventDispatcher ignored2 =
            new TimerEventDispatcher(
                buildContext.getEventHandlers(), String.format(DESCRIPTION, registry))) {
      Credential credential =
          RegistryCredentialRetriever.getTargetImageCredential(buildContext).orElse(null);
      progressDispatcher.dispatchProgress(1);

      RegistryClient registryClient =
          buildContext
              .newTargetImageRegistryClientFactory()
              .setCredential(credential)
              .newRegistryClient();
      if (!registryClient.doPushBearerAuth()) {
        // server returned "WWW-Authenticate: Basic ..." (e.g., local Docker registry)
        if (credential != null && !credential.isOAuth2RefreshToken()) {
          registryClient.configureBasicAuth();
        }
      }
      return registryClient;
    }
  }
Authenticates push to a target registry using Docker Token Authentication.

@see <a
    href="https://docs.docker.com/registry/spec/auth/token/">https://docs.docker.com/registry/spec/auth/token/</a>
jib-core/src/main/java/com/google/cloud/tools/jib/builder/steps/AuthenticatePushStep.java