AuthorizationManager.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.utils.service.authorization;

  21. import java.util.List;
  22. import java.util.stream.Collectors;

  23. import org.openspcoop2.utils.service.context.IContext;
  24. import org.openspcoop2.utils.service.fault.jaxrs.FaultCode;
  25. import org.springframework.security.core.GrantedAuthority;

  26. /**
  27.  * AuthorizationManager
  28.  *
  29.  * @author Andrea Poli (apoli@link.it)
  30.  * @author $Author$
  31.  * @version $Rev$, $Date$
  32.  */
  33. public class AuthorizationManager {

  34.     public static void authorize(IContext context, AuthorizationConfig config) {        
  35.        
  36.         if(config==null || config.getAclList()==null || config.getAclList().isEmpty()) {
  37.             return; // non esistono regole acl
  38.         }
  39.            
  40.         try {
  41.            
  42.             // 1. Cerco acl che ha un match con la richiesta in essere
  43.             String method = context.getServletRequest().getMethod();
  44.             String restPath = context.getRestPath(); // a differenza del context.getServletRequest().getRequestURI(), il path è normalizzato per i parametri dinamici {}
  45.             AuthorizationConfigACL acl = null;
  46.             for (AuthorizationConfigACL check : config.getAclList()) {
  47.                
  48.                 // method
  49.                 String methodCheck = check.getMethod();
  50.                 if(!AuthorizationConfigACL.WILDCARD.equals(methodCheck)) {
  51.                     if(!methodCheck.toUpperCase().equals(method.toUpperCase())) {
  52.                         continue;
  53.                     }
  54.                 }
  55.                
  56.                 // path
  57.                 String pathCheck = check.getPath();
  58.                 if(!AuthorizationConfigACL.WILDCARD.equals(pathCheck)) {
  59.                     if(pathCheck.endsWith(AuthorizationConfigACL.WILDCARD)) {
  60.                         String patchCheckWitoutStar = pathCheck.substring(0, (pathCheck.length()-AuthorizationConfigACL.WILDCARD.length()));
  61.                         if(!normalizePath(restPath).startsWith(normalizePath(patchCheckWitoutStar))) {
  62.                             continue;
  63.                         }
  64.                     }
  65.                     else {
  66.                         if(!normalizePath(restPath).equals(normalizePath(pathCheck))) {
  67.                             continue;
  68.                         }
  69.                     }
  70.                 }
  71.                
  72.                 acl = check;
  73.                 break;
  74.             }
  75.             if(acl==null) {
  76.                 throw new Exception("Acl rule match for request not found");
  77.             }
  78.            
  79.             // 2. Verifico acl trovata
  80.             if(acl.getPrincipal().isEmpty() && acl.getRoles().isEmpty()) {
  81.                 return; // invocazione pubblica
  82.             }
  83.             if(!acl.getPrincipal().isEmpty()) {
  84.                 for (String check : acl.getPrincipal()) {
  85.                     if(check.equals(context.getAuthentication().getName())) {
  86.                         return; // principal match
  87.                     }
  88.                 }
  89.             }
  90.             if(!acl.getRoles().isEmpty()) {
  91.                 for (String check : acl.getRoles()) {
  92.                     boolean found = false;
  93.                     if(context.getAuthentication().getAuthorities()!=null) {
  94.                         List<String> listaRuoliUtenza = context.getAuthentication().getAuthorities().stream().map(GrantedAuthority::getAuthority).collect(Collectors.toList());
  95.                         if(listaRuoliUtenza!=null && !listaRuoliUtenza.isEmpty()) {
  96.                             for (String role : listaRuoliUtenza) {
  97.                                 if(check.equals(role)) {
  98.                                     if(!acl.isRolesMatchAll()) {
  99.                                         return; // role match
  100.                                     }
  101.                                     else {
  102.                                         found = true;
  103.                                         break;
  104.                                     }
  105.                                 }          
  106.                             }
  107.                         }
  108.                     }
  109.                     if(acl.isRolesMatchAll() && !found) {
  110.                         break;
  111.                     }
  112.                 }
  113.             }
  114.             throw new Exception("Acl rule '"+acl.getName()+"' not satisfied");

  115.         }catch(Exception eAuthorized) {
  116.             FaultCode.AUTORIZZAZIONE.throwException(String.format("L'utente '%s' non è autorizzato ad invocare l'operazione '%s': %s",
  117.                     context.getAuthentication().getName(), context.getMethodName(), eAuthorized.getMessage()));
  118.         }
  119.     }
  120.    
  121.     private static String normalizePath(String path) {
  122.         if(path.endsWith("/")) {
  123.             return path.substring(0, path.length()-1);
  124.         }
  125.         else {
  126.             return path;
  127.         }
  128.     }

  129. }