[jboss-cvs] Picketbox SVN: r35 - in trunk/picketbox/src: test/java/org/picketbox/test and 3 other directories.

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Wed Feb 17 06:44:18 EST 2010


Author: anil.saldhana at jboss.com
Date: 2010-02-17 06:44:18 -0500 (Wed, 17 Feb 2010)
New Revision: 35

Added:
   trunk/picketbox/src/main/java/org/picketbox/plugins/PicketBoxProcessor.java
   trunk/picketbox/src/main/java/org/picketbox/plugins/SecurityActions.java
   trunk/picketbox/src/test/java/org/picketbox/test/annotations/
   trunk/picketbox/src/test/java/org/picketbox/test/annotations/PicketBoxProcessorUnitTestCase.java
   trunk/picketbox/src/test/java/org/picketbox/test/annotations/SecurityMappingAnnotationUnitTestCase.java
   trunk/picketbox/src/test/java/org/picketbox/test/pojos/
   trunk/picketbox/src/test/java/org/picketbox/test/pojos/SecurityMappingAnnotationPrincipalPOJO.java
   trunk/picketbox/src/test/java/org/picketbox/test/pojos/SecurityMappingAnnotationRolePOJO.java
   trunk/picketbox/src/test/resources/rolemapping.properties
Log:
unit test annotations

Added: trunk/picketbox/src/main/java/org/picketbox/plugins/PicketBoxProcessor.java
===================================================================
--- trunk/picketbox/src/main/java/org/picketbox/plugins/PicketBoxProcessor.java	                        (rev 0)
+++ trunk/picketbox/src/main/java/org/picketbox/plugins/PicketBoxProcessor.java	2010-02-17 11:44:18 UTC (rev 35)
@@ -0,0 +1,133 @@
+/*
+ * 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.picketbox.plugins;
+
+import java.security.Principal;
+
+import javax.security.auth.Subject;
+import javax.security.auth.login.LoginException;
+
+import org.jboss.security.AuthenticationManager;
+import org.jboss.security.AuthorizationManager;
+import org.jboss.security.SecurityConstants;
+import org.jboss.security.SecurityContext;
+import org.jboss.security.SimplePrincipal;
+import org.jboss.security.annotation.SecurityConfig;
+import org.jboss.security.annotation.SecurityDomain;
+import org.jboss.security.callbacks.SecurityContextCallbackHandler;
+import org.jboss.security.identity.RoleGroup;
+import org.picketbox.config.PicketBoxConfiguration;
+import org.picketbox.factories.SecurityFactory;
+
+/**
+ * Process the security annotations on a POJO
+ * @since Feb 16, 2010
+ */
+public class PicketBoxProcessor
+{
+   Principal principal = null;
+   Object credential = null;
+   
+   public PicketBoxProcessor()
+   {   
+   } 
+   
+   public void setSecurityInfo(String username, Object credential)
+   {
+      this.principal = new SimplePrincipal(username);
+      this.credential = credential; 
+   }
+   
+   public Principal getCallerPrincipal() throws Exception
+   {
+      Principal principal = null;
+      
+      SecurityContext securityContext =  SecurityActions.getSecurityContext();
+      if(securityContext != null)
+         principal = securityContext.getUtil().getUserPrincipal(); 
+      return principal;
+   }
+   
+   public RoleGroup getCallerRoles() throws Exception
+   {
+      RoleGroup roleGroup = null;
+      
+      SecurityContext securityContext =  SecurityActions.getSecurityContext();
+      if(securityContext != null)
+         roleGroup = securityContext.getUtil().getRoles(); 
+      return roleGroup;
+   }
+   
+   public Subject getCallerSubject() throws Exception
+   {
+      Subject subject = new Subject();
+      SecurityContext securityContext =  SecurityActions.getSecurityContext();
+      if(securityContext != null)
+         subject = securityContext.getUtil().getSubject();
+      return subject;
+   }
+   
+   public void process(Object pojo) throws Exception
+   {
+      String securityDomain = SecurityConstants.DEFAULT_APPLICATION_POLICY;
+      
+      Class<?> objectClass = pojo.getClass();
+      
+      SecurityDomain securityDomainAnnotation = objectClass.getAnnotation(SecurityDomain.class);
+      if(securityDomainAnnotation != null)
+         securityDomain = securityDomainAnnotation.value();
+
+      SecurityFactory.prepare();
+      try
+      {
+         SecurityConfig securityConfig = objectClass.getAnnotation(SecurityConfig.class);
+         if(securityConfig == null)
+            throw new RuntimeException("@SecurityConfig is missing");
+
+         PicketBoxConfiguration idtrustConfig = new PicketBoxConfiguration();
+         idtrustConfig.load(securityConfig.fileName());
+         
+         SecurityContext securityContext = SecurityActions.createSecurityContext(securityDomain);
+         SecurityActions.setSecurityContext(securityContext);
+         
+         AuthenticationManager authMgr = SecurityFactory.getAuthenticationManager(securityDomain);
+         
+         Subject subject = new Subject();
+         boolean valid = authMgr.isValid(principal, credential, subject);
+         if(!valid)
+            throw new LoginException("Invalid");
+         
+         SecurityActions.register(securityContext, principal, credential, subject); 
+
+         AuthorizationManager authzMgr = SecurityFactory.getAuthorizationManager(securityDomain);
+         SecurityContextCallbackHandler cbh = new SecurityContextCallbackHandler(securityContext);
+         
+         RoleGroup roles = authzMgr.getSubjectRoles(subject, cbh); 
+         if(roles == null)
+            throw new RuntimeException("Roles from subject is null");  
+      }
+      finally
+      {
+         SecurityFactory.release();
+      } 
+   }
+}
\ No newline at end of file

