View Javadoc
1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements. See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership. The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License. You may obtain a copy of the License at
9    *
10   * http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied. See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  
20  package org.apache.wss4j.dom.message;
21  
22  import org.apache.wss4j.common.util.SOAPUtil;
23  import org.apache.wss4j.dom.WSConstants;
24  
25  import org.apache.wss4j.dom.engine.WSSConfig;
26  import org.apache.wss4j.dom.engine.WSSecurityEngine;
27  
28  import org.junit.jupiter.api.Test;
29  import org.apache.wss4j.common.crypto.Crypto;
30  import org.apache.wss4j.common.crypto.CryptoFactory;
31  import org.apache.wss4j.common.util.XMLUtils;
32  import org.w3c.dom.Document;
33  
34  /**
35   * This is a test for WSS-60 - "Problems when SOAP envelope namespace prefix is null"
36   * http://issues.apache.org/jira/browse/WSS-60
37   */
38  public class NoSoapPrefixSignatureTest {
39      private static final org.slf4j.Logger LOG =
40          org.slf4j.LoggerFactory.getLogger(NoSoapPrefixSignatureTest.class);
41      private WSSecurityEngine secEngine = new WSSecurityEngine();
42      private Crypto crypto;
43  
44      public NoSoapPrefixSignatureTest() throws Exception {
45          WSSConfig.init();
46          crypto = CryptoFactory.getInstance();
47      }
48  
49      /**
50       * Test signing a SOAP message that has no SOAP namespace prefix
51       */
52      @Test
53      public void testNoSOAPNamespacePrefix() throws Exception {
54          Document doc = SOAPUtil.toSOAPPart(SOAPUtil.SAMPLE_SOAP_MSG);
55          WSSecHeader secHeader = new WSSecHeader(doc);
56          secHeader.setActor("bob");
57          secHeader.insertSecurityHeader();
58  
59          WSSecSignature sign = new WSSecSignature(secHeader);
60          sign.setUserInfo("16c73ab6-b892-458f-abf5-2f875f74882e", "security");
61          sign.setKeyIdentifierType(WSConstants.BST_DIRECT_REFERENCE);
62  
63          Document signedDoc = sign.build(crypto);
64  
65          if (LOG.isDebugEnabled()) {
66              String outputString =
67                  XMLUtils.prettyDocumentToString(signedDoc);
68              LOG.debug(outputString);
69          }
70          verify(signedDoc);
71      }
72  
73      /**
74       * Verifies the soap envelope
75       * <p/>
76       *
77       * @param doc
78       * @throws Exception Thrown when there is a problem in verification
79       */
80      private void verify(Document doc) throws Exception {
81          secEngine.processSecurityHeader(doc, null, null, crypto);
82          if (LOG.isDebugEnabled()) {
83              LOG.debug("Verfied and decrypted message:");
84              String outputString =
85                  XMLUtils.prettyDocumentToString(doc);
86              LOG.debug(outputString);
87          }
88      }
89  
90  }