SecurityContext sc = SecurityContextFactory.createSecurityContext("test");
SecurityContextAssociation.setSecurityContext(sc);
//Successful Login
SecurityContextAssociation.setSecurityContext(sc);
UsernamePasswordHandler handler = new UsernamePasswordHandler("jduke", "jduke");
LoginContext lc = new LoginContext("testAbortWithNoRestore", handler);
lc.login();
Subject subject = lc.getSubject();
assertNotNull("Subject is not null", subject);
SecurityContext currentSC = SecurityContextAssociation.getSecurityContext();
assertNotNull("Current Security Context is not null", currentSC);
this.verifySubjectInfo(currentSC);
//Failed Login - calls abort on the login modules
handler = new UsernamePasswordHandler("BAD_USER", "BAD_PASSWORD");
lc = new LoginContext("testAbortWithNoRestore", handler);
try
{
lc.login();
fail("Should have failed");
}
catch(LoginException le)
{
//pass
}
//Ensure that the failed login context does not return a subject
subject = lc.getSubject();
assertNull("Subject is null", subject);
//We have to ensure that the first successful authentication has not been removed from the stack
currentSC = SecurityContextAssociation.getSecurityContext();
assertNotNull("Current Security Context is not null", currentSC);
this.verifySubjectInfo(currentSC);
//Let us go through some logout cycles
handler = new UsernamePasswordHandler("jduke", "jduke");
lc = new LoginContext("testAbortWithNoRestore", handler);
lc.login();
subject = lc.getSubject();
assertNotNull("Subject is not null", subject);
currentSC = SecurityContextAssociation.getSecurityContext();
assertNotNull("Current Security Context is not null", currentSC);
this.verifySubjectInfo(currentSC);
lc.logout();
assertNull("Current Security Context is null", SecurityContextAssociation.getSecurityContext());
subject = lc.getSubject();
assertEquals("Subject from login context has no principals", 0, subject.getPrincipals().size());
sc = SecurityContextFactory.createSecurityContext("test");
SecurityContextAssociation.setSecurityContext(sc);
//Failed Login - calls abort on the login modules
handler = new UsernamePasswordHandler("BAD_USER", "BAD_PASSWORD");
lc = new LoginContext("testAbortWithNoRestore", handler);
try
{
lc.login();
fail("Should have failed");