WSSecurityUtils.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.message.soap;

  21. import java.util.List;

  22. import javax.xml.soap.SOAPElement;
  23. import javax.xml.soap.SOAPHeader;
  24. import javax.xml.soap.SOAPPart;

  25. import org.apache.wss4j.common.WSS4JConstants;
  26. import org.openspcoop2.message.constants.MessageType;
  27. import org.openspcoop2.message.exception.MessageException;
  28. import org.openspcoop2.message.exception.MessageNotSupportedException;
  29. import org.w3c.dom.Attr;

  30. /**
  31.  * Libreria contenente metodi utili per la gestione WSSecurity.
  32.  *
  33.  *
  34.  * @author Poli Andrea (apoli@link.it)
  35.  * @author $Author$
  36.  * @version $Rev$, $Date$
  37.  */

  38. public class WSSecurityUtils {

  39.     public static SOAPElement getSecurityHeader(SOAPPart soapPart, MessageType msgType, String actor, boolean throwExceptionIfFoundMoreSecurityHeader) throws MessageException, MessageNotSupportedException {
  40.        
  41.         SOAPHeader hdr = null;
  42.         try {
  43.             hdr = soapPart!=null && soapPart.getEnvelope()!=null ?  soapPart.getEnvelope().getHeader() : null;
  44.         }catch(Exception e) {
  45.             throw new MessageException(e.getMessage(),e);
  46.         }
  47.         if (hdr == null) {
  48.             return null;
  49.         }
  50.        
  51.         boolean soap12 = MessageType.SOAP_12.equals(msgType);
  52.        
  53.         String actorLocal = WSS4JConstants.ATTR_ACTOR;
  54.         String soapNamespace = WSS4JConstants.URI_SOAP11_ENV;
  55.         if (soap12) {
  56.             actorLocal = WSS4JConstants.ATTR_ROLE;
  57.             soapNamespace = WSS4JConstants.URI_SOAP12_ENV;
  58.         }

  59.         //
  60.         // Iterate through the security headers
  61.         //
  62.         SOAPElement foundSecurityHeader = null;
  63.         List<SOAPElement> childs = SoapUtils.getNotEmptyChildSOAPElement(hdr);
  64.         if(childs!=null && !childs.isEmpty()) {
  65.             for (SOAPElement currentChild : childs) {
  66.                 if (WSS4JConstants.WSSE_LN.equals(currentChild.getLocalName())
  67.                         && (WSS4JConstants.WSSE_NS.equals(currentChild.getNamespaceURI())
  68.                             || WSS4JConstants.OLD_WSSE_NS.equals(currentChild.getNamespaceURI()))) {

  69.                         Attr attr = currentChild.getAttributeNodeNS(soapNamespace, actorLocal);
  70.                         String hActor = (attr != null) ? attr.getValue() : null;

  71.                         if (isActorEqual(actor, hActor)) {
  72.                             if (foundSecurityHeader != null) {
  73.                                 throw new MessageException("Two or more security headers have the same actor name: '"+actor+"'");
  74.                             }
  75.                             foundSecurityHeader = currentChild;
  76.                             if(!throwExceptionIfFoundMoreSecurityHeader) {
  77.                                 break; // un header trovato
  78.                             }
  79.                         }
  80.                     }
  81.             }
  82.         }
  83.        
  84.         return foundSecurityHeader;
  85.     }
  86.    
  87.     private static boolean isActorEqual(String actor, String hActor) {
  88.         if ((hActor == null || hActor.length() == 0)
  89.             && (actor == null || actor.length() == 0)) {
  90.             return true;
  91.         }

  92.         if (hActor != null && actor != null && hActor.equalsIgnoreCase(actor)) {
  93.             return true;
  94.         }

  95.         return false;
  96.     }
  97.    
  98. }