InformazioniJWTClientAssertion.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.utils.io.Base64Utilities;
  25. import org.openspcoop2.utils.json.JSONUtils;
  26. import org.slf4j.Logger;

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

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

  36.     /**
  37.      *
  38.      */
  39.     private static final long serialVersionUID = 1L;
  40.    
  41.     public InformazioniJWTClientAssertion() {} // per serializzatore
  42.     public InformazioniJWTClientAssertion(Logger log, String base64, boolean infoNormalizzate) {
  43.         this.token = base64;
  44.        
  45.         if(infoNormalizzate) {
  46.             String [] split = this.token.split("\\.");
  47.             if(split!=null && split.length>0) {
  48.                 if(split[0]!=null) {
  49.                     String hdrBase64 = split[0];
  50.                     try {
  51.                         this.jsonHeader = new String(Base64Utilities.decode(hdrBase64));
  52.                     }catch(Exception t) {
  53.                         log.error("Decode header failed (hdr: "+hdrBase64+" assertion:"+base64+"): "+t.getMessage(),t);
  54.                     }
  55.                     if(this.jsonHeader!=null) {
  56.                         try {
  57.                             JSONUtils jsonUtils = JSONUtils.getInstance();
  58.                             if(jsonUtils.isJson(this.jsonHeader)) {
  59.                                 JsonNode root = jsonUtils.getAsNode(this.jsonHeader);
  60.                                 Map<String, Serializable> readClaims = jsonUtils.convertToSimpleMap(root);
  61.                                 if(readClaims!=null && readClaims.size()>0) {
  62.                                     this.header = new HashMap<>();
  63.                                     this.header.putAll(readClaims);
  64.                                 }
  65.                             }  
  66.                         }catch(Exception t) {
  67.                             log.error("Decode header failed (json: "+this.jsonHeader+"): "+t.getMessage(),t);
  68.                         }
  69.                     }
  70.                 }
  71.                 if(split.length>1 && split[1]!=null) {
  72.                     String payloadBase64 = split[1];
  73.                     try {
  74.                         this.jsonPayload = new String(Base64Utilities.decode(payloadBase64));
  75.                     }catch(Exception t) {
  76.                         log.error("Decode payload failed (payload: "+payloadBase64+" assertion:"+base64+"): "+t.getMessage(),t);
  77.                     }
  78.                     if(this.jsonPayload!=null) {
  79.                         try {
  80.                             JSONUtils jsonUtils = JSONUtils.getInstance();
  81.                             if(jsonUtils.isJson(this.jsonPayload)) {
  82.                                 JsonNode root = jsonUtils.getAsNode(this.jsonPayload);
  83.                                 Map<String, Serializable> readClaims = jsonUtils.convertToSimpleMap(root);
  84.                                 if(readClaims!=null && readClaims.size()>0) {
  85.                                     this.payload = new HashMap<>();
  86.                                     this.payload.putAll(readClaims);
  87.                                 }
  88.                             }  
  89.                         }catch(Exception t) {
  90.                             log.error("Decode payload failed (json: "+this.jsonPayload+"): "+t.getMessage(),t);
  91.                         }
  92.                     }
  93.                 }
  94.             }
  95.         }
  96.     }
  97.    
  98.     // NOTA: l'ordine stabilisce come viene serializzato nell'oggetto json
  99.    
  100.     // RawResponse
  101.     private String token;
  102.     // Claims
  103.     private Map<String,Serializable> header = null;
  104.     private Map<String,Serializable> payload = null;
  105.     private String jsonHeader;
  106.     private String jsonPayload;

  107.     public Map<String, Serializable> getHeader() {
  108.         return this.header;
  109.     }
  110.     public void setHeader(Map<String, Serializable> header) {
  111.         this.header = header;
  112.     }
  113.     public Map<String, Serializable> getPayload() {
  114.         return this.payload;
  115.     }
  116.     public void setPayload(Map<String, Serializable> payload) {
  117.         this.payload = payload;
  118.     }

  119.     public String getToken() {
  120.         return this.token;
  121.     }
  122.     public void setToken(String token) {
  123.         this.token = token;
  124.     }
  125.     public String getJsonHeader() {
  126.         return this.jsonHeader;
  127.     }
  128.     public void setJsonHeader(String jsonHeader) {
  129.         this.jsonHeader = jsonHeader;
  130.     }
  131.     public String getJsonPayload() {
  132.         return this.jsonPayload;
  133.     }
  134.     public void setJsonPayload(String jsonPayload) {
  135.         this.jsonPayload = jsonPayload;
  136.     }

  137. }