package com.atlassian.bitbucket.mesh.auth;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.security.PublicKey;
import java.util.Collection;

public interface PublicKeyRegistry {

    /**
     * @param nodeId    the node to add the public key for
     * @param publicKey the public key to add
     * @return the fingerprint of the added key
     */
    @Nonnull
    String add(@Nonnull String nodeId, @Nonnull PublicKey publicKey);

    /**
     * @param nodeId      the node the public key was registered to
     * @param fingerprint the public key's fingerprint
     * @return the public key, or {@code null} if no public key is registered with the specified fingerprint
     */
    @Nullable
    PublicKey findByFingerprint(@Nonnull String nodeId, @Nonnull String fingerprint);

    /**
     * @param nodeId the node to check for public keys
     * @return {@code true} if at least one public key has been registered; otherwise, {@code false}
     */
    boolean isRegistered(@Nonnull String nodeId);

    /**
     * @param nodeId the node to remove all public keys for
     * @return {@code true} if at least one public key was removed; otherwise, {@code false}
     */
    boolean removeAll(@Nonnull String nodeId);

    /**
     * @param nodeId    the node to remove the public key from
     * @param publicKey the public key to remove
     * @return {@code true} if the public key was removed; otherwise, {@code false}
     */
    boolean remove(@Nonnull String nodeId, @Nonnull PublicKey publicKey);

    /**
     * @param nodeId     the node to set public keys for
     * @param publicKeys the keys to set, which will <i>replace</i> any previously-set keys
     */
    void set(@Nonnull String nodeId, @Nonnull Collection<PublicKey> publicKeys);
}
