RicezioneBusteGestioneToken.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.services.core;

  21. import java.util.List;

  22. import org.openspcoop2.core.config.GestioneTokenAutenticazione;
  23. import org.openspcoop2.core.config.PortaApplicativa;
  24. import org.openspcoop2.core.config.PortaDelegata;
  25. import org.openspcoop2.core.id.IDPortaApplicativa;
  26. import org.openspcoop2.core.id.IDPortaDelegata;
  27. import org.openspcoop2.core.id.IDSoggetto;
  28. import org.openspcoop2.core.transazioni.utils.CredenzialiMittente;
  29. import org.openspcoop2.message.OpenSPCoop2Message;
  30. import org.openspcoop2.pdd.config.ConfigurazionePdDManager;
  31. import org.openspcoop2.pdd.config.OpenSPCoop2Properties;
  32. import org.openspcoop2.pdd.core.CostantiPdD;
  33. import org.openspcoop2.pdd.core.PdDContext;
  34. import org.openspcoop2.pdd.core.autenticazione.GestoreAutenticazione;
  35. import org.openspcoop2.pdd.core.handlers.InRequestContext;
  36. import org.openspcoop2.pdd.core.state.IOpenSPCoopState;
  37. import org.openspcoop2.pdd.core.token.GestoreToken;
  38. import org.openspcoop2.pdd.core.token.InformazioniToken;
  39. import org.openspcoop2.pdd.core.token.PolicyGestioneToken;
  40. import org.openspcoop2.pdd.core.token.pa.EsitoDynamicDiscoveryPortaApplicativa;
  41. import org.openspcoop2.pdd.core.token.pa.EsitoGestioneTokenPortaApplicativa;
  42. import org.openspcoop2.pdd.core.token.pa.EsitoPresenzaTokenPortaApplicativa;
  43. import org.openspcoop2.pdd.core.token.pa.GestioneToken;
  44. import org.openspcoop2.pdd.core.transazioni.Transaction;
  45. import org.openspcoop2.pdd.logger.MsgDiagnostico;
  46. import org.openspcoop2.pdd.logger.Tracciamento;
  47. import org.openspcoop2.pdd.services.error.RicezioneBusteExternalErrorGenerator;
  48. import org.openspcoop2.protocol.sdk.Busta;
  49. import org.openspcoop2.protocol.sdk.BustaRawContent;
  50. import org.openspcoop2.protocol.sdk.IProtocolFactory;
  51. import org.openspcoop2.protocol.sdk.constants.CodiceErroreCooperazione;
  52. import org.openspcoop2.protocol.sdk.constants.CodiceErroreIntegrazione;
  53. import org.openspcoop2.protocol.sdk.constants.ErroreCooperazione;
  54. import org.openspcoop2.protocol.sdk.constants.ErroreIntegrazione;
  55. import org.openspcoop2.protocol.sdk.constants.ErroriIntegrazione;
  56. import org.openspcoop2.protocol.sdk.constants.IntegrationFunctionError;
  57. import org.openspcoop2.protocol.sdk.state.RequestInfo;
  58. import org.openspcoop2.protocol.sdk.tracciamento.EsitoElaborazioneMessaggioTracciato;
  59. import org.openspcoop2.protocol.sdk.tracciamento.TracciamentoException;
  60. import org.openspcoop2.utils.transport.http.HttpConstants;
  61. import org.slf4j.Logger;

  62. /**
  63.  * RicezioneBusteGestioneToken
  64.  *
  65.  * @author Poli Andrea (apoli@link.it)
  66.  * @author $Author$
  67.  * @version $Rev$, $Date$
  68.  */
  69. public class RicezioneBusteGestioneToken {

  70.     private MsgDiagnostico msgDiag;
  71.     private Logger logCore;
  72.     private Tracciamento tracciamento;
  73.     private String correlazioneApplicativa;
  74.     private BustaRawContent<?> soapHeaderElement;
  75.     private Busta bustaRichiesta;
  76.    
  77.     private PortaApplicativa pa;
  78.     private IDPortaApplicativa idPA;
  79.     private PortaDelegata pd;
  80.     private IDPortaDelegata idPD;
  81.    
  82.     private OpenSPCoop2Message requestMessage;
  83.    
  84.     private boolean asincronoSimmetricoRisposta;
  85.    
  86.     private RicezioneBusteContext msgContext;
  87.     private RicezioneBusteExternalErrorGenerator generatoreErrore;
  88.     private InRequestContext inRequestContext;
  89.    
  90.     private ConfigurazionePdDManager configurazionePdDReader;
  91.    
  92.     private PdDContext pddContext;
  93.     private String idTransazione;
  94.     private IOpenSPCoopState openspcoopstate;
  95.     private Transaction transaction;
  96.     private RequestInfo requestInfo;
  97.    
  98.     private IProtocolFactory<?> protocolFactory;
  99.    
  100.     private RicezioneBusteParametriGenerazioneBustaErrore parametriGenerazioneBustaErrore;
  101.     private RicezioneBusteParametriInvioBustaErrore parametriInvioBustaErrore;
  102.    
  103.     private IDSoggetto identitaPdD;
  104.    
  105.     public RicezioneBusteGestioneToken(MsgDiagnostico msgDiag, Logger logCore,
  106.             Tracciamento tracciamento, String correlazioneApplicativa, BustaRawContent<?> soapHeaderElement, Busta bustaRichiesta,
  107.             PortaApplicativa pa, IDPortaApplicativa idPA, PortaDelegata pd, IDPortaDelegata idPD,
  108.             OpenSPCoop2Message requestMessage,
  109.             boolean asincronoSimmetricoRisposta,
  110.             RicezioneBusteContext msgContext, RicezioneBusteExternalErrorGenerator generatoreErrore, InRequestContext inRequestContext,
  111.             ConfigurazionePdDManager configurazionePdDReader,
  112.             PdDContext pddContext, String idTransazione,
  113.             IOpenSPCoopState openspcoopstate, Transaction transaction, RequestInfo requestInfo,
  114.             IProtocolFactory<?> protocolFactory,
  115.             IDSoggetto identitaPdD,
  116.             RicezioneBusteParametriGenerazioneBustaErrore parametriGenerazioneBustaErrore, RicezioneBusteParametriInvioBustaErrore parametriInvioBustaErrore) {
  117.         this.msgDiag = msgDiag;
  118.         this.logCore = logCore;
  119.        
  120.         this.tracciamento = tracciamento;
  121.         this.correlazioneApplicativa = correlazioneApplicativa;
  122.         this.soapHeaderElement = soapHeaderElement;
  123.         this.bustaRichiesta = bustaRichiesta;
  124.        
  125.         this.pa = pa;
  126.         this.idPA = idPA;
  127.         this.pd = pd;
  128.         this.idPD = idPD;
  129.        
  130.         this.requestMessage = requestMessage;
  131.        
  132.         this.asincronoSimmetricoRisposta = asincronoSimmetricoRisposta;
  133.        
  134.         this.msgContext = msgContext;
  135.         this.generatoreErrore = generatoreErrore;
  136.         this.inRequestContext = inRequestContext;
  137.        
  138.         this.configurazionePdDReader = configurazionePdDReader;
  139.        
  140.         this.pddContext = pddContext;
  141.         this.idTransazione = idTransazione;
  142.         this.openspcoopstate = openspcoopstate;
  143.         this.transaction = transaction;
  144.         this.requestInfo = requestInfo;
  145.        
  146.         this.protocolFactory = protocolFactory;
  147.        
  148.         this.identitaPdD = identitaPdD;
  149.        
  150.         this.parametriGenerazioneBustaErrore = parametriGenerazioneBustaErrore;
  151.         this.parametriInvioBustaErrore = parametriInvioBustaErrore;
  152.     }
  153.    
  154.     // Result
  155.     private GestioneTokenAutenticazione gestioneTokenAutenticazione = null;
  156.     private String token = null;
  157.     private String tipoGestioneToken = null;
  158.    
  159.     public GestioneTokenAutenticazione getGestioneTokenAutenticazione() {
  160.         return this.gestioneTokenAutenticazione;
  161.     }
  162.     public String getToken() {
  163.         return this.token;
  164.     }
  165.     public String getTipoGestioneToken() {
  166.         return this.tipoGestioneToken;
  167.     }
  168.    
  169.     public boolean process() throws TracciamentoException {
  170.         this.msgDiag.mediumDebug("GestioneToken...");
  171.         try {
  172.             if(this.pa!=null){
  173.                 this.tipoGestioneToken = this.configurazionePdDReader.getGestioneToken(this.pa);
  174.                 if(this.pa.getGestioneToken()!=null) {
  175.                     this.gestioneTokenAutenticazione = this.pa.getGestioneToken().getAutenticazione();
  176.                 }
  177.             }
  178.             else{
  179.                 this.tipoGestioneToken = this.configurazionePdDReader.getGestioneToken(this.pd);
  180.                 if(this.pd.getGestioneToken()!=null) {
  181.                     this.gestioneTokenAutenticazione = this.pd.getGestioneToken().getAutenticazione();
  182.                 }
  183.             }
  184.         }catch(Exception exception){
  185.             // ignore
  186.         }
  187.        
  188.         String gestioneTokenPrefix = "processo di gestione token ["+ this.tipoGestioneToken + "] ";
  189.        
  190.         this.msgContext.getIntegrazione().setTipoGestioneToken(this.tipoGestioneToken);
  191.         if (this.tipoGestioneToken == null || this.asincronoSimmetricoRisposta) {

  192.             if(!this.asincronoSimmetricoRisposta) {
  193.                 this.msgDiag.logPersonalizzato("gestioneTokenDisabilitata");
  194.             }
  195.            
  196.         } else {

  197.             this.transaction.getTempiElaborazione().startToken();
  198.             try {
  199.            
  200.                 ErroreCooperazione erroreCooperazione = null;
  201.                 ErroreIntegrazione erroreIntegrazione = null;
  202.                 Exception eGestioneToken = null;
  203.                 OpenSPCoop2Message errorMessageGestioneToken = null;
  204.                 String wwwAuthenticateErrorHeader = null;
  205.                 boolean fineGestione = false;
  206.                 IntegrationFunctionError integrationFunctionError = null;
  207.                 try {
  208.                    
  209.                     PolicyGestioneToken policyGestioneToken = null;
  210.                     if(this.pa!=null){
  211.                         policyGestioneToken = this.configurazionePdDReader.getPolicyGestioneToken(this.pa, this.requestInfo);
  212.                     }
  213.                     else {
  214.                         policyGestioneToken = this.configurazionePdDReader.getPolicyGestioneToken(this.pd, this.requestInfo);
  215.                     }
  216.                    
  217.                     this.pddContext.addObject(org.openspcoop2.pdd.core.token.Costanti.PDD_CONTEXT_TOKEN_POLICY,
  218.                             policyGestioneToken.getName());
  219.                     this.pddContext.addObject(org.openspcoop2.pdd.core.token.Costanti.PDD_CONTEXT_TOKEN_REALM,
  220.                             policyGestioneToken.getRealm());
  221.                     this.pddContext.addObject(org.openspcoop2.pdd.core.token.Costanti.PDD_CONTEXT_TOKEN_MESSAGE_ERROR_BODY_EMPTY,
  222.                             policyGestioneToken.isMessageErrorGenerateEmptyMessage());
  223.                     this.pddContext.addObject(org.openspcoop2.pdd.core.token.Costanti.PDD_CONTEXT_TOKEN_MESSAGE_ERROR_GENERIC_MESSAGE,
  224.                             policyGestioneToken.isMessageErrorGenerateGenericMessage());
  225.                    
  226.                     this.msgDiag.addKeyword(CostantiPdD.KEY_TOKEN_POLICY_GESTIONE, this.tipoGestioneToken);
  227.                     this.msgContext.getIntegrazione().setTokenPolicy(this.tipoGestioneToken);
  228.                     this.msgDiag.addKeyword(CostantiPdD.KEY_TOKEN_POLICY_AZIONI, policyGestioneToken.getLabelAzioniGestioneToken());
  229.                     this.msgContext.getIntegrazione().setTokenPolicyActions(policyGestioneToken.getAzioniGestioneToken());
  230.                     this.msgDiag.addKeyword(CostantiPdD.KEY_TOKEN_TIPO, policyGestioneToken.getLabelTipoToken());
  231.                     this.msgDiag.logPersonalizzato("gestioneTokenInCorso");
  232.                    
  233.                     org.openspcoop2.pdd.core.token.pa.DatiInvocazionePortaApplicativa datiInvocazione = new org.openspcoop2.pdd.core.token.pa.DatiInvocazionePortaApplicativa();
  234.                     datiInvocazione.setInfoConnettoreIngresso(this.inRequestContext.getConnettore());
  235.                     datiInvocazione.setState(this.openspcoopstate.getStatoRichiesta());
  236.                     datiInvocazione.setIdModulo(this.msgContext.getIdModulo());
  237.                     datiInvocazione.setMessage(this.requestMessage);
  238.                     datiInvocazione.setIdPA(this.idPA);
  239.                     datiInvocazione.setPa(this.pa);
  240.                     datiInvocazione.setIdPD(this.idPD);
  241.                     datiInvocazione.setPd(this.pd);    
  242.                     datiInvocazione.setPolicyGestioneToken(policyGestioneToken);
  243.                     datiInvocazione.setRequestInfo(this.requestInfo);
  244.                    
  245.                     GestoreToken.validazioneConfigurazione(datiInvocazione); // assicura che la configurazione sia corretta
  246.                    
  247.                     GestioneToken gestioneTokenEngine = new GestioneToken(this.logCore, this.idTransazione, this.pddContext, this.protocolFactory);
  248.                    
  249.                     // cerco token
  250.                    
  251.                     this.msgDiag.addKeyword(CostantiPdD.KEY_TOKEN_POSIZIONE, policyGestioneToken.getLabelPosizioneToken());
  252.                     this.msgDiag.logPersonalizzato("gestioneTokenInCorso.verificaPresenzaToken");
  253.                    
  254.                     EsitoPresenzaTokenPortaApplicativa esitoPresenzaToken = gestioneTokenEngine.verificaPresenzaToken(datiInvocazione);
  255.                     EsitoDynamicDiscoveryPortaApplicativa esitoDynamicDiscovery = null;
  256.                     EsitoGestioneTokenPortaApplicativa esitoValidazioneToken = null;
  257.                     EsitoGestioneTokenPortaApplicativa esitoIntrospectionToken = null;
  258.                     EsitoGestioneTokenPortaApplicativa esitoUserInfoToken = null;
  259.                     if(esitoPresenzaToken.isPresente()) {
  260.                         this.msgDiag.addKeyword(CostantiPdD.KEY_TOKEN, esitoPresenzaToken.getToken());
  261.                         this.msgDiag.logPersonalizzato("gestioneTokenInCorso.verificaPresenzaToken.trovato"); // stampa del token info
  262.                        
  263.                         this.token = esitoPresenzaToken.getToken();
  264.                         this.pddContext.addObject(org.openspcoop2.pdd.core.token.Costanti.PDD_CONTEXT_TOKEN_POSIZIONE, esitoPresenzaToken);
  265.                        
  266.                         this.msgDiag.logPersonalizzato("gestioneTokenInCorso.verificaPresenzaToken.completataSuccesso");
  267.    
  268.                        
  269.                         // dynamicDiscovery
  270.                         if(!fineGestione) {
  271.                            
  272.                             if(policyGestioneToken.isDynamicDiscovery()) {
  273.                            
  274.                                 this.msgDiag.addKeyword(CostantiPdD.KEY_TOKEN_ENDPOINT_SERVIZIO_DYNAMIC_DISCOVERY, policyGestioneToken.getDynamicDiscoveryEndpoint());
  275.                                
  276.                                 this.msgDiag.logPersonalizzato("gestioneTokenInCorso.dynamicDiscovery");
  277.                                
  278.                                 esitoDynamicDiscovery = gestioneTokenEngine.dynamicDiscovery(this.bustaRichiesta, datiInvocazione, esitoPresenzaToken);
  279.                                 if(esitoDynamicDiscovery.isValido()) {
  280.                                    
  281.                                     this.msgDiag.logPersonalizzato("gestioneTokenInCorso.dynamicDiscovery.completataSuccesso");
  282.                                    
  283.                                     this.msgDiag.addKeyword(CostantiPdD.KEY_DYNAMIC_DISCOVERY_INFO, esitoDynamicDiscovery.getDynamicDiscovery().getRawResponse());
  284.                                    
  285.                                     this.pddContext.addObject(org.openspcoop2.pdd.core.token.Costanti.PDD_CONTEXT_TOKEN_ESITO_DYNAMIC_DISCOVERY, esitoDynamicDiscovery);
  286.                                    
  287.                                     if(esitoDynamicDiscovery.isInCache()) {
  288.                                         this.msgDiag.logPersonalizzato("gestioneTokenInCorso.dynamicDiscovery.inCache");
  289.                                     }
  290.                                     else {
  291.                                         this.msgDiag.logPersonalizzato("gestioneTokenInCorso.dynamicDiscovery.validato");
  292.                                     }
  293.                                 }
  294.                                 else {
  295.                                    
  296.                                     this.msgDiag.addKeyword(CostantiPdD.KEY_ERRORE_PROCESSAMENTO, esitoDynamicDiscovery.getDetails());
  297.                                     // La gestione di fallire o dare warning è delegata alla funzionalità stessa jwt/introspection/userInfo che sono le azioni
  298.                                     /**if(policyGestioneToken.isDynamicDiscoveryWarningOnly()) {
  299.                                         this.msgDiag.logPersonalizzato("gestioneTokenInCorso.dynamicDiscovery.warningOnly.fallita");
  300.                                     }
  301.                                     else {
  302.                                         this.msgDiag.logPersonalizzato("gestioneTokenInCorso.dynamicDiscovery.fallita");
  303.                                         fineGestione = true;
  304.                                     }*/
  305.                                     this.msgDiag.logPersonalizzato("gestioneTokenInCorso.dynamicDiscovery.fallita");
  306.                                                                        
  307.                                     String msgErrore = gestioneTokenPrefix+"(DynamicDiscovery) fallito: " + esitoDynamicDiscovery.getDetails();
  308.                                     if(esitoDynamicDiscovery.getEccezioneProcessamento()!=null) {
  309.                                         this.logCore.error(msgErrore,esitoDynamicDiscovery.getEccezioneProcessamento());
  310.                                     }
  311.                                     else {
  312.                                         this.logCore.error(msgErrore);
  313.                                     }
  314.                                
  315.                                     // La gestione di fallire o dare warning è delegata alla funzionalità stessa jwt/introspection/userInfo che sono le azioni
  316.                                     /**
  317.                                     erroreCooperazione = esitoDynamicDiscovery.getErroreCooperazione();
  318.                                     erroreIntegrazione = esitoDynamicDiscovery.getErroreIntegrazione();
  319.                                     eGestioneToken = esitoDynamicDiscovery.getEccezioneProcessamento();
  320.                                     errorMessageGestioneToken = esitoDynamicDiscovery.getErrorMessage();
  321.                                     wwwAuthenticateErrorHeader = esitoDynamicDiscovery.getWwwAuthenticateErrorHeader();
  322.                                     integrationFunctionError = esitoDynamicDiscovery.getIntegrationFunctionError();
  323.                                     */
  324.                                    
  325.                                 }
  326.                             }
  327.                             else {
  328.                                 this.msgDiag.logPersonalizzato("gestioneTokenInCorso.introspectionToken.disabilitata");
  329.                             }
  330.                            
  331.                         }
  332.                        
  333.                        
  334.                         // validazione jwt
  335.                         if(!fineGestione) {
  336.                            
  337.                             if(policyGestioneToken.isValidazioneJWT()) {
  338.                            
  339.                                 this.msgDiag.logPersonalizzato("gestioneTokenInCorso.validazioneToken");
  340.                                
  341.                                 esitoValidazioneToken = gestioneTokenEngine.validazioneJWTToken(this.bustaRichiesta, datiInvocazione, esitoPresenzaToken, esitoDynamicDiscovery);
  342.                                 if(esitoValidazioneToken.isValido()) {
  343.                                    
  344.                                     this.msgDiag.logPersonalizzato("gestioneTokenInCorso.validazioneToken.completataSuccesso");
  345.                                    
  346.                                     this.msgDiag.addKeyword(CostantiPdD.KEY_TOKEN_INFO, esitoValidazioneToken.getInformazioniToken().getRawResponse());
  347.                                    
  348.                                     this.pddContext.addObject(org.openspcoop2.pdd.core.token.Costanti.PDD_CONTEXT_TOKEN_ESITO_VALIDAZIONE, esitoValidazioneToken);
  349.                                    
  350.                                     if(esitoValidazioneToken.isInCache()) {
  351.                                         this.msgDiag.logPersonalizzato("gestioneTokenInCorso.validazioneToken.inCache");
  352.                                     }
  353.                                     else {
  354.                                         this.msgDiag.logPersonalizzato("gestioneTokenInCorso.validazioneToken.validato");
  355.                                     }
  356.                                 }
  357.                                 else {
  358.                                    
  359.                                     this.msgDiag.addKeyword(CostantiPdD.KEY_ERRORE_PROCESSAMENTO, esitoValidazioneToken.getDetails());
  360.                                     if(policyGestioneToken.isValidazioneJWTWarningOnly()) {
  361.                                         this.msgDiag.logPersonalizzato("gestioneTokenInCorso.validazioneToken.warningOnly.fallita");
  362.                                     }
  363.                                     else {
  364.                                         this.msgDiag.logPersonalizzato("gestioneTokenInCorso.validazioneToken.fallita");
  365.                                         fineGestione = true;
  366.                                     }
  367.                                    
  368.                                     String msgErrore = gestioneTokenPrefix+"(validazione JWT) fallito: " + esitoValidazioneToken.getDetails();
  369.                                     if(esitoValidazioneToken.getEccezioneProcessamento()!=null) {
  370.                                         this.logCore.error(msgErrore,esitoValidazioneToken.getEccezioneProcessamento());
  371.                                     }
  372.                                     else {
  373.                                         this.logCore.error(msgErrore);
  374.                                     }
  375.                                
  376.                                     erroreCooperazione = esitoValidazioneToken.getErroreCooperazione();
  377.                                     erroreIntegrazione = esitoValidazioneToken.getErroreIntegrazione();
  378.                                     eGestioneToken = esitoValidazioneToken.getEccezioneProcessamento();
  379.                                     errorMessageGestioneToken = esitoValidazioneToken.getErrorMessage();
  380.                                     wwwAuthenticateErrorHeader = esitoValidazioneToken.getWwwAuthenticateErrorHeader();
  381.                                     integrationFunctionError = esitoValidazioneToken.getIntegrationFunctionError();
  382.                                    
  383.                                 }
  384.                             }
  385.                             else {
  386.                                 this.msgDiag.logPersonalizzato("gestioneTokenInCorso.validazioneToken.disabilitata");
  387.                             }
  388.                            
  389.                         }
  390.                        
  391.                        
  392.                         // introspection
  393.                         if(!fineGestione) {
  394.                            
  395.                             if(policyGestioneToken.isIntrospection()) {
  396.                            
  397.                                 String endpoint = null;
  398.                                 if(esitoDynamicDiscovery!=null) {
  399.                                     if(esitoDynamicDiscovery.isValido() && esitoDynamicDiscovery.getDynamicDiscovery()!=null) {
  400.                                         endpoint = esitoDynamicDiscovery.getDynamicDiscovery().getIntrospectionEndpoint();
  401.                                     }
  402.                                     else {
  403.                                         endpoint = "dynamic discovery";
  404.                                     }
  405.                                 }
  406.                                 else {
  407.                                     endpoint = policyGestioneToken.getIntrospectionEndpoint();
  408.                                 }
  409.                                 this.msgDiag.addKeyword(CostantiPdD.KEY_TOKEN_ENDPOINT_SERVIZIO_INTROSPECTION, endpoint);
  410.                                
  411.                                 this.msgDiag.logPersonalizzato("gestioneTokenInCorso.introspectionToken");
  412.                                
  413.                                 esitoIntrospectionToken = gestioneTokenEngine.introspectionToken(this.bustaRichiesta, datiInvocazione, esitoPresenzaToken, esitoDynamicDiscovery);
  414.                                 if(esitoIntrospectionToken.isValido()) {
  415.                                    
  416.                                     this.msgDiag.logPersonalizzato("gestioneTokenInCorso.introspectionToken.completataSuccesso");
  417.                                    
  418.                                     this.msgDiag.addKeyword(CostantiPdD.KEY_TOKEN_INFO, esitoIntrospectionToken.getInformazioniToken().getRawResponse());
  419.                                    
  420.                                     this.pddContext.addObject(org.openspcoop2.pdd.core.token.Costanti.PDD_CONTEXT_TOKEN_ESITO_INTROSPECTION, esitoIntrospectionToken);
  421.                                    
  422.                                     if(esitoIntrospectionToken.isInCache()) {
  423.                                         this.msgDiag.logPersonalizzato("gestioneTokenInCorso.introspectionToken.inCache");
  424.                                     }
  425.                                     else {
  426.                                         this.msgDiag.logPersonalizzato("gestioneTokenInCorso.introspectionToken.validato");
  427.                                     }
  428.                                 }
  429.                                 else {
  430.                                    
  431.                                     this.msgDiag.addKeyword(CostantiPdD.KEY_ERRORE_PROCESSAMENTO, esitoIntrospectionToken.getDetails());
  432.                                     if(policyGestioneToken.isIntrospectionWarningOnly()) {
  433.                                         this.msgDiag.logPersonalizzato("gestioneTokenInCorso.introspectionToken.warningOnly.fallita");
  434.                                     }
  435.                                     else {
  436.                                         this.msgDiag.logPersonalizzato("gestioneTokenInCorso.introspectionToken.fallita");
  437.                                         fineGestione = true;
  438.                                     }
  439.                                    
  440.                                     String msgErrore = gestioneTokenPrefix+"(Introspection) fallito: " + esitoIntrospectionToken.getDetails();
  441.                                     if(esitoIntrospectionToken.getEccezioneProcessamento()!=null) {
  442.                                         this.logCore.error(msgErrore,esitoIntrospectionToken.getEccezioneProcessamento());
  443.                                     }
  444.                                     else {
  445.                                         this.logCore.error(msgErrore);
  446.                                     }
  447.                                
  448.                                     erroreCooperazione = esitoIntrospectionToken.getErroreCooperazione();
  449.                                     erroreIntegrazione = esitoIntrospectionToken.getErroreIntegrazione();
  450.                                     eGestioneToken = esitoIntrospectionToken.getEccezioneProcessamento();
  451.                                     errorMessageGestioneToken = esitoIntrospectionToken.getErrorMessage();
  452.                                     wwwAuthenticateErrorHeader = esitoIntrospectionToken.getWwwAuthenticateErrorHeader();
  453.                                     integrationFunctionError = esitoIntrospectionToken.getIntegrationFunctionError();
  454.                                    
  455.                                 }
  456.                             }
  457.                             else {
  458.                                 this.msgDiag.logPersonalizzato("gestioneTokenInCorso.introspectionToken.disabilitata");
  459.                             }
  460.                            
  461.                         }
  462.                        
  463.                        
  464.                         // userInfo
  465.                         if(!fineGestione) {
  466.                            
  467.                             if(policyGestioneToken.isUserInfo()) {
  468.                            
  469.                                 String endpoint = null;
  470.                                 if(esitoDynamicDiscovery!=null) {
  471.                                     if(esitoDynamicDiscovery.isValido() && esitoDynamicDiscovery.getDynamicDiscovery()!=null) {
  472.                                         endpoint = esitoDynamicDiscovery.getDynamicDiscovery().getUserinfoEndpoint();
  473.                                     }
  474.                                     else {
  475.                                         endpoint = "dynamic discovery";
  476.                                     }
  477.                                 }
  478.                                 else {
  479.                                     endpoint = policyGestioneToken.getUserInfoEndpoint();
  480.                                 }
  481.                                 this.msgDiag.addKeyword(CostantiPdD.KEY_TOKEN_ENDPOINT_SERVIZIO_USER_INFO, endpoint);
  482.                                
  483.                                 this.msgDiag.logPersonalizzato("gestioneTokenInCorso.userInfoToken");
  484.                                
  485.                                 esitoUserInfoToken = gestioneTokenEngine.userInfoToken(this.bustaRichiesta, datiInvocazione, esitoPresenzaToken, esitoDynamicDiscovery);
  486.                                 if(esitoUserInfoToken.isValido()) {
  487.                                    
  488.                                     this.msgDiag.logPersonalizzato("gestioneTokenInCorso.userInfoToken.completataSuccesso");
  489.                                    
  490.                                     this.msgDiag.addKeyword(CostantiPdD.KEY_TOKEN_INFO, esitoUserInfoToken.getInformazioniToken().getRawResponse());
  491.                                    
  492.                                     this.pddContext.addObject(org.openspcoop2.pdd.core.token.Costanti.PDD_CONTEXT_TOKEN_ESITO_USER_INFO, esitoUserInfoToken);
  493.                                    
  494.                                     if(esitoUserInfoToken.isInCache()) {
  495.                                         this.msgDiag.logPersonalizzato("gestioneTokenInCorso.userInfoToken.inCache");
  496.                                     }
  497.                                     else {
  498.                                         this.msgDiag.logPersonalizzato("gestioneTokenInCorso.userInfoToken.validato");
  499.                                     }
  500.                                 }
  501.                                 else {
  502.                                    
  503.                                     this.msgDiag.addKeyword(CostantiPdD.KEY_ERRORE_PROCESSAMENTO, esitoUserInfoToken.getDetails());
  504.                                     if(policyGestioneToken.isUserInfoWarningOnly()) {
  505.                                         this.msgDiag.logPersonalizzato("gestioneTokenInCorso.userInfoToken.warningOnly.fallita");
  506.                                     }
  507.                                     else {
  508.                                         this.msgDiag.logPersonalizzato("gestioneTokenInCorso.userInfoToken.fallita");
  509.                                         fineGestione = true;
  510.                                     }
  511.                                    
  512.                                     String msgErrore = gestioneTokenPrefix+"(UserInfo) fallito: " + esitoUserInfoToken.getDetails();
  513.                                     if(esitoUserInfoToken.getEccezioneProcessamento()!=null) {
  514.                                         this.logCore.error(msgErrore,esitoUserInfoToken.getEccezioneProcessamento());
  515.                                     }
  516.                                     else {
  517.                                         this.logCore.error(msgErrore);
  518.                                     }
  519.                                
  520.                                     erroreCooperazione = esitoUserInfoToken.getErroreCooperazione();
  521.                                     erroreIntegrazione = esitoUserInfoToken.getErroreIntegrazione();
  522.                                     eGestioneToken = esitoUserInfoToken.getEccezioneProcessamento();
  523.                                     errorMessageGestioneToken = esitoUserInfoToken.getErrorMessage();
  524.                                     wwwAuthenticateErrorHeader = esitoUserInfoToken.getWwwAuthenticateErrorHeader();
  525.                                     integrationFunctionError = esitoUserInfoToken.getIntegrationFunctionError();
  526.                                    
  527.                                 }
  528.                             }
  529.                             else {
  530.                                 this.msgDiag.logPersonalizzato("gestioneTokenInCorso.userInfoToken.disabilitata");
  531.                             }
  532.                            
  533.                         }
  534.                        
  535.                        
  536.                     }
  537.                     else {
  538.                        
  539.                         if(!policyGestioneToken.isTokenOpzionale()) {
  540.                        
  541.                             this.msgDiag.addKeyword(CostantiPdD.KEY_ERRORE_PROCESSAMENTO, esitoPresenzaToken.getDetails());
  542.                             this.msgDiag.logPersonalizzato("gestioneTokenInCorso.verificaPresenzaToken.fallita");
  543.                            
  544.                             fineGestione = true;
  545.                            
  546.                             String msgErrore = gestioneTokenPrefix+"fallito: " + esitoPresenzaToken.getDetails();
  547.                             if(esitoPresenzaToken.getEccezioneProcessamento()!=null) {
  548.                                 this.logCore.error(msgErrore,esitoPresenzaToken.getEccezioneProcessamento());
  549.                             }
  550.                             else {
  551.                                 this.logCore.error(msgErrore);
  552.                             }
  553.                        
  554.                             erroreCooperazione = esitoPresenzaToken.getErroreCooperazione();
  555.                             erroreIntegrazione = esitoPresenzaToken.getErroreIntegrazione();
  556.                             eGestioneToken = esitoPresenzaToken.getEccezioneProcessamento();
  557.                             errorMessageGestioneToken = esitoPresenzaToken.getErrorMessage();
  558.                             wwwAuthenticateErrorHeader = esitoPresenzaToken.getWwwAuthenticateErrorHeader();
  559.                             integrationFunctionError = IntegrationFunctionError.TOKEN_NOT_FOUND;
  560.                            
  561.                         }
  562.                     }
  563.            
  564.                     if(fineGestione) {
  565.                         if(esitoPresenzaToken.isPresente()) {
  566.                             this.pddContext.addObject(org.openspcoop2.core.constants.Costanti.ERRORE_TOKEN, "true");
  567.                         }
  568.                         else {
  569.                             this.pddContext.addObject(org.openspcoop2.core.constants.Costanti.TOKEN_NON_PRESENTE, "true");
  570.                         }
  571.                         this.msgDiag.logPersonalizzato("gestioneTokenFallita");
  572.                        
  573.                         List<InformazioniToken> listaEsiti = GestoreToken.getInformazioniTokenNonValide(esitoValidazioneToken, esitoIntrospectionToken, esitoUserInfoToken);
  574.                         InformazioniToken informazioniTokenNormalizzate = null;
  575.                         if(listaEsiti!=null && !listaEsiti.isEmpty()) {
  576.                             informazioniTokenNormalizzate = GestoreToken.normalizeInformazioniToken(listaEsiti);
  577.                             informazioniTokenNormalizzate.setValid(false);
  578.                         }
  579.                         if(informazioniTokenNormalizzate==null && esitoDynamicDiscovery!=null && esitoDynamicDiscovery.getDynamicDiscovery()!=null) {
  580.                             informazioniTokenNormalizzate = new InformazioniToken();
  581.                             if(esitoPresenzaToken!=null) {
  582.                                 informazioniTokenNormalizzate.setToken(esitoPresenzaToken.getToken());
  583.                             }
  584.                             informazioniTokenNormalizzate.setDynamicDiscovery(esitoDynamicDiscovery.getDynamicDiscovery());
  585.                         }
  586.                         if(informazioniTokenNormalizzate!=null) {
  587.                            
  588.                             if(esitoDynamicDiscovery!=null) {
  589.                                 informazioniTokenNormalizzate.setDynamicDiscovery(esitoDynamicDiscovery.getDynamicDiscovery());
  590.                             }
  591.                            
  592.                             this.pddContext.addObject(org.openspcoop2.pdd.core.token.Costanti.PDD_CONTEXT_TOKEN_INFORMAZIONI_NORMALIZZATE, informazioniTokenNormalizzate);
  593.                            
  594.                             this.transaction.setInformazioniToken(informazioniTokenNormalizzate);
  595.                         }
  596.                     }
  597.                     else {
  598.                         if(esitoPresenzaToken.isPresente()) {
  599.                             List<InformazioniToken> listaEsiti = GestoreToken.getInformazioniTokenValide(esitoValidazioneToken, esitoIntrospectionToken, esitoUserInfoToken);
  600.                             InformazioniToken informazioniTokenNormalizzate = null;
  601.                             if(listaEsiti!=null && !listaEsiti.isEmpty()) {
  602.                                 informazioniTokenNormalizzate = GestoreToken.normalizeInformazioniToken(listaEsiti);
  603.                                 informazioniTokenNormalizzate.setValid(true);
  604.                             }
  605.                             if(informazioniTokenNormalizzate==null && esitoDynamicDiscovery!=null && esitoDynamicDiscovery.getDynamicDiscovery()!=null) {
  606.                                 informazioniTokenNormalizzate = new InformazioniToken();
  607.                                 if(esitoPresenzaToken!=null) {
  608.                                     informazioniTokenNormalizzate.setToken(esitoPresenzaToken.getToken());
  609.                                 }
  610.                                 informazioniTokenNormalizzate.setDynamicDiscovery(esitoDynamicDiscovery.getDynamicDiscovery());
  611.                             }
  612.                             if(informazioniTokenNormalizzate!=null) {
  613.                                
  614.                                 if(esitoDynamicDiscovery!=null) {
  615.                                     informazioniTokenNormalizzate.setDynamicDiscovery(esitoDynamicDiscovery.getDynamicDiscovery());
  616.                                 }
  617.                                
  618.                                 this.pddContext.addObject(org.openspcoop2.pdd.core.token.Costanti.PDD_CONTEXT_TOKEN_INFORMAZIONI_NORMALIZZATE, informazioniTokenNormalizzate);
  619.                                
  620.                                 this.transaction.setInformazioniToken(informazioniTokenNormalizzate);
  621.                             }
  622.                                        
  623.                             this.msgDiag.mediumDebug("Gestione forward token ...");
  624.                             gestioneTokenEngine.forwardToken(datiInvocazione,esitoPresenzaToken,
  625.                                     esitoValidazioneToken, esitoIntrospectionToken, esitoUserInfoToken,
  626.                                     informazioniTokenNormalizzate,
  627.                                     this.bustaRichiesta);
  628.                             this.msgDiag.mediumDebug("Gestione forward token completata");
  629.                            
  630.                             this.msgDiag.logPersonalizzato("gestioneTokenCompletataConSuccesso");
  631.                         }
  632.                         else {
  633.                             this.msgDiag.logPersonalizzato("gestioneTokenCompletataSenzaRilevazioneToken");
  634.                         }      
  635.                     }
  636.                    
  637.                 } catch (Exception e) {
  638.                    
  639.                     this.msgDiag.addKeyword(CostantiPdD.KEY_ERRORE_PROCESSAMENTO, e.getMessage());
  640.                     this.msgDiag.logPersonalizzato("gestioneTokenFallita.erroreGenerico");
  641.                     this.logCore.error(gestioneTokenPrefix+"fallito, " + e.getMessage(),e);
  642.                    
  643.                     erroreIntegrazione = ErroriIntegrazione.ERRORE_5XX_GENERICO_PROCESSAMENTO_MESSAGGIO.
  644.                             get5XX_ErroreProcessamento(gestioneTokenPrefix+"fallito, " + e.getMessage(),
  645.                                     CodiceErroreIntegrazione.CODICE_560_GESTIONE_TOKEN);
  646.                     erroreCooperazione = null;
  647.                     eGestioneToken = e;
  648.                    
  649.                     integrationFunctionError = IntegrationFunctionError.INTERNAL_REQUEST_ERROR;
  650.                    
  651.                     fineGestione = true;
  652.                    
  653.                 }
  654.                 if (fineGestione) {
  655.                                    
  656.                     // Tracciamento richiesta: non ancora registrata
  657.                     if(this.msgContext.isTracciamentoAbilitato()){
  658.                         EsitoElaborazioneMessaggioTracciato esitoTraccia =
  659.                                 EsitoElaborazioneMessaggioTracciato.getEsitoElaborazioneConErrore("["+ RicezioneBuste.ID_MODULO+ "] "+gestioneTokenPrefix+"fallito");
  660.                         this.tracciamento.registraRichiesta(this.requestMessage,null,this.soapHeaderElement,this.bustaRichiesta,esitoTraccia,
  661.                                 Tracciamento.createLocationString(true,this.msgContext.getSourceLocation()),
  662.                                 this.correlazioneApplicativa);
  663.                     }
  664.                    
  665.                     if(this.msgContext.isGestioneRisposta()){
  666.    
  667.                         if(errorMessageGestioneToken!=null) {
  668.                             this.msgContext.setMessageResponse(errorMessageGestioneToken);
  669.                         }
  670.                         else {
  671.                        
  672.                             this.parametriGenerazioneBustaErrore.setBusta(this.bustaRichiesta);
  673.                             if(erroreIntegrazione != null){
  674.                                 this.parametriGenerazioneBustaErrore.setErroreIntegrazione(erroreIntegrazione);
  675.                             }
  676.                             else{
  677.                                 this.parametriGenerazioneBustaErrore.setErroreCooperazione(erroreCooperazione);
  678.                             }
  679.        
  680.                             OpenSPCoop2Message errorOpenSPCoopMsg = null;
  681.                                        
  682.                             RicezioneBusteGeneratoreBustaErrore ricezioneBusteGeneratoreBustaErrore = new RicezioneBusteGeneratoreBustaErrore(this.msgContext, this.generatoreErrore);
  683.                            
  684.                             if(erroreCooperazione!=null){
  685.                                 if(CodiceErroreCooperazione.SICUREZZA_TOKEN_NON_PRESENTE.equals(erroreCooperazione.getCodiceErrore())) {
  686.                                     if(integrationFunctionError==null) {
  687.                                         integrationFunctionError = IntegrationFunctionError.TOKEN_NOT_FOUND;
  688.                                     }
  689.                                     this.parametriGenerazioneBustaErrore.setIntegrationFunctionError(integrationFunctionError);
  690.                                     errorOpenSPCoopMsg = ricezioneBusteGeneratoreBustaErrore.generaBustaErroreValidazione(this.parametriGenerazioneBustaErrore);
  691.                                 }
  692.                                 else if(CodiceErroreCooperazione.SICUREZZA_TOKEN_NON_VALIDO.equals(erroreCooperazione.getCodiceErrore())
  693.                                         ||
  694.                                         CodiceErroreCooperazione.SICUREZZA_TOKEN_PRESENTE_PIU_VOLTE.equals(erroreCooperazione.getCodiceErrore())) {
  695.                                     if(integrationFunctionError==null) {
  696.                                         integrationFunctionError = IntegrationFunctionError.TOKEN_INVALID;
  697.                                     }
  698.                                     this.parametriGenerazioneBustaErrore.setIntegrationFunctionError(integrationFunctionError);
  699.                                     errorOpenSPCoopMsg = ricezioneBusteGeneratoreBustaErrore.generaBustaErroreValidazione(this.parametriGenerazioneBustaErrore);
  700.                                 }
  701.                                 else {
  702.                                     if(integrationFunctionError==null) {
  703.                                         integrationFunctionError = IntegrationFunctionError.INTERNAL_REQUEST_ERROR;
  704.                                     }
  705.                                     this.parametriGenerazioneBustaErrore.setIntegrationFunctionError(integrationFunctionError);
  706.                                     errorOpenSPCoopMsg = ricezioneBusteGeneratoreBustaErrore.generaBustaErroreProcessamento(this.parametriGenerazioneBustaErrore,eGestioneToken);
  707.                                 }
  708.                             }
  709.                             else {
  710.                                 if(integrationFunctionError==null) {
  711.                                     integrationFunctionError = IntegrationFunctionError.INTERNAL_REQUEST_ERROR;
  712.                                 }
  713.                                 this.parametriGenerazioneBustaErrore.setIntegrationFunctionError(integrationFunctionError);
  714.                                 errorOpenSPCoopMsg = ricezioneBusteGeneratoreBustaErrore.generaBustaErroreProcessamento(this.parametriGenerazioneBustaErrore,eGestioneToken);
  715.                             }  
  716.                            
  717.                             if(wwwAuthenticateErrorHeader!=null) {
  718.                                 errorOpenSPCoopMsg.forceTransportHeader(HttpConstants.AUTHORIZATION_RESPONSE_WWW_AUTHENTICATE, wwwAuthenticateErrorHeader);
  719.                             }
  720.                            
  721.                             // Nota: la bustaRichiesta e' stata trasformata da generaErroreProcessamento
  722.                             this.parametriInvioBustaErrore.setOpenspcoopMsg(errorOpenSPCoopMsg);
  723.                             this.parametriInvioBustaErrore.setBusta(this.parametriGenerazioneBustaErrore.getBusta());
  724.                             ricezioneBusteGeneratoreBustaErrore.sendRispostaBustaErrore(this.parametriInvioBustaErrore);
  725.                         }
  726.    
  727.                     }
  728.                                
  729.                     updateCredenzialiToken();
  730.                    
  731.                     this.openspcoopstate.releaseResource();
  732.                     return false;
  733.                    
  734.                 }
  735.             }finally {
  736.                 this.transaction.getTempiElaborazione().endToken();
  737.             }

  738.         }
  739.        
  740.         return true;
  741.     }
  742.    
  743.     private void updateCredenzialiToken() {
  744.        
  745.         // Viene chiamato se la validazione fallisce
  746.        
  747.         if(OpenSPCoop2Properties.getInstance().isGestioneTokenSaveTokenAuthenticationInfoValidationFailed()) {
  748.             CredenzialiMittente credenzialiMittente = this.transaction.getCredenzialiMittente();
  749.             if(credenzialiMittente==null) {
  750.                 credenzialiMittente = new CredenzialiMittente();
  751.                 try {
  752.                     this.transaction.setCredenzialiMittente(credenzialiMittente);
  753.                 }catch(Exception e) {
  754.                     this.logCore.error("SetCredenzialiMittente error: "+e.getMessage(),e);
  755.                 }
  756.             }
  757.             InformazioniToken informazioniTokenNormalizzate = null;
  758.             if(this.pddContext.containsKey(org.openspcoop2.pdd.core.token.Costanti.PDD_CONTEXT_TOKEN_INFORMAZIONI_NORMALIZZATE)) {
  759.                 informazioniTokenNormalizzate = (InformazioniToken) this.pddContext.getObject(org.openspcoop2.pdd.core.token.Costanti.PDD_CONTEXT_TOKEN_INFORMAZIONI_NORMALIZZATE);
  760.             }
  761.             if(informazioniTokenNormalizzate!=null) {
  762.                 try {
  763.                     GestoreAutenticazione.updateCredenzialiToken(
  764.                             this.identitaPdD,
  765.                             RicezioneBuste.ID_MODULO, this.idTransazione, informazioniTokenNormalizzate, null, credenzialiMittente,
  766.                             this.openspcoopstate, "RicezioneBuste.credenzialiToken", this.requestInfo,
  767.                             this.pddContext);
  768.                 }catch(Exception e) {
  769.                     this.logCore.error("updateCredenzialiToken error: "+e.getMessage(),e);
  770.                 }
  771.             }
  772.         }
  773.     }
  774. }