AttributeAuthorityDynamicParameters.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.util.Map;

  22. import org.apache.commons.lang.StringUtils;
  23. import org.openspcoop2.pdd.core.PdDContext;
  24. import org.openspcoop2.pdd.core.dynamic.DynamicUtils;
  25. import org.openspcoop2.pdd.core.token.AbstractDynamicParameters;
  26. import org.openspcoop2.protocol.sdk.state.RequestInfo;

  27. /**
  28.  * AttributeAuthorityDynamicParameter
  29.  *
  30.  * @author Andrea Poli (apoli@link.it)
  31.  * @author $Author$
  32.  * @version $Rev$, $Date$
  33.  */
  34. public class AttributeAuthorityDynamicParameters extends AbstractDynamicParameters {

  35.     @SuppressWarnings("unused")
  36.     private PolicyAttributeAuthority policyAttributeAuthority;
  37.    
  38.     private String endpoint;
  39.     private String basicUsername;
  40.     private String basicPassword;
  41.     private String bearerToken;
  42.    
  43.     private String requestDynamicPayloadTemplate;
  44.    
  45.     private String issuer;
  46.     private String subject;
  47.     private String audience;
  48.     private String claims;
  49.    
  50.     private String responseAudience;
  51.    
  52.     public AttributeAuthorityDynamicParameters(Map<String, Object> dynamicMap,
  53.             PdDContext pddContext, RequestInfo requestInfo,
  54.             PolicyAttributeAuthority policyAttributeAuthority) throws Exception {
  55.         super(dynamicMap, pddContext, requestInfo);
  56.        
  57.         this.policyAttributeAuthority = policyAttributeAuthority;
  58.        
  59.         this.endpoint = policyAttributeAuthority.getEndpoint();
  60.         if(this.endpoint!=null && !"".equals(this.endpoint)) {
  61.             this.endpoint = DynamicUtils.convertDynamicPropertyValue("endpoint.gwt", this.endpoint, dynamicMap, pddContext);    
  62.         }
  63.        
  64.         boolean basic = policyAttributeAuthority.isBasicAuthentication();
  65.         if(basic) {
  66.             this.basicUsername = policyAttributeAuthority.getBasicAuthenticationUsername();
  67.             if(this.basicUsername!=null && !"".equals(this.basicUsername)) {
  68.                 this.basicUsername = DynamicUtils.convertDynamicPropertyValue("username.gwt", this.basicUsername, dynamicMap, pddContext);  
  69.             }
  70.            
  71.             this.basicPassword = policyAttributeAuthority.getBasicAuthenticationPassword();
  72.             if(this.basicPassword!=null && !"".equals(this.basicPassword)) {
  73.                 this.basicPassword = DynamicUtils.convertDynamicPropertyValue("password.gwt", this.basicPassword, dynamicMap, pddContext);  
  74.             }
  75.         }
  76.        
  77.         boolean bearer = policyAttributeAuthority.isBearerAuthentication();
  78.         if(bearer) {
  79.             this.bearerToken = policyAttributeAuthority.getBeareAuthenticationToken();
  80.             if(this.bearerToken!=null && !"".equals(this.bearerToken)) {
  81.                 this.bearerToken = DynamicUtils.convertDynamicPropertyValue("bearerToken.gwt", this.bearerToken, dynamicMap, pddContext);  
  82.             }
  83.         }
  84.        
  85.         if(policyAttributeAuthority.isRequestDynamicPayloadTemplate() || policyAttributeAuthority.isRequestDynamicPayloadJwt()) {
  86.             if(policyAttributeAuthority.isRequestDynamicPayloadTemplate()) {
  87.                 this.requestDynamicPayloadTemplate = DynamicUtils.convertDynamicPropertyValue("AADynamicRequest.gwt", policyAttributeAuthority.getRequestDynamicPayload(), dynamicMap, pddContext);
  88.             }
  89.             else {
  90.                 this.issuer = policyAttributeAuthority.getRequestJwtIssuer();
  91.                 if(this.issuer!=null && !"".equals(this.issuer)) {
  92.                     this.issuer = DynamicUtils.convertDynamicPropertyValue("issuer.gwt", this.issuer, dynamicMap, pddContext);  
  93.                 }
  94.                
  95.                 this.subject = policyAttributeAuthority.getRequestJwtSubject();
  96.                 if(this.subject!=null && !"".equals(this.subject)) {
  97.                     this.subject = DynamicUtils.convertDynamicPropertyValue("subject.gwt", this.subject, dynamicMap, pddContext);  
  98.                 }
  99.                
  100.                 this.audience = policyAttributeAuthority.getRequestJwtAudience();
  101.                 if(this.audience!=null && !"".equals(this.subject)) {
  102.                     this.audience = DynamicUtils.convertDynamicPropertyValue("audience.gwt", this.audience, dynamicMap, pddContext);    
  103.                 }
  104.                
  105.                 this.claims = policyAttributeAuthority.getRequestJwtClaims();
  106.                 if(this.claims!=null && !"".equals(this.claims)) {
  107.                     this.claims = DynamicUtils.convertDynamicPropertyValue("claims.gwt", this.claims, dynamicMap, pddContext);  
  108.                 }
  109.             }
  110.         }
  111.        
  112.         this.responseAudience = policyAttributeAuthority.getResponseAudience();
  113.         if(this.responseAudience!=null && !"".equals(this.responseAudience)) {
  114.             this.responseAudience = DynamicUtils.convertDynamicPropertyValue("responseAudience.gwt", this.responseAudience, dynamicMap, pddContext);    
  115.         }
  116.     }
  117.    
  118.     @Override
  119.     protected String toStringRepresentation() {
  120.         return null; // viene ridefinito il metodo toString
  121.     }
  122.     @Override
  123.     public String toString() {
  124.         return toString("\n", false);
  125.     }
  126.     public String toString(String separator, boolean cacheKey) {
  127.         StringBuilder sb = new StringBuilder();
  128.         String superS = super.toString();
  129.         if(StringUtils.isNotEmpty(superS)) {
  130.             sb.append(superS);
  131.         }
  132.        
  133.         if(StringUtils.isNotEmpty(this.endpoint)) {
  134.             if(sb.length()>0) {
  135.                 sb.append(separator);
  136.             }
  137.             sb.append("endpoint:").append(this.endpoint);
  138.         }
  139.         if(StringUtils.isNotEmpty(this.basicUsername)) {
  140.             if(sb.length()>0) {
  141.                 sb.append(separator);
  142.             }
  143.             sb.append("user:").append(this.basicUsername);
  144.         }
  145.         if(StringUtils.isNotEmpty(this.basicPassword) && (!cacheKey)) {
  146.             if(sb.length()>0) {
  147.                 sb.append(separator);
  148.             }
  149.             sb.append("password:").append(this.basicPassword);
  150.         }
  151.         if(StringUtils.isNotEmpty(this.bearerToken)) {
  152.             if(sb.length()>0) {
  153.                 sb.append(separator);
  154.             }
  155.             sb.append("token:").append(this.bearerToken);
  156.         }
  157.        
  158.         if(!cacheKey) {
  159.             // Altrimenti questi parametri concorrono alla realizzazione della richiesta che viene poi aggiunta in cache.
  160.             if(StringUtils.isNotEmpty(this.requestDynamicPayloadTemplate)) {
  161.                 if(sb.length()>0) {
  162.                     sb.append(separator);
  163.                 }
  164.                 sb.append("requestDynamicPayloadTemplate:").append(this.requestDynamicPayloadTemplate);
  165.             }
  166.             if(StringUtils.isNotEmpty(this.issuer)) {
  167.                 if(sb.length()>0) {
  168.                     sb.append(separator);
  169.                 }
  170.                 sb.append("issuer:").append(this.issuer);
  171.             }
  172.             if(StringUtils.isNotEmpty(this.subject)) {
  173.                 if(sb.length()>0) {
  174.                     sb.append(separator);
  175.                 }
  176.                 sb.append("subject:").append(this.subject);
  177.             }
  178.             if(StringUtils.isNotEmpty(this.audience)) {
  179.                 if(sb.length()>0) {
  180.                     sb.append(separator);
  181.                 }
  182.                 sb.append("audience:").append(this.audience);
  183.             }
  184.             if(StringUtils.isNotEmpty(this.claims)) {
  185.                 if(sb.length()>0) {
  186.                     sb.append(separator);
  187.                 }
  188.                 sb.append("claims:").append(this.claims);
  189.             }
  190.         }
  191.        
  192.         if(StringUtils.isNotEmpty(this.responseAudience)) {
  193.             if(sb.length()>0) {
  194.                 sb.append(separator);
  195.             }
  196.             sb.append("responseAudience:").append(this.responseAudience);
  197.         }
  198.        
  199.         return sb.toString();
  200.     }
  201.    
  202.     public String getEndpoint() {
  203.         return this.endpoint;
  204.     }

  205.     public String getBasicUsername() {
  206.         return this.basicUsername;
  207.     }
  208.     public String getBasicPassword() {
  209.         return this.basicPassword;
  210.     }
  211.    
  212.     public String getBearerToken() {
  213.         return this.bearerToken;
  214.     }
  215.    
  216.     public String getRequestDynamicPayloadTemplate() {
  217.         return this.requestDynamicPayloadTemplate;
  218.     }
  219.    
  220.     public String getIssuer() {
  221.         return this.issuer;
  222.     }

  223.     public String getSubject() {
  224.         return this.subject;
  225.     }

  226.     public String getAudience() {
  227.         return this.audience;
  228.     }

  229.     public String getClaims() {
  230.         return this.claims;
  231.     }
  232.    
  233.     public String getResponseAudience() {
  234.         return this.responseAudience;
  235.     }
  236. }