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: */
017: package org.apache.jetspeed.security.spi.ldap;
018:
019: import java.util.Random;
020:
021: import javax.naming.NamingException;
022:
023: import org.apache.commons.logging.Log;
024: import org.apache.commons.logging.LogFactory;
025: import org.apache.jetspeed.components.test.AbstractSpringTestCase;
026: import org.apache.jetspeed.security.spi.CredentialHandler;
027: import org.apache.jetspeed.security.spi.GroupSecurityHandler;
028: import org.apache.jetspeed.security.spi.RoleSecurityHandler;
029: import org.apache.jetspeed.security.spi.SecurityMappingHandler;
030: import org.apache.jetspeed.security.spi.UserSecurityHandler;
031: import org.apache.jetspeed.security.spi.impl.LdapCredentialHandler;
032: import org.apache.jetspeed.security.spi.impl.LdapGroupSecurityHandler;
033: import org.apache.jetspeed.security.spi.impl.LdapRoleSecurityHandler;
034: import org.apache.jetspeed.security.spi.impl.LdapSecurityMappingHandler;
035: import org.apache.jetspeed.security.spi.impl.LdapUserSecurityHandler;
036: import org.apache.jetspeed.security.spi.impl.ldap.InitLdapSchema;
037: import org.apache.jetspeed.security.spi.impl.ldap.LdapBindingConfig;
038: import org.apache.jetspeed.security.spi.impl.ldap.LdapGroupDaoImpl;
039: import org.apache.jetspeed.security.spi.impl.ldap.LdapMemberShipDaoImpl;
040: import org.apache.jetspeed.security.spi.impl.ldap.LdapMembershipDao;
041: import org.apache.jetspeed.security.spi.impl.ldap.LdapPrincipalDao;
042: import org.apache.jetspeed.security.spi.impl.ldap.LdapRoleDaoImpl;
043: import org.apache.jetspeed.security.spi.impl.ldap.LdapUserCredentialDao;
044: import org.apache.jetspeed.security.spi.impl.ldap.LdapUserCredentialDaoImpl;
045: import org.apache.jetspeed.security.spi.impl.ldap.LdapUserPrincipalDao;
046: import org.apache.jetspeed.security.spi.impl.ldap.LdapUserPrincipalDaoImpl;
047:
048: /**
049: * <p>
050: * Abstract test case for LDAP providers.
051: * </p>
052: *
053: * @author <a href="mailto:mike.long@dataline.com">Mike Long </a>, <a href="mailto:dlestrat@apache.org">David Le Strat</a>
054: *
055: */
056: public abstract class AbstractLdapTest extends AbstractSpringTestCase {
057: /** The logger. */
058: private static final Log logger = LogFactory
059: .getLog(AbstractLdapTest.class);
060:
061: private static final String LDAP_CONFIG = "openldap/setup2";
062:
063: /** The {@link UserSecurityHandler}. */
064: UserSecurityHandler userHandler;
065:
066: /** The {@link CredentialHandler}. */
067: CredentialHandler crHandler;
068:
069: /** The {@link GroupSecurityHandler}. */
070: GroupSecurityHandler grHandler;
071:
072: /** The {@link RoleSecurityHandler}. */
073: RoleSecurityHandler roleHandler;
074:
075: /** The {@link SecurityMappingHandler}. */
076: SecurityMappingHandler secHandler;
077:
078: /** The {@link LdapUserPrincipalDao}. */
079: LdapUserPrincipalDao ldapPrincipalDao;
080:
081: /** The {@link LdapUserCredentialDao}. */
082: LdapUserCredentialDao ldapCredDao;
083:
084: /** The {@link LdapGroupDao}. */
085: LdapPrincipalDao ldapGroupDao;
086:
087: /** The {@link LdapGroupDao}. */
088: LdapPrincipalDao ldapRoleDao;
089:
090: LdapMembershipDao ldapMembershipDao;
091:
092: /** Random seed. */
093: Random rand = new Random(System.currentTimeMillis());
094:
095: /** Group uid. */
096: protected String gpUid1 = "group1";
097:
098: /** Group uid. */
099: protected String gpUid2 = "group2";
100:
101: /** Role uid. */
102: protected String roleUid1 = "role1";
103:
104: /** Role uid. */
105: protected String roleUid2 = "role2";
106:
107: /** User uid. */
108: protected String uid1 = "user1";
109:
110: /** User uid. */
111: protected String uid2 = "user2";
112:
113: /** The test password. */
114: protected String password = "fred";
115:
116: /**
117: * @see junit.framework.TestCase#setUp()
118: */
119: protected void setUp() throws Exception {
120: super .setUp();
121: LdapBindingConfig ldapConfig = (LdapBindingConfig) ctx
122: .getBean(LdapBindingConfig.class.getName());
123: InitLdapSchema ldapSchema = new InitLdapSchema(ldapConfig);
124: try {
125: // make sure standard test case schema exists
126: ldapSchema.initOu("OrgUnit1");
127: ldapSchema.initOu("People");
128: ldapSchema.initOu("Roles");
129: ldapSchema.initOu("People", "ou=OrgUnit1");
130: ldapSchema.initOu("Groups", "ou=OrgUnit1");
131: ldapSchema.initOu("Roles", "ou=OrgUnit1");
132:
133: } catch (NamingException se) {
134: logger.error("Initializing the LDAP directory failed:", se);
135: throw se;
136: }
137:
138: ldapCredDao = new LdapUserCredentialDaoImpl(ldapConfig);
139: ldapPrincipalDao = new LdapUserPrincipalDaoImpl(ldapConfig);
140:
141: userHandler = new LdapUserSecurityHandler(ldapPrincipalDao);
142: crHandler = new LdapCredentialHandler(ldapCredDao);
143: LdapDataHelper.setUserSecurityHandler(userHandler);
144: LdapDataHelper.setCredentialHandler(crHandler);
145:
146: ldapGroupDao = new LdapGroupDaoImpl(ldapConfig);
147: ldapRoleDao = new LdapRoleDaoImpl(ldapConfig);
148: ldapMembershipDao = new LdapMemberShipDaoImpl(ldapConfig);
149: grHandler = new LdapGroupSecurityHandler(ldapGroupDao);
150: roleHandler = new LdapRoleSecurityHandler(ldapRoleDao);
151: LdapDataHelper.setGroupSecurityHandler(grHandler);
152: LdapDataHelper.setRoleSecurityHandler(roleHandler);
153:
154: secHandler = new LdapSecurityMappingHandler(ldapPrincipalDao,
155: ldapGroupDao, ldapRoleDao);
156: }
157:
158: /**
159: * @see junit.framework.TestCase#tearDown()
160: */
161: protected void tearDown() throws Exception {
162: super .tearDown();
163: }
164:
165: protected String[] getConfigurations() {
166: return new String[] { "JETSPEED-INF/directory/config/"
167: + LDAP_CONFIG + "/security-spi-ldap.xml" };
168: }
169: }
|