001: /* Copyright 2004, 2005, 2006 Acegi Technology Pty Limited
002: *
003: * Licensed under the Apache License, Version 2.0 (the "License");
004: * you may not use this file except in compliance with the License.
005: * You may obtain a copy of the License at
006: *
007: * http://www.apache.org/licenses/LICENSE-2.0
008: *
009: * Unless required by applicable law or agreed to in writing, software
010: * distributed under the License is distributed on an "AS IS" BASIS,
011: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012: * See the License for the specific language governing permissions and
013: * limitations under the License.
014: */
015:
016: package org.acegisecurity.providers.jaas;
017:
018: import junit.framework.TestCase;
019:
020: import org.acegisecurity.*;
021:
022: import org.acegisecurity.context.HttpSessionContextIntegrationFilter;
023: import org.acegisecurity.context.SecurityContextImpl;
024:
025: import org.acegisecurity.providers.TestingAuthenticationToken;
026: import org.acegisecurity.providers.UsernamePasswordAuthenticationToken;
027:
028: import org.acegisecurity.ui.session.HttpSessionDestroyedEvent;
029:
030: import org.springframework.context.ApplicationContext;
031: import org.springframework.context.support.ClassPathXmlApplicationContext;
032:
033: import org.springframework.mock.web.MockHttpSession;
034:
035: import java.net.URL;
036:
037: import java.security.Security;
038:
039: import java.util.Arrays;
040: import java.util.List;
041:
042: import javax.security.auth.login.LoginContext;
043: import javax.security.auth.login.LoginException;
044:
045: /**
046: * Tests for the JaasAuthenticationProvider
047: *
048: * @author Ray Krueger
049: * @version $Id: JaasAuthenticationProviderTests.java 1496 2006-05-23 13:38:33Z benalex $
050: */
051: public class JaasAuthenticationProviderTests extends TestCase {
052: //~ Instance fields ================================================================================================
053:
054: private ApplicationContext context;
055: private JaasAuthenticationProvider jaasProvider;
056: private JaasEventCheck eventCheck;
057:
058: //~ Methods ========================================================================================================
059:
060: protected void setUp() throws Exception {
061: String resName = "/" + getClass().getName().replace('.', '/')
062: + ".xml";
063: context = new ClassPathXmlApplicationContext(resName);
064: eventCheck = (JaasEventCheck) context.getBean("eventCheck");
065: jaasProvider = (JaasAuthenticationProvider) context
066: .getBean("jaasAuthenticationProvider");
067: }
068:
069: public void testBadPassword() {
070: try {
071: jaasProvider
072: .authenticate(new UsernamePasswordAuthenticationToken(
073: "user", "asdf"));
074: fail("LoginException should have been thrown for the bad password");
075: } catch (AuthenticationException e) {
076: }
077:
078: assertNotNull("Failure event not fired", eventCheck.failedEvent);
079: assertNotNull("Failure event exception was null",
080: eventCheck.failedEvent.getException());
081: assertNull("Success event was fired", eventCheck.successEvent);
082: }
083:
084: public void testBadUser() {
085: try {
086: jaasProvider
087: .authenticate(new UsernamePasswordAuthenticationToken(
088: "asdf", "password"));
089: fail("LoginException should have been thrown for the bad user");
090: } catch (AuthenticationException e) {
091: }
092:
093: assertNotNull("Failure event not fired", eventCheck.failedEvent);
094: assertNotNull("Failure event exception was null",
095: eventCheck.failedEvent.getException());
096: assertNull("Success event was fired", eventCheck.successEvent);
097: }
098:
099: public void testConfigurationLoop() throws Exception {
100: String resName = "/" + getClass().getName().replace('.', '/')
101: + ".conf";
102: URL url = getClass().getResource(resName);
103:
104: Security.setProperty("login.config.url.1", url.toString());
105:
106: setUp();
107: testFull();
108: }
109:
110: public void testDetectsMissingLoginConfig() throws Exception {
111: JaasAuthenticationProvider myJaasProvider = new JaasAuthenticationProvider();
112: myJaasProvider.setApplicationContext(context);
113: myJaasProvider.setAuthorityGranters(jaasProvider
114: .getAuthorityGranters());
115: myJaasProvider.setCallbackHandlers(jaasProvider
116: .getCallbackHandlers());
117: myJaasProvider.setLoginContextName(jaasProvider
118: .getLoginContextName());
119:
120: try {
121: myJaasProvider.afterPropertiesSet();
122: fail("Should have thrown ApplicationContextException");
123: } catch (IllegalArgumentException expected) {
124: assertTrue(expected.getMessage().startsWith(
125: "loginConfig must be set on"));
126: }
127: }
128:
129: public void testDetectsMissingLoginContextName() throws Exception {
130: JaasAuthenticationProvider myJaasProvider = new JaasAuthenticationProvider();
131: myJaasProvider.setApplicationContext(context);
132: myJaasProvider.setAuthorityGranters(jaasProvider
133: .getAuthorityGranters());
134: myJaasProvider.setCallbackHandlers(jaasProvider
135: .getCallbackHandlers());
136: myJaasProvider.setLoginConfig(jaasProvider.getLoginConfig());
137: myJaasProvider.setLoginContextName(null);
138:
139: try {
140: myJaasProvider.afterPropertiesSet();
141: fail("Should have thrown IllegalArgumentException");
142: } catch (IllegalArgumentException expected) {
143: assertTrue(expected.getMessage().startsWith(
144: "loginContextName must be set on"));
145: }
146:
147: myJaasProvider.setLoginContextName("");
148:
149: try {
150: myJaasProvider.afterPropertiesSet();
151: fail("Should have thrown IllegalArgumentException");
152: } catch (IllegalArgumentException expected) {
153: assertTrue(expected.getMessage().startsWith(
154: "loginContextName must be set on"));
155: }
156: }
157:
158: public void testFull() throws Exception {
159: GrantedAuthorityImpl role1 = new GrantedAuthorityImpl("ROLE_1");
160: GrantedAuthorityImpl role2 = new GrantedAuthorityImpl("ROLE_2");
161:
162: GrantedAuthority[] defaultAuths = new GrantedAuthority[] {
163: role1, role2, };
164:
165: UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(
166: "user", "password", defaultAuths);
167:
168: assertTrue(jaasProvider
169: .supports(UsernamePasswordAuthenticationToken.class));
170:
171: Authentication auth = jaasProvider.authenticate(token);
172:
173: assertNotNull(jaasProvider.getAuthorityGranters());
174: assertNotNull(jaasProvider.getCallbackHandlers());
175: assertNotNull(jaasProvider.getLoginConfig());
176: assertNotNull(jaasProvider.getLoginContextName());
177:
178: List list = Arrays.asList(auth.getAuthorities());
179:
180: assertTrue("GrantedAuthorities should contain ROLE_TEST1", list
181: .contains(new GrantedAuthorityImpl("ROLE_TEST1")));
182:
183: assertTrue("GrantedAuthorities should contain ROLE_TEST2", list
184: .contains(new GrantedAuthorityImpl("ROLE_TEST2")));
185:
186: assertTrue("GrantedAuthorities should contain ROLE_1", list
187: .contains(role1));
188:
189: assertTrue("GrantedAuthorities should contain ROLE_2", list
190: .contains(role2));
191:
192: boolean foundit = false;
193:
194: for (int i = 0; i < list.size(); i++) {
195: Object obj = list.get(i);
196:
197: if (obj instanceof JaasGrantedAuthority) {
198: JaasGrantedAuthority grant = (JaasGrantedAuthority) obj;
199: assertNotNull(
200: "Principal was null on JaasGrantedAuthority",
201: grant.getPrincipal());
202: foundit = true;
203: }
204: }
205:
206: assertTrue("Could not find a JaasGrantedAuthority", foundit);
207:
208: assertNotNull("Success event not fired",
209: eventCheck.successEvent);
210: assertEquals("Auth objects are not equal", auth,
211: eventCheck.successEvent.getAuthentication());
212:
213: assertNull("Failure event was fired", eventCheck.failedEvent);
214: }
215:
216: public void testGetApplicationContext() throws Exception {
217: assertNotNull(jaasProvider.getApplicationContext());
218: }
219:
220: public void testLoginExceptionResolver() {
221: assertNotNull(jaasProvider.getLoginExceptionResolver());
222: jaasProvider
223: .setLoginExceptionResolver(new LoginExceptionResolver() {
224: public AcegiSecurityException resolveException(
225: LoginException e) {
226: return new LockedException(
227: "This is just a test!");
228: }
229: });
230:
231: try {
232: jaasProvider
233: .authenticate(new UsernamePasswordAuthenticationToken(
234: "user", "password"));
235: } catch (LockedException e) {
236: } catch (Exception e) {
237: fail("LockedException should have been thrown and caught");
238: }
239: }
240:
241: public void testLogout() throws Exception {
242: MockLoginContext loginContext = new MockLoginContext(
243: jaasProvider.getLoginContextName());
244:
245: JaasAuthenticationToken token = new JaasAuthenticationToken(
246: null, null, loginContext);
247:
248: SecurityContextImpl context = new SecurityContextImpl();
249: context.setAuthentication(token);
250:
251: MockHttpSession mockSession = new MockHttpSession();
252: mockSession
253: .setAttribute(
254: HttpSessionContextIntegrationFilter.ACEGI_SECURITY_CONTEXT_KEY,
255: context);
256:
257: jaasProvider.onApplicationEvent(new HttpSessionDestroyedEvent(
258: mockSession));
259:
260: assertTrue(loginContext.loggedOut);
261: }
262:
263: public void testNullDefaultAuthorities() {
264: UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(
265: "user", "password", null);
266:
267: assertTrue(jaasProvider
268: .supports(UsernamePasswordAuthenticationToken.class));
269:
270: Authentication auth = jaasProvider.authenticate(token);
271: assertTrue(
272: "Only ROLE_TEST1 and ROLE_TEST2 should have been returned",
273: auth.getAuthorities().length == 2);
274: }
275:
276: public void testUnsupportedAuthenticationObjectReturnsNull() {
277: assertNull(jaasProvider
278: .authenticate(new TestingAuthenticationToken("foo",
279: "bar", new GrantedAuthority[] {})));
280: }
281:
282: //~ Inner Classes ==================================================================================================
283:
284: private static class MockLoginContext extends LoginContext {
285: boolean loggedOut = false;
286:
287: public MockLoginContext(String loginModule)
288: throws LoginException {
289: super (loginModule);
290: }
291:
292: public void logout() throws LoginException {
293: this .loggedOut = true;
294: }
295: }
296: }
|