getLog().info("testXMLLoginModule options: "+entry[0].getOptions());
AppConfigurationEntry[] entry2 = config.getAppConfigurationEntry("testXMLLoginModule2");
assertTrue("Found testXMLLoginModule2", entry2 != null);
getLog().info("testXMLLoginModule2 options: "+entry2[0].getOptions());
UsernamePasswordHandler handler = new UsernamePasswordHandler("scott", "echoman".toCharArray());
LoginContext lc = new LoginContext("testXMLLoginModule", handler);
lc.login();
Subject subject = lc.getSubject();
Set groups = subject.getPrincipals(Group.class);
assertTrue("Principals contains scott", subject.getPrincipals().contains(new SimplePrincipal("scott")));
assertTrue("Principals contains Roles", groups.contains(new SimplePrincipal("Roles")));
assertTrue("Principals contains CallerPrincipal", groups.contains(new SimplePrincipal("CallerPrincipal")));
Group roles = (Group) groups.iterator().next();
Iterator groupsIter = groups.iterator();
while (groupsIter.hasNext())
{
roles = (Group) groupsIter.next();
if (roles.getName().equals("Roles"))
{
assertTrue("Echo is a role", roles.isMember(new SimplePrincipal("Echo")));
assertTrue("Java is NOT a role", roles.isMember(new SimplePrincipal("Java")) == false);
assertTrue("Coder is NOT a role", roles.isMember(new SimplePrincipal("Coder")) == false);
}
else if (roles.getName().equals("CallerPrincipal"))
{
getLog().info("CallerPrincipal is " + roles.members().nextElement());
boolean isMember = roles.isMember(new SimplePrincipal("callerScott"));
assertTrue("CallerPrincipal is callerScott", isMember);
}
}
lc.logout();
handler = new UsernamePasswordHandler("stark", "javaman".toCharArray());
lc = new LoginContext("testXMLLoginModule", handler);
lc.login();
subject = lc.getSubject();
groups = subject.getPrincipals(Group.class);
assertTrue("Principals contains stark", subject.getPrincipals().contains(new SimplePrincipal("stark")));
assertTrue("Principals contains Roles", groups.contains(new SimplePrincipal("Roles")));
assertTrue("Principals contains CallerPrincipal", groups.contains(new SimplePrincipal("CallerPrincipal")));
groupsIter = groups.iterator();
while (groupsIter.hasNext())
{
roles = (Group) groupsIter.next();
if (roles.getName().equals("Roles"))
{
assertTrue("Echo is NOT a role", roles.isMember(new SimplePrincipal("Echo")) == false);
assertTrue("Java is a role", roles.isMember(new SimplePrincipal("Java")));
assertTrue("Coder is a role", roles.isMember(new SimplePrincipal("Coder")));
}
else if (roles.getName().equals("CallerPrincipal"))
{
getLog().info("CallerPrincipal is " + roles.members().nextElement());
boolean isMember = roles.isMember(new SimplePrincipal("callerStark"));
assertTrue("CallerPrincipal is callerStark", isMember);
}
}
lc.logout();
// Test the usernames with common prefix
getLog().info("Testing similar usernames");
handler = new UsernamePasswordHandler("jdukeman", "anotherduke".toCharArray());
lc = new LoginContext("testXMLLoginModule", handler);
lc.login();
subject = lc.getSubject();
groups = subject.getPrincipals(Group.class);
assertTrue("Principals contains jdukeman", subject.getPrincipals().contains(new SimplePrincipal("jdukeman")));
assertTrue("Principals contains Roles", groups.contains(new SimplePrincipal("Roles")));
assertTrue("Principals contains CallerPrincipal", groups.contains(new SimplePrincipal("CallerPrincipal")));
groupsIter = groups.iterator();
while (groupsIter.hasNext())
{
roles = (Group) groupsIter.next();
if (roles.getName().equals("Roles"))
{
assertTrue("Role1 is NOT a role", roles.isMember(new SimplePrincipal("Role1")) == false);
assertTrue("Role2 is a role", roles.isMember(new SimplePrincipal("Role2")));
assertTrue("Role3 is a role", roles.isMember(new SimplePrincipal("Role3")));
}
else if (roles.getName().equals("CallerPrincipal"))
{
getLog().info("CallerPrincipal is " + roles.members().nextElement());
boolean isMember = roles.isMember(new SimplePrincipal("callerJdukeman"));
assertTrue("CallerPrincipal is callerJdukeman", isMember);
}
}
lc.logout();
handler = new UsernamePasswordHandler("scott2", "echoman2".toCharArray());
lc = new LoginContext("testXMLLoginModule2", handler);
lc.login();
subject = lc.getSubject();
groups = subject.getPrincipals(Group.class);
assertTrue("Principals contains scott2", subject.getPrincipals().contains(new SimplePrincipal("scott2")));