|
16 | 16 |
|
17 | 17 | package org.springframework.ws.soap.security.wss4j2;
|
18 | 18 |
|
| 19 | +import java.util.List; |
19 | 20 | import java.util.Properties;
|
| 21 | +import java.util.regex.Pattern; |
20 | 22 |
|
21 | 23 | import org.junit.jupiter.api.Test;
|
22 | 24 | import org.w3c.dom.Document;
|
|
28 | 30 | import org.springframework.ws.soap.security.wss4j2.support.CryptoFactoryBean;
|
29 | 31 |
|
30 | 32 | import static org.assertj.core.api.Assertions.assertThat;
|
| 33 | +import static org.assertj.core.api.Assertions.assertThatCode; |
| 34 | +import static org.assertj.core.api.Assertions.assertThatExceptionOfType; |
31 | 35 |
|
32 | 36 | public abstract class Wss4jMessageInterceptorSignTest extends Wss4jTest {
|
33 | 37 |
|
@@ -123,4 +127,37 @@ public void testSignResponseWithSignatureUser() throws Exception {
|
123 | 127 | "/SOAP-ENV:Envelope/SOAP-ENV:Header/wsse:Security/ds:Signature", document);
|
124 | 128 | }
|
125 | 129 |
|
| 130 | + @Test |
| 131 | + public void testValidateCertificateSubjectDnConstraintsShouldMatchSubject() throws Exception { |
| 132 | + SoapMessage message = createSignedTestSoapMessage(); |
| 133 | + MessageContext messageContext = getSoap11MessageContext(createSignedTestSoapMessage()); |
| 134 | + this.interceptor.secureMessage(message, messageContext); |
| 135 | + |
| 136 | + this.interceptor.setValidationActions("Signature"); |
| 137 | + this.interceptor.setValidationSubjectDnConstraints(List.of(Pattern.compile(".*"))); |
| 138 | + assertThatCode(() -> this.interceptor.validateMessage(message, messageContext)).doesNotThrowAnyException(); |
| 139 | + } |
| 140 | + |
| 141 | + @Test |
| 142 | + public void testValidateCertificateSubjectDnConstraintsShouldFailForNotMatchingSubject() throws Exception { |
| 143 | + SoapMessage message = createSignedTestSoapMessage(); |
| 144 | + MessageContext messageContext = getSoap11MessageContext(createSignedTestSoapMessage()); |
| 145 | + this.interceptor.secureMessage(message, messageContext); |
| 146 | + |
| 147 | + this.interceptor.setValidationActions("Signature"); |
| 148 | + this.interceptor.setValidationSubjectDnConstraints(List.of(Pattern.compile("O=Some Other Company"))); |
| 149 | + assertThatExceptionOfType(Wss4jSecurityValidationException.class) |
| 150 | + .isThrownBy(() -> this.interceptor.validateMessage(message, messageContext)) |
| 151 | + .withMessage("The security token could not be authenticated or authorized"); |
| 152 | + } |
| 153 | + |
| 154 | + private SoapMessage createSignedTestSoapMessage() throws Exception { |
| 155 | + this.interceptor.setSecurementActions("Signature"); |
| 156 | + this.interceptor.setSecurementSignatureKeyIdentifier("DirectReference"); |
| 157 | + this.interceptor.setUseSingleCertificate(false); |
| 158 | + this.interceptor.setSecurementPassword("123456"); |
| 159 | + this.interceptor.setSecurementUsername("testkey"); |
| 160 | + return loadSoap11Message("empty-soap.xml"); |
| 161 | + } |
| 162 | + |
126 | 163 | }
|
0 commit comments