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

  21. import java.sql.Connection;
  22. import java.util.ArrayList;
  23. import java.util.List;

  24. import org.openspcoop2.generic_project.exception.NotFoundException;
  25. import org.openspcoop2.generic_project.utils.ServiceManagerProperties;
  26. import org.openspcoop2.web.monitor.core.bean.UserDetailsBean;
  27. import org.openspcoop2.web.monitor.core.dao.DBLoginDAO;
  28. import org.slf4j.Logger;
  29. import org.springframework.security.authentication.AuthenticationCredentialsNotFoundException;
  30. import org.springframework.security.authentication.AuthenticationServiceException;
  31. import org.springframework.security.authentication.BadCredentialsException;
  32. import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
  33. import org.springframework.security.core.Authentication;
  34. import org.springframework.security.core.AuthenticationException;
  35. import org.springframework.security.core.GrantedAuthority;
  36. import org.springframework.security.core.authority.SimpleGrantedAuthority;
  37. import org.springframework.security.core.userdetails.User;


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

  46.     @SuppressWarnings("unused")
  47.     private Logger log = org.slf4j.LoggerFactory.getLogger(this.getClass());

  48.     private String operatorRoleName = "operatore";  
  49.     private String diagnosticRoleName = "diagnostica";  
  50.     private String reportRoleName = "reportistica";

  51.     private static String getS(String v) {
  52.         return "sec"+v+"ret";
  53.     }
  54.    
  55.     @Override
  56.     public Authentication authenticate(Authentication authentication) throws AuthenticationException {

  57.         String username = authentication.getName();
  58.         Object passwordObject = authentication.getCredentials();
  59.         String password = (String) passwordObject;

  60.         if(username==null || password==null) {
  61.             throw new AuthenticationCredentialsNotFoundException("Credentials not found");
  62.         }
  63.        
  64.         DBManager dbManager = DBManager.getInstance();
  65.         Connection connection = null;
  66.         try {
  67.             connection = dbManager.getConnectionConfig();
  68.             ServiceManagerProperties smp = dbManager.getServiceManagerPropertiesConfig();
  69.             DBLoginDAO loginService = new DBLoginDAO(connection, true, smp, LoggerProperties.getLoggerDAO());
  70.            
  71.             UserDetailsBean u = null;
  72.             try {
  73.                 u = loginService.loadUserByUsername(username, false); // il controllo e' fatto nella acl
  74.             }
  75.             catch(NotFoundException e) {
  76.                 /**throw new UsernameNotFoundException("Username '"+username+"' not found", e);*/
  77.                 // Fix security: Make sure allowing user enumeration is safe here.
  78.                 throw new BadCredentialsException("Bad credentials");
  79.             }
  80.             catch(Exception e) {
  81.                 logAndThrowAuthenticationServiceException("AuthenticationProvider,ricerca utente fallita",e);
  82.             }
  83.            
  84.             boolean correct = false;
  85.             try {
  86.                 loginService.setPasswordManager(ServerProperties.getInstance().getUtenzeCryptConfig());
  87.                 correct = loginService.login(username, password);
  88.             }catch(Exception e) {
  89.                 logAndThrowAuthenticationServiceException("Inizializzazione AuthenticationProvider fallita",e);
  90.             }
  91.             if(!correct) {
  92.                 throw new BadCredentialsException("Bad credentials");
  93.             }

  94.             List<GrantedAuthority> roles = new ArrayList<>();
  95.             if(u.getUtente()!=null && u.getUtente().getPermessi()!=null) {
  96.                 if(u.getUtente().getPermessi().isDiagnostica()) {
  97.                     GrantedAuthority grant = new SimpleGrantedAuthority(this.diagnosticRoleName);
  98.                     roles.add(grant);
  99.                 }
  100.                 if(u.getUtente().getPermessi().isReportistica()) {
  101.                     GrantedAuthority grant = new SimpleGrantedAuthority(this.reportRoleName);
  102.                     roles.add(grant);
  103.                 }
  104.                 if(roles.size()==2) {
  105.                     // operatore se li ha tutti e due
  106.                     GrantedAuthority grant = new SimpleGrantedAuthority(this.operatorRoleName);
  107.                     roles.add(grant);
  108.                 }
  109.             }
  110.             // vi sono le acl per questo
  111.             /**else {
  112.                 throw new BadCredentialsException(LoginCostanti.MESSAGGIO_ERRORE_UTENTE_NON_ABILITATO_UTILIZZO_CONSOLE);
  113.             }*/
  114.                
  115.             // Wrap in UsernamePasswordAuthenticationToken
  116.             User user = new User(username, getS(""), true, true, true, true, roles);
  117.             UsernamePasswordAuthenticationToken userAuth = new UsernamePasswordAuthenticationToken(user, getS(""), user.getAuthorities());
  118.             userAuth.setDetails(authentication.getDetails());
  119.             return userAuth;
  120.         }
  121.         finally {
  122.             dbManager.releaseConnectionConfig(connection);
  123.         }

  124.     }
  125.    
  126.     private void logAndThrowAuthenticationServiceException(String msg, Exception e) throws AuthenticationServiceException {
  127.         LoggerProperties.getLoggerCore().error(e.getMessage(),e);
  128.         throw new AuthenticationServiceException(msg+": "+e.getMessage(),e);
  129.     }

  130.     @Override
  131.     public boolean supports(Class<?> authentication) {
  132.         return authentication.equals(UsernamePasswordAuthenticationToken.class);
  133.     }

  134.     public String getOperatorRoleName() {
  135.         return this.operatorRoleName;
  136.     }
  137.     public void setOperatorRoleName(String operatorRoleName) {
  138.         this.operatorRoleName = operatorRoleName;
  139.     }

  140.     public String getDiagnosticRoleName() {
  141.         return this.diagnosticRoleName;
  142.     }
  143.     public void setDiagnosticRoleName(String diagnosticRoleName) {
  144.         this.diagnosticRoleName = diagnosticRoleName;
  145.     }

  146.     public String getReportRoleName() {
  147.         return this.reportRoleName;
  148.     }
  149.     public void setReportRoleName(String reportRoleName) {
  150.         this.reportRoleName = reportRoleName;
  151.     }

  152. }