Added: trunk/picketbox/src/main/java/org/picketbox/plugins/SecurityActions.java
===================================================================
--- trunk/picketbox/src/main/java/org/picketbox/plugins/SecurityActions.java	                        (rev 0)
+++ trunk/picketbox/src/main/java/org/picketbox/plugins/SecurityActions.java	2010-02-17 11:44:18 UTC (rev 35)
@@ -0,0 +1,124 @@
+/*
+ * 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.picketbox.plugins;
+
+import java.security.AccessController;
+import java.security.Principal;
+import java.security.PrivilegedAction;
+import java.security.PrivilegedActionException;
+import java.security.PrivilegedExceptionAction;
+
+import javax.security.auth.Subject;
+
+import org.jboss.security.SecurityContext;
+import org.jboss.security.SecurityContextAssociation;
+import org.jboss.security.SecurityContextFactory;
+
+/**
+ * Privileged Blocks
+ * @author Anil.Saldhana at redhat.com
+ * @since November 19, 2008
+ */
+class SecurityActions
+{
+   
+   @SuppressWarnings("unchecked")
+   static ClassLoader getContextClassLoader() throws PrivilegedActionException
+   {
+      return (ClassLoader) AccessController.doPrivileged(new PrivilegedExceptionAction()
+      { 
+         public Object run() throws Exception
+         {
+            return Thread.currentThread().getContextClassLoader();
+         }
+      });
+   }
+
+   static String getSystemProperty(final String key, final String defaultValue)
+   {
+      return AccessController.doPrivileged(new PrivilegedAction<String>()
+      {
+         public String run()
+         {
+            return System.getProperty(key, defaultValue);
+         }
+      });
+   }
+
+   static void setSystemProperty(final String key, final String value)
+   {
+      AccessController.doPrivileged(new PrivilegedAction<Object>()
+      {
+         public Object run()
+         {
+            System.setProperty(key, value);
+            return null;
+         }
+      });
+   }
+   
+   static SecurityContext getSecurityContext() throws PrivilegedActionException
+   {
+      return AccessController.doPrivileged(new PrivilegedExceptionAction<SecurityContext>()
+      {
+         public SecurityContext run() throws Exception
+         {
+            return SecurityContextAssociation.getSecurityContext();
+         }
+      });
+   }
+   
+   static SecurityContext createSecurityContext(final String name) throws PrivilegedActionException
+   {
+      return AccessController.doPrivileged(new PrivilegedExceptionAction<SecurityContext>()
+      {
+         public SecurityContext run() throws Exception
+         {
+            return SecurityContextFactory.createSecurityContext(name);
+         }
+      });
+   }
+   
+   static void setSecurityContext(final SecurityContext sc)
+   {
+      AccessController.doPrivileged(new PrivilegedAction<Object>()
+      {
+         public Object run()
+         {
+            SecurityContextAssociation.setSecurityContext(sc);
+            return null;
+         }
+      });
+   }
+   
+   static void register(final SecurityContext sc, final Principal principal, final Object credential, final Subject subject)
+   {
+      AccessController.doPrivileged(new PrivilegedAction<Object>()
+      {
+         public Object run()
+         {
+            sc.getUtil().createSubjectInfo(principal, credential, subject); 
+            return null;
+         }
+      });        
+   }
+}
\ No newline at end of file

Added: trunk/picketbox/src/test/java/org/picketbox/test/annotations/PicketBoxProcessorUnitTestCase.java
===================================================================
--- trunk/picketbox/src/test/java/org/picketbox/test/annotations/PicketBoxProcessorUnitTestCase.java	                        (rev 0)
+++ trunk/picketbox/src/test/java/org/picketbox/test/annotations/PicketBoxProcessorUnitTestCase.java	2010-02-17 11:44:18 UTC (rev 35)
@@ -0,0 +1,56 @@
+/*
+ * 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.picketbox.test.annotations;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
+
+import org.jboss.security.SimplePrincipal;
+import org.jboss.security.identity.RoleGroup;
+import org.jboss.security.identity.plugins.SimpleRole;
+import org.junit.Test;
+import org.picketbox.plugins.PicketBoxProcessor;
+import org.picketbox.test.pojos.SecurityMappingAnnotationRolePOJO;
+
+/**
+ * @author Anil.Saldhana at redhat.com
+ * @since Feb 16, 2010
+ */ 
+public class PicketBoxProcessorUnitTestCase
+{
+   @Test
+   public void testSecurityMappingRoleAnnotation() throws Exception
+   {
+      SecurityMappingAnnotationRolePOJO pojo = new SecurityMappingAnnotationRolePOJO();
+      
+      PicketBoxProcessor processor = new PicketBoxProcessor(); 
+      processor.setSecurityInfo("anil", "pass");
+      processor.process(pojo);
+      
+      assertEquals("Principal == anil", new SimplePrincipal("anil"), processor.getCallerPrincipal());
+      assertNotNull("Subject is not null", processor.getCallerSubject());
+      RoleGroup callerRoles = processor.getCallerRoles();
+      assertTrue("InternalUser is a role", callerRoles.containsRole(new SimpleRole("InternalUser")));
+      assertTrue("AuthorizedUser is a role", callerRoles.containsRole(new SimpleRole("AuthorizedUser")));
+   }
+}
\ No newline at end of file

