[jboss-cvs] JBossAS SVN: r100086 - in projects/security/picketbox/trunk/security-jboss-sx/jbosssx/src: test/java/org/jboss/test/security/config and 1 other directory.

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Thu Jan 28 15:57:33 EST 2010


Author: anil.saldhana at jboss.com
Date: 2010-01-28 15:57:33 -0500 (Thu, 28 Jan 2010)
New Revision: 100086

Added:
   projects/security/picketbox/trunk/security-jboss-sx/jbosssx/src/main/java/org/jboss/security/config/parser/UsersConfigParser.java
Modified:
   projects/security/picketbox/trunk/security-jboss-sx/jbosssx/src/main/java/org/jboss/security/config/parser/ModuleOptionParser.java
   projects/security/picketbox/trunk/security-jboss-sx/jbosssx/src/test/java/org/jboss/test/security/config/StaxConfigParserUnitTestCase.java
Log:
Handle the Users xml that may be embedded in XMLLoginModule configuration

Modified: projects/security/picketbox/trunk/security-jboss-sx/jbosssx/src/main/java/org/jboss/security/config/parser/ModuleOptionParser.java
===================================================================
--- projects/security/picketbox/trunk/security-jboss-sx/jbosssx/src/main/java/org/jboss/security/config/parser/ModuleOptionParser.java	2010-01-28 20:52:17 UTC (rev 100085)
+++ projects/security/picketbox/trunk/security-jboss-sx/jbosssx/src/main/java/org/jboss/security/config/parser/ModuleOptionParser.java	2010-01-28 20:57:33 UTC (rev 100086)
@@ -24,13 +24,18 @@
 import java.util.HashMap;
 import java.util.Map;
 
+import javax.xml.stream.EventFilter;
 import javax.xml.stream.XMLEventReader;
+import javax.xml.stream.XMLInputFactory;
+import javax.xml.stream.XMLStreamConstants;
 import javax.xml.stream.XMLStreamException;
 import javax.xml.stream.events.Attribute;
 import javax.xml.stream.events.EndElement;
 import javax.xml.stream.events.StartElement;
 import javax.xml.stream.events.XMLEvent;
 
+import org.jboss.security.auth.spi.Users;
+
 /**
  * Parses the Module Option
  * @author Anil.Saldhana at redhat.com
@@ -60,10 +65,64 @@
          {
             xmlEvent = xmlEventReader.nextEvent();
             Attribute attribute = (Attribute) peekedStartElement.getAttributes().next();
-            options.put(attribute.getValue(), xmlEventReader.getElementText());
+            
+            //Sometime, there may be embedded xml in the option. We cannot use peek
+            //next event here because the event reader jumps to the next module option
+            //in the presence of a text (and not embedded xml). Since embedded xml is rare,
+            //we are going to rely on exceptions as a mode of control. The issue is that
+            //we have used an event filter on the XMLEventReader for convenience
+            Object val = null;
+            try
+            {
+               val = xmlEventReader.getElementText();
+            }
+            catch(XMLStreamException xse)
+            {
+               //Look for embedded xml
+               XMLEvent embeddedOrText = xmlEventReader.peek();
+               if(embeddedOrText.getEventType() == XMLStreamConstants.START_ELEMENT)
+               { 
+                  val = embeddedUsersXMLParsing(xmlEventReader); 
+               }   
+            } 
+            options.put(attribute.getValue(), val );
          }
          else break; 
       }
       return options;
    }
+   
+   /**
+    * The {@code XMLLoginModule} may contain an embedded {@code Users} xml representation
+    * @param xmlEventReader
+    * @return
+    * @throws XMLStreamException
+    */
+   private Users embeddedUsersXMLParsing(XMLEventReader xmlEventReader) throws XMLStreamException
+   { 
+       Users users = new Users();
+       
+       XMLInputFactory xmlInputFactory = XMLInputFactory.newInstance();
+       xmlEventReader = xmlInputFactory.createFilteredReader(xmlEventReader, new EventFilter()
+       {
+           public boolean accept(XMLEvent xmlEvent)
+           {
+               return xmlEvent.isStartElement() ;
+           }
+       });
+       while (xmlEventReader.hasNext())
+       {
+           XMLEvent xmlEvent = xmlEventReader.peek();
+           int eventType = xmlEvent.getEventType();
+           switch (eventType)
+           {
+              case XMLStreamConstants.START_ELEMENT:  
+
+                 UsersConfigParser usersParser = new UsersConfigParser();
+                 users = usersParser.parse(xmlEventReader); 
+                 return users; 
+           }
+       } 
+       return users;
+   } 
 }
