GestoreCredenzialiTest.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.credenziali;

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

  23. import org.openspcoop2.core.id.IDSoggetto;
  24. import org.openspcoop2.message.OpenSPCoop2Message;
  25. import org.openspcoop2.pdd.core.AbstractCore;
  26. import org.openspcoop2.pdd.core.connettori.InfoConnettoreIngresso;
  27. import org.openspcoop2.pdd.core.credenziali.engine.GestoreCredenzialiEngine;
  28. import org.openspcoop2.protocol.sdk.constants.IntegrationFunctionError;
  29. import org.openspcoop2.utils.transport.TransportUtils;

  30. /**
  31.  * Esempio che definisce un gestore delle credenziali
  32.  *
  33.  * @author Andrea Poli (apoli@link.it)
  34.  * @author $Author$
  35.  * @version $Rev$, $Date$
  36.  */
  37. public class GestoreCredenzialiTest extends AbstractCore implements IGestoreCredenziali {

  38.     public static final String TEST_CREDENZIALI_BASIC_USERNAME = "GovWay-TestCredenziali-BasicUsername";
  39.     public static final String TEST_CREDENZIALI_BASIC_PASSWORD = "GovWay-TestCredenziali-BasicPassword";
  40.     public static final String TEST_CREDENZIALI_SSL_SUBJECT = "GovWay-TestCredenziali-SSLSubject";
  41.     public static final String TEST_CREDENZIALI_SIMULAZIONE_ERRORE = "GovWay-TestCredenziali-SimulazioneErrore";
  42.     public static final String TEST_CREDENZIALI_SIMULAZIONE_ERRORE_CONFIGURAZIONE = "GovWay-TestCredenziali-SimulazioneErroreConfigurazione";
  43.     public static final String TEST_CREDENZIALI_SIMULAZIONE_ERRORE_FORWARD = "GovWay-TestCredenziali-SimulazioneErroreForward";
  44.    
  45.     private String identita = null;
  46.    
  47.     @Override
  48.     public Credenziali elaborazioneCredenziali(IDSoggetto idSoggetto,
  49.             InfoConnettoreIngresso infoConnettoreIngresso,
  50.             OpenSPCoop2Message messaggio) throws GestoreCredenzialiException,GestoreCredenzialiConfigurationException{
  51.        
  52.         Credenziali c = new Credenziali();
  53.        
  54.         String realm = "GovWay";
  55.         String authType = "ProxyAuth";
  56.        
  57.         if(existsHeader(infoConnettoreIngresso.getUrlProtocolContext().getHeaders(), GestoreCredenzialiTest.TEST_CREDENZIALI_BASIC_USERNAME)){
  58.             String username = getProperty(infoConnettoreIngresso.getUrlProtocolContext().getHeaders(),GestoreCredenzialiTest.TEST_CREDENZIALI_BASIC_USERNAME);  
  59.             String password = getProperty(infoConnettoreIngresso.getUrlProtocolContext().getHeaders(),GestoreCredenzialiTest.TEST_CREDENZIALI_BASIC_PASSWORD);
  60.             if(username==null || "".equals(username)){
  61.                 throw new GestoreCredenzialiConfigurationException(IntegrationFunctionError.PROXY_AUTHENTICATION_CREDENTIALS_NOT_FOUND,
  62.                         GestoreCredenzialiEngine.buildWWWProxyAuthBasic(authType, realm, true),
  63.                         "Username value non fornito");
  64.             }
  65.             if(password==null || "".equals(password)){
  66.                 throw new GestoreCredenzialiConfigurationException(IntegrationFunctionError.PROXY_AUTHENTICATION_CREDENTIALS_NOT_FOUND,
  67.                         GestoreCredenzialiEngine.buildWWWProxyAuthBasic(authType, realm, true),
  68.                         "Password value non fornito");
  69.             }
  70.             c.setUsername(username);
  71.             c.setPassword(password);
  72.         }
  73.         else if(existsHeader(infoConnettoreIngresso.getUrlProtocolContext().getHeaders(), GestoreCredenzialiTest.TEST_CREDENZIALI_SSL_SUBJECT)){
  74.             String subject = getProperty(infoConnettoreIngresso.getUrlProtocolContext().getHeaders(),GestoreCredenzialiTest.TEST_CREDENZIALI_SSL_SUBJECT);
  75.             if(subject==null || "".equals(subject)){
  76.                 throw new GestoreCredenzialiConfigurationException(IntegrationFunctionError.PROXY_AUTHENTICATION_CREDENTIALS_NOT_FOUND,
  77.                         GestoreCredenzialiEngine.buildWWWProxyAuthSSL(authType, realm, true),
  78.                         "Subject value non fornito");
  79.             }
  80.             c.setSubject(subject);
  81.         }
  82.         else if(existsHeader(infoConnettoreIngresso.getUrlProtocolContext().getHeaders(), GestoreCredenzialiTest.TEST_CREDENZIALI_SIMULAZIONE_ERRORE)){
  83.             throw new GestoreCredenzialiException("Eccezione generale richiesta dalla testsuite");
  84.         }
  85.         else if(existsHeader(infoConnettoreIngresso.getUrlProtocolContext().getHeaders(), GestoreCredenzialiTest.TEST_CREDENZIALI_SIMULAZIONE_ERRORE_CONFIGURAZIONE)){
  86.             throw new GestoreCredenzialiConfigurationException(IntegrationFunctionError.PROXY_AUTHENTICATION_INVALID_CREDENTIALS,
  87.                     GestoreCredenzialiEngine.buildWWWProxyAuthSSL(authType, realm, false),
  88.                     "Eccezione, di configurazione, richiesta dalla testsuite");
  89.         }
  90.         else if(existsHeader(infoConnettoreIngresso.getUrlProtocolContext().getHeaders(), GestoreCredenzialiTest.TEST_CREDENZIALI_SIMULAZIONE_ERRORE_FORWARD)){
  91.             throw new GestoreCredenzialiConfigurationException(IntegrationFunctionError.PROXY_AUTHENTICATION_FORWARDED_CREDENTIALS_NOT_FOUND,
  92.                     GestoreCredenzialiEngine.buildWWWAuthSSL(),
  93.                     "Eccezione, di configurazione, richiesta dalla testsuite");
  94.         }
  95.         else{
  96.             // ritorno credenziali originali
  97.             return infoConnettoreIngresso.getCredenziali();
  98.         }
  99.        
  100.         if(infoConnettoreIngresso.getCredenziali().getSubject()!=null || infoConnettoreIngresso.getCredenziali().getUsername()!=null)
  101.             this.identita = "GestoreCredenziali di test "+infoConnettoreIngresso.getCredenziali().toString();
  102.         else
  103.             this.identita = "GestoreCredenziali di test anonimo";
  104.        
  105.         return c;
  106.        
  107.     }
  108.    
  109.     @Override
  110.     public String getIdentitaGestoreCredenziali(){
  111.         return this.identita;
  112.     }
  113.    
  114.     private boolean existsHeader(Map<String, List<String>> properties, String name){
  115.         if(properties!=null){
  116.             return TransportUtils.containsKey(properties, name);
  117.         }else{
  118.             return false;
  119.         }
  120.     }
  121.    
  122.     private String getProperty(Map<String, List<String>> properties, String name){
  123.         if(properties!=null){
  124.             return TransportUtils.getFirstValue(properties, name);
  125.         }else{
  126.             return null;
  127.         }
  128.        
  129.     }
  130. }