SecurityProvider.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.message.jose;

  21. import java.util.ArrayList;
  22. import java.util.List;
  23. import java.util.Map;
  24. import java.util.Properties;

  25. import org.openspcoop2.core.mvc.properties.provider.ProviderException;
  26. import org.openspcoop2.core.mvc.properties.provider.ProviderValidationException;
  27. import org.openspcoop2.security.message.constants.SecurityConstants;
  28. import org.openspcoop2.security.message.utils.AbstractSecurityProvider;
  29. import org.openspcoop2.utils.security.JOSESerialization;
  30. import org.openspcoop2.utils.security.JWTOptions;


  31. /**    
  32.  * SecurityProvider
  33.  *
  34.  * @author Poli Andrea (poli@link.it)
  35.  * @author $Author$
  36.  * @version $Rev$, $Date$
  37.  */
  38. public class SecurityProvider extends AbstractSecurityProvider {

  39.     private boolean addSecretKey = false;
  40.     public void addSecretKey() {
  41.         this.addSecretKey = true;
  42.     }

  43.     @Override
  44.     public void validate(Map<String, Properties> mapProperties) throws ProviderException, ProviderValidationException {

  45.         JWTOptions jwtOptions = new JWTOptions(JOSESerialization.COMPACT); // una qualsaisi per la validazione
  46.         boolean useHeaders = JOSEUtils.useJwtHeadersMapProperties(mapProperties, jwtOptions);
  47.         if(useHeaders) {
  48.             if(!jwtOptions.isPermitUseHeaderJKU() &&
  49.                     !jwtOptions.isPermitUseHeaderJWK() &&
  50.                     !jwtOptions.isPermitUseHeaderX5C() &&
  51.                     !jwtOptions.isPermitUseHeaderX5U() &&
  52.                     !jwtOptions.isPermitUseHeaderX5T() &&
  53.                     !jwtOptions.isPermitUseHeaderX5T_256() &&
  54.                     !jwtOptions.isPermitUseHeaderKID()
  55.                     ){
  56.                         throw new ProviderValidationException("Selezionare almeno un header");
  57.                     }
  58.         }
  59.        
  60.         super.validate(mapProperties);
  61.     }

  62.     @Override
  63.     public List<String> getValues(String id) throws ProviderException {
  64.         if(JOSECostanti.ID_SIGNATURE_ALGORITHM.equals(id)) {
  65.             List<String> l = new ArrayList<>();
  66.             org.apache.cxf.rs.security.jose.jwa.SignatureAlgorithm [] tmp = org.apache.cxf.rs.security.jose.jwa.SignatureAlgorithm.values();
  67.             for (int i = 0; i < tmp.length; i++) {
  68.                 l.add(tmp[i].name());
  69.             }
  70.             return l;
  71.         }
  72.         else if(JOSECostanti.ID_ENCRYPT_KEY_ALGORITHM.equals(id)) {
  73.             List<String> l = new ArrayList<>();
  74.             org.apache.cxf.rs.security.jose.jwa.KeyAlgorithm [] tmp = org.apache.cxf.rs.security.jose.jwa.KeyAlgorithm.values();
  75.             for (int i = 0; i < tmp.length; i++) {
  76.                 l.add(tmp[i].name());
  77.             }
  78.             return l;
  79.         }
  80.         else if(JOSECostanti.ID_ENCRYPT_CONTENT_ALGORITHM.equals(id)) {
  81.             List<String> l = new ArrayList<>();
  82.             org.apache.cxf.rs.security.jose.jwa.ContentAlgorithm [] tmp = org.apache.cxf.rs.security.jose.jwa.ContentAlgorithm.values();
  83.             for (int i = 0; i < tmp.length; i++) {
  84.                 l.add(tmp[i].name());
  85.             }
  86.             return l;
  87.         }
  88.         else if(SecurityConstants.KEYSTORE_TYPE.equals(id) ||
  89.                 SecurityConstants.SECRETKEYSTORE_TYPE.equals(id) ||
  90.                 SecurityConstants.TRUSTSTORE_TYPE.equals(id)) {
  91.             List<String> l = super.getValues(id);
  92.             l.add(0, SecurityConstants.KEYSTORE_TYPE_JWK_VALUE);
  93.             if(SecurityConstants.TRUSTSTORE_TYPE.equals(id) ||
  94.                     (SecurityConstants.KEYSTORE_TYPE.equals(id) && (this.isAsTruststore() || this.addSecretKey))) {
  95.                 // aggiunto info mancanti come secret
  96.                 List<String> lSecret = super.getValues(SecurityConstants.SECRETKEYSTORE_TYPE);
  97.                 if(lSecret!=null && !lSecret.isEmpty()) {
  98.                     for (String type : lSecret) {
  99.                         if(!l.contains(type)) {
  100.                             l.add(type);
  101.                         }
  102.                     }
  103.                 }
  104.             }
  105.             return l;
  106.         }
  107.         else {
  108.             return super.getValues(id);
  109.         }
  110.     }

  111.     @Override
  112.     public List<String> getLabels(String id) throws ProviderException {
  113.         if(JOSECostanti.ID_SIGNATURE_ALGORITHM.equals(id) ||
  114.                 JOSECostanti.ID_ENCRYPT_KEY_ALGORITHM.equals(id) ||
  115.                 JOSECostanti.ID_ENCRYPT_CONTENT_ALGORITHM.equals(id)) {
  116.             List<String> l = this.getValues(id);
  117.             List<String> labels = new ArrayList<>();
  118.             for (String value : l) {
  119.                 if(value.contains("_")) {
  120.                     String t = new String(value);
  121.                     while(t.contains("_")) {
  122.                         t = t.replace("_", "-");
  123.                     }
  124.                     labels.add(t);
  125.                 }
  126.                 else {
  127.                     labels.add(value);
  128.                 }
  129.             }
  130.             return labels;
  131.         }
  132.         else if(SecurityConstants.KEYSTORE_TYPE.equals(id) ||
  133.                 SecurityConstants.SECRETKEYSTORE_TYPE.equals(id) ||
  134.                 SecurityConstants.TRUSTSTORE_TYPE.equals(id)) {
  135.             List<String> l = super.getLabels(id);
  136.             l.add(0, SecurityConstants.KEYSTORE_TYPE_JWK_LABEL);
  137.             if(SecurityConstants.TRUSTSTORE_TYPE.equals(id) ||
  138.                     (SecurityConstants.KEYSTORE_TYPE.equals(id) && (this.isAsTruststore() || this.addSecretKey))) {
  139.                 // aggiunto info mancanti come secret
  140.                 List<String> lSecret = super.getLabels(SecurityConstants.SECRETKEYSTORE_TYPE);
  141.                 if(lSecret!=null && !lSecret.isEmpty()) {
  142.                     for (String type : lSecret) {
  143.                         if(!l.contains(type)) {
  144.                             l.add(type);
  145.                         }
  146.                     }
  147.                 }
  148.             }
  149.             return l;
  150.         }
  151.         else {
  152.             return super.getLabels(id);
  153.         }
  154.     }

  155.     @Override
  156.     public String getDefault(String id) throws ProviderException {
  157.         if(JOSECostanti.ID_SIGNATURE_ALGORITHM.equals(id)) {
  158.             return org.apache.cxf.rs.security.jose.jwa.SignatureAlgorithm.RS256.name();
  159.         }
  160.         else if(JOSECostanti.ID_ENCRYPT_KEY_ALGORITHM.equals(id)) {
  161.             return org.apache.cxf.rs.security.jose.jwa.KeyAlgorithm.RSA_OAEP_256.name();
  162.         }
  163.         else if(JOSECostanti.ID_ENCRYPT_CONTENT_ALGORITHM.equals(id)) {
  164.             return org.apache.cxf.rs.security.jose.jwa.ContentAlgorithm.A256GCM.name();
  165.         }
  166.         else {
  167.             return super.getDefault(id);
  168.         }
  169.     }

  170. }