ModIRESTSecurity.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.protocol.modipa.validator;

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

  23. import org.openspcoop2.message.OpenSPCoop2Message;
  24. import org.openspcoop2.message.exception.MessageException;
  25. import org.openspcoop2.message.exception.MessageNotSupportedException;
  26. import org.openspcoop2.protocol.modipa.config.ModIProperties;
  27. import org.openspcoop2.protocol.sdk.ProtocolException;
  28. import org.openspcoop2.protocol.sdk.constants.RuoloMessaggio;
  29. import org.openspcoop2.security.SecurityException;
  30. import org.openspcoop2.utils.transport.http.HttpConstants;

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

  39.     private List<String> tokenHeaderNames;
  40.     private boolean cleanDigest;
  41.     private RuoloMessaggio ruoloMessaggio;
  42.    
  43.     public ModIRESTSecurity(String tokenHeaderName, boolean request) throws ProtocolException, Exception {
  44.         this.tokenHeaderNames = new ArrayList<>();
  45.         this.tokenHeaderNames.add(tokenHeaderName);
  46.         this.ruoloMessaggio =  request ? RuoloMessaggio.RICHIESTA : RuoloMessaggio.RISPOSTA;
  47.         switch (this.ruoloMessaggio) {
  48.         case RICHIESTA:
  49.             this.cleanDigest = ModIProperties.getInstance().isRestSecurityTokenRequestDigestClean();
  50.             break;
  51.         case RISPOSTA:
  52.             this.cleanDigest = ModIProperties.getInstance().isRestSecurityTokenResponseDigestClean();
  53.             break;
  54.         }
  55.     }
  56.    
  57.     public RuoloMessaggio getRuoloMessaggio() {
  58.         return this.ruoloMessaggio;
  59.     }

  60.     public void setRuoloMessaggio(RuoloMessaggio ruoloMessaggio) {
  61.         this.ruoloMessaggio = ruoloMessaggio;
  62.     }

  63.     public List<String> getTokenHeaderNames() {
  64.         return this.tokenHeaderNames;
  65.     }

  66.     public void setTokenHeaderNames(List<String> tokenHeaderNames) {
  67.         this.tokenHeaderNames = tokenHeaderNames;
  68.     }

  69.     public boolean isCleanDigest() {
  70.         return this.cleanDigest;
  71.     }

  72.     public void setCleanDigest(boolean cleanDigest) {
  73.         this.cleanDigest = cleanDigest;
  74.     }
  75.    
  76.    
  77.     public void clean(OpenSPCoop2Message msg) throws SecurityException, MessageException, MessageNotSupportedException {
  78.        
  79.         if(RuoloMessaggio.RICHIESTA.equals(this.ruoloMessaggio)) {
  80.             if(msg!=null && msg.getTransportRequestContext()!=null) {
  81.                 if(this.tokenHeaderNames!=null && !this.tokenHeaderNames.isEmpty()) {
  82.                     for (String hdr : this.tokenHeaderNames) {
  83.                         msg.getTransportRequestContext().removeHeader(hdr);
  84.                     }
  85.                 }
  86.                 if(this.cleanDigest) {
  87.                     msg.getTransportRequestContext().removeHeader(HttpConstants.DIGEST);
  88.                 }
  89.             }
  90.         }
  91.         else {
  92.             if(msg!=null && msg.getTransportResponseContext()!=null) {
  93.                 if(this.tokenHeaderNames!=null && !this.tokenHeaderNames.isEmpty()) {
  94.                     for (String hdr : this.tokenHeaderNames) {
  95.                         msg.getTransportResponseContext().removeHeader(hdr);
  96.                     }
  97.                 }
  98.                 if(this.cleanDigest) {
  99.                     msg.getTransportResponseContext().removeHeader(HttpConstants.DIGEST);
  100.                 }
  101.             }
  102.         }

  103.     }
  104.    
  105. }