AuthenticationProvider.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.core.config.rs.server.config;

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

  23. import org.openspcoop2.core.config.rs.server.api.impl.Helper;
  24. import org.openspcoop2.utils.UtilsException;
  25. import org.openspcoop2.utils.crypt.CryptConfig;
  26. import org.openspcoop2.utils.crypt.CryptFactory;
  27. import org.openspcoop2.utils.crypt.ICrypt;
  28. import org.openspcoop2.utils.service.beans.utils.BaseHelper;
  29. import org.openspcoop2.web.ctrlstat.core.ControlStationCore;
  30. import org.openspcoop2.web.ctrlstat.servlet.utenti.UtentiCore;
  31. import org.slf4j.Logger;
  32. import org.springframework.security.authentication.AuthenticationCredentialsNotFoundException;
  33. import org.springframework.security.authentication.AuthenticationServiceException;
  34. import org.springframework.security.authentication.BadCredentialsException;
  35. import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
  36. import org.springframework.security.core.Authentication;
  37. import org.springframework.security.core.AuthenticationException;
  38. import org.springframework.security.core.GrantedAuthority;
  39. import org.springframework.security.core.authority.SimpleGrantedAuthority;
  40. import org.springframework.security.core.userdetails.User;


  41. /**
  42.  * AuthenticationProvider
  43.  *
  44.  * @author Andrea Poli (poli@link.it)
  45.  * @author $Author$
  46.  * @version $Rev$, $Date$
  47.  */
  48. public class AuthenticationProvider implements org.springframework.security.authentication.AuthenticationProvider{

  49.     private Logger log = org.slf4j.LoggerFactory.getLogger(this.getClass());

  50.     private String configuratorRoleName = "configuratore";  

  51.     private static ICrypt passwordManager = null;
  52.     private static ICrypt passwordManagerBackwardCompatibility = null;
  53.     private static synchronized void initPasswordManager(Logger log, CryptConfig config) throws UtilsException {
  54.         if(passwordManager==null) {
  55.             passwordManager = CryptFactory.getCrypt(log, config);
  56.             if(config.isBackwardCompatibility()) {
  57.                 passwordManagerBackwardCompatibility = CryptFactory.getOldMD5Crypt(log);
  58.             }
  59.         }
  60.     }
  61.    
  62.     private static String getS(String v) {
  63.         return "sec"+v+"ret";
  64.     }
  65.    
  66.     @Override
  67.     public Authentication authenticate(Authentication authentication) throws AuthenticationException {

  68.         String username = authentication.getName();
  69.         Object passwordObject = authentication.getCredentials();
  70.         String password = (String) passwordObject;

  71.         if(username==null || password==null) {
  72.             throw new AuthenticationCredentialsNotFoundException("Credentials not found");
  73.         }
  74.        
  75.         String tipoProtocollo = null;
  76.         ControlStationCore core = null;
  77.         UtentiCore utentiCore = null;
  78.         try {
  79.             tipoProtocollo = BaseHelper.tipoProtocolloFromProfilo.get(Helper.getProfiloDefault());
  80.             core = new ControlStationCore(true, ServerProperties.getInstance().getConfDirectory() ,tipoProtocollo);
  81.             utentiCore = new UtentiCore(core);
  82.         }catch(Exception e) {
  83.             throw new AuthenticationServiceException("Inizializzazione AuthenticationProvider fallita: "+e.getMessage(),e);
  84.         }
  85.        
  86.         boolean trovato = false;
  87.         org.openspcoop2.web.lib.users.dao.User u = null;
  88.         try {
  89.             trovato = utentiCore.existsUser(username);
  90.             if(trovato) {
  91.                 u = utentiCore.getUser(username);
  92.             }
  93.         }catch(Exception e) {
  94.             throw new AuthenticationServiceException("AuthenticationProvider,ricerca utente fallita: "+e.getMessage(),e);
  95.         }
  96.         if(!trovato) {
  97.             /**throw new UsernameNotFoundException("Username '"+username+"' not found");*/
  98.             // Fix security: Make sure allowing user enumeration is safe here.
  99.             throw new BadCredentialsException("Bad credentials");
  100.         }
  101.         String pwcrypt = u.getPassword();
  102.        
  103.         try {
  104.             if(passwordManager==null) {
  105.                 initPasswordManager(this.log, ServerProperties.getInstance().getUtenzeCryptConfig());
  106.             }
  107.         }catch(Exception e) {
  108.             throw new AuthenticationServiceException("Inizializzazione AuthenticationProvider fallita: "+e.getMessage(),e);
  109.         }
  110.        
  111.         boolean match = passwordManager.check(password, pwcrypt);
  112.         if(!match && passwordManagerBackwardCompatibility!=null) {
  113.             match = passwordManagerBackwardCompatibility.check(password, pwcrypt);
  114.         }
  115.         if(!match) {
  116.             throw new BadCredentialsException("Bad credentials");
  117.         }

  118.         List<GrantedAuthority> roles = new ArrayList<>();
  119.         if(u.getPermessi()!=null && u.getPermessi().isServizi()) {
  120.             GrantedAuthority grant = new SimpleGrantedAuthority(this.configuratorRoleName);
  121.             roles.add(grant);
  122.         }
  123.         // vi sono le acl per questo
  124.         /**else {
  125.             throw new BadCredentialsException(LoginCostanti.MESSAGGIO_ERRORE_UTENTE_NON_ABILITATO_UTILIZZO_CONSOLE);
  126.         }*/
  127.    
  128.         // Wrap in UsernamePasswordAuthenticationToken
  129.         User user = new User(username, getS(""), true, true, true, true, roles);
  130.         UsernamePasswordAuthenticationToken userAuth = new UsernamePasswordAuthenticationToken(user, getS(""), user.getAuthorities());
  131.         userAuth.setDetails(authentication.getDetails());
  132.         return userAuth;
  133.        
  134.        

  135.     }

  136.     @Override
  137.     public boolean supports(Class<?> authentication) {
  138.         return authentication.equals(UsernamePasswordAuthenticationToken.class);
  139.     }

  140.     public String getConfiguratorRoleName() {
  141.         return this.configuratorRoleName;
  142.     }
  143.     public void setConfiguratorRoleName(String configuratorRoleName) {
  144.         this.configuratorRoleName = configuratorRoleName;
  145.     }
  146. }