// Copyright 2018 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.hosted.provision.restapi.v2.filter;

import java.security.Principal;
import java.security.cert.X509Certificate;
import java.util.List;
import java.util.Optional;

/**
 * Represents the identity of a hosted Vespa node
 *
 * @author bjorncs
 */
public class NodePrincipal implements Principal {
    private final String identityName;
    private final String hostname;
    private final List<X509Certificate> clientCertificateChain;
    private final Type type;

    public static NodePrincipal withAthenzIdentity(String identityName,
                                                   List<X509Certificate> clientCertificateChain) {
        return withAthenzIdentity(identityName, null, clientCertificateChain);
    }

    public static NodePrincipal withAthenzIdentity(String identityName,
                                                   String hostname,
                                                   List<X509Certificate> clientCertificateChain) {
        return new NodePrincipal(identityName, hostname, clientCertificateChain, Type.ATHENZ);
    }

    public static NodePrincipal withLegacyIdentity(String hostname,
                                                   List<X509Certificate> clientCertificateChain) {
        return new NodePrincipal(hostname, hostname, clientCertificateChain, Type.LEGACY);
    }

    private NodePrincipal(String identityName,
                         String hostname,
                         List<X509Certificate> clientCertificateChain,
                         Type type) {
        this.identityName = identityName;
        this.hostname = hostname;
        this.clientCertificateChain = clientCertificateChain;
        this.type = type;
    }

    public String getHostIdentityName() {
        return identityName;
    }

    public Optional<String> getHostname() {
        return Optional.ofNullable(hostname);
    }

    public List<X509Certificate> getClientCertificateChain() {
        return clientCertificateChain;
    }

    public Type getType() {
        return type;
    }

    @Override
    public String getName() {
        return identityName;
    }

    public enum Type { ATHENZ, LEGACY }

}

@@ -271,16 +271,42 @@
 l.List;%0A
+import java.util.Objects;%0A
 import j
@@ -2316,10 +2316,757 @@
 GACY %7D%0A%0A
+    @Override%0A    public boolean equals(Object o) %7B%0A        if (this == o) return true;%0A        if (o == null %7C%7C getClass() != o.getClass()) return false;%0A        NodePrincipal principal = (NodePrincipal) o;%0A        return Objects.equals(identityName, principal.identityName) &&%0A                Objects.equals(hostname, principal.hostname) &&%0A                type == principal.type;%0A    %7D%0A%0A    @Override%0A    public int hashCode() %7B%0A        return Objects.hash(identityName, hostname, type);%0A    %7D%0A%0A    @Override%0A    public String toString() %7B%0A        return %22NodePrincipal%7B%22 +%0A                %22identityName='%22 + identityName + '%5C'' +%0A                %22, hostname='%22 + hostname + '%5C'' +%0A                %22, type=%22 + type +%0A                '%7D';%0A    %7D%0A
 %7D%0A

Implement toString, equals and hashCode for NodePrincipal