HttpServletCredential.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.http;

  21. import java.io.Serializable;
  22. import java.security.cert.X509Certificate;
  23. import java.util.ArrayList;
  24. import java.util.List;

  25. import javax.servlet.http.HttpServletRequest;

  26. import org.apache.commons.lang.NotImplementedException;
  27. import org.openspcoop2.utils.certificate.Certificate;
  28. import org.openspcoop2.utils.certificate.CertificateUtils;
  29. import org.openspcoop2.utils.io.Base64Utilities;
  30. import org.openspcoop2.utils.transport.Credential;
  31. import org.slf4j.Logger;

  32. /**
  33.  * HttpServletCredentials
  34.  *
  35.  * @author Poli Andrea (apoli@link.it)
  36.  * @author $Author$
  37.  * @version $Rev$, $Date$
  38.  */
  39. public class HttpServletCredential extends Credential implements Serializable {

  40.     /**
  41.      *
  42.      */
  43.     private static final long serialVersionUID = 1L;

  44.     public static final String SERVLET_REQUEST_X509CERTIFICATE = "javax.servlet.request.X509Certificate";
  45.    
  46.     // Servlet Request
  47.     private transient HttpServletRequest httpServletRequest;
  48.    
  49.     public HttpServletCredential(){
  50.         super();
  51.     }
  52.     public HttpServletCredential(HttpServletRequest req,Logger log){
  53.         this(req, log, false);
  54.     }
  55.     public HttpServletCredential(HttpServletRequest req,Logger log,boolean debug){
  56.        
  57.         super();
  58.        
  59.         this.httpServletRequest = req;
  60.        
  61.         String auth = req.getHeader(HttpConstants.AUTHORIZATION);
  62.        
  63.         // Basic (HTTP-Based)
  64.         if(auth != null && auth.toLowerCase().startsWith(HttpConstants.AUTHORIZATION_PREFIX_BASIC.toLowerCase())){
  65.             // Sbustring(6): elimina la parte "Basic "
  66.             String cValue = auth.substring(HttpConstants.AUTHORIZATION_PREFIX_BASIC.length());
  67.             String decodeAuth = null;
  68.             try {
  69.                 decodeAuth = new String(Base64Utilities.decode(cValue));
  70.             }catch(Throwable e) {
  71.                 log.error("Password non estraibile dalla stringa ricevuta '"+cValue+"', decodifica base-64 non riuscita: "+e.getMessage(),e);
  72.             }
  73.             if(decodeAuth!=null) {
  74.                 String [] decodeAuthSplit = decodeAuth.split(":");
  75.                 if(decodeAuthSplit.length>1){
  76.                     this.username = decodeAuthSplit[0];
  77.                     try {
  78.                         this.password = decodeAuth.substring(decodeAuth.indexOf(":")+1, decodeAuth.length());
  79.                     }catch(Throwable e) {
  80.                         log.error("Password non estraibile dalla stringa ricevuta '"+decodeAuth+"'");
  81.                     }
  82.                 }
  83.                 if(debug && log!=null){
  84.                     log.info("BasicAuthentication presente nella richiesta, username ["+this.username+"] e password ["+this.password+"]");
  85.                 }
  86.             }
  87.         }
  88.        
  89.         // Bearer (Token Oauth)
  90.         if(auth != null && auth.toLowerCase().startsWith(HttpConstants.AUTHORIZATION_PREFIX_BEARER.toLowerCase())){
  91.             this.bearerToken = auth.substring(HttpConstants.AUTHORIZATION_PREFIX_BEARER.length());
  92.         }
  93.        
  94.         // SSL (HTTPS)
  95.         java.security.cert.X509Certificate[] certs =
  96.             (java.security.cert.X509Certificate[]) req.getAttribute(SERVLET_REQUEST_X509CERTIFICATE);
  97.        
  98.         if(certs!=null) {
  99.             if(debug && log!=null){
  100.                 try{
  101.                     StringBuilder bf = new StringBuilder();
  102.                     CertificateUtils.printCertificate(bf, certs);
  103.                     log.info(bf.toString());
  104.                 }catch(Throwable e){
  105.                     log.error("Print info certs error: "+e.getMessage(),e);
  106.                 }
  107.             }
  108.             if(certs.length > 0){
  109.                 //System.out.println("toString ["+certs[0].getSubjectX500Principal().toString()+"]"); // toString e' equivalente a RFC1779
  110.                 //System.out.println("getName ["+certs[0].getSubjectX500Principal().getName()+"]");
  111.                 //System.out.println("CANONICAL ["+certs[0].getSubjectX500Principal().getName(javax.security.auth.x500.X500Principal.CANONICAL)+"]");
  112.                 //System.out.println("RFC1779 ["+certs[0].getSubjectX500Principal().getName(javax.security.auth.x500.X500Principal.RFC1779)+"]");
  113.                 //System.out.println("RFC2253 ["+certs[0].getSubjectX500Principal().getName(javax.security.auth.x500.X500Principal.RFC2253)+"]");
  114.                 this.subject = certs[0].getSubjectX500Principal().toString();
  115.                 this.issuer = certs[0].getIssuerX500Principal().toString();
  116.                 List<X509Certificate> chains = new ArrayList<>();
  117.                 if(certs.length > 1){
  118.                     for (int i = 1; i < certs.length; i++) {
  119.                         chains.add(certs[i]);
  120.                     }
  121.                 }
  122.                 this.certificate = new Certificate("transport", certs[0], chains);
  123.             }
  124.         }else{
  125.             if(debug && log!=null){
  126.                 log.info("Certificati non presenti nella richiesta");
  127.             }
  128.         }
  129.        
  130.         // getUserPrincipal (SERVLET API)
  131.         if( req.getUserPrincipal()!=null ){
  132.             this.principal = req.getUserPrincipal();
  133.             this.principalName = this.principal.getName();
  134.         }
  135.     }
  136.    
  137.    
  138.     @Override
  139.     public boolean isUserInRole(String role){
  140.         if(this.httpServletRequest!=null){
  141.             return this.httpServletRequest.isUserInRole(role);
  142.         }
  143.         else{
  144.             throw new NotImplementedException();
  145.         }
  146.     }
  147.    
  148.     @Override
  149.     public Object getAttribute(String attributeName){
  150.         if(this.httpServletRequest!=null){
  151.             return this.httpServletRequest.getAttribute(attributeName);
  152.         }
  153.         else{
  154.             throw new NotImplementedException();
  155.         }
  156.     }

  157. }