1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.springframework.ws.soap.security.wss4j;
18
19 import java.text.DateFormat;
20 import java.text.SimpleDateFormat;
21
22 import org.springframework.ws.context.DefaultMessageContext;
23 import org.springframework.ws.context.MessageContext;
24 import org.springframework.ws.soap.SoapMessage;
25 import org.springframework.ws.soap.security.WsSecurityValidationException;
26
27 import org.junit.Test;
28 import org.w3c.dom.Document;
29
30 import static org.junit.Assert.assertEquals;
31
32 public abstract class Wss4jMessageInterceptorTimestampTestCase extends Wss4jTestCase {
33
34 @Test
35 public void testAddTimestamp() throws Exception {
36 Wss4jSecurityInterceptor interceptor = new Wss4jSecurityInterceptor();
37 interceptor.setSecurementActions("Timestamp");
38 interceptor.afterPropertiesSet();
39 SoapMessage message = loadSoap11Message("empty-soap.xml");
40 MessageContext context = getSoap11MessageContext(message);
41 interceptor.secureMessage(message, context);
42 Document document = getDocument(message);
43 assertXpathExists("timestamp header not found",
44 "/SOAP-ENV:Envelope/SOAP-ENV:Header/wsse:Security/wsu:Timestamp", document);
45 }
46
47 @Test
48 public void testValidateTimestamp() throws Exception {
49 Wss4jSecurityInterceptor interceptor = new Wss4jSecurityInterceptor();
50 interceptor.setValidationActions("Timestamp");
51 interceptor.afterPropertiesSet();
52 SoapMessage message = getMessageWithTimestamp();
53
54 MessageContext context = new DefaultMessageContext(message, getSoap11MessageFactory());
55 interceptor.validateMessage(message, context);
56 assertXpathNotExists("Security Header not removed", "/SOAP-ENV:Envelope/SOAP-ENV:Header/wsse:Security",
57 getDocument(message));
58 }
59
60 @Test(expected = WsSecurityValidationException.class)
61 public void testValidateTimestampWithExpiredTtl() throws Exception {
62 Wss4jSecurityInterceptor interceptor = new Wss4jSecurityInterceptor();
63 interceptor.setValidationActions("Timestamp");
64 interceptor.afterPropertiesSet();
65 SoapMessage message = loadSoap11Message("expiredTimestamp-soap.xml");
66 MessageContext context = new DefaultMessageContext(message, getSoap11MessageFactory());
67 interceptor.validateMessage(message, context);
68 }
69
70
71 @Test
72 public void testSecureTimestampWithCustomTtl() throws Exception {
73 Wss4jSecurityInterceptor interceptor = new Wss4jSecurityInterceptor();
74 interceptor.setSecurementActions("Timestamp");
75 interceptor.setTimestampStrict(true);
76 int ttlInSeconds = 1;
77 interceptor.setSecurementTimeToLive(ttlInSeconds);
78 interceptor.afterPropertiesSet();
79 SoapMessage message = loadSoap11Message("empty-soap.xml");
80 MessageContext context = new DefaultMessageContext(message, getSoap11MessageFactory());
81 interceptor.secureMessage(message, context);
82
83 String created = xpathTemplate.evaluateAsString("/SOAP-ENV:Envelope/SOAP-ENV:Header/wsse:Security/wsu:Timestamp/wsu:Created/text()",
84 message.getEnvelope().getSource());
85 String expires = xpathTemplate.evaluateAsString("/SOAP-ENV:Envelope/SOAP-ENV:Header/wsse:Security/wsu:Timestamp/wsu:Expires/text()",
86 message.getEnvelope().getSource());
87
88 DateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SS'Z'");
89
90 long actualTtl = format.parse(expires).getTime() - format.parse(created).getTime();
91 assertEquals("invalid ttl", 1000 * ttlInSeconds, actualTtl);
92 }
93
94 private SoapMessage getMessageWithTimestamp() throws Exception {
95 Wss4jSecurityInterceptor interceptor = new Wss4jSecurityInterceptor();
96 interceptor.setSecurementActions("Timestamp");
97 interceptor.afterPropertiesSet();
98 SoapMessage message = loadSoap11Message("empty-soap.xml");
99 MessageContext context = getSoap11MessageContext(message);
100 interceptor.secureMessage(message, context);
101 return message;
102 }
103 }