From 0c7a07e4db1132866889a88e560c96a34a62702e Mon Sep 17 00:00:00 2001 From: Ray Krueger Date: Mon, 19 Jul 2004 00:44:01 +0000 Subject: [PATCH] Adding in JAASAuthenticationProvider tests --- .../jaas/JAASAuthenticationProviderTests.java | 70 ++++++++++++++++++ .../jaas/JAASAuthenticationProviderTests.xml | 25 +++++++ .../providers/jaas/TestAuthorityGranter.java | 19 +++++ .../providers/jaas/TestCallbackHandler.java | 34 +++++++++ .../providers/jaas/TestLoginModule.java | 73 +++++++++++++++++++ .../acegisecurity/providers/jaas/login.conf | 3 + 6 files changed, 224 insertions(+) create mode 100644 core/src/test/java/org/acegisecurity/providers/jaas/JAASAuthenticationProviderTests.java create mode 100644 core/src/test/java/org/acegisecurity/providers/jaas/JAASAuthenticationProviderTests.xml create mode 100644 core/src/test/java/org/acegisecurity/providers/jaas/TestAuthorityGranter.java create mode 100644 core/src/test/java/org/acegisecurity/providers/jaas/TestCallbackHandler.java create mode 100644 core/src/test/java/org/acegisecurity/providers/jaas/TestLoginModule.java create mode 100644 core/src/test/java/org/acegisecurity/providers/jaas/login.conf diff --git a/core/src/test/java/org/acegisecurity/providers/jaas/JAASAuthenticationProviderTests.java b/core/src/test/java/org/acegisecurity/providers/jaas/JAASAuthenticationProviderTests.java new file mode 100644 index 0000000000..92322ab0cf --- /dev/null +++ b/core/src/test/java/org/acegisecurity/providers/jaas/JAASAuthenticationProviderTests.java @@ -0,0 +1,70 @@ +package net.sf.acegisecurity.providers.jaas; + +import junit.framework.TestCase; +import net.sf.acegisecurity.Authentication; +import net.sf.acegisecurity.AuthenticationException; +import net.sf.acegisecurity.GrantedAuthority; +import net.sf.acegisecurity.GrantedAuthorityImpl; +import net.sf.acegisecurity.providers.UsernamePasswordAuthenticationToken; +import org.springframework.context.support.FileSystemXmlApplicationContext; + +import java.util.Arrays; +import java.util.List; + +/** + * Insert comments here... + *
+ * User: raykrueger@users.sourceforge.net
+ * Date: Jul 16, 2004
+ */ +public class JAASAuthenticationProviderTests extends TestCase { + + private JAASAuthenticationProvider jaasProvider; + + protected void setUp() throws Exception { + String resName = "/" + getClass().getName().replace('.', '/') + ".xml"; + FileSystemXmlApplicationContext context = new FileSystemXmlApplicationContext(getClass().getResource(resName).toString()); + jaasProvider = (JAASAuthenticationProvider) context.getBean("jaasAuthenticationProvider"); + } + + public void testFull() throws Exception { + + GrantedAuthorityImpl role1 = new GrantedAuthorityImpl("ROLE_1"); + GrantedAuthorityImpl role2 = new GrantedAuthorityImpl("ROLE_2"); + + GrantedAuthority[] defaultAuths = new GrantedAuthority[]{ + role1, + role2, + }; + + UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken("user", "password", defaultAuths); + + Authentication auth = jaasProvider.authenticate(token); + + List list = Arrays.asList(auth.getAuthorities()); + + assertTrue("GrantedAuthorities does not contain ROLE_TEST", + list.contains(new GrantedAuthorityImpl("ROLE_TEST"))); + + assertTrue("GrantedAuthorities does not contain ROLE_1", list.contains(role1)); + + assertTrue("GrantedAuthorities does not contain ROLE_2", list.contains(role2)); + } + + public void testBadUser() { + try { + jaasProvider.authenticate(new UsernamePasswordAuthenticationToken("asdf", "password")); + fail("LoginException should have been thrown for the bad user"); + } catch (AuthenticationException e) { + } + } + + public void testBadPassword() { + try { + jaasProvider.authenticate(new UsernamePasswordAuthenticationToken("user", "asdf")); + fail("LoginException should have been thrown for the bad password"); + } catch (AuthenticationException e) { + } + } + +} diff --git a/core/src/test/java/org/acegisecurity/providers/jaas/JAASAuthenticationProviderTests.xml b/core/src/test/java/org/acegisecurity/providers/jaas/JAASAuthenticationProviderTests.xml new file mode 100644 index 0000000000..feb34a9687 --- /dev/null +++ b/core/src/test/java/org/acegisecurity/providers/jaas/JAASAuthenticationProviderTests.xml @@ -0,0 +1,25 @@ + + + + + + + JAASTest + + + classpath:net/sf/acegisecurity/providers/jaas/login.conf + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/core/src/test/java/org/acegisecurity/providers/jaas/TestAuthorityGranter.java b/core/src/test/java/org/acegisecurity/providers/jaas/TestAuthorityGranter.java new file mode 100644 index 0000000000..a014cd99e3 --- /dev/null +++ b/core/src/test/java/org/acegisecurity/providers/jaas/TestAuthorityGranter.java @@ -0,0 +1,19 @@ +package net.sf.acegisecurity.providers.jaas; + +import net.sf.acegisecurity.providers.jaas.AuthorityGranter; + +import java.security.Principal; + +/** + * Insert comments here... + *
+ * User: raykrueger@users.sourceforge.net
+ * Date: Jul 16, 2004
+ */ +public class TestAuthorityGranter implements AuthorityGranter { + public String grant(Principal principal) { + if (principal.getName().equals("TEST_PRINCIPAL")) + return "ROLE_TEST"; + return null; + } +} diff --git a/core/src/test/java/org/acegisecurity/providers/jaas/TestCallbackHandler.java b/core/src/test/java/org/acegisecurity/providers/jaas/TestCallbackHandler.java new file mode 100644 index 0000000000..d831b42c2c --- /dev/null +++ b/core/src/test/java/org/acegisecurity/providers/jaas/TestCallbackHandler.java @@ -0,0 +1,34 @@ +package net.sf.acegisecurity.providers.jaas; + +import net.sf.acegisecurity.Authentication; +import net.sf.acegisecurity.providers.jaas.JAASAuthenticationCallbackHandler; + +import javax.security.auth.callback.Callback; +import javax.security.auth.callback.TextInputCallback; +import javax.security.auth.callback.UnsupportedCallbackException; +import java.io.IOException; + +/** + * Insert comments here... + *
+ * User: raykrueger@users.sourceforge.net
+ * Date: Jul 16, 2004
+ */ +public class TestCallbackHandler implements JAASAuthenticationCallbackHandler { + + Authentication auth; + + public void setAuthentication(Authentication auth) { + this.auth = auth; + } + + public void handle(Callback callback) throws IOException, UnsupportedCallbackException { + + if (auth == null) throw new RuntimeException("TEST FAILURE: setAuthentication was never called"); + + if (callback instanceof TextInputCallback) { + TextInputCallback tic = (TextInputCallback) callback; + tic.setText(getClass().getName()); + } + } +} diff --git a/core/src/test/java/org/acegisecurity/providers/jaas/TestLoginModule.java b/core/src/test/java/org/acegisecurity/providers/jaas/TestLoginModule.java new file mode 100644 index 0000000000..c187b685cf --- /dev/null +++ b/core/src/test/java/org/acegisecurity/providers/jaas/TestLoginModule.java @@ -0,0 +1,73 @@ +package net.sf.acegisecurity.providers.jaas; + +import javax.security.auth.Subject; +import javax.security.auth.callback.*; +import javax.security.auth.login.LoginException; +import javax.security.auth.spi.LoginModule; +import java.security.Principal; +import java.util.Map; + +/** + * Insert comments here... + *
+ * @author raykrueger@users.sourceforge.net
+ * Date: Jul 16, 2004
+ */ +public class TestLoginModule implements LoginModule { + + private Subject subject; + private String user; + private String password; + + public boolean abort() throws LoginException { + return true; + } + + public boolean commit() throws LoginException { + return true; + } + + public boolean login() throws LoginException { + + if (!user.equals("user")) { + throw new LoginException("Bad User"); + } + + if (!password.equals("password")) { + throw new LoginException("Bad Password"); + } + + subject.getPrincipals().add(new Principal() { + public String getName() { + return "TEST_PRINCIPAL"; + } + }); + return true; + } + + public boolean logout() throws LoginException { + return true; + } + + public void initialize(Subject subject, CallbackHandler callbackHandler, Map sharedState, Map options) { + this.subject = subject; + try { + + TextInputCallback textCallback = new TextInputCallback("prompt"); + NameCallback nameCallback = new NameCallback("prompt"); + PasswordCallback passwordCallback = new PasswordCallback("prompt", false); + + callbackHandler.handle(new Callback[]{textCallback, nameCallback, passwordCallback}); + + password = new String(passwordCallback.getPassword()); + user = nameCallback.getName(); + + if (!TestCallbackHandler.class.getName().equals(textCallback.getText())) + throw new RuntimeException("TEST FAILURE: " + textCallback.getText() + "!=" + TestCallbackHandler.class.getName()); + + } catch (Exception e) { + e.printStackTrace(); + throw new RuntimeException(e); + } + } +} diff --git a/core/src/test/java/org/acegisecurity/providers/jaas/login.conf b/core/src/test/java/org/acegisecurity/providers/jaas/login.conf new file mode 100644 index 0000000000..e8e36f32ac --- /dev/null +++ b/core/src/test/java/org/acegisecurity/providers/jaas/login.conf @@ -0,0 +1,3 @@ +JAASTest { + net.sf.acegisecurity.providers.jaas.TestLoginModule required; +}; \ No newline at end of file