InformazioniNegoziazioneToken.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.util.ArrayList;
  23. import java.util.Date;
  24. import java.util.HashMap;
  25. import java.util.List;
  26. import java.util.Map;

  27. import org.openspcoop2.pdd.core.token.parser.INegoziazioneTokenParser;
  28. import org.openspcoop2.utils.UtilsException;
  29. import org.openspcoop2.utils.date.DateManager;
  30. import org.openspcoop2.utils.json.JSONUtils;

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

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

  40.     /**
  41.      *
  42.      */
  43.     private static final long serialVersionUID = 1L;
  44.    
  45.     public InformazioniNegoziazioneToken() {} // per serializzatore
  46.     public InformazioniNegoziazioneToken(InformazioniNegoziazioneToken_DatiRichiesta datiRichiesta,
  47.             String rawResponse, INegoziazioneTokenParser tokenParser) throws UtilsException {
  48.         this(datiRichiesta,null,rawResponse,tokenParser);
  49.     }
  50.     public InformazioniNegoziazioneToken(InformazioniNegoziazioneToken_DatiRichiesta datiRichiesta,
  51.             Integer httpResponseCode, String rawResponse, INegoziazioneTokenParser tokenParser) throws UtilsException {
  52.         this(datiRichiesta, httpResponseCode, rawResponse, tokenParser, null);
  53.     }
  54.     public InformazioniNegoziazioneToken(InformazioniNegoziazioneToken_DatiRichiesta datiRichiesta,
  55.             Integer httpResponseCode, String rawResponse, INegoziazioneTokenParser tokenParser, InformazioniNegoziazioneToken previousToken) throws UtilsException {
  56.         this.rawResponse = rawResponse;
  57.         JSONUtils jsonUtils = JSONUtils.getInstance();
  58.         if(jsonUtils.isJson(this.rawResponse)) {
  59.             JsonNode root = jsonUtils.getAsNode(this.rawResponse);
  60.             Map<String, Serializable> readClaims = jsonUtils.convertToSimpleMap(root);
  61.             if(readClaims!=null && readClaims.size()>0) {
  62.                 this.claims.putAll(readClaims);
  63.             }
  64.         }
  65.         tokenParser.init(this.rawResponse, this.claims);
  66.         if(httpResponseCode!=null) {
  67.             tokenParser.checkHttpTransaction(httpResponseCode);
  68.         }
  69.         this.request = datiRichiesta;
  70.         this.valid = tokenParser.isValid();
  71.         this.accessToken = tokenParser.getAccessToken();
  72.         this.refreshToken = tokenParser.getRefreshToken();
  73.         this.retrievedIn = DateManager.getDate();
  74.         this.expiresIn = tokenParser.getExpired();
  75.         this.retrievedRefreshTokenIn = this.retrievedIn;
  76.         this.refreshExpiresIn = tokenParser.getRefreshExpired();
  77.         this.tokenType = tokenParser.getTokenType();
  78.         List<String> s = tokenParser.getScopes();
  79.         if(s!=null && !s.isEmpty()) {
  80.             if(this.scopes == null) {
  81.                 this.scopes = new ArrayList<>();
  82.             }
  83.             this.scopes.addAll(s);
  84.         }
  85.        
  86.         if(this.refreshToken==null && previousToken!=null) {
  87.             // token aggiornato tramite refresh mode, e nella risposta non era presente nuovamente un nuovo refresh token
  88.             this.refreshToken = previousToken.getRefreshToken();
  89.             this.retrievedRefreshTokenIn = previousToken.getRetrievedRefreshTokenIn();
  90.             this.refreshExpiresIn = previousToken.getRefreshExpiresIn();
  91.         }
  92.     }
  93.    
  94.     public InformazioniNegoziazioneToken(InformazioniNegoziazioneToken_DatiRichiesta datiRichiesta,
  95.             String errorDetails, Integer httpResponseCode, byte[] rawResponse) {
  96.        
  97.         this.claims = null;
  98.        
  99.         this.errorDetails = errorDetails;
  100.         if(httpResponseCode!=null) {
  101.             this.httpResponseCode = httpResponseCode+"";
  102.         }
  103.         if(rawResponse!=null) {
  104.             this.rawResponse = new String(rawResponse);
  105.         }
  106.        
  107.         this.request = datiRichiesta;
  108.         this.valid = false;
  109.     }
  110.    
  111.        
  112.     // NOTA: l'ordine stabilisce come viene serializzato nell'oggetto json
  113.    
  114.     private TipoInformazioni type = TipoInformazioni.retrieved_token;
  115.    
  116.     private InformazioniNegoziazioneToken_DatiRichiesta request;
  117.    
  118.     // Indicazione se il token e' valido
  119.     private boolean valid;
  120.        
  121.     // String representing the access token issued by the authorization server [RFC6749].
  122.     private String accessToken;
  123.    
  124.     // String representing the refresh token, which can be used to obtain new access tokens using the same authorization grant
  125.     private String refreshToken;

  126.     // Data in cui il token è stato recuperato
  127.     private Date retrievedIn;
  128.    
  129.     // The lifetime in seconds of the access token.  For example, the value "3600" denotes that the access token will
  130.     // expire in one hour from the time the response was generated.
  131.     // If omitted, the authorization server SHOULD provide the expiration time via other means or document the default value.
  132.     private Date expiresIn;
  133.    
  134.     // Data in cui il refresh token è stato recuperato
  135.     private Date retrievedRefreshTokenIn;
  136.    
  137.     // The lifetime in seconds of the refresh token.  For example, the value "3600" denotes that the access token will
  138.     // expire in one hour from the time the response was generated.
  139.     // If omitted, the authorization server SHOULD provide the expiration time via other means or document the default value.
  140.     private Date refreshExpiresIn;
  141.    
  142.     // The type of the token issued
  143.     private String tokenType;
  144.    
  145.     // Scopes
  146.     private List<String> scopes;
  147.    
  148.     // Claims
  149.     private Map<String,Serializable> claims = new HashMap<>();
  150.        
  151.     // NOTA: l'ordine stabilisce come viene serializzato nell'oggetto json
  152.    
  153.     // RawResponse
  154.     private String rawResponse;
  155.    
  156.     // HttpCode (nel caso di errori)
  157.     private String httpResponseCode;
  158.    
  159.     // Failed
  160.     private String errorDetails;
  161.        
  162.    
  163.     public TipoInformazioni getType() {
  164.         return this.type;
  165.     }
  166.     public void setType(TipoInformazioni type) {
  167.         this.type = type;
  168.     }
  169.    
  170.     public InformazioniNegoziazioneToken_DatiRichiesta getRequest() {
  171.         return this.request;
  172.     }
  173.     public void setRequest(InformazioniNegoziazioneToken_DatiRichiesta request) {
  174.         this.request = request;
  175.     }
  176.    
  177.     public boolean isValid() {
  178.         return this.valid;
  179.     }
  180.     public boolean getValid() { // clone
  181.         return this.valid;
  182.     }
  183.     public void setValid(boolean valid) {
  184.         this.valid = valid;
  185.     }
  186.    
  187.     public String getAccessToken() {
  188.         return this.accessToken;
  189.     }
  190.     public void setAccessToken(String accessToken) {
  191.         this.accessToken = accessToken;
  192.     }
  193.    
  194.     public String getRefreshToken() {
  195.         return this.refreshToken;
  196.     }
  197.     public void setRefreshToken(String refreshToken) {
  198.         this.refreshToken = refreshToken;
  199.     }
  200.    
  201.     public Date getRetrievedIn() {
  202.         return this.retrievedIn;
  203.     }
  204.     public void setRetrievedIn(Date retrievedIn) {
  205.         this.retrievedIn = retrievedIn;
  206.     }
  207.    
  208.     public Date getExpiresIn() {
  209.         return this.expiresIn;
  210.     }
  211.     public void setExpiresIn(Date expiresIn) {
  212.         this.expiresIn = expiresIn;
  213.     }
  214.    
  215.     public Date getRetrievedRefreshTokenIn() {
  216.         return this.retrievedRefreshTokenIn;
  217.     }
  218.     public void setRetrievedRefreshTokenIn(Date retrievedRefreshTokenIn) {
  219.         this.retrievedRefreshTokenIn = retrievedRefreshTokenIn;
  220.     }
  221.    
  222.     public Date getRefreshExpiresIn() {
  223.         return this.refreshExpiresIn;
  224.     }
  225.     public void setRefreshExpiresIn(Date refreshExpiresIn) {
  226.         this.refreshExpiresIn = refreshExpiresIn;
  227.     }
  228.    
  229.     public String getTokenType() {
  230.         return this.tokenType;
  231.     }
  232.     public void setTokenType(String tokenType) {
  233.         this.tokenType = tokenType;
  234.     }
  235.        
  236.     public List<String> getScopes() {
  237.         return this.scopes;
  238.     }
  239.     public void setScopes(List<String> scopes) {
  240.         this.scopes = scopes;
  241.     }

  242.     public Map<String, Serializable> getClaims() {
  243.         return this.claims;
  244.     }
  245.     public void setClaims(Map<String, Serializable> claims) {
  246.         this.claims = claims;
  247.     }
  248.            
  249.     public String getRawResponse() {
  250.         return this.rawResponse;
  251.     }
  252.     public void setRawResponse(String rawResponse) {
  253.         this.rawResponse = rawResponse;
  254.     }
  255.     public void replaceInRawResponse(String original, String newS) {
  256.         if(this.rawResponse!=null) {
  257.             this.rawResponse = this.rawResponse.replace(original, newS);
  258.         }
  259.     }
  260.    
  261.     public String getHttpResponseCode() {
  262.         return this.httpResponseCode;
  263.     }
  264.     public void setHttpResponseCode(String httpResponseCode) {
  265.         this.httpResponseCode = httpResponseCode;
  266.     }
  267.    
  268.     public String getErrorDetails() {
  269.         return this.errorDetails;
  270.     }
  271.     public void setErrorDetails(String errorDetails) {
  272.         this.errorDetails = errorDetails;
  273.     }
  274. }