UtilitiesAutenticazione.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.core.integrazione;

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

  24. import org.openspcoop2.core.config.Proprieta;
  25. import org.openspcoop2.message.OpenSPCoop2Message;
  26. import org.openspcoop2.pdd.config.OpenSPCoop2Properties;
  27. import org.openspcoop2.protocol.sdk.Busta;
  28. import org.openspcoop2.protocol.sdk.Context;
  29. import org.slf4j.Logger;

  30. /**
  31.  * Classe contenenti utilities per le integrazioni gestite tramite autenticazione
  32.  *
  33.  * @author Poli Andrea (apoli@link.it)
  34.  * @author $Author$
  35.  * @version $Rev$, $Date$
  36.  */
  37. public class UtilitiesAutenticazione {

  38.     private OpenSPCoop2Message msg;
  39.     @SuppressWarnings("unused")
  40.     private Context context;
  41.     @SuppressWarnings("unused")
  42.     private Busta busta;
  43.     @SuppressWarnings("unused")
  44.     private Logger log;
  45.    
  46.     private List<String> headers = null;
  47.     private Map<String, String> hdrValues = null;
  48.    
  49.     public UtilitiesAutenticazione(HeaderIntegrazione integrazione,
  50.             OutRequestPDMessage inRequestPDMessage, Context context, Logger log) throws HeaderIntegrazioneException {
  51.         try {
  52.             init(inRequestPDMessage.getMessage(), context, inRequestPDMessage.getBustaRichiesta(), log);
  53.            
  54.             List<Proprieta> proprieta = null;
  55.             if(inRequestPDMessage.getPortaDelegata()!=null && inRequestPDMessage.getPortaDelegata().getProprietaList()!=null) {
  56.                 proprieta = inRequestPDMessage.getPortaDelegata().getProprietaList();
  57.             }
  58.             init(proprieta, true);
  59.            
  60.         }catch(Exception e) {
  61.             throw new HeaderIntegrazioneException(e.getMessage(),e);
  62.         }
  63.     }
  64.    
  65.     public UtilitiesAutenticazione(HeaderIntegrazione integrazione,
  66.             OutRequestPAMessage inRequestPAMessage, Context context, Logger log) throws HeaderIntegrazioneException {
  67.         try {
  68.             init(inRequestPAMessage.getMessage(), context, inRequestPAMessage.getBustaRichiesta(), log);
  69.            
  70.             List<Proprieta> proprieta = null;
  71.             if(inRequestPAMessage.getPortaApplicativa()!=null && inRequestPAMessage.getPortaApplicativa().getProprietaList()!=null) {
  72.                 proprieta = inRequestPAMessage.getPortaApplicativa().getProprietaList();
  73.             }
  74.             init(proprieta, false);
  75.            
  76.         }catch(Exception e) {
  77.             throw new HeaderIntegrazioneException(e.getMessage(),e);
  78.         }
  79.     }
  80.    
  81.     private void init(OpenSPCoop2Message msg, Context context, Busta busta, Logger log){
  82.         this.msg = msg;
  83.         this.context = context;
  84.         this.busta = busta;
  85.         this.log = log;
  86.     }
  87.     private void init(List<Proprieta> proprieta, boolean portaDelegata) throws HeaderIntegrazioneException {
  88.         try {
  89.             OpenSPCoop2Properties properties = OpenSPCoop2Properties.getInstance();

  90.             if(portaDelegata) {
  91.                 this.headers = properties.getIntegrazioneAutenticazionePortaDelegataRequestHeaders();
  92.                 this.hdrValues = properties.getIntegrazioneAutenticazionePortaDelegataRequestHeadersMap();
  93.             }
  94.             else {
  95.                 this.headers = properties.getIntegrazioneAutenticazionePortaApplicativaRequestHeaders();
  96.                 this.hdrValues = properties.getIntegrazioneAutenticazionePortaApplicativaRequestHeadersMap();
  97.             }
  98.            
  99.             if(proprieta!=null && !proprieta.isEmpty()) {
  100.                
  101.                 String headersPropertyName = properties.getIntegrazioneAutenticazionePropertyHeaders();
  102.                 String headerPrefixPropertyName = properties.getIntegrazioneAutenticazionePropertyHeaderPrefix();
  103.                
  104.                 for (Proprieta p : proprieta) {
  105.                     if(headersPropertyName.equalsIgnoreCase(p.getNome())) {
  106.                         this.headers = convert(p.getValore(), headersPropertyName);
  107.                     }
  108.                 }
  109.                 if(this.headers!=null && !this.headers.isEmpty()) {
  110.                     for (String hdr : this.headers) {
  111.                         String pName = headerPrefixPropertyName+hdr;
  112.                         for (Proprieta p : proprieta) {
  113.                             if(pName.equalsIgnoreCase(p.getNome())) {
  114.                                 String valore = p.getValore();
  115.                                 this.hdrValues.put(hdr, valore); // aggiorno valore se già definito
  116.                             }
  117.                         }
  118.                     }
  119.                 }
  120.             }

  121.             if(this.headers==null || this.headers.isEmpty()) {
  122.                 throw new HeaderIntegrazioneException("Nessun header di autenticazione configurato");
  123.             }
  124.             if(this.hdrValues==null || this.hdrValues.isEmpty()) {
  125.                 throw new HeaderIntegrazioneException("Nessun valore definito per gli header di autenticazione configurati");
  126.             }
  127.             for (String hdr : this.headers) {
  128.                 String v = this.hdrValues.get(hdr);
  129.                 if(v==null) {
  130.                     throw new HeaderIntegrazioneException("Nessun valore definito per l'header di autenticazione '"+hdr+"' configurato");
  131.                 }
  132.             }

  133.         }catch(Exception e) {
  134.             throw new HeaderIntegrazioneException(e.getMessage(),e);
  135.         }
  136.     }

  137.     public static List<String> convert(String headers, String pName) throws HeaderIntegrazioneException {
  138.         try {
  139.             List<String> l = new ArrayList<>();
  140.             if(headers!=null) {
  141.                 headers = headers.trim();
  142.                 String [] split = headers.split(",");
  143.                 if(split!=null){
  144.                     for (int i = 0; i < split.length; i++) {
  145.                         String v = split[i];
  146.                         if(v!=null) {
  147.                             v = v.trim();
  148.                         }
  149.                         if(!"".equals(v)) {
  150.                             l.add(v);
  151.                         }
  152.                     }
  153.                 }
  154.             }
  155.             if(l.isEmpty()) {
  156.                 throw new HeaderIntegrazioneException("Trovata proprietà '"+pName+"' che non contiene alcun header");
  157.             }
  158.             return l;
  159.         }catch(Exception e) {
  160.             throw new HeaderIntegrazioneException(e.getMessage(),e);
  161.         }
  162.     }

  163.        
  164.     public void process() throws HeaderIntegrazioneException {
  165.         try {
  166.             if(this.msg==null) {
  167.                 return;
  168.             }
  169.            
  170.             for (String hdr : this.headers) {
  171.                 String v = this.hdrValues.get(hdr);
  172.                 this.msg.forceTransportHeader(hdr, v);
  173.             }
  174.            
  175.         }catch(Exception e) {
  176.             throw new HeaderIntegrazioneException(e.getMessage(),e);
  177.         }
  178.     }
  179. }