AbstractRESTMessageSecurityReceiver.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.security.message;

  21. import java.util.List;
  22. import java.util.Map;

  23. import javax.xml.namespace.QName;

  24. import org.openspcoop2.message.OpenSPCoop2RestMessage;
  25. import org.openspcoop2.message.OpenSPCoop2SoapMessage;
  26. import org.openspcoop2.message.constants.MessageRole;
  27. import org.openspcoop2.message.soap.reference.Reference;
  28. import org.openspcoop2.security.SecurityException;
  29. import org.openspcoop2.security.message.constants.SecurityConstants;
  30. import org.openspcoop2.utils.io.Base64Utilities;

  31. /**
  32.  * AbstractRESTMessageSecurityReceiver
  33.  *
  34.  * @author Andrea Poli (apoli@link.it)
  35.  * @author $Author$
  36.  * @version $Rev$, $Date$
  37.  */
  38. public abstract class AbstractRESTMessageSecurityReceiver implements IMessageSecurityReceiver{

  39.     @Override
  40.     public boolean checkExistsWSSecurityHeader() {
  41.         return false;
  42.     }

  43.     @Override
  44.     public List<Reference> getDirtyElements(MessageSecurityContext messageSecurityContext,
  45.             OpenSPCoop2SoapMessage message) throws SecurityException {
  46.         return null;
  47.     }

  48.     @Override
  49.     public Map<QName, QName> checkEncryptSignatureParts(MessageSecurityContext messageSecurityContext,
  50.             List<Reference> elementsToClean, OpenSPCoop2SoapMessage message, List<SubErrorCodeSecurity> codiciErrore)
  51.             throws SecurityException {
  52.         return null;
  53.     }

  54.     @Override
  55.     public void checkEncryptionPartElements(Map<QName, QName> notResolved, OpenSPCoop2SoapMessage message,
  56.             List<SubErrorCodeSecurity> erroriRilevati) throws SecurityException {
  57.        
  58.     }

  59.     @Override
  60.     public void cleanDirtyElements(MessageSecurityContext messageSecurityContext, OpenSPCoop2SoapMessage message,
  61.             List<Reference> elementsToClean, boolean detachHeaderWSSecurity, boolean removeAllIdRef)
  62.             throws SecurityException {
  63.        
  64.     }

  65.    
  66.    
  67.    
  68.     // *** DETACHED UTILS ***
  69.    
  70.     private String signatureDetachedHeader = null;
  71.     private String signatureDetachedPropertyUrl = null;
  72.    
  73.     protected String readDetachedSignatureFromMessage(Map<String,Object> map, OpenSPCoop2RestMessage<?> restMessage, String descriptionEngine) throws SecurityException {
  74.         String detachedSignature = null;
  75.         String mode = (String) map.get(SecurityConstants.SIGNATURE_MODE);
  76.         this.signatureDetachedHeader = (String) map.get(SecurityConstants.SIGNATURE_DETACHED_HEADER);
  77.         if(this.signatureDetachedHeader==null || "".equals(this.signatureDetachedHeader.trim())){
  78.             this.signatureDetachedHeader=null; // normalizzo
  79.             if(MessageRole.REQUEST.equals(restMessage.getMessageRole())) {
  80.                 this.signatureDetachedPropertyUrl = (String) map.get(SecurityConstants.SIGNATURE_DETACHED_PROPERTY_URL);
  81.                 if(this.signatureDetachedPropertyUrl==null || "".equals(this.signatureDetachedPropertyUrl.trim())){
  82.                     this.signatureDetachedPropertyUrl=null; // normalizzo
  83.                     throw new SecurityException(descriptionEngine+" (mode:"+mode+" message-role:"+restMessage.getMessageRole()+") require '"+SecurityConstants.SIGNATURE_DETACHED_HEADER+"'/'"+SecurityConstants.SIGNATURE_DETACHED_PROPERTY_URL+"' property");
  84.                 }
  85.                 else {
  86.                     if(restMessage.getTransportRequestContext()==null || restMessage.getTransportRequestContext().getParameters()==null ||
  87.                             restMessage.getTransportRequestContext().getParameters().size()<=0) {
  88.                         throw new SecurityException(descriptionEngine+" (mode:"+mode+" message-role:"+restMessage.getMessageRole()+") property url '"+this.signatureDetachedPropertyUrl+"' not found (properties url not exists)");
  89.                     }
  90.                     detachedSignature = restMessage.getTransportRequestContext().getParameterFirstValue(this.signatureDetachedPropertyUrl);
  91.                     if(detachedSignature==null) {
  92.                         throw new SecurityException(descriptionEngine+" (mode:"+mode+" message-role:"+restMessage.getMessageRole()+") property url '"+this.signatureDetachedPropertyUrl+"' not found");
  93.                     }
  94.                 }
  95.             }
  96.             else {
  97.                 throw new SecurityException(descriptionEngine+" (mode:"+mode+" message-role:"+restMessage.getMessageRole()+") require '"+SecurityConstants.SIGNATURE_DETACHED_HEADER+"' property");
  98.             }
  99.         }
  100.         else {
  101.             if(MessageRole.REQUEST.equals(restMessage.getMessageRole())) {
  102.                 if(restMessage.getTransportRequestContext()==null || restMessage.getTransportRequestContext().getHeaders()==null ||
  103.                         restMessage.getTransportRequestContext().getHeaders().size()<=0) {
  104.                     throw new SecurityException(descriptionEngine+" (mode:"+mode+" message-role:"+restMessage.getMessageRole()+") header '"+this.signatureDetachedHeader+"' not found (header empty)");
  105.                 }
  106.                 detachedSignature = restMessage.getTransportRequestContext().getHeaderFirstValue(this.signatureDetachedHeader);
  107.             }
  108.             else {
  109.                 if(restMessage.getTransportResponseContext()==null || restMessage.getTransportResponseContext().getHeaders()==null ||
  110.                         restMessage.getTransportResponseContext().getHeaders().size()<=0) {
  111.                     throw new SecurityException(descriptionEngine+" (mode:"+mode+" message-role:"+restMessage.getMessageRole()+") header '"+this.signatureDetachedHeader+"' not found (header empty)");
  112.                 }
  113.                 detachedSignature = restMessage.getTransportResponseContext().getHeaderFirstValue(this.signatureDetachedHeader);
  114.             }
  115.             if(detachedSignature==null) {
  116.                 throw new SecurityException(descriptionEngine+" (mode:"+mode+" message-role:"+restMessage.getMessageRole()+") header '"+this.signatureDetachedHeader+"' not found");
  117.             }
  118.         }
  119.        
  120.         boolean base64Detached = SecurityConstants.SIGNATURE_DETACHED_BASE64_DEFAULT;
  121.         String tmpBase64 = (String) map.get(SecurityConstants.SIGNATURE_DETACHED_BASE64);
  122.         if(tmpBase64!=null && !"".equals(tmpBase64)) {
  123.             try {
  124.                 base64Detached = Boolean.parseBoolean(tmpBase64);
  125.             }catch(Exception e) {
  126.                 throw new SecurityException(descriptionEngine+" (mode:"+mode+" message-role:"+restMessage.getMessageRole()+") property '"+SecurityConstants.SIGNATURE_DETACHED_BASE64+
  127.                         "' with wrong value (expected "+SecurityConstants.SIGNATURE_DETACHED_BASE64_TRUE+"/"+SecurityConstants.SIGNATURE_DETACHED_BASE64_FALSE+"): "+e.getMessage(),e);
  128.             }
  129.         }
  130.         if(base64Detached) {
  131.             return new String(Base64Utilities.decode(detachedSignature));
  132.         }
  133.         else {
  134.             return detachedSignature;
  135.         }
  136.     }
  137.    
  138.     protected void deleteDetachedSignatureFromMessage(OpenSPCoop2RestMessage<?> restMessage, String descriptionEngine) throws SecurityException {
  139.         if(this.signatureDetachedHeader!=null) {
  140.             if(MessageRole.REQUEST.equals(restMessage.getMessageRole())) {
  141.                 if(restMessage.getTransportRequestContext()==null || restMessage.getTransportRequestContext().getHeaders()==null ||
  142.                         restMessage.getTransportRequestContext().getHeaders().size()<=0) {
  143.                     return;
  144.                 }
  145.                 restMessage.getTransportRequestContext().removeHeader(this.signatureDetachedHeader);
  146.             }
  147.             else {
  148.                 if(restMessage.getTransportResponseContext()==null || restMessage.getTransportResponseContext().getHeaders()==null ||
  149.                         restMessage.getTransportResponseContext().getHeaders().size()<=0) {
  150.                     return;
  151.                 }
  152.                 restMessage.getTransportResponseContext().removeHeader(this.signatureDetachedHeader);
  153.             }
  154.         }
  155.         else if(this.signatureDetachedPropertyUrl!=null) {
  156.             if(restMessage.getTransportRequestContext()==null || restMessage.getTransportRequestContext().getParameters()==null ||
  157.                     restMessage.getTransportRequestContext().getParameters().size()<=0) {
  158.                 return;
  159.             }
  160.             restMessage.getTransportRequestContext().removeParameter(this.signatureDetachedPropertyUrl);
  161.         }
  162.         else {
  163.             throw new SecurityException(descriptionEngine+"; this method required preProcess detached signature");          
  164.         }
  165.     }
  166. }