Fix formatting.
This commit is contained in:
@@ -1,50 +1,87 @@
|
||||
/* Copyright 2004 Acegi Technology Pty Limited
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
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.ApplicationContext;
|
||||
import org.springframework.context.support.ClassPathXmlApplicationContext;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
|
||||
/**
|
||||
* Insert comments here...
|
||||
* <br>
|
||||
* DOCUMENT ME!
|
||||
*
|
||||
* @author Ray Krueger
|
||||
* @version $Id$
|
||||
*/
|
||||
public class JaasAuthenticationProviderTests extends TestCase {
|
||||
//~ Instance fields ========================================================
|
||||
|
||||
private JaasAuthenticationProvider jaasProvider;
|
||||
private ApplicationContext context;
|
||||
private JaasAuthenticationProvider jaasProvider;
|
||||
private JaasEventCheck eventCheck;
|
||||
|
||||
protected void setUp() throws Exception {
|
||||
String resName = "/" + getClass().getName().replace('.', '/') + ".xml";
|
||||
context = new ClassPathXmlApplicationContext(resName);
|
||||
eventCheck = (JaasEventCheck) context.getBean("eventCheck");
|
||||
jaasProvider = (JaasAuthenticationProvider) context.getBean("jaasAuthenticationProvider");
|
||||
//~ Methods ================================================================
|
||||
|
||||
public void testBadPassword() {
|
||||
try {
|
||||
jaasProvider.authenticate(new UsernamePasswordAuthenticationToken(
|
||||
"user", "asdf"));
|
||||
fail("LoginException should have been thrown for the bad password");
|
||||
} catch (AuthenticationException e) {}
|
||||
|
||||
assertNotNull("Failure event not fired", eventCheck.failedEvent);
|
||||
assertNotNull("Failure event exception was null",
|
||||
eventCheck.failedEvent.getException());
|
||||
assertNull("Success event was fired", eventCheck.successEvent);
|
||||
}
|
||||
|
||||
public void testBadUser() {
|
||||
try {
|
||||
jaasProvider.authenticate(new UsernamePasswordAuthenticationToken(
|
||||
"asdf", "password"));
|
||||
fail("LoginException should have been thrown for the bad user");
|
||||
} catch (AuthenticationException e) {}
|
||||
|
||||
assertNotNull("Failure event not fired", eventCheck.failedEvent);
|
||||
assertNotNull("Failure event exception was null",
|
||||
eventCheck.failedEvent.getException());
|
||||
assertNull("Success event was fired", eventCheck.successEvent);
|
||||
}
|
||||
|
||||
public void testFull() throws Exception {
|
||||
|
||||
GrantedAuthorityImpl role1 = new GrantedAuthorityImpl("ROLE_1");
|
||||
GrantedAuthorityImpl role2 = new GrantedAuthorityImpl("ROLE_2");
|
||||
|
||||
GrantedAuthority[] defaultAuths = new GrantedAuthority[]{
|
||||
role1,
|
||||
role2,
|
||||
};
|
||||
GrantedAuthority[] defaultAuths = new GrantedAuthority[] {role1, role2,};
|
||||
|
||||
UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken("user", "password", defaultAuths);
|
||||
UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken("user",
|
||||
"password", defaultAuths);
|
||||
|
||||
assertTrue(jaasProvider.supports(UsernamePasswordAuthenticationToken.class));
|
||||
assertTrue(jaasProvider.supports(
|
||||
UsernamePasswordAuthenticationToken.class));
|
||||
|
||||
Authentication auth = jaasProvider.authenticate(token);
|
||||
|
||||
@@ -56,51 +93,41 @@ public class JaasAuthenticationProviderTests extends TestCase {
|
||||
List list = Arrays.asList(auth.getAuthorities());
|
||||
|
||||
assertTrue("GrantedAuthorities does not contain ROLE_TEST",
|
||||
list.contains(new GrantedAuthorityImpl("ROLE_TEST")));
|
||||
list.contains(new GrantedAuthorityImpl("ROLE_TEST")));
|
||||
|
||||
assertTrue("GrantedAuthorities does not contain ROLE_1", list.contains(role1));
|
||||
assertTrue("GrantedAuthorities does not contain ROLE_1",
|
||||
list.contains(role1));
|
||||
|
||||
assertTrue("GrantedAuthorities does not contain ROLE_2", list.contains(role2));
|
||||
assertTrue("GrantedAuthorities does not contain ROLE_2",
|
||||
list.contains(role2));
|
||||
|
||||
boolean foundit = false;
|
||||
|
||||
for (int i = 0; i < list.size(); i++) {
|
||||
Object obj = list.get(i);
|
||||
|
||||
if (obj instanceof JaasGrantedAuthority) {
|
||||
JaasGrantedAuthority grant = (JaasGrantedAuthority) obj;
|
||||
assertNotNull("Principal was null on JaasGrantedAuthority", grant.getPrincipal());
|
||||
assertNotNull("Principal was null on JaasGrantedAuthority",
|
||||
grant.getPrincipal());
|
||||
foundit = true;
|
||||
}
|
||||
}
|
||||
|
||||
assertTrue("Could not find a JaasGrantedAuthority", foundit);
|
||||
|
||||
assertNotNull("Success event not fired", eventCheck.successEvent);
|
||||
assertEquals("Auth objects are not equal", auth, eventCheck.successEvent.getAuthentication());
|
||||
assertEquals("Auth objects are not equal", auth,
|
||||
eventCheck.successEvent.getAuthentication());
|
||||
|
||||
assertNull("Failure event was fired", eventCheck.failedEvent);
|
||||
}
|
||||
|
||||
public void testBadUser() {
|
||||
try {
|
||||
jaasProvider.authenticate(new UsernamePasswordAuthenticationToken("asdf", "password"));
|
||||
fail("LoginException should have been thrown for the bad user");
|
||||
} catch (AuthenticationException e) {
|
||||
}
|
||||
|
||||
assertNotNull("Failure event not fired", eventCheck.failedEvent);
|
||||
assertNotNull("Failure event exception was null", eventCheck.failedEvent.getException());
|
||||
assertNull("Success event was fired", eventCheck.successEvent);
|
||||
protected void setUp() throws Exception {
|
||||
String resName = "/" + getClass().getName().replace('.', '/') + ".xml";
|
||||
context = new ClassPathXmlApplicationContext(resName);
|
||||
eventCheck = (JaasEventCheck) context.getBean("eventCheck");
|
||||
jaasProvider = (JaasAuthenticationProvider) context.getBean(
|
||||
"jaasAuthenticationProvider");
|
||||
}
|
||||
|
||||
public void testBadPassword() {
|
||||
try {
|
||||
jaasProvider.authenticate(new UsernamePasswordAuthenticationToken("user", "asdf"));
|
||||
fail("LoginException should have been thrown for the bad password");
|
||||
} catch (AuthenticationException e) {
|
||||
}
|
||||
|
||||
assertNotNull("Failure event not fired", eventCheck.failedEvent);
|
||||
assertNotNull("Failure event exception was null", eventCheck.failedEvent.getException());
|
||||
assertNull("Success event was fired", eventCheck.successEvent);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
|
||||
|
||||
<!-- $Id$ -->
|
||||
|
||||
<beans>
|
||||
|
||||
<bean id="eventCheck" class="net.sf.acegisecurity.providers.jaas.JaasEventCheck"/>
|
||||
|
||||
@@ -1,25 +1,48 @@
|
||||
/* Copyright 2004 Acegi Technology Pty Limited
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package net.sf.acegisecurity.providers.jaas;
|
||||
|
||||
import net.sf.acegisecurity.providers.jaas.event.JaasAuthenticationFailedEvent;
|
||||
import net.sf.acegisecurity.providers.jaas.event.JaasAuthenticationSuccessEvent;
|
||||
|
||||
import org.springframework.context.ApplicationEvent;
|
||||
import org.springframework.context.ApplicationListener;
|
||||
|
||||
|
||||
/**
|
||||
* DOCUMENT ME!
|
||||
*
|
||||
* @author Ray Krueger
|
||||
* @version $Id$
|
||||
*/
|
||||
public class JaasEventCheck implements ApplicationListener {
|
||||
//~ Instance fields ========================================================
|
||||
|
||||
JaasAuthenticationFailedEvent failedEvent;
|
||||
JaasAuthenticationSuccessEvent successEvent;
|
||||
|
||||
//~ Methods ================================================================
|
||||
|
||||
public void onApplicationEvent(ApplicationEvent event) {
|
||||
|
||||
if (event instanceof JaasAuthenticationFailedEvent)
|
||||
if (event instanceof JaasAuthenticationFailedEvent) {
|
||||
failedEvent = (JaasAuthenticationFailedEvent) event;
|
||||
}
|
||||
|
||||
if (event instanceof JaasAuthenticationSuccessEvent)
|
||||
if (event instanceof JaasAuthenticationSuccessEvent) {
|
||||
successEvent = (JaasAuthenticationSuccessEvent) event;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,19 +1,39 @@
|
||||
/* Copyright 2004 Acegi Technology Pty Limited
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package net.sf.acegisecurity.providers.jaas;
|
||||
|
||||
import java.security.Principal;
|
||||
|
||||
|
||||
/**
|
||||
* Insert comments here...
|
||||
* <br>
|
||||
* DOCUMENT ME!
|
||||
*
|
||||
* @author Ray Krueger
|
||||
* @version $Id$
|
||||
*/
|
||||
public class TestAuthorityGranter implements AuthorityGranter {
|
||||
//~ Methods ================================================================
|
||||
|
||||
public String grant(Principal principal) {
|
||||
String role = null;
|
||||
if (principal.getName().equals("TEST_PRINCIPAL"))
|
||||
|
||||
if (principal.getName().equals("TEST_PRINCIPAL")) {
|
||||
role = "ROLE_TEST";
|
||||
}
|
||||
|
||||
return role;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,28 +1,48 @@
|
||||
/* Copyright 2004 Acegi Technology Pty Limited
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package net.sf.acegisecurity.providers.jaas;
|
||||
|
||||
import net.sf.acegisecurity.Authentication;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
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>
|
||||
* DOCUMENT ME!
|
||||
*
|
||||
* @author Ray Krueger
|
||||
* @version $Id$
|
||||
*/
|
||||
public class TestCallbackHandler implements JaasAuthenticationCallbackHandler {
|
||||
//~ Instance fields ========================================================
|
||||
|
||||
Authentication auth;
|
||||
|
||||
//~ Methods ================================================================
|
||||
|
||||
public void setAuthentication(Authentication auth) {
|
||||
this.auth = auth;
|
||||
}
|
||||
|
||||
public void handle(Callback callback) throws IOException, UnsupportedCallbackException {
|
||||
public void handle(Callback callback)
|
||||
throws IOException, UnsupportedCallbackException {
|
||||
if (callback instanceof TextInputCallback) {
|
||||
TextInputCallback tic = (TextInputCallback) callback;
|
||||
tic.setText(auth.getPrincipal().toString());
|
||||
|
||||
@@ -1,24 +1,44 @@
|
||||
/* Copyright 2004 Acegi Technology Pty Limited
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package net.sf.acegisecurity.providers.jaas;
|
||||
|
||||
import java.security.Principal;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
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>
|
||||
* DOCUMENT ME!
|
||||
*
|
||||
* @author Ray Krueger
|
||||
* @version $Id$
|
||||
*/
|
||||
public class TestLoginModule implements LoginModule {
|
||||
//~ Instance fields ========================================================
|
||||
|
||||
private Subject subject;
|
||||
private String user;
|
||||
private String password;
|
||||
private String user;
|
||||
private Subject subject;
|
||||
|
||||
//~ Methods ================================================================
|
||||
|
||||
public boolean abort() throws LoginException {
|
||||
return true;
|
||||
@@ -28,8 +48,26 @@ public class TestLoginModule implements LoginModule {
|
||||
return true;
|
||||
}
|
||||
|
||||
public boolean login() throws LoginException {
|
||||
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();
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public boolean login() throws LoginException {
|
||||
if (!user.equals("user")) {
|
||||
throw new LoginException("Bad User");
|
||||
}
|
||||
@@ -39,38 +77,21 @@ public class TestLoginModule implements LoginModule {
|
||||
}
|
||||
|
||||
subject.getPrincipals().add(new Principal() {
|
||||
public String getName() {
|
||||
return "TEST_PRINCIPAL";
|
||||
}
|
||||
});
|
||||
public String getName() {
|
||||
return "TEST_PRINCIPAL";
|
||||
}
|
||||
});
|
||||
|
||||
subject.getPrincipals().add(new Principal() {
|
||||
public String getName() {
|
||||
return "NULL_PRINCIPAL";
|
||||
}
|
||||
});
|
||||
public String getName() {
|
||||
return "NULL_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();
|
||||
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user