001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */package org.apache.geronimo.security.jaas;
017:
018: import java.util.Properties;
019:
020: import javax.security.auth.Subject;
021: import javax.security.auth.login.LoginContext;
022: import javax.security.auth.login.LoginException;
023:
024: import org.apache.geronimo.gbean.AbstractName;
025: import org.apache.geronimo.gbean.GBeanData;
026: import org.apache.geronimo.security.AbstractTest;
027: import org.apache.geronimo.security.ContextManager;
028: import org.apache.geronimo.security.RealmPrincipal;
029:
030: /**
031: * An example of how to setup non-Geronimo login modules when the
032: * <code>GeronimoLoginConfiguration</code> has been installed in the JVM.
033: *
034: * @version $Rev: 554977 $ $Date: 2007-07-10 08:32:56 -0700 (Tue, 10 Jul 2007) $
035: * @see org.apache.geronimo.security.jaas.GeronimoLoginConfiguration
036: * @see javax.security.auth.login.Configuration
037: */
038: public class LoginKerberosNonGeronimoTest extends AbstractTest {
039:
040: protected AbstractName kerberosCE;
041: protected AbstractName kerberosLM;
042:
043: /**
044: * Install the <code>GeronimoLoginConfiguration</code> but setup a non-Geronimo
045: * JAAS configuration entry named kerberos-foobar. This entry does a simple
046: * Kerberos login using the ticket cache.
047: *
048: * @throws Exception
049: */
050: public void setUp() throws Exception {
051: needLoginConfiguration = true;
052: super .setUp();
053:
054: GBeanData gbean;
055:
056: gbean = buildGBeanData("name", "KerberosLoginModule",
057: LoginModuleGBean.getGBeanInfo());
058: kerberosLM = gbean.getAbstractName();
059: gbean.setAttribute("loginModuleClass",
060: "com.sun.security.auth.module.Krb5LoginModule");
061: Properties props = new Properties();
062: props.put("debug", "true");
063: props.put("useTicketCache", "true");
064: props.put("doNotPrompt", "true");
065: gbean.setAttribute("options", props);
066: kernel
067: .loadGBean(gbean, LoginModuleGBean.class
068: .getClassLoader());
069:
070: gbean = buildGBeanData("name", "kerberosConfigurationEntry",
071: DirectConfigurationEntry.getGBeanInfo());
072: kerberosCE = gbean.getAbstractName();
073: gbean.setAttribute("applicationConfigName", "kerberos-foobar");
074: gbean.setAttribute("controlFlag",
075: LoginModuleControlFlag.REQUIRED);
076: gbean.setReferencePattern("Module", kerberosLM);
077: kernel.loadGBean(gbean, DirectConfigurationEntry.class
078: .getClassLoader());
079:
080: kernel.startGBean(loginConfiguration);
081: kernel.startGBean(kerberosLM);
082: kernel.startGBean(kerberosCE);
083: }
084:
085: /**
086: * Stop and unload the configuration entry. Restpore the JAAS configuration
087: * back to <code>ConfigFile</code>.
088: *
089: * @throws Exception
090: */
091: public void tearDown() throws Exception {
092: kernel.stopGBean(kerberosCE);
093: kernel.stopGBean(kerberosLM);
094: kernel.stopGBean(loginConfiguration);
095:
096: kernel.unloadGBean(kerberosCE);
097: kernel.unloadGBean(kerberosLM);
098: kernel.unloadGBean(loginConfiguration);
099:
100: super .tearDown();
101: }
102:
103: /**
104: * Perform a vanilla Kerberos login that has nothing to do w/ a Geronimo
105: * security realm. The subject that has been created should not have any
106: * realm principals.
107: *
108: * @throws Exception
109: */
110: public void testLogin() throws Exception {
111:
112: try {
113: LoginContext context = new LoginContext("kerberos-foobar");
114:
115: context.login();
116: Subject subject = context.getSubject();
117:
118: assertTrue("expected non-null subject", subject != null);
119: assertTrue("id of subject should be null", ContextManager
120: .getSubjectId(subject) == null);
121: assertEquals("subject should have one principal", 1,
122: subject.getPrincipals().size());
123: assertEquals("subject should have no realm principal", 0,
124: subject.getPrincipals(RealmPrincipal.class).size());
125:
126: context.logout();
127: } catch (LoginException e) {
128: e.printStackTrace();
129: // May not have kerberos
130: }
131: }
132: }
|