PrincipalUtilities.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.autenticazione;

  21. import org.openspcoop2.core.config.constants.TipoAutenticazionePrincipal;
  22. import org.openspcoop2.core.constants.Costanti;
  23. import org.openspcoop2.core.transazioni.utils.TipoCredenzialeMittente;
  24. import org.openspcoop2.pdd.core.PdDContext;
  25. import org.openspcoop2.pdd.core.connettori.InfoConnettoreIngresso;
  26. import org.openspcoop2.pdd.core.token.InformazioniToken;
  27. import org.openspcoop2.utils.regexp.RegularExpressionEngine;

  28. /**
  29.  * PrincipalUtilities
  30.  *
  31.  * @author Andrea Poli (apoli@link.it)
  32.  * @author $Author$
  33.  * @version $Rev$, $Date$
  34.  */
  35. public class PrincipalUtilities {
  36.    
  37.     private PrincipalUtilities() {}

  38.     public static String getPrincipal(TipoAutenticazionePrincipal tipoAutenticazionePrincipal, String nome, String pattern, TipoCredenzialeMittente token,
  39.             InfoConnettoreIngresso infoConnettore, PdDContext pddContext, boolean throwException,
  40.             StringBuilder fullCredential) throws AutenticazioneException {
  41.        
  42.         String principal = null;
  43.         switch (tipoAutenticazionePrincipal) {
  44.         case CONTAINER:
  45.             if(infoConnettore!=null && infoConnettore.getCredenziali()!=null) {
  46.                 principal = infoConnettore.getCredenziali().getPrincipal();
  47.             }
  48.             if( (principal==null || "".equals(principal)) &&
  49.                 throwException) {
  50.                 throw new AutenticazioneException("["+tipoAutenticazionePrincipal+"] Principal non presente all'interno delle credenziali");
  51.             }
  52.             /**
  53.              * L'info 'container' viene gia' registrata in automatico tra le credenziali come principal.
  54.             if(fullCredential.length()>0) {
  55.                 fullCredential.append("\n");
  56.             }
  57.             fullCredential.append("Principal").append(" (container) '").append(principal).append("'");
  58.             */
  59.             return principal;
  60.         case HEADER:
  61.             if(nome==null && throwException) {
  62.                 throw new AutenticazioneException("["+tipoAutenticazionePrincipal+"] Nome dell'header, da cui estrarre il principal, non indicato");
  63.             }
  64.             if(nome!=null && infoConnettore!=null && infoConnettore.getUrlProtocolContext()!=null) {
  65.                 principal = infoConnettore.getUrlProtocolContext().getHeaderFirstValue(nome);
  66.             }
  67.             if( (principal==null || "".equals(principal)) &&
  68.                 throwException) {
  69.                 throw new AutenticazioneException("["+tipoAutenticazionePrincipal+"] Principal non presente nell'header http '"+nome+"'");
  70.             }
  71.             if(fullCredential.length()>0) {
  72.                 fullCredential.append("\n");
  73.             }
  74.             fullCredential.append("Principal").append(" (http) '").append(nome).append(": ").append(principal).append("'");
  75.             return principal;
  76.         case FORM:
  77.             if(nome==null && throwException) {
  78.                 throw new AutenticazioneException("["+tipoAutenticazionePrincipal+"] Nome del parametro della query, da cui estrarre il principal, non indicato");
  79.             }
  80.             if(nome!=null && infoConnettore!=null && infoConnettore.getUrlProtocolContext()!=null) {
  81.                 principal = infoConnettore.getUrlProtocolContext().getParameterFirstValue(nome);
  82.             }
  83.             if( (principal==null || "".equals(principal)) &&
  84.                 throwException) {
  85.                 throw new AutenticazioneException("["+tipoAutenticazionePrincipal+"] Principal non presente nel parametro della query '"+nome+"'");
  86.             }
  87.             if(fullCredential.length()>0) {
  88.                 fullCredential.append("\n");
  89.             }
  90.             fullCredential.append("Principal").append(" (query) '").append(nome).append(": ").append(principal).append("'");
  91.             return principal;
  92.         case URL:
  93.             if(pattern==null && throwException) {
  94.                 throw new AutenticazioneException("["+tipoAutenticazionePrincipal+"] Espressione Regolare, da utilizzare sulla url per estrarre il principal, non indicata");
  95.             }
  96.             String msgErrore = "["+tipoAutenticazionePrincipal+"] Principal non estraibile con l'espressione regolare '"+pattern+"', url non presente";
  97.             if(pattern!=null && infoConnettore!=null && infoConnettore.getUrlProtocolContext()!=null) {
  98.                 msgErrore = "["+tipoAutenticazionePrincipal+"] Principal non estraibile con l'espressione regolare '"+pattern+"' dalla url '"+infoConnettore.getUrlProtocolContext().getUrlInvocazione_formBased()+"'";
  99.                 try {
  100.                     principal = RegularExpressionEngine.getStringMatchPattern(infoConnettore.getUrlProtocolContext().getUrlInvocazione_formBased(), pattern);
  101.                 }catch(Exception t) {
  102.                     if(throwException) {
  103.                         throw new AutenticazioneException(msgErrore+": "+t.getMessage(), t);
  104.                     }
  105.                 }
  106.             }
  107.             if( (principal==null || "".equals(principal)) &&
  108.                 throwException) {
  109.                 throw new AutenticazioneException(msgErrore);
  110.             }
  111.             /**
  112.              * La url viene gia' registrata in automatico tra le info della transazione
  113.             if(fullCredential.length()>0) {
  114.                 fullCredential.append("\n");
  115.             }
  116.             fullCredential.append("Principal").append(" (url) '").append(principal).append("'");
  117.             */
  118.             return principal;
  119.         case INDIRIZZO_IP:
  120.             if(pddContext!=null && pddContext.containsKey(Costanti.CLIENT_IP_REMOTE_ADDRESS)) {
  121.                 principal = PdDContext.getValue(Costanti.CLIENT_IP_REMOTE_ADDRESS, pddContext);
  122.             }
  123.             if( (principal==null || "".equals(principal)) &&
  124.                 throwException) {
  125.                 throw new AutenticazioneException("["+tipoAutenticazionePrincipal+"] Indirizzo IP del Client non disponibile");
  126.             }
  127.             /**
  128.              * Il client ip viene gia' registrato in automatico tra le info della transazione
  129.             if(fullCredential.length()>0) {
  130.                 fullCredential.append("\n");
  131.             }
  132.             fullCredential.append("Principal").append(" (client-ip) '").append(principal).append("'");
  133.             */
  134.             return principal;
  135.         case INDIRIZZO_IP_X_FORWARDED_FOR:
  136.             if(pddContext!=null && pddContext.containsKey(Costanti.CLIENT_IP_TRANSPORT_ADDRESS)) {
  137.                 principal = PdDContext.getValue(Costanti.CLIENT_IP_TRANSPORT_ADDRESS, pddContext);
  138.             }
  139.             if( (principal==null || "".equals(principal)) &&
  140.                 throwException) {
  141.                 throw new AutenticazioneException("["+tipoAutenticazionePrincipal+"] Indirizzo IP del Client, tramite 'X-Forwarded-For', non disponibile");
  142.             }
  143.             /**
  144.              * L'indirizzo ip forwared viene gia' registrato in automatico tra le info della transazione
  145.             if(fullCredential.length()>0) {
  146.                 fullCredential.append("\n");
  147.             }
  148.             fullCredential.append("Principal").append(" (forwarded-ip) '").append(principal).append("'");
  149.             */
  150.             return principal;
  151.         // Ho levato il contenuto, poichè senno devo fare il digest per poterlo poi cachare
  152. //          case CONTENT:
  153.         case TOKEN:
  154.             InformazioniToken informazioniTokenNormalizzate = null;
  155.             if(pddContext!=null && pddContext.containsKey(org.openspcoop2.pdd.core.token.Costanti.PDD_CONTEXT_TOKEN_INFORMAZIONI_NORMALIZZATE)) {
  156.                 Object oInformazioniTokenNormalizzate = pddContext.getObject(org.openspcoop2.pdd.core.token.Costanti.PDD_CONTEXT_TOKEN_INFORMAZIONI_NORMALIZZATE);
  157.                 if(oInformazioniTokenNormalizzate!=null) {
  158.                     informazioniTokenNormalizzate = (InformazioniToken) oInformazioniTokenNormalizzate;
  159.                 }
  160.             }
  161.             String nomeClaim = null;
  162.             switch (token) {
  163.             case TOKEN_ISSUER:
  164.                 nomeClaim = "issuer";
  165.                 if(informazioniTokenNormalizzate!=null) {
  166.                     principal = informazioniTokenNormalizzate.getIss();
  167.                 }
  168.                 break;
  169.             case TOKEN_SUBJECT:
  170.                 nomeClaim = "subject";
  171.                 if(informazioniTokenNormalizzate!=null) {
  172.                     principal = informazioniTokenNormalizzate.getSub();
  173.                 }
  174.                 break;
  175.             case TOKEN_CLIENT_ID:
  176.                 nomeClaim = "clientId";
  177.                 if(informazioniTokenNormalizzate!=null) {
  178.                     principal = informazioniTokenNormalizzate.getClientId();
  179.                 }
  180.                 break;
  181.             case TOKEN_USERNAME:
  182.                 nomeClaim = "username";
  183.                 if(informazioniTokenNormalizzate!=null) {
  184.                     principal = informazioniTokenNormalizzate.getUsername();
  185.                 }
  186.                 break;
  187.             case TOKEN_EMAIL:
  188.                 nomeClaim = "eMail";
  189.                 if(informazioniTokenNormalizzate!=null && informazioniTokenNormalizzate.getUserInfo()!=null) {
  190.                     principal = informazioniTokenNormalizzate.getUserInfo().getEMail();
  191.                 }
  192.                 break;
  193.             case TRASPORTO:
  194.                 nomeClaim = nome;
  195.                 if(informazioniTokenNormalizzate!=null && informazioniTokenNormalizzate.getClaims()!=null) {
  196.                     Object oValueClaim = informazioniTokenNormalizzate.getClaims().get(nomeClaim);
  197.                     if(oValueClaim instanceof String) {
  198.                         principal = (String) oValueClaim;
  199.                     }
  200.                 }
  201.                 break;
  202.             case PDND_CLIENT_JSON:
  203.             case PDND_ORGANIZATION_JSON:
  204.             case PDND_ORGANIZATION_NAME:
  205.             case CLIENT_ADDRESS:
  206.             case GRUPPI:
  207.             case API:
  208.             case EVENTI:
  209.                 // non usati in questo contesto
  210.                 break;
  211.             }
  212.             if( (principal==null || "".equals(principal)) &&
  213.                 throwException) {
  214.                 throw new AutenticazioneException("["+tipoAutenticazionePrincipal+"] Token claim '"+nomeClaim+"' non disponibile");
  215.             }
  216.             /**
  217.              * Le info sul token vengono gia' registrate in automatico tra le info della transazione
  218.             if(fullCredential.length()>0) {
  219.                 fullCredential.append("\n");
  220.             }
  221.             fullCredential.append("Principal").append(" (token) '").append(nomeClaim).append(": ").append(principal).append("'");
  222.             */
  223.             return principal;
  224.         }
  225.         return null;
  226.     }
  227.    
  228. }