package de.mlessmann.certassist.models;
import com.fasterxml.jackson.annotation.JsonProperty;
import de.mlessmann.certassist.web.JsonIsoOffsetDate;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.persistence.*;
import jakarta.validation.constraints.Min;
import jakarta.validation.constraints.NotNull;
import lombok.*;
import org.hibernate.proxy.HibernateProxy;
import java.time.OffsetDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
@Entity
@Table(uniqueConstraints = {@UniqueConstraint(columnNames = {"fingerprint"})})
@Getter
@Setter
@ToString
@RequiredArgsConstructor
public class Certificate {
@Id
@GeneratedValue(strategy = GenerationType.UUID)
@Setter(AccessLevel.NONE)
private String id;
@NotNull
@Enumerated(EnumType.STRING)
@JsonProperty
private CertificateType type;
@JsonProperty
private String trustingAuthority;
/**
*
* - -1 = no requested key length is known (might happen with imported certificates)
* - 0 = no key is available for this certificate (might happen with trusted third party certificates)
* - > 1 = The key length in bits used for the private key of this certificate
*
*/
@Min(-1)
private int requestedKeyLength;
@JsonIsoOffsetDate
private OffsetDateTime notBefore;
@JsonIsoOffsetDate
private OffsetDateTime notAfter;
@NotNull
@JsonProperty
private String subjectCommonName;
@JsonProperty
private String subjectEmailAddress;
@JsonProperty
private String subjectOrganization;
@JsonProperty
private String subjectOrganizationalUnit;
@JsonProperty
private String subjectCountry;
@JsonProperty
private String subjectState;
@JsonProperty
private String subjectLocality;
@OneToMany(cascade = CascadeType.ALL, orphanRemoval = true)
@ToString.Exclude
private List certificateExtension = new ArrayList<>();
//@Lob - Cannot annotate column: https://github.com/xerial/sqlite-jdbc/issues/135
@Column(nullable = false)
private byte[] cert = new byte[0];
//@Lob - Cannot annotate column: https://github.com/xerial/sqlite-jdbc/issues/135
@Column
private byte[] privateKey = new byte[0];
//@Lob - Cannot annotate column: https://github.com/xerial/sqlite-jdbc/issues/135
@Column
private byte[] fullchain;
@Column(nullable = false)
@JsonProperty
@Schema(description = "The certificate fingerprint. The algorithm used to derive the fingerprint is determined by OpenSSL")
private String fingerprint;
@Override
public final boolean equals(Object o) {
if (this == o) return true;
if (o == null) return false;
Class> oEffectiveClass = o instanceof HibernateProxy
? ((HibernateProxy) o).getHibernateLazyInitializer().getPersistentClass()
: o.getClass();
Class> thisEffectiveClass = this instanceof HibernateProxy
? ((HibernateProxy) this).getHibernateLazyInitializer().getPersistentClass()
: this.getClass();
if (thisEffectiveClass != oEffectiveClass) return false;
Certificate that = (Certificate) o;
return getId() != null && Objects.equals(getId(), that.getId());
}
@Override
public final int hashCode() {
return this instanceof HibernateProxy
? ((HibernateProxy) this).getHibernateLazyInitializer().getPersistentClass().hashCode()
: getClass().hashCode();
}
}