mirror of
https://github.com/spring-projects/spring-security.git
synced 2025-07-08 03:32:39 +00:00
Adding in JAASAuthenticationProvider tests
This commit is contained in:
parent
1947819d73
commit
0c7a07e4db
@ -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...
|
||||||
|
* <br>
|
||||||
|
* User: raykrueger@users.sourceforge.net<br>
|
||||||
|
* Date: Jul 16, 2004<br>
|
||||||
|
*/
|
||||||
|
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) {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,25 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
|
||||||
|
|
||||||
|
<beans>
|
||||||
|
<bean id="jaasAuthenticationProvider" class="net.sf.acegisecurity.providers.jaas.JAASAuthenticationProvider">
|
||||||
|
<property name="loginContextName">
|
||||||
|
<value>JAASTest</value>
|
||||||
|
</property>
|
||||||
|
<property name="loginConfig">
|
||||||
|
<value>classpath:net/sf/acegisecurity/providers/jaas/login.conf</value>
|
||||||
|
</property>
|
||||||
|
<property name="callbackHandlers">
|
||||||
|
<list>
|
||||||
|
<bean class="net.sf.acegisecurity.providers.jaas.TestCallbackHandler"/>
|
||||||
|
<bean class="net.sf.acegisecurity.providers.jaas.JAASNameCallbackHandler"/>
|
||||||
|
<bean class="net.sf.acegisecurity.providers.jaas.JAASPasswordCallbackHandler"/>
|
||||||
|
</list>
|
||||||
|
</property>
|
||||||
|
<property name="authorityGranters">
|
||||||
|
<list>
|
||||||
|
<bean class="net.sf.acegisecurity.providers.jaas.TestAuthorityGranter"/>
|
||||||
|
</list>
|
||||||
|
</property>
|
||||||
|
</bean>
|
||||||
|
</beans>
|
@ -0,0 +1,19 @@
|
|||||||
|
package net.sf.acegisecurity.providers.jaas;
|
||||||
|
|
||||||
|
import net.sf.acegisecurity.providers.jaas.AuthorityGranter;
|
||||||
|
|
||||||
|
import java.security.Principal;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Insert comments here...
|
||||||
|
* <br>
|
||||||
|
* User: raykrueger@users.sourceforge.net<br>
|
||||||
|
* Date: Jul 16, 2004<br>
|
||||||
|
*/
|
||||||
|
public class TestAuthorityGranter implements AuthorityGranter {
|
||||||
|
public String grant(Principal principal) {
|
||||||
|
if (principal.getName().equals("TEST_PRINCIPAL"))
|
||||||
|
return "ROLE_TEST";
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
@ -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...
|
||||||
|
* <br>
|
||||||
|
* User: raykrueger@users.sourceforge.net<br>
|
||||||
|
* Date: Jul 16, 2004<br>
|
||||||
|
*/
|
||||||
|
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());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -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...
|
||||||
|
* <br>
|
||||||
|
* @author raykrueger@users.sourceforge.net<br>
|
||||||
|
* Date: Jul 16, 2004<br>
|
||||||
|
*/
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,3 @@
|
|||||||
|
JAASTest {
|
||||||
|
net.sf.acegisecurity.providers.jaas.TestLoginModule required;
|
||||||
|
};
|
Loading…
x
Reference in New Issue
Block a user