package com.atlassian.bitbucket.internal.ssh.server;

import com.google.common.base.MoreObjects;
import com.google.common.base.Objects;

import javax.annotation.Nonnull;
import java.security.PublicKey;

import static com.google.common.base.Preconditions.checkNotNull;

/**
 * The credentials supplied during SSH authentication.
 */
public class SshCredentials {

    private final PublicKey publicKey;
    private final String username;

    public SshCredentials(@Nonnull String username, @Nonnull PublicKey publicKey) {
        this.publicKey = checkNotNull(publicKey, "key");
        this.username = checkNotNull(username, "username");
    }

    @Nonnull
    public PublicKey getPublicKey() {
        return publicKey;
    }

    @Nonnull
    public String getUsername() {
        return username;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (!(o instanceof SshCredentials)) {
            return false;
        }

        SshCredentials that = (SshCredentials) o;

        return Objects.equal(publicKey, that.publicKey)
                && Objects.equal(username, that.username);
    }

    @Override
    public int hashCode() {
        return Objects.hashCode(publicKey, username);
    }

    @Override
    public String toString() {
        return MoreObjects.toStringHelper(this)
                .add("username", username)
                .add("publicKey", publicKey)
                .toString();
    }
}
