Skip to content

Commit f8ce579

Browse files
committed
Use Optional in CertificateData
WE2-931 Signed-off-by: Mart Somermaa <[email protected]>
1 parent 78f68e3 commit f8ce579

File tree

3 files changed

+24
-27
lines changed

3 files changed

+24
-27
lines changed

src/main/java/eu/webeid/security/certificate/CertificateData.java

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,43 +32,44 @@
3232
import java.security.cert.CertificateEncodingException;
3333
import java.security.cert.X509Certificate;
3434
import java.util.Arrays;
35+
import java.util.Optional;
3536
import java.util.stream.Collectors;
3637

3738
public final class CertificateData {
3839

39-
public static String getSubjectCN(X509Certificate certificate) throws CertificateEncodingException {
40+
public static Optional<String> getSubjectCN(X509Certificate certificate) throws CertificateEncodingException {
4041
return getSubjectField(certificate, BCStyle.CN);
4142
}
4243

43-
public static String getSubjectSurname(X509Certificate certificate) throws CertificateEncodingException {
44+
public static Optional<String> getSubjectSurname(X509Certificate certificate) throws CertificateEncodingException {
4445
return getSubjectField(certificate, BCStyle.SURNAME);
4546
}
4647

47-
public static String getSubjectGivenName(X509Certificate certificate) throws CertificateEncodingException {
48+
public static Optional<String> getSubjectGivenName(X509Certificate certificate) throws CertificateEncodingException {
4849
return getSubjectField(certificate, BCStyle.GIVENNAME);
4950
}
5051

51-
public static String getSubjectIdCode(X509Certificate certificate) throws CertificateEncodingException {
52+
public static Optional<String> getSubjectIdCode(X509Certificate certificate) throws CertificateEncodingException {
5253
return getSubjectField(certificate, BCStyle.SERIALNUMBER);
5354
}
5455

55-
public static String getSubjectCountryCode(X509Certificate certificate) throws CertificateEncodingException {
56+
public static Optional<String> getSubjectCountryCode(X509Certificate certificate) throws CertificateEncodingException {
5657
return getSubjectField(certificate, BCStyle.C);
5758
}
5859

59-
private static String getSubjectField(X509Certificate certificate, ASN1ObjectIdentifier fieldId) throws CertificateEncodingException {
60+
private static Optional<String> getSubjectField(X509Certificate certificate, ASN1ObjectIdentifier fieldId) throws CertificateEncodingException {
6061
return getField(new JcaX509CertificateHolder(certificate).getSubject(), fieldId);
6162
}
6263

63-
private static String getField(X500Name x500Name, ASN1ObjectIdentifier fieldId) throws CertificateEncodingException {
64+
private static Optional<String> getField(X500Name x500Name, ASN1ObjectIdentifier fieldId) {
6465
// Example value: [C=EE, CN=JÕEORG\,JAAK-KRISTJAN\,38001085718, 2.5.4.4=#0c074ac395454f5247, 2.5.4.42=#0c0d4a41414b2d4b524953544a414e, 2.5.4.5=#1311504e4f45452d3338303031303835373138]
6566
final RDN[] rdns = x500Name.getRDNs(fieldId);
6667
if (rdns.length == 0 || rdns[0].getFirst() == null) {
67-
throw new CertificateEncodingException("X500 name RDNs empty or first element is null");
68+
return Optional.empty();
6869
}
69-
return Arrays.stream(rdns)
70+
return Optional.of(Arrays.stream(rdns)
7071
.map(rdn -> IETFUtils.valueToString(rdn.getFirst().getValue()))
71-
.collect(Collectors.joining(", "));
72+
.collect(Collectors.joining(", ")));
7273
}
7374

7475
private CertificateData() {

src/test/java/eu/webeid/security/certificate/CertificateDataTest.java

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,37 +23,33 @@
2323

2424
import org.junit.jupiter.api.Test;
2525

26-
import java.security.cert.CertificateEncodingException;
2726
import java.security.cert.X509Certificate;
2827

2928
import static eu.webeid.security.testutil.Certificates.getOrganizationCert;
3029
import static org.assertj.core.api.Assertions.assertThat;
31-
import static org.assertj.core.api.Assertions.assertThatThrownBy;
3230

3331
class CertificateDataTest {
3432

3533
@Test
3634
void whenOrganizationCertificate_thenSubjectCNAndIdCodeAndCountryCodeExtractionSucceeds() throws Exception {
3735
final X509Certificate organizationCert = getOrganizationCert();
3836

39-
assertThat(CertificateData.getSubjectCN(organizationCert))
37+
assertThat(CertificateData.getSubjectCN(organizationCert).orElseThrow())
4038
.isEqualTo("Testijad.ee isikutuvastus");
41-
assertThat(CertificateData.getSubjectIdCode(organizationCert))
39+
assertThat(CertificateData.getSubjectIdCode(organizationCert).orElseThrow())
4240
.isEqualTo("12276279");
43-
assertThat(CertificateData.getSubjectCountryCode(organizationCert))
41+
assertThat(CertificateData.getSubjectCountryCode(organizationCert).orElseThrow())
4442
.isEqualTo("EE");
4543
}
4644

4745
@Test
48-
void whenOrganizationCertificate_thenSubjectGivenNameAndSurnameExtractionFails() throws Exception {
46+
void whenOrganizationCertificate_thenSubjectGivenNameAndSurnameAreEmpty() throws Exception {
4947
final X509Certificate organizationCert = getOrganizationCert();
5048

51-
assertThatThrownBy(() -> CertificateData.getSubjectGivenName(organizationCert))
52-
.isInstanceOf(CertificateEncodingException.class)
53-
.hasMessage("X500 name RDNs empty or first element is null");
54-
assertThatThrownBy(() -> CertificateData.getSubjectSurname(organizationCert))
55-
.isInstanceOf(CertificateEncodingException.class)
56-
.hasMessage("X500 name RDNs empty or first element is null");
49+
assertThat(CertificateData.getSubjectGivenName(organizationCert))
50+
.isEmpty();
51+
assertThat(CertificateData.getSubjectSurname(organizationCert))
52+
.isEmpty();
5753
}
5854

5955
}

src/test/java/eu/webeid/security/validator/AuthTokenSignatureTest.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,15 +47,15 @@ class AuthTokenSignatureTest extends AbstractTestWithValidator {
4747
void whenValidTokenAndNonce_thenValidationSucceeds() throws Exception {
4848
final X509Certificate result = validator.validate(validAuthToken, VALID_CHALLENGE_NONCE);
4949

50-
assertThat(CertificateData.getSubjectCN(result))
50+
assertThat(CertificateData.getSubjectCN(result).orElseThrow())
5151
.isEqualTo("JÕEORG\\,JAAK-KRISTJAN\\,38001085718");
52-
assertThat(toTitleCase(CertificateData.getSubjectGivenName(result)))
52+
assertThat(toTitleCase(CertificateData.getSubjectGivenName(result).orElseThrow()))
5353
.isEqualTo("Jaak-Kristjan");
54-
assertThat(toTitleCase(CertificateData.getSubjectSurname(result)))
54+
assertThat(toTitleCase(CertificateData.getSubjectSurname(result).orElseThrow()))
5555
.isEqualTo("Jõeorg");
56-
assertThat(CertificateData.getSubjectIdCode(result))
56+
assertThat(CertificateData.getSubjectIdCode(result).orElseThrow())
5757
.isEqualTo("PNOEE-38001085718");
58-
assertThat(CertificateData.getSubjectCountryCode(result))
58+
assertThat(CertificateData.getSubjectCountryCode(result).orElseThrow())
5959
.isEqualTo("EE");
6060
}
6161

0 commit comments

Comments
 (0)