package com.atlassian.bitbucket.mesh.credentials;

import com.atlassian.bitbucket.mesh.rpc.v1.RpcRemoteRepository;
import com.google.protobuf.ByteString;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.nio.file.Path;

public interface CredentialsHelper {

    /**
     * Writes the provided {@code privateKeyBytes} on disk in a secured temporary file and registers it for cleanup
     * when the current RPC finished
     *
     * @param privateKeyBytes the private key bytes
     * @return the temporary file
     */
    @Nonnull
    Path createTemporaryPrivateKey(@Nonnull ByteString privateKeyBytes);

    /**
     * Creates a {@link Credentials} instance for the provided repository if credentials were specified on the
     * {@link RpcRemoteRepository} and invokes the provided {@code action}.
     *
     * @param repository the remote repository
     * @return the {@link Credentials} or {@code null} if no credentials were specified on the request
     */
    @Nullable
    Credentials getCredentials(@Nonnull RpcRemoteRepository repository);
}
