-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathWebEidAuthenticationTest.java
More file actions
113 lines (97 loc) · 7.01 KB
/
WebEidAuthenticationTest.java
File metadata and controls
113 lines (97 loc) · 7.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/*
* Copyright (c) 2020-2025 Estonian Information System Authority
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package eu.webeid.example.security;
import eu.webeid.security.certificate.CertificateLoader;
import java.math.BigInteger;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.time.LocalDate;
import java.time.ZoneId;
import java.util.Date;
import org.bouncycastle.asn1.x500.X500Name;
import org.bouncycastle.asn1.x500.X500NameBuilder;
import org.bouncycastle.asn1.x500.style.BCStyle;
import org.bouncycastle.cert.jcajce.JcaX509CertificateConverter;
import org.bouncycastle.cert.jcajce.JcaX509v3CertificateBuilder;
import org.bouncycastle.operator.jcajce.JcaContentSignerBuilder;
import org.junit.jupiter.api.Test;
import org.springframework.security.core.Authentication;
import java.security.cert.X509Certificate;
import java.util.Collections;
import static org.assertj.core.api.Assertions.assertThat;
class WebEidAuthenticationTest {
private static final String ORGANIZATION_CERT = "MIIF2zCCA8OgAwIBAgIQJs4xyGoNzixjYmV9gUjYljANBgkqhkiG9w0BAQsFADCBjjELMAkGA1UEBhMCRUUxIjAgBgNVBAoMGUFTIFNlcnRpZml0c2VlcmltaXNrZXNrdXMxITAfBgNVBAsMGFNlcnRpZml0c2VlcmltaXN0ZWVudXNlZDEXMBUGA1UEYQwOTlRSRUUtMTA3NDcwMTMxHzAdBgNVBAMMFlRFU1Qgb2YgS0xBU1MzLVNLIDIwMTYwHhcNMjIxMTAyMTI0MTA0WhcNMjUxMjAxMTI0MTA0WjB7MREwDwYDVQQFEwgxMjI3NjI3OTERMA8GA1UECAwISGFyanVtYWExEDAOBgNVBAcMB1RhbGxpbm4xCzAJBgNVBAYTAkVFMRAwDgYDVQQKDAdUVFQgT8OcMSIwIAYDVQQDDBlUZXN0aWphZC5lZSBpc2lrdXR1dmFzdHVzMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAzSV4zydk5WY2AuUJ50lNpH3q2C+WH0dE/wqq4nFqpNYkyzFNHecFDFlU0YcpPrhFKDZfJtaAP/drvmdqaVdAcCGIPnXhZ+01pCvmlebe7//kQXaZ6ZHS3EAtwy0EBsVVOMapw1kC58YYymlJhTrdzDFrqjdgv1t1Ph9Gkg/PhaHvqGtKp3IY+v33EwxEV3nPIhZHHC/d0YnzVaN5QiSHbU+mRt8+d2vHPNPNY3qVDh8MPOrJIDeIHp9oSS1+FF4crnvfxmg99d7zemsSstR8/SX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dIyOg7/o+WK3RczvMjjT8X8F4XKo8JPjZBYyGBx5XkqhwVrX3SjEmRPFdcvy+glYRoTslgM2fsj5fSNxCIsq1fQN8yVjYnxk8/X53AsorcpWpLMHxtoxT+YvNZzryY00QjS5kgUQBNmFaU";
@Test
void whenOrganizationCertificate_thenSucceeds() throws Exception {
final X509Certificate certificate = CertificateLoader.decodeCertificateFromBase64(ORGANIZATION_CERT);
final Authentication authentication = WebEidAuthentication.fromCertificate(certificate, Collections.emptyList());
assertThat(authentication.getPrincipal()).isEqualTo("Testijad.ee isikutuvastus");
}
@Test
void whenPersonHasGivenNameAndSurnamePresent_thenBothAreReturned() throws Exception {
final Authentication authentication = createMockAuthenticationUsingCertificateWithSubject("JAAK-KRISTJAN", "JÕEORG");
assertThat(authentication.getPrincipal()).isEqualTo("JAAK-KRISTJAN JÕEORG");
}
@Test
void whenPersonHasOnlyOneNameAsGivenName_thenGivenNameIsReturned() throws Exception {
final Authentication authentication = createMockAuthenticationUsingCertificateWithSubject("JÕEORG", "-");
assertThat(authentication.getPrincipal()).isEqualTo("JÕEORG");
}
@Test
void whenPersonHasOnlyOneNameAsSurname_thenSurnameIsReturned() throws Exception {
final Authentication authentication = createMockAuthenticationUsingCertificateWithSubject(null, "JÕEORG");
assertThat(authentication.getPrincipal()).isEqualTo("JÕEORG");
}
private Authentication createMockAuthenticationUsingCertificateWithSubject(String givenName, String surname) throws Exception {
return WebEidAuthentication.fromCertificate(createMockCertificateForSubject(givenName, surname), Collections.emptyList());
}
private X509Certificate createMockCertificateForSubject(String givenName, String surname) throws Exception {
KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA");
keyGen.initialize(2048);
KeyPair keyPair = keyGen.generateKeyPair();
X500NameBuilder x500NameBuilder = new X500NameBuilder(BCStyle.INSTANCE)
.addRDN(BCStyle.SERIALNUMBER, "38001085718")
.addRDN(BCStyle.SURNAME, surname);
if (givenName != null) {
x500NameBuilder
.addRDN(BCStyle.GIVENNAME, givenName)
.addRDN(BCStyle.CN, "%s,%s,38001085718".formatted(surname, givenName));
} else {
x500NameBuilder
.addRDN(BCStyle.CN, "%s,38001085718".formatted(surname));
}
X500Name subject = x500NameBuilder.build();
LocalDate start = LocalDate.now().minusDays(1);
LocalDate end = LocalDate.now().plusDays(1);
return new JcaX509CertificateConverter()
.getCertificate(
new JcaX509v3CertificateBuilder(
subject,
BigInteger.ONE,
Date.from(start.atStartOfDay(ZoneId.of("Europe/Tallinn")).toInstant()),
Date.from(end.atStartOfDay(ZoneId.of("Europe/Tallinn")).toInstant()),
subject,
keyPair.getPublic()
).build(new JcaContentSignerBuilder("SHA256withRSA").build(keyPair.getPrivate()))
);
}
}