DynamicDiscovery.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;

  21. import java.io.Serializable;
  22. import java.util.HashMap;
  23. import java.util.Map;

  24. import org.openspcoop2.pdd.core.token.parser.IDynamicDiscoveryParser;
  25. import org.openspcoop2.pdd.core.token.parser.TipologiaClaims;
  26. import org.openspcoop2.utils.UtilsException;
  27. import org.openspcoop2.utils.json.JSONUtils;

  28. import com.fasterxml.jackson.databind.JsonNode;

  29. /**    
  30.  * DynamicDiscovery
  31.  *
  32.  * @author Poli Andrea (poli@link.it)
  33.  * @author $Author$
  34.  * @version $Rev$, $Date$
  35.  */
  36. public class DynamicDiscovery extends org.openspcoop2.utils.beans.BaseBean implements Serializable , Cloneable {

  37.     /**
  38.      *
  39.      */
  40.     private static final long serialVersionUID = 1L;
  41.    
  42.     public DynamicDiscovery() {} // per serializzatore
  43.     public DynamicDiscovery(Integer httpResponseCode, TipologiaClaims tipo, String rawResponse, IDynamicDiscoveryParser parser) throws UtilsException {
  44.         this.rawResponse = rawResponse;
  45.         this.type = tipo;
  46.         JSONUtils jsonUtils = JSONUtils.getInstance();
  47.         if(jsonUtils.isJson(this.rawResponse)) {
  48.             JsonNode root = jsonUtils.getAsNode(this.rawResponse);
  49.             Map<String, Serializable> readClaims = jsonUtils.convertToSimpleMap(root);
  50.             if(readClaims!=null && readClaims.size()>0) {
  51.                 this.claims.putAll(readClaims);
  52.             }
  53.         }
  54.         parser.init(this.rawResponse, this.claims);
  55.         if(httpResponseCode!=null) {
  56.             parser.checkHttpTransaction(httpResponseCode);
  57.         }
  58.         this.jwksUri = parser.getJwksUri();
  59.         this.introspectionEndpoint = parser.getIntrospectionEndpoint();
  60.         this.userinfoEndpoint = parser.getUserInfoEndpoint();
  61.        
  62.         this.valid = true;
  63.     }
  64.    
  65.     public DynamicDiscovery(String errorDetails, Integer httpResponseCode, byte[] rawResponse, TipologiaClaims tipo) {
  66.         this.valid = false;
  67.         this.claims = null;
  68.         this.type = tipo;
  69.         if(httpResponseCode!=null) {
  70.             this.httpResponseCode = httpResponseCode+"";
  71.         }
  72.         if(rawResponse!=null) {
  73.             this.rawResponse = new String(rawResponse);
  74.         }
  75.         this.errorDetails = errorDetails;
  76.     }
  77.        
  78.     // NOTA: l'ordine stabilisce come viene serializzato nell'oggetto json
  79.    
  80.     // Indicazione se il token e' valido
  81.     private boolean valid;
  82.    
  83.     // jwk
  84.     private String jwksUri;
  85.    
  86.     // introspection
  87.     private String introspectionEndpoint;
  88.    
  89.     // userInfo
  90.     private String userinfoEndpoint;
  91.            
  92.     // Claims
  93.     private Map<String,Serializable> claims = new HashMap<>();
  94.        
  95.     // NOTA: l'ordine stabilisce come viene serializzato nell'oggetto json

  96.     // RawResponse
  97.     private String rawResponse;
  98.    
  99.     // TipologiaClaims
  100.     private TipologiaClaims type;
  101.    
  102.     // HttpCode (nel caso di errori)
  103.     private String httpResponseCode;
  104.    
  105.     // Failed
  106.     private String errorDetails;
  107.        
  108.     public boolean isValid() {
  109.         return this.valid;
  110.     }
  111.     public boolean getValid() { // clone
  112.         return this.valid;
  113.     }
  114.     public void setValid(boolean valid) {
  115.         this.valid = valid;
  116.     }
  117.    
  118.     public String getJwksUri() {
  119.         return this.jwksUri;
  120.     }
  121.     public void setJwksUri(String jwksUri) {
  122.         this.jwksUri = jwksUri;
  123.     }
  124.    
  125.     public String getIntrospectionEndpoint() {
  126.         return this.introspectionEndpoint;
  127.     }
  128.     public void setIntrospectionEndpoint(String introspectionEndpoint) {
  129.         this.introspectionEndpoint = introspectionEndpoint;
  130.     }
  131.    
  132.     public String getUserinfoEndpoint() {
  133.         return this.userinfoEndpoint;
  134.     }
  135.     public void setUserinfoEndpoint(String userinfoEndpoint) {
  136.         this.userinfoEndpoint = userinfoEndpoint;
  137.     }
  138.    
  139.     public Map<String, Serializable> getClaims() {
  140.         return this.claims;
  141.     }
  142.     public void setClaims(Map<String, Serializable> claims) {
  143.         this.claims = claims;
  144.     }
  145.            
  146.     public String getRawResponse() {
  147.         return this.rawResponse;
  148.     }
  149.     public void setRawResponse(String rawResponse) {
  150.         this.rawResponse = rawResponse;
  151.     }
  152.    
  153.     public TipologiaClaims getType() {
  154.         return this.type;
  155.     }
  156.     public void setType(TipologiaClaims type) {
  157.         this.type = type;
  158.     }
  159.    
  160.     public String getHttpResponseCode() {
  161.         return this.httpResponseCode;
  162.     }
  163.     public void setHttpResponseCode(String httpResponseCode) {
  164.         this.httpResponseCode = httpResponseCode;
  165.     }
  166.    
  167.     public String getErrorDetails() {
  168.         return this.errorDetails;
  169.     }
  170.     public void setErrorDetails(String errorDetails) {
  171.         this.errorDetails = errorDetails;
  172.     }
  173.    
  174. }