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.util.test;
018:
019: import java.util.ArrayList;
020: import java.util.Arrays;
021: import java.util.Collection;
022: import java.util.Iterator;
023: import java.util.List;
024:
025: import javax.security.auth.Subject;
026:
027: import org.apache.jetspeed.JetspeedActions;
028: import org.apache.jetspeed.prefs.util.test.AbstractPrefsSupportedTestCase;
029: import org.apache.jetspeed.security.AuthenticationProvider;
030: import org.apache.jetspeed.security.AuthenticationProviderProxy;
031: import org.apache.jetspeed.security.GroupManager;
032: import org.apache.jetspeed.security.PermissionManager;
033: import org.apache.jetspeed.security.RoleManager;
034: import org.apache.jetspeed.security.SecurityProvider;
035: import org.apache.jetspeed.security.UserManager;
036: import org.apache.jetspeed.security.impl.SecurityProviderImpl;
037: import org.apache.jetspeed.security.spi.CredentialHandler;
038: import org.apache.jetspeed.security.spi.GroupSecurityHandler;
039: import org.apache.jetspeed.security.spi.RoleSecurityHandler;
040: import org.apache.jetspeed.security.spi.SecurityAccess;
041: import org.apache.jetspeed.security.spi.SecurityMappingHandler;
042: import org.apache.jetspeed.security.spi.UserSecurityHandler;
043:
044: /**
045: * @author <a href="mailto:sweaver@einnovation.com">Scott T. Weaver </a>
046: * @author <a href="mailto:dlestrat@apache.org">David Le Strat </a>
047: * @version $Id: AbstractSecurityTestcase.java 516448 2007-03-09 16:25:47Z ate $
048: *
049: */
050: public class AbstractSecurityTestcase extends
051: AbstractPrefsSupportedTestCase {
052: /** SPI Common Queries. */
053: protected SecurityAccess securityAccess;
054:
055: /** SPI Default Credential Handler. */
056: protected CredentialHandler ch;
057:
058: /** SPI Default User Security Handler. */
059: protected UserSecurityHandler ush;
060:
061: /** SPI Default Role Security Handler. */
062: protected RoleSecurityHandler rsh;
063:
064: /** SPI Default Group Security Handler. */
065: protected GroupSecurityHandler gsh;
066:
067: /** SPI Default Security Mapping Handler. */
068: protected SecurityMappingHandler smh;
069:
070: /** The security provider. */
071: protected SecurityProvider securityProvider;
072:
073: /** The user manager. */
074: protected UserManager ums;
075:
076: /** The group manager. */
077: protected GroupManager gms;
078:
079: /** The role manager. */
080: protected RoleManager rms;
081:
082: /** The permission manager. */
083: protected PermissionManager pms;
084:
085: /**
086: * @see junit.framework.TestCase#setUp()
087: */
088: protected void setUp() throws Exception {
089:
090: super .setUp();
091:
092: // SPI Security handlers.
093: securityAccess = (SecurityAccess) ctx
094: .getBean("org.apache.jetspeed.security.spi.SecurityAccess");
095: ch = (CredentialHandler) ctx
096: .getBean("org.apache.jetspeed.security.spi.CredentialHandler");
097: ush = (UserSecurityHandler) ctx
098: .getBean("org.apache.jetspeed.security.spi.UserSecurityHandler");
099: rsh = (RoleSecurityHandler) ctx
100: .getBean("org.apache.jetspeed.security.spi.RoleSecurityHandler");
101: gsh = (GroupSecurityHandler) ctx
102: .getBean("org.apache.jetspeed.security.spi.GroupSecurityHandler");
103: smh = (SecurityMappingHandler) ctx
104: .getBean("org.apache.jetspeed.security.spi.SecurityMappingHandler");
105:
106: // Security Providers.
107: AuthenticationProvider atnProvider = (AuthenticationProvider) ctx
108: .getBean("org.apache.jetspeed.security.AuthenticationProvider");
109: List atnProviders = new ArrayList();
110: atnProviders.add(atnProvider);
111:
112: AuthenticationProviderProxy atnProviderProxy = (AuthenticationProviderProxy) ctx
113: .getBean("org.apache.jetspeed.security.AuthenticationProviderProxy");
114: securityProvider = new SecurityProviderImpl(atnProviderProxy,
115: rsh, gsh, smh);
116:
117: securityProvider = (SecurityProvider) ctx
118: .getBean("org.apache.jetspeed.security.SecurityProvider");
119:
120: ums = (UserManager) ctx
121: .getBean("org.apache.jetspeed.security.UserManager");
122: gms = (GroupManager) ctx
123: .getBean("org.apache.jetspeed.security.GroupManager");
124: rms = (RoleManager) ctx
125: .getBean("org.apache.jetspeed.security.RoleManager");
126:
127: // Authorization.
128: pms = (PermissionManager) ctx
129: .getBean("org.apache.jetspeed.security.PermissionManager");
130:
131: new JetspeedActions(new String[] { "secure" }, new String[] {});
132: }
133:
134: /**
135: * Returns subject's principals of type claz
136: *
137: * @param subject
138: * @param claz
139: * @return Returns subject's principals of type claz
140: */
141: protected Collection getPrincipals(Subject subject, Class claz) {
142: List principals = new ArrayList();
143: for (Iterator iter = subject.getPrincipals().iterator(); iter
144: .hasNext();) {
145: Object element = iter.next();
146: if (claz.isInstance(element))
147: principals.add(element);
148:
149: }
150: return principals;
151: }
152:
153: protected String[] getConfigurations() {
154: String[] confs = super .getConfigurations();
155: List confList = new ArrayList(Arrays.asList(confs));
156: confList.add("security-atn.xml");
157: confList.add("security-atz.xml");
158: confList.add("security-managers.xml");
159: confList.add("security-providers.xml");
160: confList.add("security-spi.xml");
161: confList.add("security-spi-atn.xml");
162: confList.add("security-spi-atz.xml");
163: return (String[]) confList.toArray(new String[1]);
164: }
165:
166: }
|