ImbustamentoUtils.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.protocol.modipa.builder;

  21. import org.apache.commons.lang.StringUtils;
  22. import org.openspcoop2.core.config.Connettore;
  23. import org.openspcoop2.core.config.GenericProperties;
  24. import org.openspcoop2.core.config.Property;
  25. import org.openspcoop2.core.config.constants.CostantiConfigurazione;
  26. import org.openspcoop2.core.constants.CostantiDB;
  27. import org.openspcoop2.core.id.IDServizio;
  28. import org.openspcoop2.core.id.IDSoggetto;
  29. import org.openspcoop2.pdd.config.ConfigurazionePdDManager;
  30. import org.openspcoop2.pdd.core.token.Costanti;
  31. import org.openspcoop2.pdd.core.token.GestoreTokenNegoziazioneUtilities;
  32. import org.openspcoop2.pdd.core.token.PolicyNegoziazioneToken;
  33. import org.openspcoop2.pdd.core.token.TokenUtilities;
  34. import org.openspcoop2.protocol.sdk.ProtocolException;
  35. import org.openspcoop2.protocol.sdk.state.IState;
  36. import org.openspcoop2.protocol.sdk.state.RequestInfo;
  37. import org.openspcoop2.utils.certificate.KeystoreParams;
  38. import org.slf4j.Logger;

  39. /**
  40.  * ImbustamentoUtils
  41.  *
  42.  * @author Poli Andrea (apoli@link.it)
  43.  * @author $Author$
  44.  * @version $Rev$, $Date$
  45.  */
  46. public class ImbustamentoUtils {
  47.    
  48.     private ImbustamentoUtils() {}

  49.     private static void logError(Logger log, String msg, Exception e) {
  50.         log.error(msg,e);
  51.     }
  52.    
  53.     // invocato in Imbustamento
  54.    
  55.     private static final String PREFIX = "l'assegnazione di una token policy di negoziazione al connettore";
  56.     private static final String NO_SIGNED_JWT = "non è di tipo 'SignedJWT'";
  57.     private static String getPolicyIndicata(String tokenPolicy) {
  58.         return "; la policy indicata '"+tokenPolicy+"' ";
  59.     }
  60.    
  61.     public static PolicyNegoziazioneToken readPolicyNegoziazioneToken(Logger log, IState state, IDSoggetto soggettoFruitore, IDServizio idServizio, String azione,
  62.             RequestInfo requestInfo, StringBuilder sbRequired) throws ProtocolException {
  63.         try {
  64.            
  65.             PolicyNegoziazioneToken policyNegoziazioneToken = null;
  66.            
  67.             IDServizio idServizioClone = idServizio.clone();
  68.             idServizioClone.setAzione(azione);
  69.            
  70.             ConfigurazionePdDManager configurazionePdDManager = ConfigurazionePdDManager.getInstance(state);
  71.             boolean functionAsRouter = false;
  72.             Connettore connettore = configurazionePdDManager.getForwardRoute(soggettoFruitore,idServizio, functionAsRouter, requestInfo);
  73.             if(connettore==null) {
  74.                 throw new ProtocolException("Connettore non individuato");
  75.             }
  76.            
  77.             if(connettore.sizePropertyList()<=0) {
  78.                 sbRequired.append(PREFIX); // prefisso sarà "... richiede "
  79.                 return policyNegoziazioneToken;
  80.             }
  81.             String tokenPolicy = null;
  82.             for (Property p : connettore.getPropertyList()) {
  83.                 if(CostantiDB.CONNETTORE_TOKEN_POLICY.equals(p.getNome())) {
  84.                     tokenPolicy = p.getValore();
  85.                 }
  86.             }
  87.             if(tokenPolicy!=null) {
  88.                 tokenPolicy = tokenPolicy.trim();
  89.             }
  90.             if(tokenPolicy==null || StringUtils.isEmpty(tokenPolicy)) {
  91.                 sbRequired.append(PREFIX); // prefisso sarà "... richiede "
  92.                 return policyNegoziazioneToken;
  93.             }
  94.            
  95.             GenericProperties gp = configurazionePdDManager.getGenericProperties(CostantiConfigurazione.GENERIC_PROPERTIES_TOKEN_TIPOLOGIA_RETRIEVE, tokenPolicy);
  96.             if(gp==null) {
  97.                 sbRequired.append(PREFIX+getPolicyIndicata(tokenPolicy)+"non esiste"); // prefisso sarà "... richiede "
  98.                 return policyNegoziazioneToken;
  99.             }
  100.            
  101.             return TokenUtilities.convertTo(gp);
  102.            
  103.         }catch(Exception e) {
  104.             logError(log, "Errore durante la lettura del connettore (tramite la sicurezza messaggio): "+e.getMessage(),e);
  105.             throw new ProtocolException(e.getMessage(),e);
  106.         }
  107.     }
  108.    
  109.     public static KeystoreParams readKeystoreParams(Logger log, PolicyNegoziazioneToken policyNegoziazioneToken, StringBuilder sbRequired) throws ProtocolException {
  110.         try {
  111.            
  112.             KeystoreParams kp = null;
  113.            
  114.             String tokenPolicy = policyNegoziazioneToken.getName();
  115.            
  116.             if(!policyNegoziazioneToken.isRfc7523x509Grant()) {
  117.                 sbRequired.append(PREFIX+getPolicyIndicata(tokenPolicy)+NO_SIGNED_JWT); // prefisso sarà "... richiede "
  118.                 return kp;
  119.             }
  120.            
  121.             if(policyNegoziazioneToken.isJwtSignKeystoreApplicativoModI()) {
  122.                 sbRequired.append(PREFIX+getPolicyIndicata(tokenPolicy)+"non è utilizzabile essendo configurata con una modalità di keystore '"+Costanti.KEYSTORE_TYPE_APPLICATIVO_MODI_LABEL+"'"); // prefisso sarà "... richiede "
  123.                 return kp;
  124.             }
  125.             else if(policyNegoziazioneToken.isJwtSignKeystoreFruizioneModI()) {
  126.                 sbRequired.append(PREFIX+getPolicyIndicata(tokenPolicy)+"non è utilizzabile essendo configurata con una modalità di keystore '"+Costanti.KEYSTORE_TYPE_FRUIZIONE_MODI_LABEL+"'"); // prefisso sarà "... richiede "
  127.                 return kp;
  128.             }
  129.             else {
  130.                 kp = GestoreTokenNegoziazioneUtilities.readKeystoreParams(policyNegoziazioneToken);
  131.                 if(kp==null) {
  132.                     sbRequired.append(PREFIX+getPolicyIndicata(tokenPolicy)+"non è configurata correttamente"); // prefisso sarà "... richiede "
  133.                     return kp;
  134.                 }
  135.             }
  136.            
  137.             return kp;
  138.            
  139.         }catch(Exception e) {
  140.             logError(log, "Errore durante la lettura del connettore (tramite la sicurezza messaggio): "+e.getMessage(),e);
  141.             throw new ProtocolException(e.getMessage(),e);
  142.         }
  143.     }
  144.    
  145.     public static String readClientId(Logger log, PolicyNegoziazioneToken policyNegoziazioneToken,
  146.             StringBuilder sbRequired) throws ProtocolException {
  147.         try {
  148.            
  149.             String clientId = null;
  150.            
  151.             String tokenPolicy = policyNegoziazioneToken.getName();
  152.            
  153.             if(!policyNegoziazioneToken.isRfc7523x509Grant()) {
  154.                 sbRequired.append(PREFIX+getPolicyIndicata(tokenPolicy)+NO_SIGNED_JWT); // prefisso sarà "... richiede "
  155.                 return clientId;
  156.             }
  157.            
  158.             if(policyNegoziazioneToken.isJwtClientIdApplicativoModI()) {
  159.                 sbRequired.append(PREFIX+getPolicyIndicata(tokenPolicy)+"non è utilizzabile essendo configurato un identificativo client con modalità '"+Costanti.KEYSTORE_TYPE_APPLICATIVO_MODI_LABEL+"'"); // prefisso sarà "... richiede "
  160.                 return clientId;
  161.             }
  162.             else if(policyNegoziazioneToken.isJwtClientIdFruizioneModI()) {
  163.                 sbRequired.append(PREFIX+getPolicyIndicata(tokenPolicy)+"non è utilizzabile essendo configurata un identificativo client con modalità '"+Costanti.KEYSTORE_TYPE_FRUIZIONE_MODI_LABEL+"'"); // prefisso sarà "... richiede "
  164.                 return clientId;
  165.             }
  166.             else {
  167.                 clientId = policyNegoziazioneToken.getJwtClientId();
  168.             }
  169.            
  170.             return clientId;
  171.            
  172.         }catch(Exception e) {
  173.             logError(log, "Errore durante la lettura del kid presente nella token policy (tramite la sicurezza messaggio): "+e.getMessage(),e);
  174.             throw new ProtocolException(e.getMessage(),e);
  175.         }
  176.     }
  177.    
  178.     public static String readKID(Logger log, PolicyNegoziazioneToken policyNegoziazioneToken,
  179.             String clientId, KeystoreParams kp, StringBuilder sbRequired) throws ProtocolException {
  180.         try {
  181.            
  182.             String kid = null;
  183.            
  184.             String tokenPolicy = policyNegoziazioneToken.getName();
  185.            
  186.             if(!policyNegoziazioneToken.isRfc7523x509Grant()) {
  187.                 sbRequired.append(PREFIX+getPolicyIndicata(tokenPolicy)+NO_SIGNED_JWT); // prefisso sarà "... richiede "
  188.                 return kid;
  189.             }
  190.            
  191.             if(policyNegoziazioneToken.isJwtSignIncludeKeyIdApplicativoModI()) {
  192.                 sbRequired.append(PREFIX+getPolicyIndicata(tokenPolicy)+"non è utilizzabile essendo configurato un identificativo kid con modalità '"+Costanti.KEYSTORE_TYPE_APPLICATIVO_MODI_LABEL+"'"); // prefisso sarà "... richiede "
  193.                 return kid;
  194.             }
  195.             else if(policyNegoziazioneToken.isJwtSignIncludeKeyIdFruizioneModI()) {
  196.                 sbRequired.append(PREFIX+getPolicyIndicata(tokenPolicy)+"non è utilizzabile essendo configurata un identificativo kid con modalità '"+Costanti.KEYSTORE_TYPE_FRUIZIONE_MODI_LABEL+"'"); // prefisso sarà "... richiede "
  197.                 return kid;
  198.             }
  199.             else if( policyNegoziazioneToken.isJwtSignIncludeKeyIdWithClientId() ) {
  200.                 kid = clientId;
  201.             }
  202.             else if( policyNegoziazioneToken.isJwtSignIncludeKeyIdWithKeyAlias() ) {
  203.                 kid = kp.getKeyAlias();
  204.             }
  205.             else if( policyNegoziazioneToken.isJwtSignIncludeKeyIdCustom()) {
  206.                 kid = policyNegoziazioneToken.getJwtSignIncludeKeyIdCustom();
  207.             }
  208.            
  209.             return kid;
  210.            
  211.         }catch(Exception e) {
  212.             logError(log, "Errore durante la lettura del kid presente nella token policy (tramite la sicurezza messaggio): "+e.getMessage(),e);
  213.             throw new ProtocolException(e.getMessage(),e);
  214.         }
  215.     }
  216.    
  217. }