ModISOAPSecurity.java

  1. /*
  2.  * GovWay - A customizable API Gateway
  3.  * https://govway.org
  4.  *
  5.  * Copyright (c) 2005-2025 Link.it srl (https://link.it).
  6.  *
  7.  * This program is free software: you can redistribute it and/or modify
  8.  * it under the terms of the GNU General Public License version 3, as published by
  9.  * the Free Software Foundation.
  10.  *
  11.  * This program is distributed in the hope that it will be useful,
  12.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.  * GNU General Public License for more details.
  15.  *
  16.  * You should have received a copy of the GNU General Public License
  17.  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  18.  *
  19.  */

  20. package org.openspcoop2.protocol.modipa.validator;

  21. import java.util.List;

  22. import javax.xml.namespace.QName;
  23. import javax.xml.soap.SOAPBody;
  24. import javax.xml.soap.SOAPEnvelope;
  25. import javax.xml.soap.SOAPHeader;
  26. import javax.xml.soap.SOAPHeaderElement;

  27. import org.openspcoop2.message.OpenSPCoop2Message;
  28. import org.openspcoop2.message.OpenSPCoop2MessageFactory;
  29. import org.openspcoop2.message.OpenSPCoop2SoapMessage;
  30. import org.openspcoop2.message.constants.MessageRole;
  31. import org.openspcoop2.message.constants.MessageType;
  32. import org.openspcoop2.message.exception.MessageException;
  33. import org.openspcoop2.message.exception.MessageNotSupportedException;
  34. import org.openspcoop2.message.soap.reference.Reference;
  35. import org.openspcoop2.message.soap.wsaddressing.WSAddressingHeader;
  36. import org.openspcoop2.security.SecurityException;
  37. import org.openspcoop2.security.message.MessageSecurityContext;
  38. import org.openspcoop2.security.message.wss4j.MessageSecurityReceiver_wss4j;

  39. /**
  40.  * ModISOAPSecurity
  41.  *
  42.  * @author Poli Andrea (apoli@link.it)
  43.  * @author $Author$
  44.  * @version $Rev$, $Date$
  45.  */
  46. public class ModISOAPSecurity {

  47.     private SOAPHeaderElement securityHeader;
  48.     private WSAddressingHeader wsAddressingHeader;
  49.     private SOAPHeaderElement requestDigestHeader;
  50.     private String wsuIdBodyRef;
  51.    
  52.     private MessageSecurityReceiver_wss4j wss4jSignature;
  53.     private MessageSecurityContext messageSecurityContext;
  54.     private List<Reference> elementsToClean;
  55.    
  56.     public SOAPHeaderElement getSecurityHeader() {
  57.         return this.securityHeader;
  58.     }
  59.     public void setSecurityHeader(SOAPHeaderElement securityHeader) {
  60.         this.securityHeader = securityHeader;
  61.     }
  62.     public WSAddressingHeader getWsAddressingHeader() {
  63.         return this.wsAddressingHeader;
  64.     }
  65.     public void setWsAddressingHeader(WSAddressingHeader wsAddressingHeader) {
  66.         this.wsAddressingHeader = wsAddressingHeader;
  67.     }
  68.     public SOAPHeaderElement getRequestDigestHeader() {
  69.         return this.requestDigestHeader;
  70.     }
  71.     public void setRequestDigestHeader(SOAPHeaderElement requestDigestHeader) {
  72.         this.requestDigestHeader = requestDigestHeader;
  73.     }
  74.     public String getWsuIdBodyRef() {
  75.         return this.wsuIdBodyRef;
  76.     }
  77.     public void setWsuIdBodyRef(String wsuIdBodyRef) {
  78.         this.wsuIdBodyRef = wsuIdBodyRef;
  79.     }
  80.     public List<Reference> getElementsToClean() {
  81.         return this.elementsToClean;
  82.     }
  83.     public void setElementsToClean(List<Reference> elementsToClean) {
  84.         this.elementsToClean = elementsToClean;
  85.     }
  86.     public MessageSecurityReceiver_wss4j getWss4jSignature() {
  87.         return this.wss4jSignature;
  88.     }
  89.     public void setWss4jSignature(MessageSecurityReceiver_wss4j wss4jSignature) {
  90.         this.wss4jSignature = wss4jSignature;
  91.     }
  92.     public MessageSecurityContext getMessageSecurityContext() {
  93.         return this.messageSecurityContext;
  94.     }
  95.     public void setMessageSecurityContext(MessageSecurityContext messageSecurityContext) {
  96.         this.messageSecurityContext = messageSecurityContext;
  97.     }
  98.    
  99.     public void clean(OpenSPCoop2SoapMessage soapMessage) throws SecurityException, MessageException, MessageNotSupportedException {
  100.         boolean detachValue = true; // per default l'header WSS viene eliminato
  101.         boolean removeAllIdRefValue = true;
  102.         this.wss4jSignature.cleanDirtyElements(this.messageSecurityContext, soapMessage, this.elementsToClean, detachValue, removeAllIdRefValue);
  103.        
  104.         if(this.wsAddressingHeader!=null || this.requestDigestHeader!=null) {
  105.            
  106.             SOAPHeader header = soapMessage.getSOAPHeader();
  107.            
  108.             if(header!=null) {
  109.                
  110.                 if(this.wsAddressingHeader!=null) {
  111.                     if(this.wsAddressingHeader.getTo()!=null) {
  112.                         removeHeader(header, this.wsAddressingHeader.getTo());
  113.                     }
  114.                     if(this.wsAddressingHeader.getFrom()!=null) {
  115.                         removeHeader(header, this.wsAddressingHeader.getFrom());
  116.                     }
  117.                     if(this.wsAddressingHeader.getAction()!=null) {
  118.                         removeHeader(header, this.wsAddressingHeader.getAction());
  119.                     }
  120.                     if(this.wsAddressingHeader.getId()!=null) {
  121.                         removeHeader(header, this.wsAddressingHeader.getId());
  122.                     }
  123.                     if(this.wsAddressingHeader.getRelatesTo()!=null) {
  124.                         removeHeader(header, this.wsAddressingHeader.getRelatesTo());
  125.                     }
  126.                     if(this.wsAddressingHeader.getReplyTo()!=null) {
  127.                         removeHeader(header, this.wsAddressingHeader.getReplyTo());
  128.                     }
  129.                     if(this.wsAddressingHeader.getFaultTo()!=null) {
  130.                         removeHeader(header, this.wsAddressingHeader.getFaultTo());
  131.                     }
  132.                 }
  133.                
  134.                 if(this.requestDigestHeader!=null) {
  135.                     removeHeader(header, this.requestDigestHeader);
  136.                 }
  137.             }
  138.         }
  139.     }
  140.     private void removeHeader(SOAPHeader hdr, SOAPHeaderElement soapHeader) {

  141.         //hdr.removeChild(soapHeader);
  142.         // FIX: NOT_FOUND_ERR: An attempt is made to reference a node in a context where it does not exist.
  143.         // Succede in determinate condizione di utilizzo dell'header ottimizzato e della bufferizzazione read only
  144.        
  145.         // cerco la busta per il namespace
  146.         java.util.Iterator<?> it = hdr.examineAllHeaderElements();
  147.         while( it.hasNext()  ){

  148.             // Test Header Element
  149.             SOAPHeaderElement headerElementCheck = (SOAPHeaderElement) it.next();

  150. //          // Controllo Actor
  151. //          if(soapHeader.getActor()!=null) {
  152. //              if(headerElementCheck.getActor()==null) {
  153. //                  continue;
  154. //              }
  155. //              if(!soapHeader.getActor().equals(headerElementCheck.getActor())) {
  156. //                  continue;
  157. //              }
  158. //          }
  159. //          else {
  160. //              if(headerElementCheck.getActor()!=null) {
  161. //                  continue;
  162. //              }
  163. //          }
  164. //          // Controllo role
  165. //          if(soapHeader.getRole()!=null) {
  166. //              if(headerElementCheck.getRole()==null) {
  167. //                  continue;
  168. //              }
  169. //              if(!soapHeader.getRole().equals(headerElementCheck.getRole())) {
  170. //                  continue;
  171. //              }
  172. //          }
  173. //          else {
  174. //              if(headerElementCheck.getRole()!=null) {
  175. //                  continue;
  176. //              }
  177. //          }
  178.            
  179.             //Controllo Namespace
  180.             if(soapHeader.getNamespaceURI()!=null) {
  181.                 if(headerElementCheck.getNamespaceURI()==null) {
  182.                     continue;
  183.                 }
  184.                 if(!soapHeader.getNamespaceURI().equals(headerElementCheck.getNamespaceURI())) {
  185.                     continue;
  186.                 }
  187.             }
  188.             else {
  189.                 if(headerElementCheck.getNamespaceURI()!=null) {
  190.                     continue;
  191.                 }
  192.             }
  193.            
  194.             //Controllo LocalName
  195.             if(soapHeader.getLocalName()!=null) {
  196.                 if(headerElementCheck.getLocalName()==null) {
  197.                     continue;
  198.                 }
  199.                 if(!soapHeader.getLocalName().equals(headerElementCheck.getLocalName())) {
  200.                     continue;
  201.                 }
  202.             }
  203.             else {
  204.                 if(headerElementCheck.getLocalName()!=null) {
  205.                     continue;
  206.                 }
  207.             }
  208.            
  209.             //Controllo Prefix
  210.             if(soapHeader.getPrefix()!=null) {
  211.                 if(headerElementCheck.getPrefix()==null) {
  212.                     continue;
  213.                 }
  214.                 if(!soapHeader.getPrefix().equals(headerElementCheck.getPrefix())) {
  215.                     continue;
  216.                 }
  217.             }
  218.             else {
  219.                 if(headerElementCheck.getPrefix()!=null) {
  220.                     continue;
  221.                 }
  222.             }
  223.            
  224.            
  225.             // found
  226.             hdr.removeChild(headerElementCheck);
  227.            
  228.             break;
  229.         }
  230.     }
  231.    
  232.     public SOAPEnvelope buildTraccia(MessageType type) throws MessageException {
  233.        
  234.         try {
  235.            
  236.             OpenSPCoop2Message msg = OpenSPCoop2MessageFactory.getDefaultMessageFactory().createEmptyMessage(type, MessageRole.NONE);
  237.             OpenSPCoop2SoapMessage soapMessage = msg.castAsSoap();
  238.            
  239.             SOAPHeader header = soapMessage.getSOAPHeader();
  240.             if(header==null) {
  241.                 header = soapMessage.getSOAPPart().getEnvelope().addHeader();
  242.             }
  243.            
  244.             boolean add = false;
  245.            
  246.             if(this.securityHeader!=null) {
  247.                 header.addChildElement(this.securityHeader);
  248.                 add = true;
  249.             }
  250.            
  251.             if(this.wsAddressingHeader!=null) {
  252.                 if(this.wsAddressingHeader.getTo()!=null) {
  253.                     header.addChildElement(this.wsAddressingHeader.getTo());
  254.                     add = true;
  255.                 }
  256.                 if(this.wsAddressingHeader.getFrom()!=null) {
  257.                     header.addChildElement(this.wsAddressingHeader.getFrom());
  258.                     add = true;
  259.                 }
  260.                 if(this.wsAddressingHeader.getAction()!=null) {
  261.                     header.addChildElement(this.wsAddressingHeader.getAction());
  262.                     add = true;
  263.                 }
  264.                 if(this.wsAddressingHeader.getId()!=null) {
  265.                     header.addChildElement(this.wsAddressingHeader.getId());
  266.                     add = true;
  267.                 }
  268.                 if(this.wsAddressingHeader.getRelatesTo()!=null) {
  269.                     header.addChildElement(this.wsAddressingHeader.getRelatesTo());
  270.                     add = true;
  271.                 }
  272.                 if(this.wsAddressingHeader.getReplyTo()!=null) {
  273.                     header.addChildElement(this.wsAddressingHeader.getReplyTo());
  274.                     add = true;
  275.                 }
  276.                 if(this.wsAddressingHeader.getFaultTo()!=null) {
  277.                     header.addChildElement(this.wsAddressingHeader.getFaultTo());
  278.                     add = true;
  279.                 }
  280.             }
  281.            
  282.             if(this.requestDigestHeader!=null) {
  283.                 header.addChildElement(this.requestDigestHeader);
  284.                 add = true;
  285.             }
  286.            
  287.             SOAPBody body = soapMessage.getSOAPBody();
  288.             if(body==null) {
  289.                 body = soapMessage.getSOAPPart().getEnvelope().addBody();
  290.             }
  291.             if(this.wsuIdBodyRef!=null) {
  292.                 QName qname = new QName("http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd", "Id", "wsu");
  293.                 body.addAttribute(qname, this.wsuIdBodyRef);
  294.             }
  295.             body.setTextContent("...");
  296.            
  297.             if(add) {
  298.                 return soapMessage.getSOAPPart().getEnvelope();
  299.             }
  300.             else {
  301.                 return null;
  302.             }
  303.            
  304.         }catch(Exception e) {
  305.             throw new MessageException(e.getMessage(), e);
  306.         }
  307.        
  308.     }
  309. }