ModIJWTTokenClaims.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.modipa.builder;

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

  25. import org.openspcoop2.protocol.modipa.constants.ModICostanti;
  26. import org.openspcoop2.protocol.sdk.Busta;

  27. /**
  28.  * ModIJWTTokenClaims
  29.  *
  30.  * @author Poli Andrea (apoli@link.it)
  31.  * @author $Author$
  32.  * @version $Rev$, $Date$
  33.  */
  34. public class ModIJWTTokenClaims implements Serializable {
  35.    
  36.     private static final long serialVersionUID = 1L;
  37.    
  38.     private boolean audit;
  39.     private String idTransazione; // no cache key
  40.     private boolean request;
  41.     private String porta;
  42.     private String operazione;
  43.     private boolean sicurezzaRidefinitaOperazione;
  44.     public ModIJWTTokenClaims(boolean audit, String idTransazione, boolean request, String porta, String operazione, boolean sicurezzaRidefinitaOperazione) {
  45.         this.audit = audit;
  46.         this.idTransazione = idTransazione;
  47.         this.request = request;
  48.         this.porta = porta;
  49.         this.operazione = operazione;
  50.         this.sicurezzaRidefinitaOperazione = sicurezzaRidefinitaOperazione;
  51.     }
  52.    
  53.     // header
  54.    
  55.     private boolean addKid;
  56.     private boolean addX5c;
  57.     private boolean addX5cChain;
  58.     private boolean addX5t;
  59.     private boolean addX5u;
  60.    
  61.     private String kid;
  62.    
  63.     private String algorithm;
  64.    
  65.     private String x5uUrl;
  66.    
  67.     private String pem;
  68.     private String jwk;
  69.    
  70.    
  71.     // payload
  72.    
  73.     private Date exp; // no cache key
  74.     private String expValue;
  75.     private Date nbf; // no cache key
  76.     private String nbfValue;
  77.     private Date iat; // no cache key
  78.     private String iatValue;
  79.    
  80.     private String jti; // no cache key
  81.    
  82.     private String audience;
  83.    
  84.     private String clientId;
  85.    
  86.     private String iss;
  87.    
  88.     private String sub;
  89.    
  90.     private Map<String, String> corniceSicurezzaAudit = new HashMap<>();
  91.    
  92.     private String purposeId;
  93.    
  94.     private String dNonce; // no cache key
  95.    
  96.     private Map<String, String> customClaims = new HashMap<>();
  97.    
  98.    
  99.     // claims non cachable (integrita)
  100.    
  101.     private String requestDigest;
  102.    

  103.     public String toCacheKey() {
  104.        
  105.         StringBuilder sb = new StringBuilder("ModI-Token");
  106.         if(this.audit) {
  107.             sb.append("-Audit");
  108.         }
  109.         else {
  110.             sb.append("-Authorization");
  111.         }
  112.         if(this.request) {
  113.             sb.append("-PD-");
  114.         }
  115.         else {
  116.             sb.append("-PA-");
  117.         }
  118.         sb.append(this.porta);
  119.        
  120.         if(this.sicurezzaRidefinitaOperazione) {
  121.             sb.append(" ");
  122.             sb.append(this.operazione);
  123.         }
  124.        
  125.         // header
  126.         addCacheKeyHeader(sb);
  127.    
  128.         // payload
  129.         addCacheKeyPayload(sb);

  130.         return sb.toString();
  131.     }
  132.     private void addCacheKeyHeader(StringBuilder sb) {
  133.                
  134.         sb.append(" add-kid:");
  135.         sb.append(this.addKid);
  136.        
  137.         sb.append(" add-x5c:");
  138.         sb.append(this.addX5c);
  139.        
  140.         sb.append(" add-x5c-chain:");
  141.         sb.append(this.addX5cChain);
  142.        
  143.         sb.append(" add-x5t:");
  144.         sb.append(this.addX5t);
  145.        
  146.         sb.append(" add-x5u:");
  147.         sb.append(this.addX5u);
  148.            
  149.         if(this.kid!=null) {
  150.             sb.append(" kid:");
  151.             sb.append(this.kid);
  152.         }
  153.        
  154.         if(this.algorithm!=null) {
  155.             sb.append(" algorithm:");
  156.             sb.append(this.algorithm);
  157.         }
  158.        
  159.         if(this.x5uUrl!=null) {
  160.             sb.append(" x5u-url:");
  161.             sb.append(this.x5uUrl);
  162.         }
  163.        
  164.         if(this.pem!=null) {
  165.             sb.append(" pem:");
  166.             sb.append(this.pem);
  167.         }
  168.        
  169.         if(this.jwk!=null) {
  170.             sb.append(" jwk:");
  171.             sb.append(this.jwk);
  172.         }
  173.     }
  174.     private void addCacheKeyPayload(StringBuilder sb) {
  175.         if(this.audience!=null) {
  176.             sb.append(" aud:");
  177.             sb.append(this.audience);
  178.         }
  179.        
  180.         if(this.clientId!=null) {
  181.             sb.append(" client_id:");
  182.             sb.append(this.clientId);
  183.         }
  184.        
  185.         if(this.iss!=null) {
  186.             sb.append(" iss:");
  187.             sb.append(this.iss);
  188.         }
  189.        
  190.         if(this.sub!=null) {
  191.             sb.append(" sub:");
  192.             sb.append(this.sub);
  193.         }
  194.        
  195.         if(!this.corniceSicurezzaAudit.isEmpty()) {
  196.             for (Map.Entry<String,String> entry : this.corniceSicurezzaAudit.entrySet()) {
  197.                 sb.append(" audit-").append(entry.getKey()).append(":");
  198.                 sb.append(entry.getValue());
  199.             }
  200.         }
  201.        
  202.         if(this.purposeId!=null) {
  203.             sb.append(" purposeId:");
  204.             sb.append(this.purposeId);
  205.         }
  206.        
  207.         if(!this.customClaims.isEmpty()) {
  208.             for (Map.Entry<String,String> entry : this.customClaims.entrySet()) {
  209.                 sb.append(" custom-").append(entry.getKey()).append(":");
  210.                 sb.append(entry.getValue());
  211.             }
  212.         }
  213.        
  214.     }

  215.     public void setInfoNonCachableInBusta(Busta busta) {
  216.        
  217.         // id transazione
  218.         if(this.idTransazione!=null) {
  219.             if(this.audit) {
  220.                 busta.addProperty(ModICostanti.MODIPA_BUSTA_EXT_PROFILO_SICUREZZA_MESSAGGIO_CORNICE_SICUREZZA_AUDIT_ORIGIN_TRANSACTION_ID, this.idTransazione);
  221.             }
  222.             else {
  223.                 busta.addProperty(ModICostanti.MODIPA_BUSTA_EXT_PROFILO_SICUREZZA_MESSAGGIO_ORIGIN_TRANSACTION_ID, this.idTransazione);
  224.             }
  225.         }
  226.        
  227.        
  228.         // iat, nbf, exp
  229.         setDateNonCachableInBusta(busta);
  230.        
  231.         // jti
  232.         if(this.jti!=null) {
  233.             if(this.audit) {
  234.                 busta.addProperty(ModICostanti.MODIPA_BUSTA_EXT_PROFILO_SICUREZZA_MESSAGGIO_CORNICE_SICUREZZA_AUDIT_ID, this.jti);
  235.             }
  236.             else {
  237.                 busta.addProperty(ModICostanti.MODIPA_BUSTA_EXT_PROFILO_SICUREZZA_MESSAGGIO_ID, this.jti);
  238.             }
  239.         }
  240.        
  241.         // dnonce
  242.         // per adesso non viene registrato nella busta
  243.     }
  244.     private void setDateNonCachableInBusta(Busta busta) {
  245.         // iat
  246.         if(this.iatValue!=null) {
  247.             if(this.audit) {
  248.                 busta.addProperty(ModICostanti.MODIPA_BUSTA_EXT_PROFILO_SICUREZZA_MESSAGGIO_CORNICE_SICUREZZA_AUDIT_IAT, this.iatValue);
  249.             }
  250.             else {
  251.                 busta.addProperty(ModICostanti.MODIPA_BUSTA_EXT_PROFILO_SICUREZZA_MESSAGGIO_IAT, this.iatValue);
  252.             }
  253.         }

  254.         // nbf
  255.         if(this.nbfValue!=null) {
  256.             if(this.audit) {
  257.                 busta.addProperty(ModICostanti.MODIPA_BUSTA_EXT_PROFILO_SICUREZZA_MESSAGGIO_CORNICE_SICUREZZA_AUDIT_NBF, this.nbfValue);
  258.             }
  259.             else {
  260.                 busta.addProperty(ModICostanti.MODIPA_BUSTA_EXT_PROFILO_SICUREZZA_MESSAGGIO_NBF, this.nbfValue);
  261.             }
  262.         }
  263.        
  264.         // exp
  265.         if(this.expValue!=null) {
  266.             if(this.audit) {
  267.                 busta.addProperty(ModICostanti.MODIPA_BUSTA_EXT_PROFILO_SICUREZZA_MESSAGGIO_CORNICE_SICUREZZA_AUDIT_EXP, this.expValue);
  268.             }
  269.             else {
  270.                 busta.addProperty(ModICostanti.MODIPA_BUSTA_EXT_PROFILO_SICUREZZA_MESSAGGIO_EXP, this.expValue);
  271.             }
  272.         }
  273.     }
  274.    
  275.     public String getExpValue() {
  276.         return this.expValue;
  277.     }
  278.     public void setExpValue(String expValue) {
  279.         this.expValue = expValue;
  280.     }
  281.     public String getNbfValue() {
  282.         return this.nbfValue;
  283.     }
  284.     public void setNbfValue(String nbfValue) {
  285.         this.nbfValue = nbfValue;
  286.     }
  287.     public String getIatValue() {
  288.         return this.iatValue;
  289.     }
  290.     public void setIatValue(String iatValue) {
  291.         this.iatValue = iatValue;
  292.     }
  293.     public Date getExp() {
  294.         return this.exp;
  295.     }
  296.     public void setExp(Date exp) {
  297.         this.exp = exp;
  298.     }
  299.     public Date getNbf() {
  300.         return this.nbf;
  301.     }
  302.     public void setNbf(Date nbf) {
  303.         this.nbf = nbf;
  304.     }
  305.     public Date getIat() {
  306.         return this.iat;
  307.     }
  308.     public void setIat(Date iat) {
  309.         this.iat = iat;
  310.     }
  311.    
  312.     public String getJti() {
  313.         return this.jti;
  314.     }
  315.     public void setJti(String jti) {
  316.         this.jti = jti;
  317.     }
  318.    
  319.     public String getAudience() {
  320.         return this.audience;
  321.     }
  322.     public void setAudience(String audience) {
  323.         this.audience = audience;
  324.     }
  325.    
  326.     public String getClientId() {
  327.         return this.clientId;
  328.     }
  329.     public void setClientId(String clientId) {
  330.         this.clientId = clientId;
  331.     }
  332.    
  333.     public String getKid() {
  334.         return this.kid;
  335.     }
  336.     public void setKid(String kid) {
  337.         this.kid = kid;
  338.     }
  339.    
  340.     public String getRequestDigest() {
  341.         return this.requestDigest;
  342.     }
  343.     public void setRequestDigest(String requestDigest) {
  344.         this.requestDigest = requestDigest;
  345.     }
  346.    
  347.     public Map<String, String> getCorniceSicurezzaAudit() {
  348.         return this.corniceSicurezzaAudit;
  349.     }
  350.     public void setCorniceSicurezzaAudit(Map<String, String> corniceSicurezzaAudit) {
  351.         this.corniceSicurezzaAudit = corniceSicurezzaAudit;
  352.     }
  353.     public void addCorniceSicurezzaAudit(String key, String value) {
  354.         this.corniceSicurezzaAudit.put(key, value);
  355.     }
  356.    
  357.     public String getIss() {
  358.         return this.iss;
  359.     }
  360.     public void setIss(String iss) {
  361.         this.iss = iss;
  362.     }
  363.     public String getSub() {
  364.         return this.sub;
  365.     }
  366.     public void setSub(String sub) {
  367.         this.sub = sub;
  368.     }
  369.    
  370.     public String getPurposeId() {
  371.         return this.purposeId;
  372.     }
  373.     public void setPurposeId(String purposeId) {
  374.         this.purposeId = purposeId;
  375.     }
  376.    
  377.     public String getdNonce() {
  378.         return this.dNonce;
  379.     }
  380.     public void setdNonce(String dNonce) {
  381.         this.dNonce = dNonce;
  382.     }
  383.    
  384.     public Map<String, String> getCustomClaims() {
  385.         return this.customClaims;
  386.     }
  387.     public void setCustomClaims(Map<String, String> customClaims) {
  388.         this.customClaims = customClaims;
  389.     }
  390.     public void addCustomClaim(String key, String value) {
  391.         this.customClaims.put(key, value);
  392.     }
  393.    
  394.     public String getAlgorithm() {
  395.         return this.algorithm;
  396.     }
  397.     public void setAlgorithm(String algorithm) {
  398.         this.algorithm = algorithm;
  399.     }
  400.    
  401.     public boolean isAddKid() {
  402.         return this.addKid;
  403.     }
  404.     public void setAddKid(boolean addKid) {
  405.         this.addKid = addKid;
  406.     }
  407.     public boolean isAddX5c() {
  408.         return this.addX5c;
  409.     }
  410.     public void setAddX5c(boolean addX5c) {
  411.         this.addX5c = addX5c;
  412.     }
  413.     public boolean isAddX5cChain() {
  414.         return this.addX5cChain;
  415.     }
  416.     public void setAddX5cChain(boolean addX5cChain) {
  417.         this.addX5cChain = addX5cChain;
  418.     }
  419.     public boolean isAddX5t() {
  420.         return this.addX5t;
  421.     }
  422.     public void setAddX5t(boolean addX5t) {
  423.         this.addX5t = addX5t;
  424.     }
  425.     public boolean isAddX5u() {
  426.         return this.addX5u;
  427.     }
  428.     public void setAddX5u(boolean addX5u) {
  429.         this.addX5u = addX5u;
  430.     }
  431.     public String getX5uUrl() {
  432.         return this.x5uUrl;
  433.     }
  434.     public void setX5uUrl(String x5uUrl) {
  435.         this.x5uUrl = x5uUrl;
  436.     }
  437.    
  438.     public String getPem() {
  439.         return this.pem;
  440.     }
  441.     public void setPem(String pem) {
  442.         this.pem = pem;
  443.     }
  444.     public String getJwk() {
  445.         return this.jwk;
  446.     }
  447.     public void setJwk(String jwk) {
  448.         this.jwk = jwk;
  449.     }
  450.    
  451.     public String getIdTransazione() {
  452.         return this.idTransazione;
  453.     }
  454.     public void setIdTransazione(String idTransazione) {
  455.         this.idTransazione = idTransazione;
  456.     }
  457. }