InformazioniToken.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.lang.reflect.Method;
  23. import java.util.ArrayList;
  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.attribute_authority.InformazioniAttributi;
  29. import org.openspcoop2.pdd.core.token.parser.ITokenParser;
  30. import org.openspcoop2.utils.UtilsException;
  31. import org.openspcoop2.utils.json.JSONUtils;

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

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

  41.     /**
  42.      *
  43.      */
  44.     private static final long serialVersionUID = 1L;
  45.    
  46.     public InformazioniToken() {} // per serializzatore
  47.     public InformazioniToken(SorgenteInformazioniToken sourceType, String rawResponse, ITokenParser tokenParser) throws UtilsException {
  48.         this(null,sourceType, rawResponse,tokenParser);
  49.     }
  50.     public InformazioniToken(Integer httpResponseCode, SorgenteInformazioniToken sourceType, String rawResponse, ITokenParser tokenParser) throws UtilsException {
  51.         this.rawResponse = rawResponse;
  52.         this.sourceType = sourceType;
  53.         JSONUtils jsonUtils = JSONUtils.getInstance();
  54.         if(jsonUtils.isJson(this.rawResponse)) {
  55.             JsonNode root = jsonUtils.getAsNode(this.rawResponse);
  56.             Map<String, Serializable> readClaims = jsonUtils.convertToSimpleMap(root);
  57.             if(readClaims!=null && readClaims.size()>0) {
  58.                 this.claims.putAll(readClaims);
  59.             }
  60.         }
  61.         tokenParser.init(this.rawResponse, this.claims);
  62.         if(httpResponseCode!=null) {
  63.             tokenParser.checkHttpTransaction(httpResponseCode);
  64.         }
  65.         this.valid = tokenParser.isValid();
  66.         this.iss = tokenParser.getIssuer();
  67.         this.sub = tokenParser.getSubject();
  68.         this.username = tokenParser.getUsername();
  69.         List<String> a = tokenParser.getAudience();
  70.         if(a!=null && !a.isEmpty()) {
  71.             this.aud = new ArrayList<>();
  72.             this.aud.addAll(a);
  73.         }
  74.         this.exp = tokenParser.getExpired();
  75.         this.iat = tokenParser.getIssuedAt();
  76.         this.nbf = tokenParser.getNotToBeUsedBefore();
  77.         this.clientId = tokenParser.getClientId();
  78.         this.jti = tokenParser.getJWTIdentifier();
  79.         List<String> r = tokenParser.getRoles();
  80.         if(r!=null && !r.isEmpty()) {
  81.             this.roles = new ArrayList<>();
  82.             this.roles.addAll(r);
  83.         }
  84.         List<String> s = tokenParser.getScopes();
  85.         if(s!=null && !s.isEmpty()) {
  86.             this.scopes = new ArrayList<>();
  87.             this.scopes.addAll(s);
  88.         }
  89.         if(tokenParser.getUserInfoParser()!=null) {
  90.             this.userInfo = new InformazioniTokenUserInfo(this, tokenParser.getUserInfoParser());
  91.         }
  92.     }
  93.    
  94.     public InformazioniToken(String errorDetails, SorgenteInformazioniToken sourceType, String token) {
  95.         this(errorDetails,null,null,sourceType, token);
  96.     }
  97.     public InformazioniToken(String errorDetails, Integer httpResponseCode, byte[] rawResponse, SorgenteInformazioniToken sourceType, String token) {
  98.         this.valid = false;
  99.         this.token = token;
  100.         this.claims = null;
  101.         this.sourceType = sourceType;
  102.         if(httpResponseCode!=null) {
  103.             this.httpResponseCode = httpResponseCode+"";
  104.         }
  105.         if(rawResponse!=null) {
  106.             this.rawResponse = new String(rawResponse);
  107.         }
  108.         this.errorDetails = errorDetails;
  109.     }
  110.    
  111.     @SuppressWarnings("unchecked")
  112.     public InformazioniToken(boolean saveSourceTokenInfo, InformazioniToken ... informazioniTokens ) throws UtilsException {
  113.         if(informazioniTokens!=null && informazioniTokens.length>0) {
  114.            
  115.             if(saveSourceTokenInfo) {
  116.                 this.sourcesTokenInfo = new HashMap<>();
  117.                 for (int i = 0; i < informazioniTokens.length; i++) {
  118.                     this.sourcesTokenInfo.put(informazioniTokens[i].getSourceType(),
  119.                             informazioniTokens[i].getRawResponse());
  120.                 }
  121.             }
  122.             else {
  123.                 this.sourceTypes = new ArrayList<>();
  124.                 for (int i = 0; i < informazioniTokens.length; i++) {
  125.                     this.sourceTypes.add(informazioniTokens[i].getSourceType());
  126.                 }
  127.             }
  128.            
  129.             for (int i = 0; i < informazioniTokens.length; i++) {
  130.                 if(informazioniTokens[i].getClaims().size()>0) {
  131.                     this.claims.putAll(informazioniTokens[i].getClaims());
  132.                 }
  133.             }
  134.            
  135.             Object issV = getValue("iss", informazioniTokens);
  136.             if(issV instanceof String) {
  137.                 this.iss = (String) issV;
  138.             }
  139.            
  140.             Object subV = getValue("sub", informazioniTokens);
  141.             if(subV instanceof String) {
  142.                 this.sub = (String) subV;
  143.             }
  144.            
  145.             Object usernameV = getValue("username", informazioniTokens);
  146.             if(usernameV instanceof String) {
  147.                 this.username = (String) usernameV;
  148.             }
  149.            
  150.             Object audV = getValue("aud", informazioniTokens);
  151.             if(audV instanceof List) {
  152.                 List<String> l = (List<String>) audV;
  153.                 if(!l.isEmpty()) {
  154.                     if(this.aud == null) {
  155.                         this.aud = new ArrayList<>();
  156.                     }
  157.                     this.aud.addAll(l);
  158.                 }
  159.             }
  160.            
  161.             Object expV = getValue("exp", informazioniTokens);
  162.             if(expV instanceof Date) {
  163.                 this.exp = (Date) expV;
  164.             }
  165.            
  166.             Object iatV = getValue("iat", informazioniTokens);
  167.             if(iatV instanceof Date) {
  168.                 this.iat = (Date) iatV;
  169.             }
  170.            
  171.             Object nbfV = getValue("nbf", informazioniTokens);
  172.             if(nbfV instanceof Date) {
  173.                 this.nbf = (Date) nbfV;
  174.             }
  175.            
  176.             Object clientIdV = getValue("clientId", informazioniTokens);
  177.             if(clientIdV instanceof String) {
  178.                 this.clientId = (String) clientIdV;
  179.             }
  180.            
  181.             Object jtiV = getValue("jti", informazioniTokens);
  182.             if(jtiV instanceof String) {
  183.                 this.jti = (String) jtiV;
  184.             }
  185.            
  186.             Object rolesV = getValue("roles", informazioniTokens);
  187.             if(rolesV instanceof List) {
  188.                 List<String> l = (List<String>) rolesV;
  189.                 if(!l.isEmpty()) {
  190.                     if(this.roles == null) {
  191.                         this.roles = new ArrayList<>();
  192.                     }
  193.                     this.roles.addAll(l);
  194.                 }
  195.             }
  196.            
  197.             Object scopesV = getValue("scopes", informazioniTokens);
  198.             if(scopesV instanceof List) {
  199.                 List<String> l = (List<String>) scopesV;
  200.                 if(!l.isEmpty()) {
  201.                     if(this.scopes == null) {
  202.                         this.scopes = new ArrayList<>();
  203.                     }
  204.                     this.scopes.addAll(l);
  205.                 }
  206.             }
  207.            
  208.             List<InformazioniTokenUserInfo> listUserInfo = new ArrayList<>();
  209.             for (int i = 0; i < informazioniTokens.length; i++) {
  210.                 InformazioniTokenUserInfo userInfoV = informazioniTokens[i].getUserInfo();
  211.                 if(userInfoV!=null) {
  212.                     listUserInfo.add(userInfoV);
  213.                 }
  214.             }
  215.             if(listUserInfo.size()==1) {
  216.                 this.userInfo = listUserInfo.get(0);
  217.             }
  218.             else {
  219.                 this.userInfo = new InformazioniTokenUserInfo(listUserInfo.toArray(new InformazioniTokenUserInfo[1]));
  220.             }
  221.         }
  222.     }
  223.     private static Object getValue(String field, InformazioniToken ... informazioniTokens) throws UtilsException {
  224.         Object tmp = null;
  225.         List<Object> listTmp = new ArrayList<>();
  226.         String getMethodName = "get"+((field.charAt(0)+"").toUpperCase())+field.substring(1);
  227.         Method getMethod = null;
  228.         try {
  229.             getMethod = InformazioniToken.class.getMethod(getMethodName);
  230.         }catch(Exception e) {
  231.             throw new UtilsException(e.getMessage(),e);
  232.         }
  233.        
  234.         // La fusione avviene per precedenza dall'ultimo fino al primo (a meno che non sia una lista)
  235.         tmp = getValue(getMethod, listTmp, informazioniTokens);
  236.         if(!listTmp.isEmpty()) {
  237.             return listTmp;
  238.         }
  239.         else {
  240.             return tmp;
  241.         }
  242.     }
  243.     private static Object getValue(Method getMethod, List<Object> listTmp, InformazioniToken ... informazioniTokens) throws UtilsException {
  244.         Object tmp = null;
  245.         for (int i = 0; i < informazioniTokens.length; i++) {
  246.             InformazioniToken infoToken = informazioniTokens[i];
  247.             Object o = null;
  248.             try {
  249.                 o = getMethod.invoke(infoToken);
  250.             }catch(Exception e) {
  251.                 throw new UtilsException(e.getMessage(),e);
  252.             }
  253.             if(o!=null) {
  254.                 tmp = getValue(o, listTmp);
  255.             }
  256.         }
  257.         return tmp;
  258.     }
  259.     private static Object getValue(Object o, List<Object> listTmp) {
  260.         Object tmp = null;
  261.         if(o instanceof List<?>) {
  262.             List<?> list = (List<?>) o;
  263.             if(!list.isEmpty()) {
  264.                 for (Object object : list) {
  265.                     if(!listTmp.contains(object)) {
  266.                         listTmp.add(object);
  267.                     }
  268.                 }
  269.             }
  270.         }
  271.         else {
  272.             tmp = o;
  273.         }
  274.         return tmp;
  275.     }
  276.        
  277.     // NOTA: l'ordine stabilisce come viene serializzato nell'oggetto json
  278.    
  279.     private TipoInformazioni type = TipoInformazioni.validated_token;
  280.    
  281.     // Indicazione se il token e' valido
  282.     private boolean valid;
  283.    
  284.     // String representing the issuer of this token, as defined in JWT [RFC7519].
  285.     private String iss;
  286.    
  287.     // Subject of the token, as defined in JWT [RFC7519].
  288.     // Usually a machine-readable identifier of the resource owner who authorized this token.
  289.     private String sub;
  290.    
  291.     // Human-readable identifier for the resource owner who authorized this token.
  292.     private String username;
  293.    
  294.     // Service-specific string identifier or list of string identifiers representing the intended audience for this token,
  295.     // as defined in JWT [RFC7519].
  296.     private List<String> aud;
  297.    
  298.     // Integer timestamp, measured in the number of seconds since January 1 1970 UTC,
  299.     // indicating when this token will expire, as defined in JWT [RFC7519].
  300.     private Date exp;
  301.    
  302.     // Integer timestamp, measured in the number of seconds since January 1 1970 UTC,
  303.     // indicating when this token was originally issued, as defined in JWT [RFC7519].
  304.     private Date iat;
  305.    
  306.     // Integer timestamp, measured in the number of seconds since January 1 1970 UTC,
  307.     // indicating when this token is not to be used before, as defined in JWT [RFC7519].
  308.     private Date nbf;
  309.    
  310.     // Client identifier for the OAuth 2.0 client that requested this token.
  311.     // Per un ID_TOKEN, If present, it MUST contain the OAuth 2.0 Client ID of this party.
  312.     private String clientId; // in oidc e' azp
  313.    
  314.     //  The "jti" (JWT ID) claim provides a unique identifier for the JWT.
  315.     private String jti;
  316.    
  317.     // Ruoli di un utente
  318.     private List<String> roles;
  319.    
  320.     // Scopes
  321.     private List<String> scopes;

  322.     // UserInfo
  323.     private InformazioniTokenUserInfo userInfo;
  324.            
  325.     // Claims
  326.     private Map<String,Serializable> claims = new HashMap<>();
  327.        
  328.     // NOTA: l'ordine stabilisce come viene serializzato nell'oggetto json

  329.     // RawResponse
  330.     private String rawResponse;
  331.    
  332.     // Token (nel caso di errori)
  333.     private String token;
  334.    
  335.     // HttpCode (nel caso di errori)
  336.     private String httpResponseCode;
  337.    
  338.     // Failed
  339.     private String errorDetails;
  340.    
  341.     // SorgenteInformazioniToken
  342.     private SorgenteInformazioniToken sourceType;
  343.    
  344.     // Multiple Source
  345.     private List<SorgenteInformazioniToken> sourceTypes = null;
  346.     private Map<SorgenteInformazioniToken,String> sourcesTokenInfo = null;
  347.    
  348.     // DynamicDiscovery (ulteriore informazione valorizzata con la funzionalità specifica)
  349.     private DynamicDiscovery dynamicDiscovery;
  350.    
  351.     // Attributes Authority (ulteriore informazione valorizzata con la funzionalità specifica)
  352.     private InformazioniAttributi aa;
  353.    
  354.     // InformazioniNegoziazioneToken (ulteriore informazione valorizzata con la funzionalità specifica)
  355.     private InformazioniNegoziazioneToken retrievedToken;
  356.        
  357.     // InformazioniPDND (ulteriore informazione valorizzata con la funzionalità specifica)
  358.     private Map<String,Serializable> pdnd = null;
  359.    
  360.     public TipoInformazioni getType() {
  361.         return this.type;
  362.     }
  363.     public void setType(TipoInformazioni type) {
  364.         this.type = type;
  365.     }
  366.    
  367.     public boolean isValid() {
  368.         return this.valid;
  369.     }
  370.     public boolean getValid() { // clone
  371.         return this.valid;
  372.     }
  373.     public void setValid(boolean valid) {
  374.         this.valid = valid;
  375.     }
  376.    
  377.     public String getIss() {
  378.         return this.iss;
  379.     }
  380.     public void setIss(String iss) {
  381.         this.iss = iss;
  382.     }

  383.     public String getSub() {
  384.         return this.sub;
  385.     }
  386.     public void setSub(String sub) {
  387.         this.sub = sub;
  388.     }

  389.     public String getUsername() {
  390.         return this.username;
  391.     }
  392.     public void setUsername(String username) {
  393.         this.username = username;
  394.     }

  395.     public List<String> getAud() {
  396.         return this.aud;
  397.     }
  398.     public void setAud(List<String> aud) {
  399.         this.aud = aud;
  400.     }

  401.     public Date getExp() {
  402.         return this.exp;
  403.     }
  404.     public void setExp(Date exp) {
  405.         this.exp = exp;
  406.     }

  407.     public Date getIat() {
  408.         return this.iat;
  409.     }
  410.     public void setIat(Date iat) {
  411.         this.iat = iat;
  412.     }

  413.     public Date getNbf() {
  414.         return this.nbf;
  415.     }
  416.     public void setNbf(Date nbf) {
  417.         this.nbf = nbf;
  418.     }

  419.     public String getClientId() {
  420.         return this.clientId;
  421.     }
  422.     public void setClientId(String clientId) {
  423.         this.clientId = clientId;
  424.     }

  425.     public String getJti() {
  426.         return this.jti;
  427.     }
  428.     public void setJti(String jti) {
  429.         this.jti = jti;
  430.     }
  431.    
  432.     public List<String> getRoles() {
  433.         return this.roles;
  434.     }
  435.     public void setRoles(List<String> roles) {
  436.         this.roles = roles;
  437.     }

  438.     public List<String> getScopes() {
  439.         return this.scopes;
  440.     }
  441.     public void setScopes(List<String> scopes) {
  442.         this.scopes = scopes;
  443.     }

  444.     public InformazioniTokenUserInfo getUserInfo() {
  445.         return this.userInfo;
  446.     }
  447.     public void setUserInfo(InformazioniTokenUserInfo userInfo) {
  448.         this.userInfo = userInfo;
  449.     }
  450.    
  451.     public Map<String, Serializable> getClaims() {
  452.         return this.claims;
  453.     }
  454.     public void setClaims(Map<String, Serializable> claims) {
  455.         this.claims = claims;
  456.     }
  457.            
  458.     public String getRawResponse() {
  459.         return this.rawResponse;
  460.     }
  461.     public void setRawResponse(String rawResponse) {
  462.         this.rawResponse = rawResponse;
  463.     }
  464.    
  465.     public String getToken() {
  466.         return this.token;
  467.     }
  468.     public void setToken(String token) {
  469.         this.token = token;
  470.     }
  471.    
  472.     public String getHttpResponseCode() {
  473.         return this.httpResponseCode;
  474.     }
  475.     public void setHttpResponseCode(String httpResponseCode) {
  476.         this.httpResponseCode = httpResponseCode;
  477.     }
  478.    
  479.     public String getErrorDetails() {
  480.         return this.errorDetails;
  481.     }
  482.     public void setErrorDetails(String errorDetails) {
  483.         this.errorDetails = errorDetails;
  484.     }
  485.    
  486.     public SorgenteInformazioniToken getSourceType() {
  487.         return this.sourceType;
  488.     }
  489.     public void setSourceType(SorgenteInformazioniToken sourceType) {
  490.         this.sourceType = sourceType;
  491.     }
  492.    
  493.     public Map<SorgenteInformazioniToken, String> getSourcesTokenInfo() {
  494.         return this.sourcesTokenInfo;
  495.     }
  496.     public void setSourcesTokenInfo(Map<SorgenteInformazioniToken, String> sourcesTokenInfo) {
  497.         this.sourcesTokenInfo = sourcesTokenInfo;
  498.     }

  499.     public List<SorgenteInformazioniToken> getSourceTypes() {
  500.         return this.sourceTypes;
  501.     }
  502.     public void setSourceTypes(List<SorgenteInformazioniToken> sourceTypes) {
  503.         this.sourceTypes = sourceTypes;
  504.     }
  505.    
  506.     public DynamicDiscovery getDynamicDiscovery() {
  507.         return this.dynamicDiscovery;
  508.     }
  509.     public void setDynamicDiscovery(DynamicDiscovery dynamicDiscovery) {
  510.         this.dynamicDiscovery = dynamicDiscovery;
  511.     }
  512.    
  513.     public InformazioniAttributi getAa() {
  514.         return this.aa;
  515.     }
  516.     public void setAa(InformazioniAttributi aa) {
  517.         this.aa = aa;
  518.     }
  519.    
  520.     public InformazioniNegoziazioneToken getRetrievedToken() {
  521.         return this.retrievedToken;
  522.     }
  523.     public void setRetrievedToken(InformazioniNegoziazioneToken retrievedToken) {
  524.         this.retrievedToken = retrievedToken;
  525.     }
  526.    
  527.     public Map<String, Serializable> getPdnd() {
  528.         return this.pdnd;
  529.     }
  530.     public void setPdnd(Map<String, Serializable> pdnd) {
  531.         this.pdnd = pdnd;
  532.     }
  533. }