Skip to content

Commit 0470d72

Browse files
committed
Use Optional in CertificateData
WE2-931 Signed-off-by: Mart Somermaa <[email protected]>
1 parent 495ab8a commit 0470d72

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
@@ -2,37 +2,33 @@
22

33
import org.junit.jupiter.api.Test;
44

5-
import java.security.cert.CertificateEncodingException;
65
import java.security.cert.X509Certificate;
76

87
import static eu.webeid.security.testutil.Certificates.getOrganizationCert;
98
import static org.assertj.core.api.Assertions.assertThat;
10-
import static org.assertj.core.api.Assertions.assertThatThrownBy;
119

1210
class CertificateDataTest {
1311

1412
@Test
1513
void whenOrganizationCertificate_thenSubjectCNAndIdCodeAndCountryCodeExtractionSucceeds() throws Exception {
1614
final X509Certificate organizationCert = getOrganizationCert();
1715

18-
assertThat(CertificateData.getSubjectCN(organizationCert))
16+
assertThat(CertificateData.getSubjectCN(organizationCert).orElseThrow())
1917
.isEqualTo("Testijad.ee isikutuvastus");
20-
assertThat(CertificateData.getSubjectIdCode(organizationCert))
18+
assertThat(CertificateData.getSubjectIdCode(organizationCert).orElseThrow())
2119
.isEqualTo("12276279");
22-
assertThat(CertificateData.getSubjectCountryCode(organizationCert))
20+
assertThat(CertificateData.getSubjectCountryCode(organizationCert).orElseThrow())
2321
.isEqualTo("EE");
2422
}
2523

2624
@Test
27-
void whenOrganizationCertificate_thenSubjectGivenNameAndSurnameExtractionFails() throws Exception {
25+
void whenOrganizationCertificate_thenSubjectGivenNameAndSurnameAreEmpty() throws Exception {
2826
final X509Certificate organizationCert = getOrganizationCert();
2927

30-
assertThatThrownBy(() -> CertificateData.getSubjectGivenName(organizationCert))
31-
.isInstanceOf(CertificateEncodingException.class)
32-
.hasMessage("X500 name RDNs empty or first element is null");
33-
assertThatThrownBy(() -> CertificateData.getSubjectSurname(organizationCert))
34-
.isInstanceOf(CertificateEncodingException.class)
35-
.hasMessage("X500 name RDNs empty or first element is null");
28+
assertThat(CertificateData.getSubjectGivenName(organizationCert))
29+
.isEmpty();
30+
assertThat(CertificateData.getSubjectSurname(organizationCert))
31+
.isEmpty();
3632
}
3733

3834
}

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)