RestMessageSecurityToken.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.sdk;

  21. import java.io.Serializable;
  22. import java.util.ArrayList;
  23. import java.util.HashMap;
  24. import java.util.List;
  25. import java.util.Map;

  26. import org.openspcoop2.utils.UtilsException;
  27. import org.openspcoop2.utils.io.Base64Utilities;
  28. import org.openspcoop2.utils.json.JSONUtils;

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

  30. /**    
  31.  * RestMessageSecurityToken
  32.  *
  33.  * @author Poli Andrea (poli@link.it)
  34.  * @author $Author$
  35.  * @version $Rev$, $Date$AuthorizationMessageSecurityToken
  36.  */
  37. public class RestMessageSecurityToken extends AbstractMessageSecurityToken<String> implements Serializable {

  38.     /**
  39.      *
  40.      */
  41.     private static final long serialVersionUID = 1L;
  42.    
  43.     private String httpHeaderName;
  44.     private String queryParameterName;
  45.     private String formParameterName;
  46.    
  47.     private String jweDecodedPayload; // jwe

  48.     public String getQueryParameterName() {
  49.         return this.queryParameterName;
  50.     }
  51.     public void setQueryParameterName(String queryParameterName) {
  52.         this.queryParameterName = queryParameterName;
  53.     }
  54.     public String getFormParameterName() {
  55.         return this.formParameterName;
  56.     }
  57.     public void setFormParameterName(String formParameterName) {
  58.         this.formParameterName = formParameterName;
  59.     }
  60.     public String getHttpHeaderName() {
  61.         return this.httpHeaderName;
  62.     }
  63.     public void setHttpHeaderName(String httpHeaderName) {
  64.         this.httpHeaderName = httpHeaderName;
  65.     }
  66.    
  67.     public void setJweDecodedPayload(String jweDecodedPayload) {
  68.         this.jweDecodedPayload = jweDecodedPayload;
  69.     }
  70.    
  71.     public String getHeader() {
  72.         if(this.token!=null) {
  73.             String [] split = this.token.split("\\.");
  74.             if(split!=null && split.length>0) {
  75.                 return split[0];
  76.             }
  77.         }
  78.         return null;
  79.     }
  80.     public String getDecodedHeader() {
  81.         String hdr = this.getHeader();
  82.         if(hdr!=null) {
  83.             return new String(Base64Utilities.decode(hdr));
  84.         }
  85.         return null;
  86.     }
  87.     public Map<String, String> getHeaderClaims() throws UtilsException {
  88.         Map<String, String> m = null;
  89.         if(this.token!=null) {
  90.             if(this.readClaimsHeader==null) {
  91.                 this.initReadClaimsHeader();
  92.             }
  93.             return this.readClaimsHeader;
  94.         }
  95.         return m;
  96.     }
  97.     public String getHeaderClaim(String claim) throws UtilsException {
  98.         if(this.token!=null) {
  99.             if(this.readClaimsHeader==null) {
  100.                 this.initReadClaimsHeader();
  101.             }
  102.             if(this.readClaimsHeader!=null) {
  103.                 return this.readClaimsHeader.get(claim);
  104.             }
  105.         }
  106.         return null;
  107.     }
  108.    
  109.     public String getPayload() {
  110.         if(this.jweDecodedPayload!=null) {
  111.             return this.jweDecodedPayload;
  112.         }
  113.         if(this.token!=null) {
  114.             String [] split = this.token.split("\\.");
  115.             if(split!=null && split.length>1) {
  116.                 return split[1];
  117.             }
  118.         }
  119.         return null;
  120.     }
  121.     public String getDecodedPayload() {
  122.         if(this.jweDecodedPayload!=null) {
  123.             return this.jweDecodedPayload;
  124.         }
  125.         String payload = this.getPayload();
  126.         if(payload!=null) {
  127.             return new String(Base64Utilities.decode(payload));
  128.         }
  129.         return null;
  130.     }
  131.     public Map<String, String> getPayloadClaims() throws UtilsException {
  132.         Map<String, String> m = null;
  133.         if(this.token!=null) {
  134.             if(this.readClaimsPayload==null) {
  135.                 this.initReadClaimsPayload();
  136.             }
  137.             return this.readClaimsPayload;
  138.         }
  139.         return m;
  140.     }
  141.     public String getPayloadClaim(String claim) throws UtilsException {
  142.         if(this.token!=null) {
  143.             if(this.readClaimsPayload==null) {
  144.                 this.initReadClaimsPayload();
  145.             }
  146.             if(this.readClaimsPayload!=null) {
  147.                 return this.readClaimsPayload.get(claim);
  148.             }
  149.         }
  150.         return null;
  151.     }
  152.    
  153.    
  154.     // -- Utilities
  155.    
  156.     private Map<String, String> readClaimsHeader = null;
  157.     private synchronized void initReadClaimsHeader() throws UtilsException {
  158.         if(this.readClaimsHeader==null) {
  159.             this.readClaimsHeader = new HashMap<>();
  160.             String hdr = getDecodedHeader();
  161.             JSONUtils jsonUtils = JSONUtils.getInstance();
  162.             if(jsonUtils.isJson(hdr)) {
  163.                 JsonNode root = jsonUtils.getAsNode(hdr);
  164.                 Map<String, Serializable> readClaims = jsonUtils.convertToSimpleMap(root);
  165.                 initReadClaimsHeader(readClaims);
  166.             }
  167.         }
  168.     }
  169.     private void initReadClaimsHeader(Map<String, Serializable> readClaims) {
  170.         if(readClaims!=null && readClaims.size()>0) {
  171.             for (Map.Entry<String,Serializable> entry: readClaims.entrySet()) {
  172.                 String claim = entry.getKey();
  173.                 Serializable o = readClaims.get(claim);
  174.                 putClaimHeader(claim, o);
  175.             }
  176.         }
  177.     }
  178.     private void putClaimHeader(String claim, Serializable o) {
  179.         if(o!=null) {
  180.             List<String> lClaimValues = getClaimValues(o);
  181.             if(lClaimValues!=null && !lClaimValues.isEmpty()) {
  182.                 String v = getClaimValuesAsString(lClaimValues);
  183.                 if(v!=null) {
  184.                     this.readClaimsHeader.put(claim, v);
  185.                 }
  186.             }
  187.         }
  188.     }
  189.    
  190.     private Map<String, String> readClaimsPayload = null;
  191.     private synchronized void initReadClaimsPayload() throws UtilsException {
  192.         if(this.readClaimsPayload==null) {
  193.             this.readClaimsPayload = new HashMap<>();
  194.             String payload = getDecodedPayload();
  195.             JSONUtils jsonUtils = JSONUtils.getInstance();
  196.             if(jsonUtils.isJson(payload)) {
  197.                 JsonNode root = jsonUtils.getAsNode(payload);
  198.                 Map<String, Serializable> readClaims = jsonUtils.convertToSimpleMap(root);
  199.                 initReadClaimsPayload(readClaims);
  200.             }
  201.         }
  202.     }
  203.     private void initReadClaimsPayload(Map<String, Serializable> readClaims) {
  204.         if(readClaims!=null && readClaims.size()>0) {
  205.             for (Map.Entry<String,Serializable> entry: readClaims.entrySet()) {
  206.                 String claim = entry.getKey();
  207.                 Serializable o = readClaims.get(claim);
  208.                 putClaimPayload(claim, o);
  209.             }
  210.         }
  211.     }
  212.     private void putClaimPayload(String claim, Serializable o) {
  213.         if(o!=null) {
  214.             List<String> lClaimValues = getClaimValues(o);
  215.             if(lClaimValues!=null && !lClaimValues.isEmpty()) {
  216.                 String v = getClaimValuesAsString(lClaimValues);
  217.                 if(v!=null) {
  218.                     this.readClaimsPayload.put(claim, v);
  219.                 }
  220.             }
  221.         }
  222.     }

  223.     public static List<String> getClaimValues(Object value) {
  224.         List<String> lreturn = null;
  225.         if(value!=null) {
  226.             if(value instanceof List<?>) {
  227.                 List<?> l = (List<?>) value;
  228.                 List<String> lString = convertTo(l);
  229.                 if(lString!=null) {
  230.                     return lString;
  231.                 }
  232.             }
  233.             else {
  234.                 String sValue = value.toString();
  235.                 List<String> l = new ArrayList<>();
  236.                 l.add(sValue);
  237.                 return l;
  238.             }
  239.         }
  240.         return lreturn;
  241.     }
  242.     private static List<String> convertTo(List<?> l){
  243.         List<String> lreturn = null;
  244.         if(!l.isEmpty()) {
  245.             List<String> lString = new ArrayList<>();
  246.             for (Object o : l) {
  247.                 if(o!=null) {
  248.                     lString.add(o.toString());
  249.                 }
  250.             }
  251.             if(!lString.isEmpty()) {
  252.                 return lString;
  253.             }
  254.         }
  255.         return lreturn;
  256.     }
  257.     private static String getClaimValuesAsString(List<String> claimValues) {
  258.         String claimValue = null;
  259.         if(claimValues==null || claimValues.isEmpty()) {
  260.             return null;
  261.         }
  262.         if(claimValues.size()>1) {
  263.             StringBuilder sb = new StringBuilder();
  264.             for (String c : claimValues) {
  265.                 if(sb.length()>0) {
  266.                     sb.append(",");
  267.                 }
  268.                 sb.append(c);
  269.             }
  270.             claimValue = sb.toString();
  271.         }
  272.         else {
  273.             claimValue = claimValues.get(0);
  274.         }
  275.         return claimValue;
  276.     }
  277.    
  278. }