Package org.jboss.identity.federation.bindings.jboss.wstrust

Source Code of org.jboss.identity.federation.bindings.jboss.wstrust.JBossSTSConfiguration

/*
* JBoss, Home of Professional Open Source.
* Copyright 2009, Red Hat Middleware LLC, and individual contributors
* as indicated by the @author tags. See the copyright.txt file in the
* distribution for a full listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.jboss.identity.federation.bindings.jboss.wstrust;

import java.security.KeyPair;
import java.security.PublicKey;
import java.util.HashMap;
import java.util.Map;

import org.jboss.identity.federation.api.wstrust.STSConfiguration;
import org.jboss.identity.federation.api.wstrust.SecurityTokenProvider;
import org.jboss.identity.federation.api.wstrust.WSTrustRequestHandler;
import org.jboss.identity.federation.api.wstrust.WSTrustServiceFactory;
import org.jboss.identity.federation.bindings.config.KeyProviderType;
import org.jboss.identity.federation.bindings.config.STSType;
import org.jboss.identity.federation.bindings.config.ServiceProviderType;
import org.jboss.identity.federation.bindings.config.ServiceProvidersType;
import org.jboss.identity.federation.bindings.config.TokenProviderType;
import org.jboss.identity.federation.bindings.config.TokenProvidersType;
import org.jboss.identity.federation.bindings.interfaces.TrustKeyManager;

/**
* <p>
* Standard JBoss STS configuration implementation.
* </p>
*
* @author <a href="mailto:sguilhen@redhat.com">Stefan Guilhen</a>
*/
public class JBossSTSConfiguration implements STSConfiguration
{

   // the delegate contains all the information extracted from the jboss-sts.xml configuration file.
   private final STSType delegate;

   private final Map<String, SecurityTokenProvider> tokenProviders = new HashMap<String, SecurityTokenProvider>();

   private final Map<String, ServiceProviderType> spMetadata = new HashMap<String, ServiceProviderType>();

   private final Map<String, Object> options = new HashMap<String, Object>();

   private TrustKeyManager trustManager;

   private WSTrustRequestHandler handler;
  
   /**
    * <p>
    * Creates an instance of {@code JBossSTSConfiguration} with default configuration values.
    * </p>
    */
   public JBossSTSConfiguration()
   {
      this.delegate = new STSType();
      // set the default values in the delegate.
      this.delegate.setSTSName("JBossSTS");
      this.delegate.setEncryptToken(false);
      this.delegate.setTokenTimeout(3600);
      this.delegate.setRequestHandler("org.jboss.identity.federation.api.wstrust.StandardRequestHandler");
      // TODO: add default token provider classes.
   }

   /**
    * <p>
    * Creates an instance of {@code JBossSTSConfiguration} with the specified configuration.
    * </p>
    *
    * @param config a reference to the object that holds the configuration of the STS.
    */
   public JBossSTSConfiguration(STSType config)
   {
      this.delegate = config;
      // set the default request handler if one hasn't been specified.
      if (this.delegate.getRequestHandler() == null)
         this.delegate.setRequestHandler("org.jboss.identity.federation.api.wstrust.StandardRequestHandler");

      // build the token-provider and service-metadata maps.
      TokenProvidersType providers = this.delegate.getTokenProviders();
      if (providers != null)
      {
         WSTrustServiceFactory serviceFactory = WSTrustServiceFactory.getInstance();
         for (TokenProviderType provider : providers.getTokenProvider())
         {
            this.tokenProviders.put(provider.getTokenType(), serviceFactory.createTokenProvider(provider
                  .getProviderClass()));
         }
      }
      ServiceProvidersType serviceProviders = this.delegate.getServiceProviders();
      if (serviceProviders != null)
      {
         for (ServiceProviderType provider : serviceProviders.getServiceProvider())
            this.spMetadata.put(provider.getEndpoint(), provider);
      }
      // setup the key store.
      KeyProviderType keyProviderType = config.getKeyProvider();
      if (keyProviderType != null)
      {
         String keyManagerClassName = keyProviderType.getClassName();
         try
         {
            this.trustManager = (TrustKeyManager) SecurityActions.instantiateClass(keyManagerClassName);
            this.trustManager.setAuthProperties(keyProviderType.getAuth());
            this.trustManager.setValidatingAlias(keyProviderType.getValidatingAlias());
         }
         catch (Exception e)
         {
            throw new RuntimeException("Unable to construct the key manager:", e);
         }
      }
   }

