Credenziali.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.credenziali;

  21. import javax.servlet.http.HttpServletRequest;

  22. import org.openspcoop2.utils.LoggerWrapperFactory;
  23. import org.openspcoop2.utils.transport.Credential;
  24. import org.openspcoop2.utils.transport.http.HttpServletCredential;
  25. import org.slf4j.Logger;

  26. /**
  27.  * Classe utilizzata per rappresentare le informazioni utilizzata da un generico connettore.
  28.  *
  29.  * @author Poli Andrea (apoli@link.it)
  30.  * @author $Author$
  31.  * @version $Rev$, $Date$
  32.  */

  33. public class Credenziali  extends HttpServletCredential implements java.io.Serializable  {

  34.     public static final boolean SHOW_BASIC_PASSWORD = true;
  35.     public static final boolean SHOW_ISSUER = true;
  36.     public static final boolean SHOW_DIGEST_CLIENT_CERT = true;
  37.     public static final boolean SHOW_SERIAL_NUMBER_CLIENT_CERT = true;
  38.    
  39.     /**
  40.      *
  41.      */
  42.     private static final long serialVersionUID = 1L;

  43.     public Credenziali(){
  44.         super();
  45.     }
  46.     public Credenziali(Credential credentials){
  47.         if(credentials!=null){

  48.             this.principal = credentials.getPrincipalObject();
  49.             this.principalName = credentials.getPrincipal();

  50.             this.subject = credentials.getSubject();
  51.             this.issuer = credentials.getIssuer();
  52.             this.certificate = credentials.getCertificate();

  53.             this.username = credentials.getUsername();
  54.             this.password = credentials.getPassword();

  55.             this.bearerToken = credentials.getBearerToken();

  56.         }
  57.     }
  58.     public Credenziali(HttpServletRequest req){
  59.         super(req, null);
  60.     }
  61.     public Credenziali(HttpServletRequest req,Logger log){
  62.         super(req, log);
  63.     }




  64.     @Override
  65.     public boolean equals(Object c){
  66.         if(c==null) {
  67.             return false;
  68.         }
  69.         if (this.getClass() != c.getClass()) {
  70.             return false;
  71.         }
  72.         return this.toString().equals(((Credenziali)c).toString());
  73.     }
  74.    
  75.     @Override
  76.     public int hashCode(){
  77.         String s = this.toString();
  78.         return s!=null ? s.hashCode() : 0;
  79.     }

  80.     @Override
  81.     public String toString(){
  82.         return this.toString(false, false, false, false);
  83.     }
  84.     public String toString(boolean showBasicPassword, boolean showIssuer, boolean showDigestClientCert, boolean showSerialNumberClientCert){
  85.         return this.toString(showBasicPassword, showIssuer, showDigestClientCert, showSerialNumberClientCert,
  86.                 "( ", " ) ", ", ");
  87.     }
  88.     public String toString(boolean showBasicPassword, boolean showIssuer, boolean showDigestClientCert, boolean showSerialNumberClientCert,
  89.             String start, String end, String separator){
  90.         String credenzialiFornite = "";
  91.         if (this.getUsername() != null || this.getSubject() != null || this.getPassword() != null || this.getPrincipal()!=null) {
  92.             credenzialiFornite = start;
  93.             boolean printPrincipal = true;
  94.             if (this.getUsername() != null){
  95.                 String label = "BasicUsername";
  96.                 if (this.getPrincipal() != null && this.getPrincipal().equals(this.getUsername())){
  97.                     label = label + "/Principal";
  98.                     printPrincipal = false;
  99.                 }
  100.                 if(this.getPassword()==null)
  101.                     credenzialiFornite = credenzialiFornite + label+" '"+ this.getUsername() + "'"+separator+"BasicPassword undefined";
  102.                 else if("".equals(this.getPassword()) )
  103.                     credenzialiFornite = credenzialiFornite + label+" '"+ this.getUsername() + "'"+separator+"BasicPassword empty";
  104.                 else{
  105.                     if(showBasicPassword){
  106.                         credenzialiFornite = credenzialiFornite + label+" '"+ this.getUsername() + "'"+separator+"BasicPassword '"+this.getPassword()+"'";
  107.                     }
  108.                     else{
  109.                         credenzialiFornite = credenzialiFornite + label+" '"+ this.getUsername() + "'";
  110.                     }
  111.                 }
  112.             }
  113.             if (this.getSubject() != null){
  114.                 String label = "SSL-Subject";
  115.                 if (this.getPrincipal() != null && this.getPrincipal().equals(this.getSubject())){
  116.                     label = label + "/Principal";
  117.                     printPrincipal = false;
  118.                 }
  119.                 if (this.getUsername() != null){
  120.                     label = separator + label;
  121.                 }
  122.                 credenzialiFornite = credenzialiFornite + label+" '"+ this.getSubject() + "'";

  123.                 if(showIssuer) {
  124.                     if(this.getIssuer()!=null){
  125.                         label = separator+"SSL-Issuer";
  126.                         credenzialiFornite = credenzialiFornite + label+" '"+ this.getIssuer() + "'";
  127.                     }
  128.                 }
  129.                 if(showDigestClientCert) {
  130.                     if(this.getCertificate()!=null && this.getCertificate().getCertificate()!=null) {
  131.                         String digest = null;
  132.                         try {
  133.                             digest = this.getCertificate().getCertificate().digestBase64Encoded();
  134.                             label = separator+"SSL-ClientCert-Digest";
  135.                             credenzialiFornite = credenzialiFornite + label+" '"+ digest + "'";
  136.                         }catch(Exception e) {
  137.                             LoggerWrapperFactory.getLogger(Credenziali.class).error("Errore Digest Certificato: "+e.getMessage(),e);
  138.                         }      
  139.                     }
  140.                 }
  141.                 if(showSerialNumberClientCert) {
  142.                     if(this.getCertificate()!=null && this.getCertificate().getCertificate()!=null) {              
  143.                         label = separator+"SSL-ClientCert-SerialNumber";
  144.                         credenzialiFornite = credenzialiFornite + label+" '"+ this.getCertificate().getCertificate().getSerialNumber() + "'";
  145.                     }
  146.                 }
  147.             }
  148.             if (this.getPrincipal() != null && printPrincipal){
  149.                 String label = "Principal";
  150.                 if ( (this.getUsername() != null) || (this.getSubject() != null) ){
  151.                     label = separator + label;
  152.                 }
  153.                 credenzialiFornite = credenzialiFornite + label+ " '"+ this.getPrincipal() + "'";
  154.             }
  155.             credenzialiFornite = credenzialiFornite + end;
  156.         }
  157.         return credenzialiFornite;
  158.     }
  159. }