LockUtilities.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.web.lib.mvc.byok;

  21. import org.apache.commons.lang.StringEscapeUtils;
  22. import org.apache.commons.lang.StringUtils;
  23. import org.openspcoop2.core.byok.BYOKUtilities;
  24. import org.openspcoop2.pdd.core.byok.DriverBYOKUtilities;
  25. import org.openspcoop2.utils.UtilsException;
  26. import org.openspcoop2.utils.certificate.byok.BYOKManager;
  27. import org.openspcoop2.web.lib.mvc.DataElement;
  28. import org.openspcoop2.web.lib.mvc.DataElementType;

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

  37.     private DriverBYOKUtilities driverBYOKUtilities;
  38.     private boolean visualizzaInformazioniCifrate;
  39.     private boolean visualizzaCampiPasswordComeLock;
  40.     private String warningMessage;
  41.     private String servletNameSecretDecoder;
  42.     private String messaggioInformativoInformazioneNonCifrata;
  43.     private String messaggioInformativoInformazioneCifrataDifferenteSecurityPolicy;
  44.    
  45.    
  46.     public LockUtilities(DriverBYOKUtilities driverBYOKUtilities, boolean visualizzaInformazioniCifrate,
  47.             String warningMessage, String servletNameSecretDecoder,
  48.             String messaggioInformativoInformazioneNonCifrata, String messaggioInformativoInformazioneCifrataDifferenteSecurityPolicy, boolean visualizzaCampiPasswordComeLock) {
  49.         this.driverBYOKUtilities = driverBYOKUtilities;
  50.         this.visualizzaInformazioniCifrate = visualizzaInformazioniCifrate;
  51.         this.warningMessage = warningMessage;
  52.         this.servletNameSecretDecoder = servletNameSecretDecoder;
  53.         this.messaggioInformativoInformazioneNonCifrata = messaggioInformativoInformazioneNonCifrata;
  54.         this.messaggioInformativoInformazioneCifrataDifferenteSecurityPolicy = messaggioInformativoInformazioneCifrataDifferenteSecurityPolicy;
  55.         this.visualizzaCampiPasswordComeLock = visualizzaCampiPasswordComeLock;
  56.     }
  57.    
  58.    
  59.     public void lockProperty(DataElement de, String value) {
  60.         lockProperty(de, value, true);
  61.     }
  62.     public void lockProperty(DataElement de, String value, boolean escapeHtml) {
  63.         lockPropertyEngine(de, value, escapeHtml, false, false);
  64.     }
  65.     public void lockPropertyReadOnly(DataElement de, String value) {
  66.         lockPropertyReadOnly(de, value, true);
  67.     }
  68.     public void lockPropertyReadOnly(DataElement de, String value, boolean escapeHtml) {
  69.         lockPropertyEngine(de, value, escapeHtml, false, true);
  70.     }
  71.     public void lockPropertyHidden(DataElement de, String value) {
  72.         lockPropertyHidden(de, value, true);
  73.     }
  74.     public void lockPropertyHidden(DataElement de, String value, boolean escapeHtml) {
  75.         lockPropertyEngine(de, value, escapeHtml, true, false);
  76.     }
  77.     private void lockPropertyEngine(DataElement de, String value, boolean escapeHtml, boolean hidden, boolean readOnly) {
  78.         if(BYOKManager.isEnabledBYOK()) {
  79.             lockEngineWithBIOK(de, value, escapeHtml, hidden, readOnly,
  80.                     value); // non viene effettuato qual il wrap del valore, ma dovrà essere effettuato dalla servlet chiamante, in seguito alla chiamata dell'utente.
  81.             de.forceLockVisualizzazioneInputUtente(this.driverBYOKUtilities.isWrappedWithAnyPolicy(value),this.visualizzaInformazioniCifrate);
  82.         }
  83.         else {
  84.             de.setType(DataElementType.TEXT_EDIT);
  85.             de.setValue(value);
  86.         }
  87.     }
  88.    
  89.    
  90.     public void lock(DataElement de, String value) throws UtilsException {
  91.         lock(de, value, true);
  92.     }
  93.     public void lock(DataElement de, String value, boolean escapeHtml) throws UtilsException {
  94.         lockEngine(de, value, escapeHtml, false, false);
  95.     }
  96.     public void lockReadOnly(DataElement de, String value) throws UtilsException {
  97.         lockReadOnly(de, value, true);
  98.     }
  99.     public void lockReadOnly(DataElement de, String value, boolean escapeHtml) throws UtilsException {
  100.         lockEngine(de, value, escapeHtml, false, true);
  101.     }
  102.     public void lockHidden(DataElement de, String value) throws UtilsException {
  103.         lockHidden(de, value, true);
  104.     }
  105.     public void lockHidden(DataElement de, String value, boolean escapeHtml) throws UtilsException {
  106.         lockEngine(de, value, escapeHtml, true, false);
  107.     }
  108.     private void lockEngine(DataElement de, String value, boolean escapeHtml, boolean hidden, boolean readOnly) throws UtilsException {
  109.         if(BYOKManager.isEnabledBYOK()) {
  110.             lockEngineWithBIOK(de, value, escapeHtml, hidden, readOnly);
  111.         }
  112.         else {
  113.             processByByokDisabled(de, value, escapeHtml, hidden, readOnly);
  114.         }
  115.     }
  116.     private void processByByokDisabled(DataElement de, String value, boolean escapeHtml, boolean hidden, boolean readOnly) {
  117.         if(isForceHidden(de, hidden)){
  118.             de.setType(DataElementType.HIDDEN);
  119.         }
  120.         else if(isForceReadOnly(de, readOnly)){
  121.             if(this.visualizzaCampiPasswordComeLock) {
  122.                 this.lockEngineWithoutBIOK(de, value, escapeHtml, hidden, readOnly);
  123.             } else {
  124.                 de.setType(DataElementType.TEXT);
  125.             }
  126.         }
  127.         else if( de.getType()==null || StringUtils.isEmpty(de.getType()) ||
  128.                 ( (!DataElementType.TEXT_EDIT.toString().equals(de.getType())) && (!DataElementType.TEXT_AREA.toString().equals(de.getType())) )
  129.                 ){
  130.             processByByokDisabledDataElement(de, value, escapeHtml, hidden, readOnly);
  131.         } else {
  132.             processByByokDisabledDataElement(de, value, escapeHtml, hidden, readOnly);
  133.         }
  134.         de.setValue(escapeHtml ? StringEscapeUtils.escapeHtml(value) : value);
  135.     }
  136.     private void processByByokDisabledDataElement(DataElement de, String value, boolean escapeHtml, boolean hidden, boolean readOnly) {
  137.         if(this.visualizzaCampiPasswordComeLock) {
  138.             this.lockEngineWithoutBIOK(de, value, escapeHtml, hidden, readOnly);
  139.         } else {
  140.             if(hidden) {
  141.                 de.setType(DataElementType.HIDDEN);
  142.             }
  143.             else {
  144.                 de.setType(DataElementType.TEXT_EDIT);
  145.             }
  146.         }
  147.     }
  148.     private boolean isForceHidden(DataElement de, boolean hidden) {
  149.         return hidden &&
  150.                 (de.getType()==null || StringUtils.isEmpty(de.getType()) || !DataElementType.HIDDEN.toString().equals(de.getType())) ;
  151.     }
  152.     private boolean isForceReadOnly(DataElement de, boolean readOnly) {
  153.         return readOnly &&
  154.                 (de.getType()==null || StringUtils.isEmpty(de.getType()) || !DataElementType.TEXT.toString().equals(de.getType())) ;
  155.     }
  156.     private void lockEngineWithBIOK(DataElement de, String value, boolean escapeHtml, boolean hidden, boolean readOnly ) throws UtilsException {
  157.         String wrapValue = this.driverBYOKUtilities.wrap(value); // viene lasciato il valore wrapped, non viene effettuato nuovamente il wrap
  158.         lockEngineWithBIOK(de, value, escapeHtml, hidden, readOnly, wrapValue);
  159.     }
  160.     private void lockEngineWithBIOK(DataElement de, String value, boolean escapeHtml, boolean hidden, boolean readOnly, String wrapValue) {
  161.         if(hidden) {
  162.             if(de.getType()==null || StringUtils.isEmpty(de.getType()) || !DataElementType.HIDDEN.toString().equals(de.getType())) {
  163.                 de.setType(DataElementType.HIDDEN);
  164.             }
  165.             de.setValue(escapeHtml ? StringEscapeUtils.escapeHtml(wrapValue) : wrapValue);
  166.         }
  167.         else {
  168.             lockEngineWithBIOK(de, wrapValue, value, escapeHtml, readOnly );
  169.         }
  170.     }
  171.     private void lockEngineWithBIOK(DataElement de, String wrapValue, String originalValue, boolean escapeHtml, boolean readOnly) {
  172.         lockEngineWithBIOK(de, wrapValue, originalValue, escapeHtml, readOnly,
  173.                 // il valore viene decifrato tramite le chiamate getLockedParameter, quindi questo controllo non può essere implementato.
  174.                 false);
  175.     }
  176.     private void lockEngineWithBIOK(DataElement de, String wrapValue, String originalValue, boolean escapeHtml, boolean readOnly, boolean checkOriginalValue) {
  177.         StringBuilder sb = new StringBuilder();
  178.         if(originalValue!=null) {
  179.             // nop
  180.         }
  181.         /**String checkValue = originalValue;*/ // il valore viene decifrato tramite le chiamate getLockedParameter, quindi questo controllo non può essere implementato.
  182.         String checkValue = wrapValue; // viene verificato sempre il wrapValue, il quale se già cifrato non viene nuovamente cifrato e quindi permane la vecchia security policy.
  183.         if(checkValue!=null && StringUtils.isNotEmpty(checkValue)) {
  184.             if(BYOKUtilities.isWrappedValue(checkValue)) {
  185.                 if(!this.driverBYOKUtilities.isWrappedWithActivePolicy(checkValue)) {
  186.                     appendErrorMessageSecurityPolicyDifferente(sb, checkValue);
  187.                 }
  188.             }
  189.             else if(checkOriginalValue && this.messaggioInformativoInformazioneNonCifrata!=null && StringUtils.isNotEmpty(this.messaggioInformativoInformazioneNonCifrata)) {
  190.                 sb.append(this.messaggioInformativoInformazioneNonCifrata);
  191.             }
  192.         }
  193.         if(sb.length()>0) {
  194.             de.setNote(sb.toString());
  195.         }
  196.         de.setLock(escapeHtml ? StringEscapeUtils.escapeHtml(wrapValue) : wrapValue, readOnly, this.visualizzaInformazioniCifrate, true, this.warningMessage, this.servletNameSecretDecoder);
  197.     }
  198.     private void lockEngineWithoutBIOK(DataElement de, String wrapValue, boolean escapeHtml, boolean hidden, boolean readOnly) {
  199.         if(hidden) {
  200.             if(de.getType()==null || StringUtils.isEmpty(de.getType()) || !DataElementType.HIDDEN.toString().equals(de.getType())) {
  201.                 de.setType(DataElementType.HIDDEN);
  202.             }
  203.             de.setValue(escapeHtml ? StringEscapeUtils.escapeHtml(wrapValue) : wrapValue);
  204.         }
  205.         else {
  206.             de.setLock(escapeHtml ? StringEscapeUtils.escapeHtml(wrapValue) : wrapValue, readOnly, false, false, null, null);
  207.         }
  208.     }
  209.     private void appendErrorMessageSecurityPolicyDifferente(StringBuilder sb, String wrapValue) {
  210.         appendErrorMessageSecurityPolicyDifferente(this.messaggioInformativoInformazioneCifrataDifferenteSecurityPolicy, sb, wrapValue);
  211.     }
  212.     public static void appendErrorMessageSecurityPolicyDifferente(String messaggioInformativoInformazioneCifrataDifferenteSecurityPolicy,StringBuilder sb, String wrapValue) {
  213.         if(messaggioInformativoInformazioneCifrataDifferenteSecurityPolicy!=null && StringUtils.isNotEmpty(messaggioInformativoInformazioneCifrataDifferenteSecurityPolicy)) {
  214.             String suffix ="";
  215.             try {
  216.                 String old = BYOKUtilities.getPolicy(wrapValue);
  217.                 if(old!=null && StringUtils.isNotEmpty(old)) {
  218.                     suffix = old;
  219.                 }
  220.             }catch(Exception ignore) {
  221.                 // ignore
  222.             }
  223.             String s = messaggioInformativoInformazioneCifrataDifferenteSecurityPolicy.replace("@SECURITY_POLICY_ID@", suffix);
  224.             sb.append(s);
  225.         }
  226.     }
  227.    

  228.     public DriverBYOKUtilities getDriverBYOKUtilities() {
  229.         return this.driverBYOKUtilities;
  230.     }
  231. }