SecurityTokenUtilities.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.protocol.engine;

  21. import org.openspcoop2.protocol.sdk.ChannelSecurityToken;
  22. import org.openspcoop2.protocol.sdk.Context;
  23. import org.openspcoop2.protocol.sdk.SecurityToken;
  24. import org.openspcoop2.protocol.sdk.state.RequestInfo;

  25. /**    
  26.  * SecurityTokenUtilities
  27.  *
  28.  * @author Poli Andrea (poli@link.it)
  29.  * @author $Author$
  30.  * @version $Rev$, $Date$
  31.  */
  32. public class SecurityTokenUtilities {
  33.    
  34.     private SecurityTokenUtilities() {}

  35.     public static SecurityToken readSecurityToken(Context context) {
  36.         SecurityToken securityTokenForContext = null;
  37.         if(context.containsKey(org.openspcoop2.core.constants.Costanti.SECURITY_TOKEN)) {
  38.             securityTokenForContext = (SecurityToken) context.getObject(org.openspcoop2.core.constants.Costanti.SECURITY_TOKEN);
  39.         }
  40.         return securityTokenForContext;
  41.     }
  42.     public static SecurityToken newSecurityToken(Context context) {
  43.         SecurityToken securityTokenForContext = null;
  44.         if(context.containsKey(org.openspcoop2.core.constants.Costanti.SECURITY_TOKEN)) {
  45.             securityTokenForContext = (SecurityToken) context.getObject(org.openspcoop2.core.constants.Costanti.SECURITY_TOKEN);
  46.         }
  47.         else {
  48.             securityTokenForContext = new SecurityToken();
  49.             context.addObject(org.openspcoop2.core.constants.Costanti.SECURITY_TOKEN, securityTokenForContext);
  50.            
  51.             if(context.containsKey(org.openspcoop2.core.constants.Costanti.REQUEST_INFO)) {
  52.                 RequestInfo requestInfo = (RequestInfo) context.getObject(org.openspcoop2.core.constants.Costanti.REQUEST_INFO);
  53.                 if(requestInfo!=null && requestInfo.getProtocolContext()!=null && requestInfo.getProtocolContext().getCredential()!=null &&
  54.                         requestInfo.getProtocolContext().getCredential().getCertificate()!=null &&
  55.                         requestInfo.getProtocolContext().getCredential().getCertificate().getCertificate()!=null) {
  56.                     ChannelSecurityToken channelSecurityToken = new ChannelSecurityToken();
  57.                     channelSecurityToken.setCertificate(requestInfo.getProtocolContext().getCredential().getCertificate().getCertificate());
  58.                     securityTokenForContext.setChannel(channelSecurityToken);
  59.                 }
  60.             }
  61.         }
  62.        
  63.         return securityTokenForContext;
  64.     }
  65.    
  66. }