InfoMittenteFormatUtils.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.logger.info;

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

  23. import org.apache.commons.lang.StringUtils;
  24. import org.openspcoop2.core.config.constants.TipoAutenticazione;
  25. import org.openspcoop2.core.id.IDSoggetto;
  26. import org.openspcoop2.pdd.core.autenticazione.ApiKeyUtilities;
  27. import org.openspcoop2.protocol.engine.utils.NamingUtils;
  28. import org.openspcoop2.utils.certificate.CertificateUtils;
  29. import org.openspcoop2.utils.certificate.PrincipalType;

  30. /**
  31.  * InfoMittenteFormatUtils
  32.  *
  33.  * @author Andrea Poli (apoli@link.it)
  34.  * @author $Author$
  35.  * @version $Rev$, $Date$
  36.  *
  37.  */
  38. public class InfoMittenteFormatUtils {
  39.    
  40.     private InfoMittenteFormatUtils() {}

  41.     public static String getRichiedente(DatiMittente infoDatiMittente) {
  42.        
  43.         /**
  44.          * Logica:
  45.          * - prevale l'utente descritto in forma umana (username);
  46.          * - altrimenti prevale un eventuale applicativo identificato (registrato su GovWay) dando precedenza ad un applicativo token rispetto ad un applicativo di trasporto;
  47.          * - altrimenti prevalgono le informazioni di un eventuale token presente rispetto al trasporto; se si tratta di client credentials (subject non presente o client_id=subject) prevale l'informazione sul client-id altrimenti quella sul subject.
  48.          */
  49.        
  50.         // 1) Username del Token
  51.         String sTokenUsername = infoDatiMittente.getTokenUsername();
  52.         if(StringUtils.isNotEmpty(sTokenUsername)) {
  53.             return sTokenUsername;
  54.         }
  55.        
  56.         // 2) Applicativo Token identificato tramite ClientID
  57.         String sTokenClient = infoDatiMittente.getTokenClient();
  58.         if(StringUtils.isNotEmpty(sTokenClient)) {
  59.             return sTokenClient;
  60.         }
  61.        
  62.         // 2b) Applicativo Token identificato tramite PDND
  63.         String sTokenClientPdndOrganizationName = infoDatiMittente.getPdndOrganizationName();
  64.         if(StringUtils.isNotEmpty(sTokenClientPdndOrganizationName)) {
  65.             return sTokenClientPdndOrganizationName;
  66.         }
  67.        
  68.         // 3) Applicativo Fruitore
  69.         String sApplicativoFruitore = infoDatiMittente.getServizioApplicativoFruitore();
  70.         if(StringUtils.isNotEmpty(sApplicativoFruitore)) {
  71.             return sApplicativoFruitore;
  72.         }
  73.            
  74.         // 4) ClientId/Subject/Issuer del Token
  75.         String sTokenClientId = infoDatiMittente.getTokenClientId();
  76.         String sTokenSubject = infoDatiMittente.getTokenSubject();
  77.         boolean clientCredentialsFlow = false;
  78.         if(StringUtils.isNotEmpty(sTokenClientId)) {
  79.             clientCredentialsFlow = (sTokenSubject==null) || (StringUtils.isEmpty(sTokenSubject)) || (sTokenSubject.equals(sTokenClientId));
  80.         }
  81.                
  82.         // 4a) Client ID, per il caso di ClientCredential
  83.         if(clientCredentialsFlow &&
  84.             StringUtils.isNotEmpty(sTokenClientId)) {
  85.             return sTokenClientId;
  86.         }
  87.        
  88.         // 4b) Subject/Issuer del Token
  89.         if(StringUtils.isNotEmpty(sTokenSubject)) {
  90.            
  91.             String sTokenIssuer = infoDatiMittente.getTokenIssuer();
  92.             if(StringUtils.isNotEmpty(sTokenIssuer)) {
  93.                 return sTokenSubject + NamingUtils.LABEL_DOMINIO + sTokenIssuer;
  94.             }
  95.             else {
  96.                 return sTokenSubject;
  97.             }
  98.         }
  99.        
  100.         // 4c) Client ID, per il caso diverso da ClientCredential
  101.         if(!clientCredentialsFlow &&
  102.             StringUtils.isNotEmpty(sTokenClientId)) {
  103.             return sTokenClientId;
  104.         }
  105.        
  106.         // 5) Credenziali dell'autenticazione di trasporto
  107.         // volutamente uso l'id autenticato.
  108.         // se l'api è pubblica non deve essere visualizzata questa informazione!
  109.         String sTrasporto = getRichiedenteTrasporto(infoDatiMittente);
  110.         if(sTrasporto!=null) {
  111.             return sTrasporto;
  112.         }
  113.                        
  114.         return null;
  115.        
  116.     }
  117.    
  118.     private static String getRichiedenteTrasporto(DatiMittente infoDatiMittente) {
  119.         String sTrasportoMittente = infoDatiMittente.getTrasportoMittente();
  120.         String sTipoTrasportoMittente = infoDatiMittente.getTipoTrasportoMittente();
  121.         if(StringUtils.isNotEmpty(sTrasportoMittente) && StringUtils.isNotEmpty(sTipoTrasportoMittente)) {
  122.             if(sTipoTrasportoMittente.endsWith("_"+TipoAutenticazione.SSL.getValue())) {
  123.                 return getRichiedenteTrasportoSSLEngine(sTrasportoMittente);
  124.             }
  125.             else {
  126.                 return sTrasportoMittente;
  127.             }
  128.         }
  129.         return null;
  130.     }
  131.     private static String getRichiedenteTrasportoSSLEngine(String sTrasportoMittente) {
  132.         try {
  133.             Map<String, List<String>> l = CertificateUtils.getPrincipalIntoMap(sTrasportoMittente, PrincipalType.SUBJECT);
  134.             if(l!=null && !l.isEmpty()) {
  135.                 List<String> cnList = getCNList(l);
  136.                 if(cnList!=null && !cnList.isEmpty()) {
  137.                     StringBuilder bfList = new StringBuilder();
  138.                     for (String s : cnList) {
  139.                         if(bfList.length()>0) {
  140.                             bfList.append(", ");
  141.                         }
  142.                         bfList.append(s);
  143.                     }
  144.                     return bfList.toString();
  145.                 }
  146.             }
  147.             return sTrasportoMittente;
  148.         }catch(Exception t) {  
  149.             return sTrasportoMittente;
  150.         }
  151.     }
  152.     private static List<String> getCNList(Map<String, List<String>> l){
  153.         List<String> cnList = l.get("CN");
  154.         if(cnList==null || cnList.isEmpty()) {
  155.             cnList = l.get("cn");
  156.         }
  157.         if(cnList==null || cnList.isEmpty()) {
  158.             cnList = l.get("Cn");
  159.         }
  160.         if(cnList==null || cnList.isEmpty()) {
  161.             cnList = l.get("cN");
  162.         }
  163.         return cnList;
  164.     }
  165.    
  166.     public static String getIpRichiedente(DatiMittente infoDatiMittente) {
  167.        
  168.         String t = infoDatiMittente.getTransportClientAddress();
  169.         if(StringUtils.isNotEmpty(t)) {
  170.             return t;
  171.         }
  172.        
  173.         String s = infoDatiMittente.getSocketClientAddress();
  174.         if(StringUtils.isNotEmpty(s)) {
  175.             return s;
  176.         }
  177.        
  178.         return null;
  179.        
  180.     }
  181.    
  182.     public static String getLabelRichiedenteConFruitore(DatiMittente infoDatiMittente) {
  183.         StringBuilder bf = new StringBuilder();
  184.        
  185.         String richiedente = getRichiedente(infoDatiMittente);
  186.         if(StringUtils.isNotEmpty(richiedente)) {
  187.             bf.append(richiedente);
  188.         }
  189.        
  190.         String sTokenClient = infoDatiMittente.getTokenClient();
  191.         if(StringUtils.isNotEmpty(sTokenClient)) {
  192.            
  193.             // dominio di un applicativo client
  194.             processLabelRichiedenteConFruitoreTokenClient(infoDatiMittente, bf);
  195.            
  196.         }
  197.         else {
  198.            
  199.             // dominio del soggetto fruitore
  200.             processLabelRichiedenteConFruitore(infoDatiMittente, bf, richiedente);
  201.            
  202.         }
  203.        
  204.         return bf.toString();
  205.     }
  206.    
  207.     private static void processLabelRichiedenteConFruitoreTokenClient(DatiMittente infoDatiMittente, StringBuilder bfParam) {
  208.         if(infoDatiMittente.getTokenClientSoggettoFruitore()!=null &&
  209.                 infoDatiMittente.getTokenClientTipoSoggettoFruitore()!=null && infoDatiMittente.getTokenClientNomeSoggettoFruitore()!=null) {
  210.            
  211.             boolean addFruitore = true;
  212.            
  213.             IDSoggetto idSoggettoFruitore = new IDSoggetto(infoDatiMittente.getTokenClientTipoSoggettoFruitore(), infoDatiMittente.getTokenClientNomeSoggettoFruitore());
  214.            
  215.             if(org.openspcoop2.core.transazioni.constants.PddRuolo.DELEGATA.equals(infoDatiMittente.getPddRuolo())) {
  216.                 addFruitore =
  217.                         /**(idSoggettoFruitore==null) ||*/
  218.                         (infoDatiMittente.getSoggettoOperativo()==null) || (!infoDatiMittente.getSoggettoOperativo().equals(idSoggettoFruitore.toString()));
  219.             }
  220.            
  221.             if(addFruitore) {
  222.                 if(bfParam.length()>0) {
  223.                     bfParam.append(NamingUtils.LABEL_DOMINIO);
  224.                 }
  225.                
  226.                 bfParam.append(infoDatiMittente.getTokenClientSoggettoFruitore());  
  227.             }
  228.         }
  229.     }
  230.    
  231.     private static void processLabelRichiedenteConFruitore(DatiMittente infoDatiMittente, StringBuilder bfParam, String richiedente) {
  232.         String sFruitore = infoDatiMittente.getSoggettoFruitore();
  233.         if(StringUtils.isNotEmpty(sFruitore)) {

  234.             boolean addFruitore = true;
  235.                        
  236.             if(org.openspcoop2.core.transazioni.constants.PddRuolo.APPLICATIVA.equals(infoDatiMittente.getPddRuolo())) {
  237.                
  238.                 processLabelRichiedenteConFruitorePortaApplicativa(infoDatiMittente, bfParam, richiedente);
  239.                
  240.             }
  241.             else if(org.openspcoop2.core.transazioni.constants.PddRuolo.DELEGATA.equals(infoDatiMittente.getPddRuolo()) &&
  242.                     (infoDatiMittente.getSoggettoOperativo()!=null && StringUtils.isNotEmpty(infoDatiMittente.getTipoSoggettoFruitore()) && StringUtils.isNotEmpty(infoDatiMittente.getNomeSoggettoFruitore()))
  243.                 ) {
  244.            
  245.                 IDSoggetto idSoggettoFruitore = new IDSoggetto(infoDatiMittente.getTipoSoggettoFruitore(), infoDatiMittente.getNomeSoggettoFruitore());
  246.                 addFruitore = !infoDatiMittente.getSoggettoOperativo().equals(idSoggettoFruitore.toString());
  247.            
  248.             }
  249.            
  250.             if(addFruitore) {
  251.                 if(bfParam.length()>0) {
  252.                     bfParam.append(NamingUtils.LABEL_DOMINIO);
  253.                 }
  254.                
  255.                 bfParam.append(sFruitore);  
  256.             }
  257.         }
  258.     }
  259.    
  260.     private static void processLabelRichiedenteConFruitorePortaApplicativa(DatiMittente infoDatiMittente, StringBuilder bfParam, String richiedente) {
  261.         // L'AppId di un soggetto è già il soggetto. L'informazione sarebbe ridondante.
  262.         String sTrasportoMittente = infoDatiMittente.getTrasportoMittente();
  263.         if(richiedente!=null && sTrasportoMittente!=null && richiedente.equals(sTrasportoMittente)) { // se e' stato selezionato l'appId
  264.             String sTipoTrasportoMittente = infoDatiMittente.getTipoTrasportoMittente();
  265.             if(sTipoTrasportoMittente!=null && StringUtils.isNotEmpty(sTipoTrasportoMittente) &&
  266.                     sTipoTrasportoMittente.endsWith("_"+TipoAutenticazione.APIKEY.getValue()) &&
  267.                 // autenticazione api-key
  268.                 (!sTrasportoMittente.contains(ApiKeyUtilities.APPLICATIVO_SOGGETTO_SEPARATOR))
  269.                 &&
  270.                 bfParam.length()>0
  271.                 ){
  272.                 // appId di un soggetto
  273.                 bfParam.setLength(0); // svuoto il buffer poichè aggiungo solo il soggetto
  274.             }
  275.         }
  276.     }
  277.    
  278. }