   /*
    * (non-Javadoc)
    *
    * @see org.jboss.identity.federation.api.wstrust.STSConfiguration#getSTSName()
    */
   public String getSTSName()
   {
      return this.delegate.getSTSName();
   }

   /*
    * (non-Javadoc)
    *
    * @see org.jboss.identity.federation.api.wstrust.STSConfiguration#getEncryptIssuedToken()
    */
   public boolean encryptIssuedToken()
   {
      return this.delegate.isEncryptToken();
   }

   /*
    * (non-Javadoc)
    *
    * @see org.jboss.identity.federation.api.wstrust.STSConfiguration#signIssuedToken()
    */
   public boolean signIssuedToken()
   {
     //TODO: add the sign-by-default property to the configuration schema.
     return true;
   }
  
   /*
    * (non-Javadoc)
    *
    * @see org.jboss.identity.federation.api.wstrust.STSConfiguration#getIssuedTokenTimeout()
    */
   public long getIssuedTokenTimeout()
   {
      // return the timeout value in milliseconds.
      return this.delegate.getTokenTimeout() * 1000;
   }

   /*
    * (non-Javadoc)
    *
    * @see org.jboss.identity.federation.api.wstrust.STSConfiguration#getRequestHandlerClass()
    */
   public WSTrustRequestHandler getRequestHandler()
   {
      if (this.handler == null)
         this.handler = WSTrustServiceFactory.getInstance().createRequestHandler(
               this.delegate.getRequestHandler(), this);
      return this.handler;
   }

   /*
    * (non-Javadoc)
    *
    * @see org.jboss.identity.federation.api.wstrust.STSConfiguration#getOptions()
    */
   public Map<String, Object> getOptions()
   {
      return this.options;
   }

   /*
    * (non-Javadoc)
    *
    * @see org.jboss.identity.federation.api.wstrust.STSConfiguration#getProviderForService(java.lang.String)
    */
   public SecurityTokenProvider getProviderForService(String serviceName)
   {
      ServiceProviderType provider = this.spMetadata.get(serviceName);
      if (provider != null)
      {
         return this.tokenProviders.get(provider.getTokenType());
      }
      return null;
   }

   /*
    * (non-Javadoc)
    *
    * @see org.jboss.identity.federation.api.wstrust.STSConfiguration#getProviderForTokenType(java.lang.String)
    */
   public SecurityTokenProvider getProviderForTokenType(String tokenType)
   {
      return this.tokenProviders.get(tokenType);
   }

   /*
    * (non-Javadoc)
    *
    * @see org.jboss.identity.federation.api.wstrust.STSConfiguration#getTokenTypeForService(java.lang.String)
    */
   public String getTokenTypeForService(String serviceName)
   {
      ServiceProviderType provider = this.spMetadata.get(serviceName);
      if (provider != null)
         return provider.getTokenType();
      return null;
   }

   /*
    * (non-Javadoc)
    * @see org.jboss.identity.federation.api.wstrust.STSConfiguration#getServiceProviderPublicKey(java.lang.String)
    */
   public PublicKey getServiceProviderPublicKey(String serviceName)
   {
      PublicKey key = null;
      if (this.trustManager != null)
      {
         try
         {
            key = this.trustManager.getValidatingKey(serviceName);
         }
         catch (Exception e)
         {
            throw new RuntimeException("Error obtaining public key for service " + serviceName, e);
         }
      }
      return key;
   }

   /*
    * (non-Javadoc)
    * @see org.jboss.identity.federation.api.wstrust.STSConfiguration#getSTSKeyPair()
    */
   public KeyPair getSTSKeyPair()
   {
      KeyPair keyPair = null;
      if (this.trustManager != null)
      {
         try
         {
            keyPair = this.trustManager.getSigningKeyPair();
         }
         catch (Exception e)
         {
            throw new RuntimeException("Error obtaining signing key pair:", e);
         }
      }
      return keyPair;
   }

}
TOP

Related Classes of org.jboss.identity.federation.bindings.jboss.wstrust.JBossSTSConfiguration

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.