\ No newline at end of file

Added: projects/security/picketbox/trunk/security-jboss-sx/jbosssx/src/main/java/org/jboss/security/config/parser/UsersConfigParser.java
===================================================================
--- projects/security/picketbox/trunk/security-jboss-sx/jbosssx/src/main/java/org/jboss/security/config/parser/UsersConfigParser.java	                        (rev 0)
+++ projects/security/picketbox/trunk/security-jboss-sx/jbosssx/src/main/java/org/jboss/security/config/parser/UsersConfigParser.java	2010-01-28 20:57:33 UTC (rev 100086)
@@ -0,0 +1,131 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2008, 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.security.config.parser;
+
+import java.util.Iterator;
+
+import javax.xml.stream.XMLEventReader;
+import javax.xml.stream.XMLStreamConstants;
+import javax.xml.stream.XMLStreamException;
+import javax.xml.stream.events.Attribute;
+import javax.xml.stream.events.EndElement;
+import javax.xml.stream.events.StartElement;
+import javax.xml.stream.events.XMLEvent;
+
+import org.jboss.security.auth.spi.Users;
+import org.jboss.security.auth.spi.Users.User;
+
+
+/**
+ * Parse the Users configuration embeddable within
+ * {@code XMLLoginModule} module option
+ * @author Anil.Saldhana at redhat.com
+ * @since Jan 27, 2010
+ */
+public class UsersConfigParser
+{
+   /**
+    * Parse the embedded xml in the module option representing
+    * the {@code Users} object
+    * @param xmlEventReader
+    * @return
+    * @throws XMLStreamException
+    */
+   @SuppressWarnings("unchecked")
+   public Users parse(XMLEventReader xmlEventReader) throws XMLStreamException
+   {
+      Users users = new Users();
+      XMLEvent xmlEvent = null;
+      while(xmlEventReader.hasNext())
+      {   
+         xmlEvent = xmlEventReader.peek(); 
+         if(xmlEvent instanceof StartElement)
+         {
+            StartElement se = (StartElement) xmlEvent;
+            if("module-option".equals(se.getName().getLocalPart()))
+               return users;
+         }
+         if(xmlEvent instanceof EndElement)
+         { 
+            xmlEvent = xmlEventReader.nextEvent(); 
+            continue;
+         }
+         
+         xmlEvent = xmlEventReader.nextEvent();
+         User user = new Users.User();
+         
+         StartElement peekedStartElement = (StartElement) xmlEvent;
+         Iterator<Attribute> attribs = peekedStartElement.getAttributes();
+         while(attribs.hasNext())
+         {
+            Attribute attrib  = attribs.next();
+            if("name".equals(attrib.getName().getLocalPart()))
+            {
+               user.setName(attrib.getValue()); 
+            }
+            else if("password".equals(attrib.getName().getLocalPart()))
+            {
+               user.setPassword(attrib.getValue()); 
+            }
+            else if("encoding".equals(attrib.getName().getLocalPart()))
+            {
+               user.setEncoding(attrib.getValue()); 
+            }
+         }
+         //Get the roles
+         xmlEvent = xmlEventReader.peek();
+         while(xmlEvent != null && xmlEvent.getEventType() == XMLStreamConstants.START_ELEMENT)
+         {
+            StartElement roleElement = (StartElement) xmlEvent;
+            if("role".equals(roleElement.getName().getLocalPart()))
+            {
+               xmlEvent = xmlEventReader.nextEvent();
+               Iterator<Attribute> roleAttribs = roleElement.getAttributes();
+
+               String roleName = null;
+               String groupName = "Roles";
+               
+               while(roleAttribs.hasNext())
+               {
+                  Attribute roleAttribute = roleAttribs.next();
+                  
+                  if("name".equals(roleAttribute.getName().getLocalPart()))
+                  {
+                    roleName = roleAttribute.getValue();  
+                  }
+                  else if("group".equals(roleAttribute.getName().getLocalPart()))
+                  {
+                     groupName = roleAttribute.getValue();  
+                  } 
+               }
+               if(roleName != null)
+                  user.addRole(roleName, groupName);
+            } 
+            else break;
+            xmlEvent = xmlEventReader.peek();
+         } 
+         
+         users.addUser(user);
+      }
+      return users; 
+   } 
+}
\ No newline at end of file

