BasicDynamicDiscoveryParser.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.pdd.core.token.parser;

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

  25. import org.openspcoop2.pdd.core.token.Costanti;
  26. import org.openspcoop2.pdd.core.token.TokenUtilities;
  27. import org.openspcoop2.utils.UtilsException;

  28. /**    
  29.  * BasicDynamicDiscoveryParser
  30.  *
  31.  * @author Poli Andrea (poli@link.it)
  32.  * @author $Author$
  33.  * @version $Rev$, $Date$
  34.  */
  35. public class BasicDynamicDiscoveryParser implements IDynamicDiscoveryParser {

  36.     protected Integer httpResponseCode;
  37.     protected String raw;
  38.     protected Map<String, Serializable> claims;
  39.     protected TipologiaClaims parser;
  40.     protected Properties parserConfig;
  41.    
  42.     public BasicDynamicDiscoveryParser(TipologiaClaims parser) {
  43.         this(parser, null);
  44.     }
  45.     public BasicDynamicDiscoveryParser(TipologiaClaims parser, Properties parserConfig) {
  46.         this.parser = parser;
  47.         this.parserConfig = parserConfig;
  48.     }
  49.    
  50.     @Override
  51.     public void init(String raw, Map<String, Serializable> claims) {
  52.         this.raw = raw;
  53.         this.claims = claims;
  54.     }
  55.    
  56.     private void throwUnsupportedParser() throws UtilsException {
  57.         String msgError = "Tipo di parser '"+this.parser+"' non supportato";
  58.         throw new UtilsException(msgError);
  59.     }
  60.    
  61.     @Override
  62.     public void checkHttpTransaction(Integer httpResponseCode) throws UtilsException{
  63.         this.httpResponseCode = httpResponseCode;
  64.         switch (this.parser) {
  65.         case OIDC_ID_TOKEN:
  66.         case MAPPING:
  67.         case CUSTOM:
  68.             if(this.httpResponseCode!=null &&
  69.                 (this.httpResponseCode.intValue() < 200 || this.httpResponseCode.intValue()>299)) {
  70.                 String msgError = "Connessione terminata con errore (codice trasporto: "+this.httpResponseCode.intValue()+")";
  71.                 throw new UtilsException(msgError+": "+this.raw);
  72.             }
  73.             break;
  74.         default:
  75.             throwUnsupportedParser();
  76.         }
  77.     }
  78.    
  79.     @Override
  80.     public String getJwksUri() {
  81.         switch (this.parser) {
  82.         case OIDC_ID_TOKEN:
  83.             return TokenUtilities.getClaimAsString(this.claims,Claims.OIDC_ID_DISCOVERY_DYNAMIC_JWK_URI);
  84.         case MAPPING:
  85.             List<String> claimNames = TokenUtilities.getClaims(this.parserConfig, Costanti.POLICY_DISCOVERY_JWK_CUSTOM);
  86.             return TokenUtilities.getFirstClaimAsString(this.claims, claimNames);
  87.         case CUSTOM:
  88.             return null;
  89.         default:
  90.             break;
  91.         }
  92.         return null;
  93.     }
  94.        
  95.     @Override
  96.     public String getIntrospectionEndpoint() {
  97.         switch (this.parser) {
  98.         case OIDC_ID_TOKEN:
  99.             return TokenUtilities.getClaimAsString(this.claims,Claims.OIDC_ID_DISCOVERY_DYNAMIC_INTROSPECTION_ENDPOINT);
  100.         case MAPPING:
  101.             List<String> claimNames = TokenUtilities.getClaims(this.parserConfig, Costanti.POLICY_DISCOVERY_INTROSPECTION_CUSTOM);
  102.             return TokenUtilities.getFirstClaimAsString(this.claims, claimNames);
  103.         case CUSTOM:
  104.             return null;
  105.         default:
  106.             break;
  107.         }
  108.         return null;
  109.     }

  110.     @Override
  111.     public String getUserInfoEndpoint() {
  112.         switch (this.parser) {
  113.         case OIDC_ID_TOKEN:
  114.             return TokenUtilities.getClaimAsString(this.claims,Claims.OIDC_ID_DISCOVERY_DYNAMIC_USERINFO_ENDPOINT);
  115.         case MAPPING:
  116.             List<String> claimNames = TokenUtilities.getClaims(this.parserConfig, Costanti.POLICY_DISCOVERY_USERINFO_CUSTOM);
  117.             return TokenUtilities.getFirstClaimAsString(this.claims, claimNames);
  118.         case CUSTOM:
  119.             return null;
  120.         default:
  121.             break;
  122.         }
  123.         return null;
  124.     }
  125.    
  126. }