AuthorizationConfigACL.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.ArrayList;
  22. import java.util.List;
  23. import java.util.Properties;

  24. import org.openspcoop2.utils.UtilsException;

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

  33.     public static final String WILDCARD = "*";
  34.    
  35.     public static final String PREFIX = "auth.";
  36.     public static final String SUFFIX_METHOD = ".resource.method";
  37.     public static final String SUFFIX_PATH = ".resource.path";
  38.     public static final String SUFFIX_PRINCIPAL = ".principal";
  39.     public static final String SUFFIX_ROLES = ".roles";
  40.     public static final String SUFFIX_ROLES_MATCH_ALL = ".roles.matchAll";
  41.    
  42.     private String name;
  43.    
  44.     private String method;
  45.     private String path;
  46.    
  47.     private List<String> principal = new ArrayList<>();
  48.     private List<String> roles = new ArrayList<>();
  49.     private boolean rolesMatchAll = false;
  50.    
  51.     public AuthorizationConfigACL(String name) {
  52.         this.name = name;
  53.     }
  54.     public AuthorizationConfigACL(String name, Properties p) throws UtilsException {
  55.         this(name);
  56.        
  57.         this.method = p.getProperty(PREFIX+name+SUFFIX_METHOD);
  58.         if(this.method==null) {
  59.             throw new UtilsException("Method undefined for acl '"+this.name+"'");
  60.         }
  61.         else {
  62.             this.method = this.method.trim();
  63.         }
  64.        
  65.         this.path = p.getProperty(PREFIX+name+SUFFIX_PATH);
  66.         if(this.path==null) {
  67.             throw new UtilsException("Path undefined for acl '"+this.name+"'");
  68.         }
  69.         else {
  70.             this.path = this.path.trim();
  71.         }
  72.        
  73.         String tmp = p.getProperty(PREFIX+name+SUFFIX_PRINCIPAL);
  74.         if(tmp!=null) {
  75.             tmp = tmp.trim();
  76.             if(tmp.contains(",")) {
  77.                 String [] tmpList = tmp.split(",");
  78.                 for (int i = 0; i < tmpList.length; i++) {
  79.                     this.principal.add(tmpList[i].trim());
  80.                 }
  81.             }
  82.             else {
  83.                 this.principal.add(tmp);
  84.             }
  85.         }
  86.        
  87.         tmp = p.getProperty(PREFIX+name+SUFFIX_ROLES);
  88.         if(tmp!=null) {
  89.             tmp = tmp.trim();
  90.             if(tmp.contains(",")) {
  91.                 String [] tmpList = tmp.split(",");
  92.                 for (int i = 0; i < tmpList.length; i++) {
  93.                     this.roles.add(tmpList[i].trim());
  94.                 }
  95.             }
  96.             else {
  97.                 this.roles.add(tmp);
  98.             }
  99.         }
  100.        
  101.         if(!this.roles.isEmpty()) {
  102.             tmp = p.getProperty(PREFIX+name+SUFFIX_ROLES_MATCH_ALL);
  103.             if(tmp!=null) {
  104.                 tmp = tmp.trim();
  105.                 this.rolesMatchAll = Boolean.parseBoolean(tmp);
  106.             }
  107.         }
  108.     }
  109.    
  110.    
  111.     public String getName() {
  112.         return this.name;
  113.     }

  114.     public String getMethod() {
  115.         return this.method;
  116.     }
  117.     public void setMethod(String method) {
  118.         this.method = method;
  119.     }
  120.    
  121.     public String getPath() {
  122.         return this.path;
  123.     }
  124.     public void setPath(String path) {
  125.         this.path = path;
  126.     }
  127.    
  128.     public List<String> getPrincipal() {
  129.         return this.principal;
  130.     }
  131.     public void addPrincipal(String principal) {
  132.         this.principal.add(principal);
  133.     }
  134.    
  135.     public List<String> getRoles() {
  136.         return this.roles;
  137.     }
  138.     public void addRole(String role) {
  139.         this.roles.add(role);
  140.     }
  141.    
  142.     public boolean isRolesMatchAll() {
  143.         return this.rolesMatchAll;
  144.     }
  145.     public void setRolesMatchAll(boolean rolesMatchAll) {
  146.         this.rolesMatchAll = rolesMatchAll;
  147.     }
  148.    
  149. }