1 22 package org.jboss.test.security.container.auth.modules; 23 24 import java.io.IOException ; 25 import java.util.Arrays ; 26 import java.util.HashMap ; 27 import java.util.List ; 28 import java.util.Map ; 29 import java.util.Set ; 30 31 import javax.security.auth.Subject ; 32 import javax.security.auth.callback.CallbackHandler ; 33 import javax.security.auth.login.AppConfigurationEntry ; 34 import javax.security.auth.login.Configuration ; 35 import javax.security.auth.message.AuthException; 36 37 import org.jboss.security.SimplePrincipal; 38 import org.jboss.security.auth.container.modules.DelegatingServerAuthModule; 39 import org.jboss.security.auth.login.LoginModuleStackHolder; 40 import org.jboss.security.auth.login.XMLLoginConfigImpl; 41 import org.jboss.test.JBossTestCase; 42 import org.jboss.test.util.AppCallbackHandler; 43 44 46 52 public class DelegatingServerAuthModuleUnitTestCase extends JBossTestCase 53 { 54 private DelegatingServerAuthModule authm = null; 55 56 public DelegatingServerAuthModuleUnitTestCase(String name) 57 { 58 super(name); 59 } 60 61 public void testCtrWithConfigName() 62 { 63 authm = new DelegatingServerAuthModule("other"); 64 assertNotNull("DelegatingServerAuthModule != null", authm); 65 } 66 67 public void testCtrWithConfigPassed() throws Exception 68 { 69 authm = new DelegatingServerAuthModule("other", getConfiguration()); 70 assertNotNull("DelegatingServerAuthModule != null", authm); 71 } 72 73 public void testCtrWithLoginModuleStackHolder() throws Exception 74 { 75 authm = new DelegatingServerAuthModule(getLoginModuleStackHolder()); 76 assertNotNull("DelegatingServerAuthModule != null", authm); 77 } 78 79 public void testValidateRequestWithConfigName() throws Exception 80 { 81 authm = new DelegatingServerAuthModule("other"); 82 assertNotNull("DelegatingServerAuthModule != null", authm); 83 Map options = new HashMap (); 84 options.put("securityConfigService","jboss.security:service=SecurityConfig"); 85 options.put("testMode", "TRUE"); 86 authm.initialize(null,null,getCallbackHandler("admin","secret"),options); 87 try 88 { 89 authm.validateRequest(null,null,null,null); 90 }catch(AuthException e) 91 { 92 fail(e.getLocalizedMessage()); 93 } 94 95 checkAuthenticatedSubject(authm.getAuthenticatedSubject()); 96 } 97 98 public void testValidateRequestWithConfiguration() throws Exception 99 { 100 authm = new DelegatingServerAuthModule("other", getConfiguration()); 101 assertNotNull("DelegatingServerAuthModule != null", authm); 102 authm.initialize(null,null,getCallbackHandler("admin","secret"),null); 103 try 104 { 105 authm.validateRequest(null,null,null,null); 106 }catch(AuthException e) 107 { 108 fail(e.getLocalizedMessage()); 109 } 110 checkAuthenticatedSubject(authm.getAuthenticatedSubject()); 111 } 112 113 public void testValidateRequestWithLoginModuleStackHolder() throws Exception 114 { 115 authm = new DelegatingServerAuthModule(getLoginModuleStackHolder()); 116 assertNotNull("DelegatingServerAuthModule != null", authm); 117 authm.initialize(null,null,getCallbackHandler("admin","secret"),null); 118 try 119 { 120 authm.validateRequest(null,null,null,null); 121 }catch(AuthException e) 122 { 123 fail(e.getLocalizedMessage()); 124 } 125 checkAuthenticatedSubject(authm.getAuthenticatedSubject()); 126 } 127 128 public void testInValidRequestWithLoginModuleStackHolder() throws Exception 129 { 130 authm = new DelegatingServerAuthModule( getLoginModuleStackHolder()); 131 assertNotNull("DelegatingServerAuthModule != null", authm); 132 authm.initialize(null,null,getCallbackHandler("admin","bad"),null ); 133 try 134 { 135 authm.validateRequest(null,null,null,null); 136 fail("Test Should have failed"); 137 }catch(AuthException ae) 138 { 139 } 141 } 142 143 public void testInValidRequestWithConfiguration() throws Exception 144 { 145 authm = new DelegatingServerAuthModule("other", getConfiguration()); 146 assertNotNull("DelegatingServerAuthModule != null", authm); 147 authm.initialize(null,null,getCallbackHandler("admin","bad"),null ); 148 try 149 { 150 authm.validateRequest(null,null,null,null); 151 fail("Test Should have failed"); 152 }catch(AuthException ae) 153 { 154 } 156 } 157 158 public void testInValidRequestWithConfigName() throws Exception 159 { 160 authm = new DelegatingServerAuthModule("other"); 161 assertNotNull("DelegatingServerAuthModule != null", authm); 162 Map options = new HashMap (); 163 options.put("securityConfigService","jboss.security:service=SecurityConfig"); 164 options.put("testMode", "TRUE"); 165 authm.initialize(null,null,getCallbackHandler("admin","bad"),options ); 166 try 167 { 168 authm.validateRequest(null,null,null,null); 169 fail("Test Should have failed"); 170 }catch(AuthException ae) 171 { 172 } 174 } 175 176 private Configuration getConfiguration() throws IOException 178 { 179 XMLLoginConfigImpl config = new XMLLoginConfigImpl(); 181 config.setConfigResource("security/login-config.xml"); 182 config.loadConfig(); 183 return config; 184 } 185 186 private CallbackHandler getCallbackHandler(String username, String pwd) 187 { 188 return new AppCallbackHandler(username,pwd.toCharArray()); 189 } 190 191 private void checkAuthenticatedSubject(Subject authenticatedSubject) 192 { 193 assertNotNull("Authenticated Subject != null",authenticatedSubject ); 194 Set principals = authenticatedSubject.getPrincipals(SimplePrincipal.class); 195 assertTrue("admin is contained in principal", 196 principals.contains(new SimplePrincipal("admin")) ); 197 } 198 199 private LoginModuleStackHolder getLoginModuleStackHolder() throws IOException 200 { 201 Configuration config = getConfiguration(); 202 AppConfigurationEntry [] appEntries = config.getAppConfigurationEntry("other"); 203 List aList = Arrays.asList(appEntries); 204 LoginModuleStackHolder lmsh = new LoginModuleStackHolder("other", aList); 205 return lmsh; 206 } 207 } 208 | Popular Tags |