ExternalPWCallback.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.security.utils;

  21. import java.io.File;
  22. import java.io.FileInputStream;
  23. import java.io.IOException;
  24. import java.util.Date;
  25. import java.util.Iterator;
  26. import java.util.Properties;

  27. import javax.security.auth.callback.Callback;
  28. import javax.security.auth.callback.CallbackHandler;
  29. import javax.security.auth.callback.UnsupportedCallbackException;

  30. import org.apache.wss4j.common.ext.WSPasswordCallback;
  31. import org.openspcoop2.security.SecurityException;
  32. import org.openspcoop2.utils.date.DateManager;
  33. import org.openspcoop2.utils.properties.PropertiesReader;

  34. /**
  35.  * Gestore delle password dei certificati scambiati con wssecurity. Le password vengono mantenute in un file di proprietà
  36.  *  
  37.  * @author Andrea Poli (apoli@link.it)
  38.  * @author $Author$
  39.  * @version $Rev$, $Date$
  40.  */
  41. public class ExternalPWCallback
  42.     implements CallbackHandler
  43. {

  44.     public ExternalPWCallback()
  45.     {
  46.     }

  47.     @Override
  48.     public void handle(Callback[] callbacks) throws IOException,
  49.                UnsupportedCallbackException {
  50.         for(int i = 0; i < callbacks.length; i++)
  51.             if(callbacks[i] instanceof WSPasswordCallback)
  52.             {
  53.                 WSPasswordCallback pc = (WSPasswordCallback)callbacks[i];
  54.                
  55.                 Properties pSource = null;
  56.                 try{
  57.                     pSource = getProperties();
  58.                 }catch(Exception e){
  59.                     throw new UnsupportedCallbackException(callbacks[i], "Identifier ["+pc.getIdentifier()+"], occurs error (read password from properties file): "+e.getMessage());
  60.                 }
  61.                 PropertiesReader pr = new PropertiesReader(pSource, true);
  62.                 Properties p = null;
  63.                 try{
  64.                     p = pr.readProperties_convertEnvProperties("user.",true);
  65.                 }catch(Exception e){
  66.                     throw new UnsupportedCallbackException(callbacks[i], "Identifier ["+pc.getIdentifier()+"], occurs error (read password from properties file): "+e.getMessage());
  67.                 }
  68.                 String key = pc.getIdentifier();
  69.                 if(p.containsKey(key)==false){
  70.                    
  71.                     // check se fosse con maiuscolo minuscolo
  72.                     if(p.size()>0){
  73.                         Iterator<?> it = p.keySet().iterator();
  74.                         while (it.hasNext()) {
  75.                             Object keyCheck = (Object) it.next();
  76.                             if(keyCheck instanceof String){
  77.                                 String tmp = (String) keyCheck;
  78.                                 if(tmp.toLowerCase().equals(key)){
  79.                                     key = tmp; // aggiorno la chiave
  80.                                 }
  81.                             }
  82.                         }
  83.                     }
  84.                     if(key==null){
  85.                         throw new UnsupportedCallbackException(callbacks[i], "Identifier ["+pc.getIdentifier()+"] unknown");
  86.                     }
  87.                 }
  88.                 String password = p.getProperty(key);
  89.                 pc.setPassword(password);
  90.                
  91.             } else
  92.             {
  93.                 throw new UnsupportedCallbackException(callbacks[i], "Unrecognized Callback");
  94.             }

  95.     }
  96.    
  97.    
  98.     private static String propertiesFilePath = "/etc/govway/wssPassword.properties";
  99.     private static Properties wssProperties = null;
  100.    
  101.     private static String wssRefreshProps = "refresh";
  102.     private static boolean wssRefresh = true; // default
  103.     private static Date wssRead = null;
  104.     private static int wssTime = 1*1000*60; // ogni minuti refresh
  105.    
  106.     public static synchronized void initialize() throws SecurityException{
  107.         initialize(propertiesFilePath);
  108.     }
  109.     public static synchronized void initialize(String path) throws SecurityException{
  110.        
  111.         propertiesFilePath = path; // aggiorno path nel caso mi venga inizializzato un path differente. Il path verrà utilizzato poi in presenza del file scaduto
  112.        
  113.         if(wssProperties==null || isScaduto()){
  114.             FileInputStream fin = null;
  115.             try{
  116.                 File f = new File(path);
  117.                 if(f.exists()==false){
  118.                     throw new SecurityException("File properties ["+path+"] doesn't exists");
  119.                 }
  120.                 if(f.canRead()==false){
  121.                     throw new SecurityException("File properties ["+path+"] cannot read");
  122.                 }
  123.                 fin = new FileInputStream(f);
  124.                
  125.                 wssProperties = new Properties();
  126.                 wssProperties.load(fin);
  127.                
  128.                 if(wssProperties.containsKey(wssRefreshProps)){
  129.                     String tmp = wssProperties.getProperty(wssRefreshProps);
  130.                     wssRefresh = "true".equalsIgnoreCase(tmp.trim());
  131.                 }
  132.                
  133.                 if(wssRefresh){
  134.                     wssRead = DateManager.getDate(); // update date
  135.                 }
  136.             }
  137.             catch(SecurityException e){
  138.                 throw e;
  139.             }
  140.             catch(Exception e){
  141.                 throw new SecurityException("Errore durante la lettura del file properties ["+path+"]: "+e.getMessage(),e);
  142.             }
  143.             finally{
  144.                 try{
  145.                     if(fin!=null)
  146.                         fin.close();
  147.                 }catch(Exception eClose){
  148.                     // close
  149.                 }
  150.             }
  151.         }
  152.     }
  153.     public static Properties getProperties() throws SecurityException{
  154.         if(wssProperties==null || isScaduto()){
  155.             initialize();
  156.         }
  157.         return wssProperties;
  158.     }
  159.     private static boolean isScaduto(){
  160.         boolean scaduto = false;
  161.         if(wssRefresh && wssRead!=null){
  162.             long read = wssRead.getTime();
  163.             Date now = DateManager.getDate();
  164.             long diff = now.getTime() - read;
  165.             if(diff > wssTime){
  166.                 scaduto = true;
  167.             }
  168.         }
  169.         return scaduto;
  170.     }

  171.    
  172. }