private Authorization authenticate(@Nullable Credential credential, String scope)
      throws RegistryAuthenticationFailedException, RegistryCredentialsNotSentException {
    // try authorizing against both the main repository and the source repository too
    // to enable cross-repository mounts on pushes
    String sourceImageName = registryEndpointRequestProperties.getSourceImageName();
    String imageName = registryEndpointRequestProperties.getImageName();
    if (sourceImageName != null && !sourceImageName.equals(imageName)) {
      try {
        return authenticate(credential, ImmutableMap.of(imageName, scope, sourceImageName, "pull"));
      } catch (RegistryAuthenticationFailedException ex) {
        // Unable to obtain authorization with source image: fall through and try without
      }
    }
    return authenticate(credential, ImmutableMap.of(imageName, scope));
  }
Sends the authentication request and retrieves the Bearer authorization token.

@param credential the credential used to authenticate
@param scope the scope of permissions to authenticate for
@return the {@link Authorization} response
@throws RegistryAuthenticationFailedException if authentication fails
@throws RegistryCredentialsNotSentException if authentication is failed and credentials were
    not sent over plain HTTP
@see <a
    href="https://docs.docker.com/registry/spec/auth/token/#how-to-authenticate">https://docs.docker.com/registry/spec/auth/token/#how-to-authenticate</a>
jib-core/src/main/java/com/google/cloud/tools/jib/registry/RegistryAuthenticator.java