MessageSecurityUtilities.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.Map;

  22. import org.openspcoop2.security.message.constants.SecurityConstants;

  23. /**
  24.  * MessageSecurityUtilities
  25.  *
  26.  * @author Poli Andrea (apoli@link.it)
  27.  * @author $Author$
  28.  * @version $Rev$, $Date$
  29.  */
  30. public class MessageSecurityUtilities {
  31.    
  32.     private MessageSecurityUtilities() {}

  33.     public static boolean processSOAPFault(Map<String,Object> messageSecurityProperties){
  34.         // Default disabilitati
  35.         boolean processEncryptSOAPFault = false;
  36.         boolean processSignatureSOAPFault = false;
  37.         boolean processUsernameTokenFault = false;
  38.         boolean processSAMLTokenFault = false;
  39.        
  40.         String propertyEncrypt = (String) messageSecurityProperties.get(SecurityConstants.ENCRYPTION_SOAP_FAULT);
  41.         if(propertyEncrypt!=null &&
  42.             SecurityConstants.TRUE.equalsIgnoreCase(propertyEncrypt)){
  43.             processEncryptSOAPFault = true;
  44.         }
  45.        
  46.         String propertySignature = (String) messageSecurityProperties.get(SecurityConstants.SIGNATURE_SOAP_FAULT);
  47.         if(propertySignature!=null &&
  48.             SecurityConstants.TRUE.equalsIgnoreCase(propertySignature)){
  49.             processSignatureSOAPFault = true;
  50.         }
  51.        
  52.         String propertyUsernameToken = (String) messageSecurityProperties.get(SecurityConstants.USERNAME_TOKEN_SOAP_FAULT);
  53.         if(propertyUsernameToken!=null &&
  54.             SecurityConstants.TRUE.equalsIgnoreCase(propertyUsernameToken)){
  55.             processUsernameTokenFault = true;
  56.         }
  57.        
  58.         String propertySAMLToken = (String) messageSecurityProperties.get(SecurityConstants.SAML_TOKEN_SOAP_FAULT);
  59.         if(propertySAMLToken!=null &&
  60.             SecurityConstants.TRUE.equalsIgnoreCase(propertySAMLToken)){
  61.             processSAMLTokenFault = true;
  62.         }
  63.        
  64.         return processFaultEngine(messageSecurityProperties,
  65.                 processEncryptSOAPFault, processSignatureSOAPFault,
  66.                 processUsernameTokenFault, processSAMLTokenFault);
  67.     }
  68.     public static boolean processProblemDetails(Map<String,Object> messageSecurityProperties){
  69.         // Default disabilitati
  70.         boolean processEncryptProblemDetails = false;
  71.         boolean processSignatureProblemDetails = false;
  72.         boolean processUsernameTokenProblemDetails = false;
  73.         boolean processSAMLTokenProblemDetails = false;
  74.        
  75.         String propertyEncrypt = (String) messageSecurityProperties.get(SecurityConstants.ENCRYPTION_PROBLEM_DETAILS);
  76.         if(propertyEncrypt!=null &&
  77.             SecurityConstants.TRUE.equalsIgnoreCase(propertyEncrypt)){
  78.             processEncryptProblemDetails = true;
  79.         }
  80.        
  81.         String propertySignature = (String) messageSecurityProperties.get(SecurityConstants.SIGNATURE_PROBLEM_DETAILS);
  82.         if(propertySignature!=null &&
  83.             SecurityConstants.TRUE.equalsIgnoreCase(propertySignature)){
  84.             processSignatureProblemDetails = true;
  85.         }
  86.        
  87.         String propertyUsernameToken = (String) messageSecurityProperties.get(SecurityConstants.USERNAME_TOKEN_PROBLEM_DETAILS);
  88.         if(propertyUsernameToken!=null &&
  89.             SecurityConstants.TRUE.equalsIgnoreCase(propertyUsernameToken)){
  90.             processUsernameTokenProblemDetails = true;
  91.         }
  92.        
  93.         String propertySAMLToken = (String) messageSecurityProperties.get(SecurityConstants.SAML_TOKEN_PROBLEM_DETAILS);
  94.         if(propertySAMLToken!=null &&
  95.             SecurityConstants.TRUE.equalsIgnoreCase(propertySAMLToken)){
  96.             processSAMLTokenProblemDetails = true;
  97.         }
  98.        
  99.         return processFaultEngine(messageSecurityProperties,
  100.                 processEncryptProblemDetails, processSignatureProblemDetails,
  101.                 processUsernameTokenProblemDetails, processSAMLTokenProblemDetails);
  102.     }
  103.     private static boolean processFaultEngine(Map<String,Object> messageSecurityProperties,
  104.             boolean processEncryptFault, boolean processSignatureFault,
  105.             boolean processUsernameTokenFault, boolean processSAMLTokenFault){
  106.                
  107.         String action = (String) messageSecurityProperties.remove(SecurityConstants.ACTION);
  108.        
  109.         String [] splitActions = action.split(" ");
  110.         StringBuilder bfNewActions = new StringBuilder();
  111.         for (int i = 0; i < splitActions.length; i++) {
  112.            
  113.             String a = splitActions[i].trim();
  114.            
  115.             if(SecurityConstants.isActionEncryption(a) ||
  116.                     SecurityConstants.isActionDecryption(a)){
  117.                 if(processEncryptFault){
  118.                     if(bfNewActions.length()>0){
  119.                         bfNewActions.append(" ");
  120.                     }
  121.                     bfNewActions.append(a);
  122.                 }
  123.             }
  124.             else if(SecurityConstants.SIGNATURE_ACTION.equals(a)){
  125.                 if(processSignatureFault){
  126.                     if(bfNewActions.length()>0){
  127.                         bfNewActions.append(" ");
  128.                     }
  129.                     bfNewActions.append(a);
  130.                 }
  131.             }
  132.             else if(SecurityConstants.isActionUsernameToken(a) ){
  133.                 if(processUsernameTokenFault){
  134.                     if(bfNewActions.length()>0){
  135.                         bfNewActions.append(" ");
  136.                     }
  137.                     bfNewActions.append(a);
  138.                 }
  139.             }
  140.             else if(SecurityConstants.isActionSAMLToken(a) ){
  141.                 if(processSAMLTokenFault){
  142.                     if(bfNewActions.length()>0){
  143.                         bfNewActions.append(" ");
  144.                     }
  145.                     bfNewActions.append(a);
  146.                 }
  147.             }
  148.             else{
  149.                 // altra azione la aggiungo
  150.                 if(bfNewActions.length()>0){
  151.                     bfNewActions.append(" ");
  152.                 }
  153.                 bfNewActions.append(a);
  154.             }
  155.         }
  156.        
  157.        
  158.         if(bfNewActions.length()>0){
  159.             String newActions = bfNewActions.toString();
  160.             if(SecurityConstants.TIMESTAMP_ACTION.equals(newActions)){
  161.                 // se rimane solo TIMESTAMP disabilitiamo
  162.                 return false;
  163.             }else{
  164.                 messageSecurityProperties.put(SecurityConstants.ACTION,newActions);
  165.                 return true;
  166.             }
  167.         }
  168.         else{
  169.             return false;
  170.         }
  171.     }
  172.    
  173.    
  174. }