AbstractLoginModule implements LoginModule { @Override public boolean logout() throws LoginException { boolean success = false; Set<Object> creds = ImmutableSet.builder() .addAll(subject.getPublicCredentials(Credentials.class)) .addAll(subject.getPublicCredentials(AuthInfo.class)).build(); if (!subject.getPrincipals().isEmpty() && !creds.isEmpty()) { if (!subject.isReadOnly()) { subject.getPrincipals().clear(); subject.getPublicCredentials().removeAll(creds); } else { destroyCredentials(creds); } success = true; } return success; }  @Override void initialize(Subject subject, CallbackHandler callbackHandler, Map<String, ?> sharedState, Map<String, ?> options); @Override boolean logout(); @Override boolean abort(); static final String SHARED_KEY_CREDENTIALS; static final String SHARED_KEY_LOGIN_NAME; static final String SHARED_KEY_ATTRIBUTES; static final String SHARED_KEY_PRE_AUTH_LOGIN; }
@Test public void testLogout() throws Exception { AbstractLoginModule loginModule = initLoginModule(TestCredentials.class, ImmutableMap.of()); assertFalse(loginModule.logout()); }
@Test public void testLogoutCPIgnored() throws LoginException { AbstractLoginModule loginModule = initLoginModule(new Subject()); assertFalse(loginModule.logout(null, null)); }
@Test public void testLogoutCPDestroyable() throws Exception { Credentials creds = mock(TestCredentials.class, withSettings().extraInterfaces(Destroyable.class)); Credentials foreign1 = new GuestCredentials(); Credentials foreign2 = new TokenCredentials("token"); Subject subject = new Subject(true, ImmutableSet.<Principal>of(new PrincipalImpl("pName")), ImmutableSet.of(creds, foreign1, foreign2), ImmutableSet.of()); AbstractLoginModule loginModule = initLoginModule(subject); assertTrue(loginModule.logout(ImmutableSet.of(creds), Collections.emptySet())); verify(((Destroyable) creds), times(1)).destroy(); }
@Test(expected = LoginException.class) public void testLogoutCPDestroyFails() throws Exception { Credentials creds = mock(TestCredentials.class, withSettings().extraInterfaces(Destroyable.class)); doThrow(new DestroyFailedException()).when((Destroyable)creds).destroy(); Subject subject = new Subject(true, ImmutableSet.of(), ImmutableSet.of(creds), ImmutableSet.of()); AbstractLoginModule loginModule = initLoginModule(subject); loginModule.logout(ImmutableSet.of(creds), null); }
@Test public void testLogoutCPNotDestroyable() throws LoginException { Credentials creds = new TestCredentials(); Subject subject = new Subject(true, ImmutableSet.of(), ImmutableSet.of(creds), ImmutableSet.of()); TestLoginModule loginModule = (TestLoginModule) initLoginModule(subject); assertTrue(loginModule.logout(null, Collections.emptySet())); }