/**
*
* Copyright 2003-2004 The Apache Software Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.geronimo.security.jaas;
import org.apache.geronimo.gbean.AbstractName;
import org.apache.geronimo.gbean.GBeanData;
import org.apache.geronimo.security.AbstractTest;
import org.apache.geronimo.security.ContextManager;
import org.apache.geronimo.security.DomainPrincipal;
import org.apache.geronimo.security.IdentificationPrincipal;
import org.apache.geronimo.security.RealmPrincipal;
import org.apache.geronimo.security.realm.GenericSecurityRealm;
import javax.security.auth.Subject;
import javax.security.auth.login.LoginContext;
import java.io.File;
import java.util.Properties;
import java.util.Set;
/**
* @version $Rev: 384406 $ $Date: 2006-03-09 03:35:05 +0100 (Thu, 09 Mar 2006) $
*/
public class TimeoutTest extends AbstractTest {
protected AbstractName testCE;
protected AbstractName testRealm;
protected AbstractName clientLM;
protected AbstractName clientCE;
public void setUp() throws Exception {
timeoutTest = true;
needServerInfo = true;
needLoginConfiguration = true;
super.setUp();
GBeanData gbean;
// Create all the parts
gbean = buildGBeanData ("name", "PropertiesLoginModule", LoginModuleGBean.getGBeanInfo());
testCE = gbean.getAbstractName();
gbean.setAttribute("loginModuleClass", "org.apache.geronimo.security.realm.providers.PropertiesFileLoginModule");
gbean.setAttribute("serverSide", Boolean.TRUE);
Properties props = new Properties();
props.put("usersURI", new File(new File("."), "src/test-data/data/users.properties").toURI().toString());
props.put("groupsURI", new File(new File("."), "src/test-data/data/groups.properties").toURI().toString());
gbean.setAttribute("options", props);
gbean.setAttribute("loginDomainName", "PropertiesDomain");
gbean.setAttribute("wrapPrincipals", Boolean.TRUE);
kernel.loadGBean(gbean, LoginModuleGBean.class.getClassLoader());
gbean = buildGBeanData("name", "PropertiesLoginModuleUse", JaasLoginModuleUse.getGBeanInfo());
AbstractName testUseName = gbean.getAbstractName();
gbean.setAttribute("controlFlag", "REQUIRED");
gbean.setReferencePattern("LoginModule", testCE);
kernel.loadGBean(gbean, JaasLoginModuleUse.class.getClassLoader());
gbean = buildGBeanData("name", "PropertiesSecurityRealm", GenericSecurityRealm.getGBeanInfo());
testRealm = gbean.getAbstractName();
gbean.setAttribute("realmName", "properties-realm");
gbean.setReferencePattern("LoginModuleConfiguration", testUseName);
gbean.setReferencePattern("ServerInfo", serverInfo);
gbean.setReferencePattern("LoginService", loginService);
kernel.loadGBean(gbean, GenericSecurityRealm.class.getClassLoader());
gbean = buildGBeanData("name", "ClientPropertiesLoginModule", LoginModuleGBean.getGBeanInfo());
clientLM = gbean.getAbstractName();
gbean.setAttribute("loginModuleClass", "org.apache.geronimo.security.jaas.client.JaasLoginCoordinator");
gbean.setAttribute("serverSide", Boolean.FALSE);
props = new Properties();
props.put("host", "localhost");
props.put("port", "4242");
props.put("realm", "properties-realm");
gbean.setAttribute("options", props);
kernel.loadGBean(gbean, LoginModuleGBean.class.getClassLoader());
gbean = buildGBeanData("name", "ClientConfigurationEntry", DirectConfigurationEntry.getGBeanInfo());
clientCE = gbean.getAbstractName();
gbean.setAttribute("applicationConfigName", "properties-client");
gbean.setAttribute("controlFlag", LoginModuleControlFlag.REQUIRED);
gbean.setReferencePattern("Module", clientLM);
kernel.loadGBean(gbean, DirectConfigurationEntry.class.getClassLoader());
kernel.startGBean(loginConfiguration);
kernel.startGBean(clientLM);
kernel.startGBean(clientCE);
kernel.startGBean(testCE);
kernel.startGBean(testUseName);
kernel.startGBean(testRealm);
}
public void tearDown() throws Exception {
kernel.stopGBean(testRealm);
kernel.stopGBean(testCE);
kernel.stopGBean(clientCE);
kernel.stopGBean(clientLM);
kernel.stopGBean(loginConfiguration);
kernel.stopGBean(serverInfo);
kernel.unloadGBean(testCE);
kernel.unloadGBean(testRealm);
kernel.unloadGBean(clientCE);
kernel.unloadGBean(clientLM);
kernel.unloadGBean(loginConfiguration);
kernel.unloadGBean(serverInfo);
kernel.stopGBean(serverStub);
kernel.stopGBean(loginService);
kernel.unloadGBean(loginService);
kernel.unloadGBean(serverStub);
kernel.shutdown();
}
public void testNothing() {
}
public void testTimeout() throws Exception {
LoginContext context = new LoginContext("properties-client", new AbstractTest.UsernamePasswordCallback("alan", "starcraft"));
context.login();
Subject subject = context.getSubject();
assertTrue("expected non-null client subject", subject != null);
Set set = subject.getPrincipals(IdentificationPrincipal.class);
assertEquals("client subject should have one ID principal", set.size(), 1);
IdentificationPrincipal idp = (IdentificationPrincipal) set.iterator().next();
subject = ContextManager.getRegisteredSubject(idp.getId());
assertTrue("expected non-null server subject", subject != null);
assertTrue("server subject should have one remote principal", subject.getPrincipals(IdentificationPrincipal.class).size() == 1);
IdentificationPrincipal remote = (IdentificationPrincipal) subject.getPrincipals(IdentificationPrincipal.class).iterator().next();
assertTrue("server subject should be associated with remote id", ContextManager.getRegisteredSubject(remote.getId()) != null);
assertEquals("server-side subject should have seven principal", 7, subject.getPrincipals().size());
assertTrue("server subject should have two realm principal", subject.getPrincipals(RealmPrincipal.class).size() == 2);
assertTrue("server subject should have two domain principal", subject.getPrincipals(DomainPrincipal.class).size() == 2);
assertTrue("id of server subject should be non-null", ContextManager.getSubjectId(subject) != null);
Thread.sleep(3000); // wait for timeout to kick in
assertTrue("id of server subject should be non-null", ContextManager.getSubjectId(subject) != null);
Thread.sleep(7000); // wait for timeout to kick in
assertTrue("id of server subject should be null", ContextManager.getSubjectId(subject) == null);
}
}