AbstractInformazioniJWT.java

/*
 * GovWay - A customizable API Gateway
 * https://govway.org
 *
 * Copyright (c) 2005-2026 Link.it srl (https://link.it).
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 3, as published by
 * the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

package org.openspcoop2.pdd.core.token;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

import org.openspcoop2.utils.io.Base64Utilities;
import org.openspcoop2.utils.json.JSONUtils;
import org.slf4j.Logger;

import com.fasterxml.jackson.databind.JsonNode;

/**
 * AbstractInformazioniJWT
 *
 * @author Poli Andrea (poli@link.it)
 * @author $Author$
 * @version $Rev$, $Date$
 */
public abstract class AbstractInformazioniJWT extends org.openspcoop2.utils.beans.BaseBean implements Serializable, Cloneable {

	/**
	 *
	 */
	private static final long serialVersionUID = 1L;

	protected AbstractInformazioniJWT() {
		// per serializzatore
	}
	protected AbstractInformazioniJWT(Logger log, String base64, boolean infoNormalizzate, String tokenType) {
		this.token = base64;

		if(infoNormalizzate) {
			String [] split = this.token.split("\\.");
			if(split!=null && split.length>0) {
				if(split[0]!=null) {
					String hdrBase64 = split[0];
					initHeader(log, base64, tokenType, hdrBase64);
				}
				if(split.length>1 && split[1]!=null) {
					String payloadBase64 = split[1];
					initPayload(log, base64, tokenType, payloadBase64);
				}
			}
		}
	}
	private void initHeader(Logger log, String base64, String tokenType, String hdrBase64) {
		try {
			this.jsonHeader = new String(Base64Utilities.decode(hdrBase64));
		}catch(Exception t) {
			log.error("Decode header failed (hdr: "+hdrBase64+" "+tokenType+":"+base64+"): "+t.getMessage(),t);
		}
		if(this.jsonHeader!=null) {
			try {
				JSONUtils jsonUtils = JSONUtils.getInstance();
				if(jsonUtils.isJson(this.jsonHeader)) {
					JsonNode root = jsonUtils.getAsNode(this.jsonHeader);
					Map<String, Serializable> readClaims = jsonUtils.convertToSimpleMap(root);
					if(readClaims!=null && readClaims.size()>0) {
						this.header = new HashMap<>();
						this.header.putAll(readClaims);
					}
				}
			}catch(Exception t) {
				log.error("Decode header failed (json: "+this.jsonHeader+"): "+t.getMessage(),t);
			}
		}
	}
	private void initPayload(Logger log, String base64, String tokenType, String payloadBase64) {
		try {
			this.jsonPayload = new String(Base64Utilities.decode(payloadBase64));
		}catch(Exception t) {
			log.error("Decode payload failed (payload: "+payloadBase64+" "+tokenType+":"+base64+"): "+t.getMessage(),t);
		}
		if(this.jsonPayload!=null) {
			try {
				JSONUtils jsonUtils = JSONUtils.getInstance();
				if(jsonUtils.isJson(this.jsonPayload)) {
					JsonNode root = jsonUtils.getAsNode(this.jsonPayload);
					Map<String, Serializable> readClaims = jsonUtils.convertToSimpleMap(root);
					if(readClaims!=null && readClaims.size()>0) {
						this.payload = new HashMap<>();
						this.payload.putAll(readClaims);
					}
				}
			}catch(Exception t) {
				log.error("Decode payload failed (json: "+this.jsonPayload+"): "+t.getMessage(),t);
			}
		}
	}

	// NOTA: l'ordine stabilisce come viene serializzato nell'oggetto json

	// RawResponse
	private String token;
	// Claims
	private Map<String,Serializable> header = null;
	private Map<String,Serializable> payload = null;
	private String jsonHeader;
	private String jsonPayload;

	public Map<String, Serializable> getHeader() {
		return this.header;
	}
	public void setHeader(Map<String, Serializable> header) {
		this.header = header;
	}
	public Map<String, Serializable> getPayload() {
		return this.payload;
	}
	public void setPayload(Map<String, Serializable> payload) {
		this.payload = payload;
	}

	public String getToken() {
		return this.token;
	}
	public void setToken(String token) {
		this.token = token;
	}
	public String getJsonHeader() {
		return this.jsonHeader;
	}
	public void setJsonHeader(String jsonHeader) {
		this.jsonHeader = jsonHeader;
	}
	public String getJsonPayload() {
		return this.jsonPayload;
	}
	public void setJsonPayload(String jsonPayload) {
		this.jsonPayload = jsonPayload;
	}

}