CredenzialeSearchTrasporto.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.core.transazioni.utils.credenziali;

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

  24. import org.openspcoop2.core.config.constants.TipoAutenticazione;
  25. import org.openspcoop2.core.transazioni.CredenzialeMittente;
  26. import org.openspcoop2.core.transazioni.dao.ICredenzialeMittenteService;
  27. import org.openspcoop2.core.transazioni.utils.TipoCredenzialeMittente;
  28. import org.openspcoop2.generic_project.exception.ExpressionException;
  29. import org.openspcoop2.generic_project.exception.ExpressionNotImplementedException;
  30. import org.openspcoop2.generic_project.expression.IPaginatedExpression;
  31. import org.openspcoop2.generic_project.expression.LikeMode;
  32. import org.openspcoop2.utils.UtilsException;
  33. import org.openspcoop2.utils.certificate.CertificateUtils;
  34. import org.openspcoop2.utils.certificate.PrincipalType;
  35. import org.slf4j.Logger;

  36. /**    
  37.  * CredenzialeTrasporto
  38.  *
  39.  * @author Poli Andrea (poli@link.it)
  40.  * @author $Author$
  41.  * @version $Rev$, $Date$
  42.  */
  43. public class CredenzialeSearchTrasporto extends AbstractSearchCredenziale {

  44.     private String tipoAutenticazione;
  45.    
  46.     public CredenzialeSearchTrasporto(String tipoAutenticazione) {
  47.         super(TipoCredenzialeMittente.TRASPORTO);
  48.         this.tipoAutenticazione = tipoAutenticazione;
  49.     }

  50.     @Override
  51.     public String getTipo() {
  52.         return CredenzialeTrasporto.getTipoTrasporto(this.tipo, this.tipoAutenticazione);
  53.     }
  54.    
  55.     public boolean isSsl() {
  56.         return TipoAutenticazione.SSL.getValue().equalsIgnoreCase(this.tipoAutenticazione);
  57.     }
  58.    
  59.     @Override
  60.     protected String getExactValueDatabase(String credentialParam, boolean ricercaEsatta) throws UtilsException {
  61.         throw new UtilsException("Not Implemented"); // il metodo non viene usato poich' createExpression รจ ridefinito per questo tipo di classe
  62.     }
  63.    
  64.     @Override
  65.     public IPaginatedExpression createExpression(ICredenzialeMittenteService credenzialeMittentiService, String credential, boolean ricercaEsatta, boolean caseSensitive) throws UtilsException {
  66.        
  67.         try {
  68.        
  69.             IPaginatedExpression pagExpression = credenzialeMittentiService.newPaginatedExpression();
  70.             pagExpression.and();
  71.            
  72.             pagExpression.equals(CredenzialeMittente.model().TIPO, CredenzialeTrasporto.getTipoTrasporto(this.tipo, this.tipoAutenticazione));
  73.            
  74.             if(CredenzialeTrasporto.isSsl(this.tipoAutenticazione) && ricercaEsatta) {
  75.                 setLikeConditionSslRicercaEsatta(pagExpression, caseSensitive,
  76.                         credential);        
  77.             }
  78.             else {
  79.                 if(ricercaEsatta) {
  80.                     if(caseSensitive) {
  81.                         pagExpression.equals(CredenzialeMittente.model().CREDENZIALE, credential);
  82.                     }
  83.                     else {
  84.                         pagExpression.ilike(CredenzialeMittente.model().CREDENZIALE, credential, LikeMode.EXACT);
  85.                     }
  86.                 }
  87.                 else if(!caseSensitive) {
  88.                     pagExpression.ilike(CredenzialeMittente.model().CREDENZIALE, credential, LikeMode.ANYWHERE);
  89.                 }
  90.                 else { // !ricercaEsatta && caseSensitive
  91.                     pagExpression.like(CredenzialeMittente.model().CREDENZIALE, credential, LikeMode.ANYWHERE);
  92.                 }
  93.             }
  94.        
  95.             return pagExpression;
  96.            
  97.         }catch(Exception e) {
  98.             throw new UtilsException(e.getMessage(), e);
  99.         }
  100.        
  101.     }
  102.    
  103.     private void setLikeConditionSslRicercaEsatta(IPaginatedExpression pagExpression, boolean caseSensitive,
  104.             String credential) throws ExpressionNotImplementedException, ExpressionException, UtilsException {
  105.         Map<String,  List<String>> hashSubject = CertificateUtils.getPrincipalIntoMap(credential, PrincipalType.SUBJECT);
  106.         for (Map.Entry<String,List<String>> entry : hashSubject.entrySet()) {
  107.             String key = entry.getKey();
  108.             List<String> listValues = hashSubject.get(key);
  109.             for (String value : listValues) {
  110.                 if(caseSensitive) {
  111.                     pagExpression.like(CredenzialeMittente.model().CREDENZIALE, "/"+CertificateUtils.formatKeyPrincipal(key)+"="+CertificateUtils.formatValuePrincipal(value)+"/", LikeMode.ANYWHERE);
  112.                 }
  113.                 else {
  114.                     pagExpression.ilike(CredenzialeMittente.model().CREDENZIALE, "/"+CertificateUtils.formatKeyPrincipal(key)+"="+CertificateUtils.formatValuePrincipal(value)+"/", LikeMode.ANYWHERE);
  115.                 }
  116.             }
  117.         }  
  118.     }
  119.    

  120.     public static List<CredenzialeMittente> filterList(List<CredenzialeMittente> originalList, String credenziale, Logger log) {
  121.         // Possono esistere piu' sil che hanno una porzione di subject uguale, devo quindi verificare che sia proprio quello che cerco
  122.         List<CredenzialeMittente> filteredList = new ArrayList<>();
  123.         if(originalList!=null) {
  124.             for (CredenzialeMittente credenzialeMittentePotenziale : originalList) {
  125.                 String subjectPotenziale =  credenzialeMittentePotenziale.getCredenziale();
  126.                 try {
  127.                     boolean subjectValid = CertificateUtils.sslVerify(subjectPotenziale, credenziale, PrincipalType.SUBJECT, log);
  128.                     if(subjectValid) {
  129.                         filteredList.add(credenzialeMittentePotenziale);
  130.                     }
  131.                 }catch(Exception e) {
  132.                     log.error("Analisi della credenziale '"+subjectPotenziale+"' non riuscita: "+e.getMessage(),e);
  133.                 }
  134.             }
  135.         }
  136.         /**System.out.println("FILTRO PRIMA["+originalList.size()+"] DOPO["+filteredList.size()+"]");*/
  137.         return filteredList;
  138.     }
  139. }