From 88a046379f82bdb5a22c10b89485d079e78c7ffe Mon Sep 17 00:00:00 2001 From: anhefti Date: Thu, 30 Mar 2023 12:13:22 +0200 Subject: [PATCH] Code cleanup --- .../servicelayer/dao/CertificateDAO.java | 47 ++++++++++++++++++- .../dao/impl/CertificateDAOImpl.java | 7 +-- .../impl/CertificateServiceImpl.java | 9 +++- 3 files changed, 55 insertions(+), 8 deletions(-) diff --git a/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/dao/CertificateDAO.java b/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/dao/CertificateDAO.java index 964c08e4..121e453f 100644 --- a/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/dao/CertificateDAO.java +++ b/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/dao/CertificateDAO.java @@ -22,29 +22,74 @@ import ch.ethz.seb.sebserver.webservice.servicelayer.bulkaction.BulkActionSuppor /** Concrete EntityDAO interface of Certificate entities */ public interface CertificateDAO extends BulkActionSupportDAO { + /** Get the certificate with given alias for specified institution. + * + * @param institutionId Institution identifier + * @param alias the alias name of the certificate to get + * @return Result refer to the Certificate or to an error when happened. */ Result getCertificate(final Long institutionId, String alias); + /** Get all certificates of a given institution, + * + * @param institutionId Institution identifier + * @return Result to the Certificates or to an error when happend */ Result getCertificates(Long institutionId); + /** Add a new uploaded certificate to the certificate store of the institution. + * + * @param institutionId Institution identifier + * @param alias the alias name of the institution + * @param certificate the certificate to add. + * @return Result refer to the generated CertificateInfo or to an error when happened */ Result addCertificate( Long institutionId, String alias, Certificate certificate); + /** Add a new uploaded certificate with private key to the certificate store of the institution. + * + * @param institutionId Institution identifier + * @param alias the alias name of the institution + * @param certificate the certificate to add. + * @param privateKey the private key of the certificate + * @return Result refer to the generated CertificateInfo or to an error when happened */ Result addCertificate( Long institutionId, String alias, Certificate certificate, PrivateKey privateKey); + /** Removes specified certificate from the certificate store of a given institution. + * + * @param institutionId The institution identifier + * @param alias the alias name of the certificate + * @return Result refer to the entity key of the removed certificate or to an error when happened */ Result removeCertificate(Long institutionId, String alias); + /** Get all alias names of all certificated that exists for a given institution. + * + * @param institutionId The institution identifier + * @return Result refer to the collection of all certificate alias names or to an error when happened */ Result> getAllIdentityAlias(Long institutionId); + /** Get the certification information for a specific certificate from the the given Certificates. + * + * @param certificates The certificates bucket to get the info from + * @param alias the alias name of the certificate to get the info from + * @return Result refer to the certificate info or to an error when happened. */ Result getDataFromCertificate(Certificates certificates, String alias); + /** Get a collection of all alias names of all identity certificates for a given institution. + * + * @param institutionId The institution identifier + * @return Result refer to the collection of certificate alias or to an error when happened */ Result> getIdentityAlias(Long institutionId); - String extractAlias(X509Certificate a, String alias); + /** Get or extract the alias name of a given certificate. If there is not given a explicit alias name + * within the certificate, this will create one generic from the data that is available. + * + * @param certificate The X509Certificate to extract the alias name from + * @return the extracted alias */ + String extractAlias(X509Certificate certificate); } diff --git a/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/dao/impl/CertificateDAOImpl.java b/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/dao/impl/CertificateDAOImpl.java index 9a3cd86e..18cc1579 100644 --- a/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/dao/impl/CertificateDAOImpl.java +++ b/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/dao/impl/CertificateDAOImpl.java @@ -180,7 +180,7 @@ public class CertificateDAOImpl implements CertificateDAO { final X509Certificate cert = certificate; return new CertificateInfo( - extractAlias(cert, alias), + StringUtils.isNotBlank(alias) ? alias : extractAlias(cert), new DateTime(cert.getNotBefore()), new DateTime(cert.getNotAfter()), getTypes(certificates, cert)); @@ -224,10 +224,7 @@ public class CertificateDAOImpl implements CertificateDAO { } @Override - public String extractAlias(final X509Certificate certificate, final String alias) { - if (StringUtils.isNotBlank(alias)) { - return alias; - } + public String extractAlias(final X509Certificate certificate) { try { final X500Name x500name = new JcaX509CertificateHolder(certificate).getSubject(); diff --git a/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/institution/impl/CertificateServiceImpl.java b/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/institution/impl/CertificateServiceImpl.java index 6bf62af3..abe763b4 100644 --- a/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/institution/impl/CertificateServiceImpl.java +++ b/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/institution/impl/CertificateServiceImpl.java @@ -41,6 +41,7 @@ import ch.ethz.seb.sebserver.webservice.servicelayer.dao.CertificateDAO; import ch.ethz.seb.sebserver.webservice.servicelayer.dao.FilterMap; import ch.ethz.seb.sebserver.webservice.servicelayer.dao.SEBClientConfigDAO; import ch.ethz.seb.sebserver.webservice.servicelayer.institution.CertificateService; +import io.micrometer.core.instrument.util.StringUtils; @Lazy @Service @@ -97,14 +98,18 @@ public class CertificateServiceImpl implements CertificateService { return loadCertFromPEM(in) .flatMap(cert -> this.certificateDAO.addCertificate( institutionId, - this.certificateDAO.extractAlias(cert, alias), + StringUtils.isNotBlank(alias) + ? alias + : this.certificateDAO.extractAlias(cert), cert)); case PKCS12: return loadCertFromPKC(in, password) .flatMap(pair -> this.certificateDAO.addCertificate( institutionId, - this.certificateDAO.extractAlias(pair.a, alias), + StringUtils.isNotBlank(alias) + ? alias + : this.certificateDAO.extractAlias(pair.a), pair.a, pair.b)); default: