AbstractRESTMessageSecuritySender.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.HashMap;
  22. import java.util.Map;

  23. import org.openspcoop2.message.OpenSPCoop2RestMessage;
  24. import org.openspcoop2.message.constants.MessageRole;
  25. import org.openspcoop2.security.SecurityException;
  26. import org.openspcoop2.security.message.constants.SecurityConstants;
  27. import org.openspcoop2.utils.io.Base64Utilities;

  28. /**
  29.  * AbstractRESTMessageSecuritySender
  30.  *
  31.  * @author Andrea Poli (apoli@link.it)
  32.  * @author $Author$
  33.  * @version $Rev$, $Date$
  34.  */
  35. public abstract class AbstractRESTMessageSecuritySender implements IMessageSecuritySender{

  36.    
  37.    
  38.    
  39.     // *** DETACHED UTILS ***
  40.    
  41.     protected void setDetachedSignatureInMessage(Map<String,Object> map, OpenSPCoop2RestMessage<?> restMessage, String descriptionEngine, String detachedSignatureParam) throws SecurityException {
  42.        
  43.         String signatureDetachedHeader = null;
  44.         String signatureDetachedPropertyUrl = null;
  45.        
  46.         String mode = (String) map.get(SecurityConstants.SIGNATURE_MODE);
  47.        
  48.         boolean base64Detached = SecurityConstants.SIGNATURE_DETACHED_BASE64_DEFAULT;
  49.         String tmpBase64 = (String) map.get(SecurityConstants.SIGNATURE_DETACHED_BASE64);
  50.         if(tmpBase64!=null && !"".equals(tmpBase64)) {
  51.             try {
  52.                 base64Detached = Boolean.parseBoolean(tmpBase64);
  53.             }catch(Exception e) {
  54.                 throw new SecurityException(descriptionEngine+" (mode:"+mode+" message-role:"+restMessage.getMessageRole()+") property '"+SecurityConstants.SIGNATURE_DETACHED_BASE64+
  55.                         "' with wrong value (expected "+SecurityConstants.SIGNATURE_DETACHED_BASE64_TRUE+"/"+SecurityConstants.SIGNATURE_DETACHED_BASE64_FALSE+"): "+e.getMessage(),e);
  56.             }
  57.         }
  58.         String detachedSignature = null;
  59.         if(base64Detached) {
  60.             detachedSignature = Base64Utilities.encodeAsString(detachedSignatureParam.getBytes());
  61.         }
  62.         else {
  63.             detachedSignature = detachedSignatureParam;
  64.         }
  65.        
  66.         signatureDetachedHeader = (String) map.get(SecurityConstants.SIGNATURE_DETACHED_HEADER);
  67.         if(signatureDetachedHeader==null || "".equals(signatureDetachedHeader.trim())){
  68.             signatureDetachedHeader=null; // normalizzo
  69.             if(MessageRole.REQUEST.equals(restMessage.getMessageRole())) {
  70.                 signatureDetachedPropertyUrl = (String) map.get(SecurityConstants.SIGNATURE_DETACHED_PROPERTY_URL);
  71.                 if(signatureDetachedPropertyUrl==null || "".equals(signatureDetachedPropertyUrl.trim())){
  72.                     signatureDetachedPropertyUrl=null; // normalizzo
  73.                     throw new SecurityException(descriptionEngine+" (mode:"+mode+" message-role:"+restMessage.getMessageRole()+") require '"+SecurityConstants.SIGNATURE_DETACHED_HEADER+"'/'"+SecurityConstants.SIGNATURE_DETACHED_PROPERTY_URL+"' property");
  74.                 }
  75.                 else {
  76.                     if(restMessage.getTransportRequestContext()==null) {
  77.                         throw new SecurityException(descriptionEngine+" (mode:"+mode+" message-role:"+restMessage.getMessageRole()+") property url '"+signatureDetachedPropertyUrl+"'; transporto context undefined");
  78.                     }
  79.                     if(restMessage.getTransportRequestContext().getParameters()==null) {
  80.                         restMessage.getTransportRequestContext().setParameters(new HashMap<>());
  81.                     }
  82.                     restMessage.getTransportRequestContext().removeParameter(signatureDetachedPropertyUrl); // sovrascrivo
  83.                     //restMessage.getTransportRequestContext().getParametersFormBased().put(signatureDetachedPropertyUrl, detachedSignature);
  84.                     restMessage.forceUrlProperty(signatureDetachedPropertyUrl, detachedSignature);
  85.                 }
  86.             }
  87.             else {
  88.                 throw new SecurityException(descriptionEngine+" (mode:"+mode+" message-role:"+restMessage.getMessageRole()+") require '"+SecurityConstants.SIGNATURE_DETACHED_HEADER+"' property");
  89.             }
  90.         }
  91.         else {
  92.             if(MessageRole.REQUEST.equals(restMessage.getMessageRole())) {
  93.                 if(restMessage.getTransportRequestContext()==null) {
  94.                     throw new SecurityException(descriptionEngine+" (mode:"+mode+" message-role:"+restMessage.getMessageRole()+") property url '"+signatureDetachedPropertyUrl+"'; transporto context undefined");
  95.                 }
  96.                 if(restMessage.getTransportRequestContext().getHeaders()==null) {
  97.                     restMessage.getTransportRequestContext().setHeaders(new HashMap<>());
  98.                 }
  99.                 restMessage.getTransportRequestContext().removeHeader(signatureDetachedHeader); // sovrascrivo
  100.                 //restMessage.getTransportRequestContext().getParametersTrasporto().put(signatureDetachedHeader, detachedSignature);
  101.                 restMessage.forceTransportHeader(signatureDetachedHeader, detachedSignature);
  102.             }
  103.             else {
  104.                 if(restMessage.getTransportResponseContext()==null) {
  105.                     throw new SecurityException(descriptionEngine+" (mode:"+mode+" message-role:"+restMessage.getMessageRole()+") property url '"+signatureDetachedPropertyUrl+"'; transporto context undefined");
  106.                 }
  107.                 if(restMessage.getTransportResponseContext().getHeaders()==null) {
  108.                     restMessage.getTransportResponseContext().setHeaders(new HashMap<>());
  109.                 }
  110.                 restMessage.getTransportResponseContext().removeHeader(signatureDetachedHeader); // sovrascrivo
  111.                 //restMessage.getTransportResponseContext().getParametersTrasporto().put(signatureDetachedHeader, detachedSignature);
  112.                 restMessage.forceTransportHeader(signatureDetachedHeader, detachedSignature);
  113.             }
  114.             if(detachedSignature==null) {
  115.                 throw new SecurityException(descriptionEngine+" (mode:"+mode+" message-role:"+restMessage.getMessageRole()+") header '"+signatureDetachedHeader+"' not found");
  116.             }
  117.         }
  118.     }
  119.    
  120. }