BasicRetrieveAttributeAuthorityResponseParser.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.Date;
  23. import java.util.List;
  24. import java.util.Map;

  25. import org.openspcoop2.pdd.core.token.TokenUtilities;
  26. import org.openspcoop2.pdd.core.token.parser.Claims;
  27. import org.openspcoop2.pdd.core.token.parser.TokenUtils;
  28. import org.openspcoop2.utils.UtilsException;
  29. import org.openspcoop2.utils.UtilsRuntimeException;
  30. import org.openspcoop2.utils.date.DateManager;
  31. import org.openspcoop2.utils.json.JSONUtils;
  32. import org.slf4j.Logger;

  33. /**    
  34.  * BasicRetrieveAttributeAuthorityResponseParser
  35.  *
  36.  * @author Poli Andrea (poli@link.it)
  37.  * @author $Author$
  38.  * @version $Rev$, $Date$
  39.  */
  40. public class BasicRetrieveAttributeAuthorityResponseParser implements IRetrieveAttributeAuthorityResponseParser {

  41.     protected Integer httpResponseCode;
  42.     protected String raw;
  43.     protected Map<String, Serializable> claims;
  44.     protected TipologiaResponseAttributeAuthority parser;
  45.     protected Date now;
  46.     protected List<String> attributesClaims;
  47.    
  48.     protected String attributeAuthority;
  49.     protected Logger log;
  50.    
  51.     public BasicRetrieveAttributeAuthorityResponseParser(String attributeAuthority, Logger log, TipologiaResponseAttributeAuthority parser, List<String> attributesClaims) {
  52.         this.attributeAuthority = attributeAuthority;
  53.         this.log = log;
  54.         this.parser = parser;
  55.         this.attributesClaims = attributesClaims;
  56.     }
  57.    
  58.     @Override
  59.     public void init(String raw, Map<String, Serializable> claims) {
  60.         this.raw = raw;
  61.         this.claims = claims;
  62.         this.now = DateManager.getDate();
  63.     }
  64.     @Override
  65.     public void init(byte[] content) {
  66.         throw new UtilsRuntimeException("unsupported");
  67.     }
  68.    
  69.     @Override
  70.     public String getContentAsString() {
  71.         throw new UtilsRuntimeException("unsupported");
  72.     }

  73.     @Override
  74.     public void checkHttpTransaction(Integer httpResponseCode) throws UtilsException{
  75.         this.httpResponseCode = httpResponseCode;
  76.         switch (this.parser) {
  77.         case jws:
  78.         case json:
  79.         case custom:
  80.             if(this.httpResponseCode!=null &&
  81.                 (this.httpResponseCode.intValue() < 200 || this.httpResponseCode.intValue()>299)) {
  82.                 String msgError = "Connessione terminata con errore (codice trasporto: "+this.httpResponseCode.intValue()+")";
  83.                 throw new UtilsException(msgError+": "+this.raw);
  84.             }
  85.             break;
  86.         }
  87.     }
  88.    
  89.     @Override
  90.     public boolean isValid() {
  91.        
  92.         if(this.claims==null || this.claims.size()<=0) {
  93.             return false;
  94.         }
  95.        
  96.         switch (this.parser) {
  97.         case custom:
  98.         case json:
  99.         case jws:
  100.             return true;
  101.         }
  102.        
  103.         return false;
  104.     }
  105.    
  106.     @Override
  107.     public Map<String, Serializable> getAttributes() {
  108.        
  109.         Map<String, Serializable> attributes = null;
  110.         if(TipologiaResponseAttributeAuthority.custom.equals(this.parser)) {
  111.             return attributes; // null voluto
  112.         }
  113.        
  114.         if(TipologiaResponseAttributeAuthority.json.equals(this.parser) &&
  115.                 (this.attributesClaims==null || this.attributesClaims.isEmpty())) {
  116.             return this.claims;
  117.         }
  118.        
  119.         if(TipologiaResponseAttributeAuthority.jws.equals(this.parser) &&
  120.             (this.attributesClaims==null || this.attributesClaims.isEmpty())
  121.             ){
  122.             return attributes; // null voluto
  123.         }
  124.        
  125.         JSONUtils jsonUtils = JSONUtils.getInstance();
  126.         attributes = jsonUtils.convertToMap(this.log, ("Attribute Authority: "+this.attributeAuthority), this.raw, this.attributesClaims);
  127.                
  128.         return attributes;
  129.     }
  130.    
  131.     // String representing the issuer for this attribute response
  132.     @Override
  133.     public String getIssuer() {
  134.         String tmp = null;
  135.         switch (this.parser) {
  136.         case jws:
  137.             tmp =  TokenUtilities.getClaimAsString(this.claims,Claims.JSON_WEB_TOKEN_RFC_7519_ISSUER);
  138.             break;
  139.         case custom:
  140.         case json:
  141.             return null;
  142.         }
  143.         return tmp;
  144.     }
  145.    
  146.     // String representing the Subject of this attribute response
  147.     @Override
  148.     public String getSubject() {
  149.         String tmp = null;
  150.         switch (this.parser) {
  151.         case jws:
  152.             tmp =  TokenUtilities.getClaimAsString(this.claims,Claims.JSON_WEB_TOKEN_RFC_7519_SUBJECT);
  153.             break;
  154.         case custom:
  155.         case json:
  156.             return null;
  157.         }
  158.         return tmp;
  159.     }
  160.    
  161.     // Service-specific string identifier or list of string identifiers representing the intended audience for this attribute response
  162.     @Override
  163.     public List<String> getAudience() {
  164.         List<String> lNull = null;
  165.         switch (this.parser) {
  166.         case jws:
  167.             return TokenUtilities.getClaimAsList(this.claims,Claims.JSON_WEB_TOKEN_RFC_7519_AUDIENCE);
  168.         case custom:
  169.         case json:
  170.         default:
  171.             return lNull;
  172.         }
  173.     }
  174.    
  175.     // Indicate when this attribute response will expire
  176.     @Override
  177.     public Date getExpired() {
  178.         String tmp = null;
  179.         switch (this.parser) {
  180.         case jws:
  181.             tmp =  TokenUtilities.getClaimAsString(this.claims,Claims.JSON_WEB_TOKEN_RFC_7519_EXPIRED);
  182.             break;
  183.         case custom:
  184.         case json:
  185.             return null;
  186.         }
  187.         if(tmp!=null) {
  188.             return TokenUtils.parseTimeInSecond(tmp);
  189.         }
  190.         return null;
  191.     }
  192.    
  193.     // Indicate when this attribute response was originally issued
  194.     @Override
  195.     public Date getIssuedAt() {
  196.         String tmp = null;
  197.         switch (this.parser) {
  198.         case jws:
  199.             tmp =  TokenUtilities.getClaimAsString(this.claims,Claims.JSON_WEB_TOKEN_RFC_7519_ISSUED_AT);
  200.             break;
  201.         case custom:
  202.         case json:
  203.             return null;
  204.         }
  205.         if(tmp!=null) {
  206.             return TokenUtils.parseTimeInSecond(tmp);
  207.         }
  208.         return null;
  209.     }
  210.    
  211.     // Indicate when this attribute response is not to be used before.
  212.     @Override
  213.     public Date getNotToBeUsedBefore() {
  214.         String tmp = null;
  215.         switch (this.parser) {
  216.         case jws:
  217.             tmp =  TokenUtilities.getClaimAsString(this.claims,Claims.JSON_WEB_TOKEN_RFC_7519_NOT_TO_BE_USED_BEFORE);
  218.             break;
  219.         case custom:
  220.         case json:
  221.             return null;
  222.         }
  223.         if(tmp!=null) {
  224.             return TokenUtils.parseTimeInSecond(tmp);
  225.         }
  226.         return null;
  227.     }
  228.    
  229.     // String representing the unique identifier for the attribute response
  230.     @Override
  231.     public String getIdentifier() {
  232.         String tmp = null;
  233.         switch (this.parser) {
  234.         case jws:
  235.             tmp =  TokenUtilities.getClaimAsString(this.claims,Claims.JSON_WEB_TOKEN_RFC_7519_JWT_ID);
  236.             break;
  237.         case custom:
  238.         case json:
  239.             return null;
  240.         }
  241.         return tmp;
  242.     }
  243.    
  244.    



  245.    
  246. }