HashGenerator.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.pdd.core.response_caching;

  21. import java.io.ByteArrayOutputStream;
  22. import java.security.MessageDigest;
  23. import java.util.ArrayList;
  24. import java.util.Collections;
  25. import java.util.HashMap;
  26. import java.util.Iterator;
  27. import java.util.List;
  28. import java.util.Map;

  29. import org.openspcoop2.core.config.ResponseCachingConfigurazione;
  30. import org.openspcoop2.core.config.ResponseCachingConfigurazioneHashGenerator;
  31. import org.openspcoop2.core.config.constants.StatoFunzionalita;
  32. import org.openspcoop2.core.config.constants.StatoFunzionalitaCacheDigestQueryParameter;
  33. import org.openspcoop2.message.OpenSPCoop2Message;
  34. import org.openspcoop2.message.constants.ServiceBinding;
  35. import org.openspcoop2.protocol.sdk.state.RequestInfo;
  36. import org.openspcoop2.utils.io.Base64Utilities;
  37. import org.openspcoop2.utils.transport.TransportUtils;

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

  46.     private String algoritmo;
  47.    
  48.     public HashGenerator(String algoritmo) {
  49.         // MD5, SHA-1, SHA-256
  50.         this.algoritmo = algoritmo;
  51.     }
  52.    
  53.     // NOTA:
  54.     // La generazione dell'hash deve essere fatta all'inizio prima che il messaggio venga modificato.
  55.     // L'hash deve essere calcolato, se il response caching รจ abilitato, subito dopo i vari controlli di auth e rateLimiting ma prima degli handler di out ed imbustamento
  56.     // In modo che si calcola sulla richiesta in ingresso effettiva.
  57.    
  58.     // NOTA2: Mascherare con una doppia entry in cache per non ritornare hash
  59.    
  60.     public String buildKeyCache(OpenSPCoop2Message message, RequestInfo requestInfo, ResponseCachingConfigurazione responseCachingConfig) throws Exception {
  61.        
  62.         MessageDigest digest  = MessageDigest.getInstance(this.algoritmo);
  63.        
  64.         StringBuilder sb = new StringBuilder();
  65.         sb.append("interfaceName").append("=").append(requestInfo.getProtocolContext().getInterfaceName());
  66.         sb.append("\n").append("function").append("=").append(requestInfo.getProtocolContext().getFunction());
  67.         boolean printAzione = true;
  68.         sb.append("\n").append("idServizio").append("=").append(requestInfo.getIdServizio().toString(printAzione));
  69.         digest.update(sb.toString().getBytes());
  70.         //System.out.println("TESTa: "+sb.toString());
  71.        
  72.         ResponseCachingConfigurazioneHashGenerator configHash = responseCachingConfig.getHashGenerator();
  73.         if(configHash==null) {
  74.             configHash = new ResponseCachingConfigurazioneHashGenerator(); // utilizzo i valori di default
  75.         }
  76.        
  77.         if(configHash!=null) {
  78.            
  79.             if(StatoFunzionalita.ABILITATO.equals(configHash.getRequestUri())) {
  80.                
  81.                 // I parametri vengono riordinati proprio per far si differenze nell'ordine non impattano nel digest
  82.                
  83.                 sb = new StringBuilder();
  84.                 sb.append("requestType").append("=").append(requestInfo.getProtocolContext().getRequestType());
  85.                 sb.append("\nrequestURI").append("=").append(requestInfo.getProtocolContext().getRequestURI());
  86.                 digest.update(sb.toString().getBytes());
  87.                 //System.out.println("TESTb: "+sb.toString());
  88.                
  89.             }
  90.            
  91.             if(StatoFunzionalitaCacheDigestQueryParameter.ABILITATO.equals(configHash.getQueryParameters()) ||
  92.                     StatoFunzionalitaCacheDigestQueryParameter.SELEZIONE_PUNTUALE.equals(configHash.getQueryParameters())) {
  93.                
  94.                 // I parametri vengono riordinati proprio per far si differenze nell'ordine non impattano nel digest
  95.                
  96.                 sb = new StringBuilder();
  97.                 sb.append("ParametriURL");
  98.                 if(StatoFunzionalitaCacheDigestQueryParameter.ABILITATO.equals(configHash.getQueryParameters())) {
  99.                     this.addList(requestInfo.getProtocolContext().getParameters(), false, sb);
  100.                 }
  101.                 else {
  102.                     Map<String, List<String>> pUrlForDigest = new HashMap<>();
  103.                     if(requestInfo.getProtocolContext().getParameters()!=null && configHash.sizeQueryParameterList()>0) {
  104.                         for (String queryParameter : configHash.getQueryParameterList()) {
  105.                             List<String> v = requestInfo.getProtocolContext().getParameterValues(queryParameter);
  106.                             if(v!=null && !v.isEmpty()) {
  107.                                 pUrlForDigest.put(queryParameter, v);
  108.                             }
  109.                         }
  110.                     }
  111.                    
  112.                     if(!pUrlForDigest.isEmpty()) {
  113.                         this.addList(pUrlForDigest, false, sb);
  114.                     }
  115.                 }
  116.                 digest.update(sb.toString().getBytes());
  117.                 //System.out.println("TESTb: "+sb.toString());
  118.                
  119.             }
  120.            
  121.             if(StatoFunzionalita.ABILITATO.equals(configHash.getHeaders())) {
  122.                
  123.                 // Gli header vengono riordinati e le chiavi vengono prese lowerCase proprio per far si che tali differenze non impattano nel digest
  124.                
  125.                 Map<String, List<String>> pTrasportoForDigest = new HashMap<>();
  126.                 if(requestInfo.getProtocolContext().getHeaders()!=null && configHash.sizeHeaderList()>0) {
  127.                     for (String header : configHash.getHeaderList()) {
  128.                         List<String> v = requestInfo.getProtocolContext().getHeaderValues(header);
  129.                         if(v!=null && !v.isEmpty()) {
  130.                             pTrasportoForDigest.put(header, v);
  131.                         }
  132.                     }
  133.                 }
  134.                
  135.                 if(!pTrasportoForDigest.isEmpty()) {
  136.                     sb = new StringBuilder("HEADER");
  137.                     this.addList(pTrasportoForDigest, true, sb);
  138.                     digest.update(sb.toString().getBytes());
  139.                     //System.out.println("TESTb: "+sb.toString());
  140.                 }
  141.                
  142.             }
  143.            
  144.             if(StatoFunzionalita.ABILITATO.equals(configHash.getPayload())) {

  145.                 boolean doDigest = true;
  146.                 message.saveChanges();
  147.                 if(ServiceBinding.REST.equals(message.getServiceBinding())) {
  148.                     doDigest = message.castAsRest().hasContent();
  149.                 }
  150.                
  151.                 if(doDigest) {
  152.                     ByteArrayOutputStream bout = new ByteArrayOutputStream();
  153.                     message.writeTo(bout, false);
  154.                     bout.flush();
  155.                     bout.close();
  156.                     digest.update(bout.toByteArray());
  157.                     //System.out.println("TESTd: "+bout.toString());
  158.                 }
  159.             }
  160.            
  161.         }
  162.        
  163.         return Base64Utilities.encodeAsString(digest.digest());
  164.     }
  165.    
  166.     private void addList(Map<String, List<String>> p, boolean toLowerCase, StringBuilder sb) {
  167.         if(p!=null &&
  168.                 !p.isEmpty()) {
  169.             List<String> sortKeys = new ArrayList<>();
  170.             Iterator<String> keys = p.keySet().iterator();
  171.             while (keys.hasNext()) {
  172.                 String key = (String) keys.next();
  173.                 sortKeys.add(key);
  174.             }
  175.             Collections.sort(sortKeys);
  176.             for (String sortKey : sortKeys) {
  177.                 List<String> values = TransportUtils.getRawObject(p, sortKey);
  178.                 List<String> ordinatedValues = new ArrayList<>();
  179.                 ordinatedValues.addAll(values);
  180.                 if(ordinatedValues.size()>1) {
  181.                     Collections.sort(ordinatedValues);
  182.                 }
  183.                 String key = sortKey;
  184.                 if(toLowerCase) {
  185.                     key = key.toLowerCase();
  186.                 }
  187.                 if(ordinatedValues!=null && !ordinatedValues.isEmpty()) {
  188.                     for (String value : ordinatedValues) {
  189.                         sb.append("\n").append(key).append("=").append(value);      
  190.                     }
  191.                 }
  192.             }

  193.         }
  194.     }
  195.    
  196. }