Added: trunk/picketbox/src/test/java/org/picketbox/test/annotations/SecurityMappingAnnotationUnitTestCase.java
===================================================================
--- trunk/picketbox/src/test/java/org/picketbox/test/annotations/SecurityMappingAnnotationUnitTestCase.java	                        (rev 0)
+++ trunk/picketbox/src/test/java/org/picketbox/test/annotations/SecurityMappingAnnotationUnitTestCase.java	2010-02-17 11:44:18 UTC (rev 35)
@@ -0,0 +1,50 @@
+/*
+ * 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.picketbox.test.annotations;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+
+import java.lang.annotation.Annotation;
+
+import org.jboss.security.annotation.SecurityMapping;
+import org.junit.Test;
+import org.picketbox.test.pojos.SecurityMappingAnnotationPrincipalPOJO;
+
+/**
+ * @author Anil.Saldhana at redhat.com
+ * @since Feb 14, 2010
+ */ 
+public class SecurityMappingAnnotationUnitTestCase
+{
+   @Test
+   public void testSecurityMapping()
+   {   
+      Class<?> clazz = SecurityMappingAnnotationPrincipalPOJO.class;
+      Annotation[] annotations = clazz.getAnnotations();
+      assertEquals("1 annotation", 1, annotations.length);  
+      Annotation annotation = annotations[0]; 
+      assertTrue(annotation instanceof SecurityMapping); 
+      SecurityMapping sm = (SecurityMapping) annotation;
+      assertEquals("PRINCIPAL", sm.type());
+   } 
+}
\ No newline at end of file

Added: trunk/picketbox/src/test/java/org/picketbox/test/pojos/SecurityMappingAnnotationPrincipalPOJO.java
===================================================================
--- trunk/picketbox/src/test/java/org/picketbox/test/pojos/SecurityMappingAnnotationPrincipalPOJO.java	                        (rev 0)
+++ trunk/picketbox/src/test/java/org/picketbox/test/pojos/SecurityMappingAnnotationPrincipalPOJO.java	2010-02-17 11:44:18 UTC (rev 35)
@@ -0,0 +1,33 @@
+/*
+ * 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.picketbox.test.pojos;
+
+import org.jboss.security.annotation.SecurityMapping;
+
+/**
+ * @author Anil.Saldhana at redhat.com
+ * @since Feb 14, 2010
+ */ 
+ at SecurityMapping(type="PRINCIPAL")
+public class SecurityMappingAnnotationPrincipalPOJO
+{
+}
\ No newline at end of file

Added: trunk/picketbox/src/test/java/org/picketbox/test/pojos/SecurityMappingAnnotationRolePOJO.java
===================================================================
--- trunk/picketbox/src/test/java/org/picketbox/test/pojos/SecurityMappingAnnotationRolePOJO.java	                        (rev 0)
+++ trunk/picketbox/src/test/java/org/picketbox/test/pojos/SecurityMappingAnnotationRolePOJO.java	2010-02-17 11:44:18 UTC (rev 35)
@@ -0,0 +1,37 @@
+/*
+ * 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.picketbox.test.pojos;
+
+import org.jboss.security.annotation.SecurityConfig;
+import org.jboss.security.annotation.SecurityDomain;
+import org.jboss.security.annotation.SecurityMapping;
+
+/**
+ * @author Anil.Saldhana at redhat.com
+ * @since Feb 16, 2010
+ */
+ at SecurityMapping(type="ROLE")
+ at SecurityDomain(value="role-mapping-test")
+ at SecurityConfig(fileName="config/mapping.conf")
+public class SecurityMappingAnnotationRolePOJO
+{
+}
\ No newline at end of file

Added: trunk/picketbox/src/test/resources/rolemapping.properties
===================================================================
--- trunk/picketbox/src/test/resources/rolemapping.properties	                        (rev 0)
+++ trunk/picketbox/src/test/resources/rolemapping.properties	2010-02-17 11:44:18 UTC (rev 35)
@@ -0,0 +1 @@
+validuser=role1,role2
\ No newline at end of file




More information about the jboss-cvs-commits mailing list