Credential.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.utils.transport;

  21. import java.io.Serializable;
  22. import java.security.Principal;

  23. import org.apache.commons.lang.NotImplementedException;
  24. import org.openspcoop2.utils.LoggerWrapperFactory;
  25. import org.openspcoop2.utils.certificate.Certificate;

  26. /**
  27.  * Credentials
  28.  *
  29.  * @author Poli Andrea (apoli@link.it)
  30.  * @author $Author$
  31.  * @version $Rev$, $Date$
  32.  */
  33. public class Credential implements Serializable {

  34.     /**
  35.      *
  36.      */
  37.     private static final long serialVersionUID = 1L;
  38.    
  39.     // getUserPrincipal (SERVLET API)
  40.     protected String principalName;
  41.     protected Principal principal;
  42.    
  43.     // SSL (HTTPS)
  44.     protected String subject;
  45.     protected String issuer;
  46.     protected Certificate certificate;
  47.    
  48.     // Basic (HTTP-Based)
  49.     protected String username;
  50.     protected String password;
  51.    
  52.     // Bearer (token)
  53.     protected String bearerToken;
  54.        
  55.    
  56.     public Credential(){
  57.        
  58.     }
  59.    
  60.     public String getIdentity() {
  61.         if(this.principalName!=null) {
  62.             return this.principalName;
  63.         }
  64.         else if(this.subject!=null) {
  65.             return this.subject;
  66.         }
  67.         else if(this.username!=null) {
  68.             return this.username;
  69.         }
  70.         return null;
  71.     }
  72.    
  73.     public Principal getPrincipalObject() {
  74.         return this.principal;
  75.     }
  76.     public void setPrincipalObject(Principal principalObject) {
  77.         this.principal = principalObject;
  78.     }
  79.     public String getPrincipal() {
  80.         return this.principalName;
  81.     }
  82.     public void setPrincipal(String principal) {
  83.         this.principalName = principal;
  84.     }
  85.     public String getSubject() {
  86.         return this.subject;
  87.     }
  88.     public void setSubject(String subject) {
  89.         this.subject = subject;
  90.     }
  91.     public String getUsername() {
  92.         return this.username;
  93.     }
  94.     public void setUsername(String username) {
  95.         this.username = username;
  96.     }
  97.     public String getIssuer() {
  98.         return this.issuer;
  99.     }
  100.     public void setIssuer(String issuer) {
  101.         this.issuer = issuer;
  102.     }
  103.     public String getPassword() {
  104.         return this.password;
  105.     }
  106.     public void setPassword(String password) {
  107.         this.password = password;
  108.     }
  109.     public String getBearerToken() {
  110.         return this.bearerToken;
  111.     }
  112.     public void setBearerToken(String bearerToken) {
  113.         this.bearerToken = bearerToken;
  114.     }
  115.     public Certificate getCertificateChain() {
  116.         return this.certificate;
  117.     }
  118.     public Certificate getCertificate() {
  119.         return this.certificate;
  120.     }
  121.     public void setCertificate(Certificate certificate) {
  122.         this.certificate = certificate;
  123.     }
  124.    
  125.     public boolean isUserInRole(String role){
  126.         throw new NotImplementedException();
  127.     }
  128.    
  129.     public Object getAttribute(String attributeName){
  130.         throw new NotImplementedException();
  131.     }
  132.    
  133.     public Object getAttribute(String role, String attributeName){
  134.         throw new NotImplementedException();
  135.     }

  136.    
  137.    
  138.     @Override
  139.     public String toString(){
  140.         StringBuilder bf = new StringBuilder();

  141.         if(this.principal!=null){
  142.            
  143.             if(bf.length()>0){
  144.                 bf.append(" ");
  145.             }
  146.            
  147.             bf.append("principal(");
  148.             bf.append(this.principal);
  149.             bf.append(")");
  150.         }
  151.                
  152.         if(this.subject!=null){
  153.            
  154.             if(bf.length()>0){
  155.                 bf.append(" ");
  156.             }
  157.            
  158.             bf.append("subject(");
  159.             bf.append(this.subject);
  160.             bf.append(")");
  161.         }
  162.        
  163.         if(this.issuer!=null){
  164.            
  165.             if(bf.length()>0){
  166.                 bf.append(" ");
  167.             }
  168.            
  169.             bf.append("issuer(");
  170.             bf.append(this.subject);
  171.             bf.append(")");
  172.         }
  173.         if(this.certificate!=null) {
  174.             if(this.certificate.getCertificate()!=null) {
  175.                 if(bf.length()>0){
  176.                     bf.append(" ");
  177.                 }
  178.                
  179.                 bf.append("certificate(");
  180.                 try {
  181.                     bf.append(this.certificate.getCertificate().digestBase64Encoded());
  182.                 }catch(Exception e) {
  183.                     bf.append("Errore Digest Certificato");
  184.                     LoggerWrapperFactory.getLogger(Credential.class).error("Errore Digest Certificato: "+e.getMessage(),e);
  185.                 }
  186.                 bf.append(")");
  187.             }
  188.         }
  189.        
  190.         if(this.username!=null){
  191.            
  192.             if(bf.length()>0){
  193.                 bf.append(" ");
  194.             }
  195.            
  196.             bf.append("username(");
  197.             bf.append(this.username);
  198.             bf.append(")");
  199.         }
  200.        
  201.         if(this.password!=null){
  202.            
  203.             if(bf.length()>0){
  204.                 bf.append(" ");
  205.             }
  206.            
  207.             bf.append("password(");
  208.             bf.append(this.password);
  209.             bf.append(")");
  210.         }
  211.        
  212.         if(this.bearerToken!=null){
  213.            
  214.             if(bf.length()>0){
  215.                 bf.append(" ");
  216.             }
  217.            
  218.             bf.append("bearerToken(");
  219.             bf.append(this.bearerToken);
  220.             bf.append(")");
  221.         }
  222.        
  223.         return bf.toString();
  224.     }
  225. }