Modified: projects/security/picketbox/trunk/security-jboss-sx/jbosssx/src/test/java/org/jboss/test/security/config/StaxConfigParserUnitTestCase.java
===================================================================
--- projects/security/picketbox/trunk/security-jboss-sx/jbosssx/src/test/java/org/jboss/test/security/config/StaxConfigParserUnitTestCase.java	2010-01-28 20:52:17 UTC (rev 100085)
+++ projects/security/picketbox/trunk/security-jboss-sx/jbosssx/src/test/java/org/jboss/test/security/config/StaxConfigParserUnitTestCase.java	2010-01-28 20:57:33 UTC (rev 100086)
@@ -35,6 +35,8 @@
 import org.jboss.security.auth.container.config.AuthModuleEntry;
 import org.jboss.security.auth.login.BaseAuthenticationInfo;
 import org.jboss.security.auth.login.LoginModuleStackHolder;
+import org.jboss.security.auth.spi.Users;
+import org.jboss.security.auth.spi.Users.User;
 import org.jboss.security.authorization.config.AuthorizationModuleEntry;
 import org.jboss.security.config.ACLInfo;
 import org.jboss.security.config.ApplicationPolicy;
@@ -95,6 +97,21 @@
       TestIdentityTrustConfig.testConfJavaEE();
    }
    
+   public void testSecurityConfig41() throws Exception
+   {
+      Configuration.setConfiguration(StandaloneConfiguration.getInstance());
+      ClassLoader tcl = Thread.currentThread().getContextClassLoader();
+      InputStream is = tcl.getResourceAsStream("config/securityConfig41.xml");
+      StaxBasedConfigParser parser = new StaxBasedConfigParser(); 
+      
+      parser.schemaValidate(is);
+      
+      is = tcl.getResourceAsStream("config/securityConfig41.xml");
+      parser.parse(is); 
+      
+      TestSecurityConfig41.validateJAASConfiguration();
+   }
+   
    private static ApplicationPolicy getApplicationPolicy(String domainName)
    {
       Configuration config = Configuration.getConfiguration();
@@ -371,4 +388,31 @@
          assertEquals("org.jboss.security.identitytrust.modules.JavaEETrustModule", itme.getName());
       }
    }
+   
+// Internal class to represent the securityConfig41.xml validation
+   private static class TestSecurityConfig41
+   { 
+      public static void validateJAASConfiguration()
+      {
+         ApplicationPolicy jaasConfig = getApplicationPolicy("conf1");
+         BaseAuthenticationInfo authInfo = jaasConfig.getAuthenticationInfo();
+         List<?> entries = authInfo.getModuleEntries();
+         assertEquals("Number of entries = 1", 1, entries.size());
+
+         // First Entry
+         Object entry = entries.get(0);
+         assertTrue("Entry instanceof AppConfigurationEntry", entry instanceof AppConfigurationEntry);
+         AppConfigurationEntry ace = (AppConfigurationEntry) entry;
+         assertEquals("LM Name", "org.jboss.security.auth.spi.XMLLoginModule", ace.getLoginModuleName());
+         assertEquals("Required", AppConfigurationEntry.LoginModuleControlFlag.REQUIRED, ace.getControlFlag());
+         Map<String, ?> aceOptions = ace.getOptions();
+         assertEquals("Number of options = 2", 2, aceOptions.size());
+         Users users = (Users) aceOptions.get("userInfo"); 
+         assertNotNull("Users object is not null", users);
+         User jduke = users.getUser("jduke");
+         assertEquals("3 roles", 3, jduke.getRoleNames().length);
+         assertEquals("callerJduke", "callerJduke", jduke.getRoleNames("CallerPrincipal")[0]);
+         assertEquals("unauthenticatedIdentity=guest", "guest", aceOptions.get("unauthenticatedIdentity")); 
+      }
+   }
 }
\ No newline at end of file




More information about the jboss-cvs-commits mailing list