1 28 package net.sf.jguard.ext.authentication.manager; 29 30 import java.net.URL ; 31 import java.security.Principal ; 32 import java.util.Collection ; 33 import java.util.HashMap ; 34 import java.util.HashSet ; 35 import java.util.Iterator ; 36 import java.util.Map ; 37 import java.util.Set ; 38 39 import javax.security.auth.Subject ; 40 41 import junit.framework.Assert; 42 import junit.framework.TestCase; 43 import junitx.util.PrivateAccessor; 44 import net.sf.jguard.core.CoreConstants; 45 import net.sf.jguard.core.authentication.credentials.JGuardCredential; 46 import net.sf.jguard.core.principals.RolePrincipal; 47 import net.sf.jguard.ext.SecurityConstants; 48 import net.sf.jguard.ext.authentication.AuthenticationException; 49 import net.sf.jguard.ext.principals.PrincipalUtils; 50 import net.sf.jguard.ext.registration.SubjectTemplate; 51 52 public class XmlAuthenticationManagerTest extends TestCase { 53 54 private AuthenticationManager auth; 55 56 57 protected void setUp() throws Exception { 58 super.setUp(); 59 PrivateAccessor.setField(AuthenticationManagerFactory.class,"authenticationManager",null); 60 auth = getXmlAuthenticationManager(); 61 62 } 63 64 65 protected void tearDown() throws Exception { 66 super.tearDown(); 67 } 68 69 public void initAuthenticationManager() { 70 Map settings = new HashMap (); 71 URL url = getClass().getResource("/jGuardUsersPrincipals.xml"); 72 settings.put(SecurityConstants.AUTHENTICATION_XML_FILE_LOCATION, url.toString()); 73 try { 74 AuthenticationManagerFactory.createAuthenticationManager(XmlAuthenticationManager.class.getName(), settings); 75 } catch (AuthenticationException e1) { 76 TestCase.fail(" authenticationDAoFactory not properly initialized "); 77 } 78 } 79 80 public void testAddAndRemoveRolePrincipal(){ 81 initAuthenticationManager(); 82 AuthenticationManager authManager = AuthenticationManagerFactory.getAuthenticationManager(); 83 84 } 85 86 public void testCreateUser(){ 87 88 SubjectTemplate st = buildSubjectTemplate(); 89 Subject user = null; 90 try { 91 user = auth.createUser(st); 92 System.out.println(" test succeed "); 93 94 System.out.println(user); 95 96 Collection credentials = user.getPublicCredentials(); 98 credentials.addAll(user.getPrivateCredentials()); 99 Collection users = auth.findUsers(credentials); 100 assertTrue("multiple users are found user unicity is not here",users.size()==1); 101 Iterator itUsers = users.iterator(); 102 Subject user2 = (Subject )itUsers.next(); 103 assertTrue(user.equals(user2)); 104 105 106 } catch (AuthenticationException e) { 107 TestCase.fail(e.getMessage()); 108 } 109 110 } 111 112 113 private SubjectTemplate buildSubjectTemplate() { 114 SubjectTemplate st = new SubjectTemplate(); 115 116 JGuardCredential jcred1 = new JGuardCredential(); 118 jcred1.setId("login"); 119 jcred1.setValue("toto"+System.currentTimeMillis()); 120 JGuardCredential jcred2 = new JGuardCredential(); 121 jcred2.setId("password"); 122 jcred2.setValue("toto"+System.currentTimeMillis()); 123 Set privRequiredCred = new HashSet (); 124 privRequiredCred.add(jcred1); 125 privRequiredCred.add(jcred2); 126 st.setPrivateRequiredCredentials(privRequiredCred); 127 return st; 128 } 129 130 131 private Principal buildRolePrincipal(){ 132 Principal ppal = PrincipalUtils.getPrincipal("net.sf.jguard.core.principals.RolePrincipal",RolePrincipal.getName("stuff")); 133 134 return ppal; 135 } 136 137 138 public void testCreateUserWithEmptySubjectTemplate(){ 139 140 SubjectTemplate st = new SubjectTemplate(); 142 st.setPrivateOptionalCredentials(new HashSet ()); 143 st.setPublicOptionalCredentials(new HashSet ()); 144 st.setPublicRequiredCredentials(new HashSet ()); 145 st.setPrivateRequiredCredentials(new HashSet ()); 146 st.setPrincipals(new HashSet ()); 147 try { 148 auth.createUser(st); 149 } catch (AuthenticationException e) { 150 System.out.println(" test succeeed => an exception is the normal result login and password private credentials are required "+e.getMessage()); 151 } 152 } 153 154 155 private AuthenticationManager getXmlAuthenticationManager() { 156 157 Map options = new HashMap (); 158 options.put(CoreConstants.APPLICATION_NAME,"jGuardExample"); 159 URL url = getClass().getResource("/jGuardUsersPrincipals.xml"); 160 161 options.put(SecurityConstants.AUTHENTICATION_XML_FILE_LOCATION,url.toString()); 162 try { 163 AuthenticationManagerFactory.createAuthenticationManager(XmlAuthenticationManager.class.getName(),options); 164 } catch (AuthenticationException e1) { 165 e1.printStackTrace(); 166 TestCase.fail(" authenticationManager fail!! "); 167 } 168 AuthenticationManager auth = AuthenticationManagerFactory.getAuthenticationManager(); 169 return auth; 170 } 171 172 173 public void testRemoveUser(){ 174 175 Set users = null; 176 try { 177 users = auth.getUsers(); 178 } catch (AuthenticationException e) { 179 TestCase.fail(" auth.getUsers()!! "+e.getMessage()); 180 } 181 System.out.println(users); 182 183 SubjectTemplate st = new SubjectTemplate(); 185 186 JGuardCredential jcred1 = new JGuardCredential(); 187 jcred1.setId("login"); 188 jcred1.setValue("abc"); 189 JGuardCredential jcred2 = new JGuardCredential(); 190 jcred2.setId("password"); 191 jcred2.setValue("toto"+System.currentTimeMillis()); 192 Set privRequiredCred = new HashSet (); 193 privRequiredCred.add(jcred1); 194 privRequiredCred.add(jcred2); 195 st.setPrivateRequiredCredentials(privRequiredCred); 196 197 try { 198 auth.createUser(st); 199 } catch (AuthenticationException e) { 200 TestCase.fail(" user creation fail!! "+e.getMessage()); 201 } 202 Subject sj = new Subject (); 203 JGuardCredential cred = new JGuardCredential(); 204 cred.setId("login"); 205 cred.setValue("abc"); 206 sj.getPublicCredentials().add(cred); 207 try { 208 auth.deleteUser(sj); 209 } catch (AuthenticationException e) { 210 TestCase.fail(" remove user fail!! "+e.getMessage()); 211 } 212 213 Set coll = new HashSet (); 214 coll.add(cred); 215 try { 216 Collection usersFound = auth.findUsers(coll); 217 System.out.println(usersFound); 218 } catch (AuthenticationException e) { 219 TestCase.fail(" remove user fail!! "+e.getMessage()); 220 } 221 } 222 223 public void testCreateAndRemoveUSer(){ 224 225 SubjectTemplate st = buildSubjectTemplate(); 226 Subject user = null; 227 try { 228 user = auth.createUser(st); 229 } catch (AuthenticationException e) { 230 TestCase.fail(e.getMessage()); 231 } 232 Assert.assertNotNull(user); 233 try { 234 auth.deleteUser(user); 235 Set users = auth.getUsers(); 236 assertTrue(!users.contains(user)); 237 } catch (AuthenticationException e) { 238 TestCase.fail(e.getMessage()); 239 } 240 } 241 242 243 public void deletePrincipal(){ 244 245 Principal ppal = buildRolePrincipal(); 246 try { 247 boolean result= auth.deletePrincipal(ppal); 248 } catch (AuthenticationException e) { 249 TestCase.fail(e.getMessage()); 250 } 251 } 252 253 254 255 256 public void testUpdateUser(){ 257 Subject user = createUser(auth); 258 System.out.println(user); 259 JGuardCredential identityCred = new JGuardCredential(); 260 identityCred.setId("login"); 261 identityCred.setValue("toto"); 262 try { 263 auth.updateUser(identityCred,user); 264 } catch (AuthenticationException e) { 265 TestCase.fail(" update user fail!! "+e.getMessage()); 266 } 267 } 268 269 270 private Subject createUser(AuthenticationManager auth) { 271 272 SubjectTemplate st = buildSubjectTemplate(); 273 Subject user = null; 274 try { 275 user = auth.createUser(st); 276 277 } catch (AuthenticationException e) { 278 TestCase.fail(e.getMessage()); 279 } 280 return user; 281 } 282 283 284 public void testGetSubjectTemplate(){ 285 SubjectTemplate st = null; 286 try { 287 st = auth.getSubjectTemplate("default"); 288 } catch (AuthenticationException e) { 289 TestCase.fail("SubjectTemplate intitiled default cannot be grabbed from the XML file "+e.getMessage()); 290 } 291 System.out.println("subjectTemplate="+st); 292 } 293 294 } 295 | Popular Tags |