InformazioniAttributi.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.attribute_authority;

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

  28. import org.openspcoop2.pdd.core.token.TipoInformazioni;
  29. import org.openspcoop2.pdd.core.token.TokenUtilities;
  30. import org.openspcoop2.pdd.logger.OpenSPCoop2Logger;
  31. import org.openspcoop2.utils.BooleanNullable;
  32. import org.openspcoop2.utils.json.JSONUtils;

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

  34. /**    
  35.  * InformazioniAttributi
  36.  *
  37.  * @author Poli Andrea (poli@link.it)
  38.  * @author $Author$
  39.  * @version $Rev$, $Date$
  40.  */
  41. public class InformazioniAttributi extends org.openspcoop2.utils.beans.BaseBean implements Serializable, Cloneable {

  42.     /**
  43.      *
  44.      */
  45.     private static final long serialVersionUID = 1L;
  46.    
  47.     public InformazioniAttributi() {} // per serializzatore
  48.     public InformazioniAttributi(String sourceAttributeAuthority) {
  49.         this.sourceAttributeAuthority = sourceAttributeAuthority;
  50.     } // per test
  51.     public InformazioniAttributi(String sourceAttributeAuthority, String rawResponse, IRetrieveAttributeAuthorityResponseParser responseParser) throws Exception {
  52.         this(null,sourceAttributeAuthority, rawResponse,responseParser);
  53.     }
  54.     public InformazioniAttributi(Integer httpResponseCode, String sourceAttributeAuthority, String rawResponse, IRetrieveAttributeAuthorityResponseParser responseParser) throws Exception {
  55.         init(httpResponseCode, sourceAttributeAuthority,
  56.                 null, rawResponse, responseParser);
  57.     }
  58.     public InformazioniAttributi(String sourceAttributeAuthority, byte[]raw, IRetrieveAttributeAuthorityResponseParser responseParser) throws Exception {
  59.         this(null,sourceAttributeAuthority, raw,responseParser);
  60.     }
  61.     public InformazioniAttributi(Integer httpResponseCode, String sourceAttributeAuthority, byte[]raw, IRetrieveAttributeAuthorityResponseParser responseParser) throws Exception {
  62.         init(httpResponseCode, sourceAttributeAuthority,
  63.                 raw, null, responseParser);
  64.     }
  65.     private void init(Integer httpResponseCode, String sourceAttributeAuthority,
  66.             byte[]raw, String rawResponse, IRetrieveAttributeAuthorityResponseParser responseParser) throws Exception {
  67.         this.raw = raw;
  68.         this.rawResponse = rawResponse;
  69.         this.sourceAttributeAuthority = sourceAttributeAuthority;

  70.         if(this.raw!=null) {
  71.             responseParser.init(this.raw);
  72.             String rawString = responseParser.getContentAsString();
  73.             if(rawString!=null && !"".equals(rawString)) {
  74.                 this.rawResponse = rawString;
  75.             }
  76.         }
  77.         else {
  78.             JSONUtils jsonUtils = JSONUtils.getInstance();
  79.             if(jsonUtils.isJson(this.rawResponse)) {
  80.                 JsonNode root = jsonUtils.getAsNode(this.rawResponse);
  81.                 Map<String, Serializable> readClaims = jsonUtils.convertToSimpleMap(root);
  82.                 if(readClaims!=null && readClaims.size()>0) {
  83.                     this.claims.putAll(readClaims);
  84.                 }
  85.             }
  86.             responseParser.init(this.rawResponse, this.claims);
  87.         }
  88.         if(httpResponseCode!=null) {
  89.             responseParser.checkHttpTransaction(httpResponseCode);
  90.         }
  91.         this.valid = responseParser.isValid();
  92.         this.attributes = responseParser.getAttributes();
  93.         this.iss = responseParser.getIssuer();
  94.         this.sub = responseParser.getSubject();
  95.         List<String> a = responseParser.getAudience();
  96.         if(a!=null && !a.isEmpty()) {
  97.             if(this.aud == null) {
  98.                 this.aud = new ArrayList<>();
  99.             }
  100.             this.aud.addAll(a);
  101.         }
  102.         this.exp = responseParser.getExpired();
  103.         this.iat = responseParser.getIssuedAt();
  104.         this.nbf = responseParser.getNotToBeUsedBefore();
  105.         this.identifier = responseParser.getIdentifier();
  106.     }
  107.     public InformazioniAttributi(boolean saveSourceAttributeResponseInfo, InformazioniAttributi ... informazioniTokens ) throws Exception {
  108.         if(informazioniTokens!=null && informazioniTokens.length>0) {
  109.            
  110.             this.multipleAttributeAuthorities = true;
  111.             for (int i = 0; i < informazioniTokens.length; i++) {
  112.                 this.attributeAuthorities.add(informazioniTokens[i].getSourceAttributeAuthority());
  113.             }
  114.            
  115.             if(saveSourceAttributeResponseInfo) {
  116.                 this.sourcesAttributeInfo = new HashMap<>();
  117.                 for (int i = 0; i < informazioniTokens.length; i++) {
  118.                     if(informazioniTokens[i].getRawResponse()!=null) {
  119.                         this.sourcesAttributeInfo.put(informazioniTokens[i].getSourceAttributeAuthority(),
  120.                                 informazioniTokens[i].getRawResponse());
  121.                     }
  122.                     else {
  123.                         this.sourcesAttributeInfo.put(informazioniTokens[i].getSourceAttributeAuthority(),
  124.                                 "N.D.");
  125.                     }
  126.                 }
  127.             }
  128.             else {
  129.                 this.sourceAttributeAuthorities = new ArrayList<>();
  130.                 for (int i = 0; i < informazioniTokens.length; i++) {
  131.                     this.sourceAttributeAuthorities.add(informazioniTokens[i].getSourceAttributeAuthority());
  132.                 }
  133.             }
  134.            
  135.             for (int i = 0; i < informazioniTokens.length; i++) {
  136.                 if(informazioniTokens[i].getClaims().size()>0) {
  137.                     this.claims.put(informazioniTokens[i].getSourceAttributeAuthority(),TokenUtilities.toHashMapSerializable(informazioniTokens[i].getClaims()));
  138.                 }
  139.             }
  140.            
  141.             for (int i = 0; i < informazioniTokens.length; i++) {
  142.                 if(informazioniTokens[i].getAttributes()!=null && informazioniTokens[i].getAttributes().size()>0) {
  143.                     if(this.attributes==null) {
  144.                         this.attributes = new HashMap<>();
  145.                     }
  146.                     this.attributes.put(informazioniTokens[i].getSourceAttributeAuthority(),TokenUtilities.toHashMapSerializable(informazioniTokens[i].getAttributes()));
  147.                 }
  148.             }
  149.            
  150.             for (int i = 0; i < informazioniTokens.length; i++) {
  151.                 if(informazioniTokens[i].getIss()!=null) {
  152.                     if(this.aaIss==null) {
  153.                         this.aaIss = new HashMap<>();
  154.                     }
  155.                     this.aaIss.put(informazioniTokens[i].getSourceAttributeAuthority(),informazioniTokens[i].getIss());
  156.                 }
  157.             }
  158.             for (int i = 0; i < informazioniTokens.length; i++) {
  159.                 if(informazioniTokens[i].getSub()!=null) {
  160.                     if(this.aaSub==null) {
  161.                         this.aaSub = new HashMap<>();
  162.                     }
  163.                     this.aaSub.put(informazioniTokens[i].getSourceAttributeAuthority(),informazioniTokens[i].getSub());
  164.                 }
  165.             }
  166.            
  167.             for (int i = 0; i < informazioniTokens.length; i++) {
  168.                 if(informazioniTokens[i].getAud()!=null && !informazioniTokens[i].getAud().isEmpty()) {
  169.                     if(this.aaAud==null) {
  170.                         this.aaAud = new HashMap<>();
  171.                     }
  172.                     this.aaAud.put(informazioniTokens[i].getSourceAttributeAuthority(),informazioniTokens[i].getAud());
  173.                 }
  174.             }

  175.             for (int i = 0; i < informazioniTokens.length; i++) {
  176.                 if(informazioniTokens[i].getExp()!=null) {
  177.                     if(this.aaExp==null) {
  178.                         this.aaExp = new HashMap<>();
  179.                     }
  180.                     this.aaExp.put(informazioniTokens[i].getSourceAttributeAuthority(),informazioniTokens[i].getExp());
  181.                 }
  182.             }
  183.             for (int i = 0; i < informazioniTokens.length; i++) {
  184.                 if(informazioniTokens[i].getIat()!=null) {
  185.                     if(this.aaIat==null) {
  186.                         this.aaIat = new HashMap<>();
  187.                     }
  188.                     this.aaIat.put(informazioniTokens[i].getSourceAttributeAuthority(),informazioniTokens[i].getIat());
  189.                 }
  190.             }
  191.             for (int i = 0; i < informazioniTokens.length; i++) {
  192.                 if(informazioniTokens[i].getNbf()!=null) {
  193.                     if(this.aaNbf==null) {
  194.                         this.aaNbf = new HashMap<>();
  195.                     }
  196.                     this.aaNbf.put(informazioniTokens[i].getSourceAttributeAuthority(),informazioniTokens[i].getNbf());
  197.                 }
  198.             }
  199.            
  200.             for (int i = 0; i < informazioniTokens.length; i++) {
  201.                 if(informazioniTokens[i].getIdentifier()!=null) {
  202.                     if(this.aaIdentifier==null) {
  203.                         this.aaIdentifier = new HashMap<>();
  204.                     }
  205.                     this.aaIdentifier.put(informazioniTokens[i].getSourceAttributeAuthority(),informazioniTokens[i].getIdentifier());
  206.                 }
  207.             }
  208.         }
  209.     }
  210.    

  211.        
  212.     // NOTA: l'ordine stabilisce come viene serializzato nell'oggetto json
  213.    
  214.     private TipoInformazioni type = TipoInformazioni.attribute_authority;
  215.    
  216.     // Indicazione se il token e' valido
  217.     private boolean valid;
  218.    
  219.     // Attributi
  220.     private Map<String, Serializable> attributes;

  221.     // String representing the issuer for this attribute response
  222.     private String iss;
  223.     private Map<String, String> aaIss;
  224.    
  225.     // String representing the Subject of this attribute response
  226.     private String sub;
  227.     private Map<String, String> aaSub;
  228.        
  229.     // Service-specific string identifier or list of string identifiers representing the intended audience for this attribute response
  230.     private List<String> aud;
  231.     private Map<String, List<String>> aaAud;
  232.    
  233.     // Indicate when this attribute response will expire
  234.     private Date exp;
  235.     private Map<String, Date> aaExp;
  236.    
  237.     // Indicate when this attribute response was originally issued
  238.     private Date iat;
  239.     private Map<String, Date> aaIat;
  240.    
  241.     // Indicate when this attribute response is not to be used before.
  242.     private Date nbf;
  243.     private Map<String, Date> aaNbf;
  244.    
  245.     // String representing the unique identifier for the attribute response
  246.     private String identifier;
  247.     private Map<String, String> aaIdentifier;
  248.    
  249.     // Claims
  250.     private Map<String,Serializable> claims = new HashMap<>();
  251.    
  252.     // NOTA: l'ordine stabilisce come viene serializzato nell'oggetto json
  253.    
  254.     private Boolean multipleAttributeAuthorities = false;
  255.     private List<String> attributeAuthorities = new ArrayList<>();
  256.    
  257.     // RawResponse
  258.     private byte[] raw;
  259.     private String rawResponse;
  260.    
  261.     // Nome dell'Attribute Authority dove sono stati reperiti gli attributi
  262.     private String sourceAttributeAuthority;
  263.    
  264.     // Multiple Source
  265.     private List<String> sourceAttributeAuthorities = null;
  266.     private Map<String,String> sourcesAttributeInfo = null;
  267.    
  268.    
  269.     public TipoInformazioni getType() {
  270.         return this.type;
  271.     }
  272.     public void setType(TipoInformazioni type) {
  273.         this.type = type;
  274.     }
  275.    
  276.     public boolean isValid() {
  277.         return this.valid;
  278.     }
  279.     public boolean getValid() { // clone
  280.         return this.valid;
  281.     }
  282.     public void setValid(boolean valid) {
  283.         this.valid = valid;
  284.     }
  285.    
  286.     public BooleanNullable isMultipleAttributeAuthorities() {
  287.         if(this.multipleAttributeAuthorities!=null) {
  288.             return this.multipleAttributeAuthorities.booleanValue() ? BooleanNullable.TRUE() : BooleanNullable.FALSE();
  289.         }
  290.         return BooleanNullable.NULL();
  291.     }
  292.     public Boolean getMultipleAttributeAuthorities() {
  293.         return this.multipleAttributeAuthorities;
  294.     }
  295.     public void setMultipleAttributeAuthorities(Boolean multipleAttributeAuthorities) {
  296.         this.multipleAttributeAuthorities = multipleAttributeAuthorities;
  297.     }
  298.    
  299.     public List<String> getAttributeAuthorities() {
  300.         List<String> l = null;
  301.         if(this.attributeAuthorities!=null && !this.attributeAuthorities.isEmpty()) {
  302.             return this.attributeAuthorities;
  303.         }
  304.         return l;
  305.     }
  306.     public void setAttributeAuthorities(List<String> attributeAuthorities) {
  307.         this.attributeAuthorities = attributeAuthorities;
  308.     }
  309.    
  310.     public Map<String, Serializable> getAttributes() {
  311.         return this.attributes;
  312.     }
  313.     public void setAttributes(Map<String, Serializable> attributes) {
  314.         this.attributes = attributes;
  315.     }
  316.     public List<String> getAttributesNames(){
  317.         List<String> l = null;
  318.         if(this.attributes!=null && !this.attributes.isEmpty()) {
  319.             if(this.multipleAttributeAuthorities!=null && this.multipleAttributeAuthorities) {
  320.                 // informazioni normalizzate, devo scendere di un livello, senno al primo degli attributi ci sono le attribute authority
  321.                 List<String> attributesNames = new ArrayList<>();
  322.                 for (String attrAuthName : this.attributes.keySet()) {
  323.                     Object o = this.attributes.get(attrAuthName);
  324.                     if(o instanceof Map) {
  325.                         try {
  326.                             @SuppressWarnings("unchecked")
  327.                             Map<String, Object> attributesO = (Map<String, Object>) o;
  328.                             if(attributesO!=null && !attributesO.isEmpty()) {
  329.                                 for (String attrName : attributesO.keySet()) {
  330.                                     if(!attributesNames.contains(attrName)) {
  331.                                         attributesNames.add(attrName);
  332.                                     }
  333.                                 }
  334.                             }
  335.                         }catch(Exception t) {
  336.                             OpenSPCoop2Logger.getLoggerOpenSPCoopCore().error("getAttributesNames failed (A.A. "+attrAuthName+"): "+t.getMessage(),t);
  337.                         }
  338.                     }
  339.                 }
  340.                 Collections.sort(attributesNames);
  341.                 return attributesNames;
  342.             }
  343.             else {
  344.                 List<String> attributesNames = new ArrayList<>();
  345.                 for (String attrName : this.attributes.keySet()) {
  346.                     attributesNames.add(attrName);
  347.                 }
  348.                 Collections.sort(attributesNames);
  349.                 return attributesNames;
  350.             }
  351.         }
  352.         return l;
  353.     }
  354.    
  355.     public String getIss() {
  356.         return this.iss;
  357.     }
  358.     public void setIss(String iss) {
  359.         this.iss = iss;
  360.     }
  361.     public Map<String, String> getAaIss() {
  362.         return this.aaIss;
  363.     }
  364.     public void setAaIss(Map<String, String> aaIss) {
  365.         this.aaIss = aaIss;
  366.     }

  367.     public String getSub() {
  368.         return this.sub;
  369.     }
  370.     public void setSub(String sub) {
  371.         this.sub = sub;
  372.     }
  373.     public Map<String, String> getAaSub() {
  374.         return this.aaSub;
  375.     }
  376.     public void setAaSub(Map<String, String> aaSub) {
  377.         this.aaSub = aaSub;
  378.     }

  379.     public List<String> getAud() {
  380.         return this.aud;
  381.     }
  382.     public void setAud(List<String> aud) {
  383.         this.aud = aud;
  384.     }
  385.     public Map<String, List<String>> getAaAud() {
  386.         return this.aaAud;
  387.     }
  388.     public void setAaAud(Map<String, List<String>> aaAud) {
  389.         this.aaAud = aaAud;
  390.     }

  391.     public Date getExp() {
  392.         return this.exp;
  393.     }
  394.     public void setExp(Date exp) {
  395.         this.exp = exp;
  396.     }
  397.     public Map<String, Date> getAaExp() {
  398.         return this.aaExp;
  399.     }
  400.     public void setAaExp(Map<String, Date> aaExp) {
  401.         this.aaExp = aaExp;
  402.     }

  403.     public Date getIat() {
  404.         return this.iat;
  405.     }
  406.     public void setIat(Date iat) {
  407.         this.iat = iat;
  408.     }
  409.     public Map<String, Date> getAaIat() {
  410.         return this.aaIat;
  411.     }
  412.     public void setAaIat(Map<String, Date> aaIat) {
  413.         this.aaIat = aaIat;
  414.     }

  415.     public Date getNbf() {
  416.         return this.nbf;
  417.     }
  418.     public void setNbf(Date nbf) {
  419.         this.nbf = nbf;
  420.     }
  421.     public Map<String, Date> getAaNbf() {
  422.         return this.aaNbf;
  423.     }
  424.     public void setAaNbf(Map<String, Date> aaNbf) {
  425.         this.aaNbf = aaNbf;
  426.     }
  427.    
  428.     public String getIdentifier() {
  429.         return this.identifier;
  430.     }
  431.     public void setIdentifier(String id) {
  432.         this.identifier = id;
  433.     }
  434.     public Map<String, String> getAaIdentifier() {
  435.         return this.aaIdentifier;
  436.     }
  437.     public void setAaIdentifier(Map<String, String> aaIdentifier) {
  438.         this.aaIdentifier = aaIdentifier;
  439.     }
  440.    
  441.     public Map<String, Serializable> getClaims() {
  442.         return this.claims;
  443.     }
  444.     public void setClaims(Map<String, Serializable> claims) {
  445.         this.claims = claims;
  446.     }
  447.            
  448.     public String getRawResponse() {
  449.         return this.rawResponse;
  450.     }
  451.     public void setRawResponse(String rawResponse) {
  452.         this.rawResponse = rawResponse;
  453.     }
  454.    
  455.     public String getSourceAttributeAuthority() {
  456.         return this.sourceAttributeAuthority;
  457.     }
  458.     public void setSourceAttributeAuthority(String sourceAttributeAuthority) {
  459.         this.sourceAttributeAuthority = sourceAttributeAuthority;
  460.     }
  461.    
  462.     public Map<String, String> getSourcesAttributeInfo() {
  463.         return this.sourcesAttributeInfo;
  464.     }
  465.     public void setSourcesAttributeInfo(Map<String, String> sourcesAttributeInfo) {
  466.         this.sourcesAttributeInfo = sourcesAttributeInfo;
  467.     }
  468.    
  469.     public List<String> getSourceAttributeAuthorities() {
  470.         return this.sourceAttributeAuthorities;
  471.     }
  472.     public void setSourceAttributeAuthorities(List<String> sourceAttributeAuthorities) {
  473.         this.sourceAttributeAuthorities = sourceAttributeAuthorities;
  474.     }
  475.    
  476. }