Lines 19-35
Link Here
|
19 |
import java.io.IOException; |
19 |
import java.io.IOException; |
20 |
import java.lang.reflect.InvocationTargetException; |
20 |
import java.lang.reflect.InvocationTargetException; |
21 |
import java.lang.reflect.Method; |
21 |
import java.lang.reflect.Method; |
|
|
22 |
import java.security.Principal; |
23 |
import java.util.Arrays; |
24 |
import java.util.HashSet; |
25 |
import java.util.Set; |
22 |
|
26 |
|
|
|
27 |
import javax.security.auth.Subject; |
23 |
import javax.security.auth.callback.Callback; |
28 |
import javax.security.auth.callback.Callback; |
24 |
import javax.security.auth.callback.CallbackHandler; |
29 |
import javax.security.auth.callback.CallbackHandler; |
25 |
import javax.security.auth.callback.UnsupportedCallbackException; |
30 |
import javax.security.auth.callback.UnsupportedCallbackException; |
|
|
31 |
import javax.security.auth.message.callback.CallerPrincipalCallback; |
32 |
import javax.security.auth.message.callback.GroupPrincipalCallback; |
33 |
import javax.security.auth.message.callback.PasswordValidationCallback; |
26 |
import javax.servlet.http.HttpServletResponse; |
34 |
import javax.servlet.http.HttpServletResponse; |
27 |
|
35 |
|
28 |
import org.junit.Assert; |
36 |
import org.apache.catalina.Contained; |
29 |
import org.junit.Test; |
37 |
import org.apache.catalina.Container; |
30 |
|
|
|
31 |
import org.apache.catalina.authenticator.jaspic.CallbackHandlerImpl; |
38 |
import org.apache.catalina.authenticator.jaspic.CallbackHandlerImpl; |
32 |
import org.apache.catalina.connector.Request; |
39 |
import org.apache.catalina.connector.Request; |
|
|
40 |
import org.apache.catalina.core.ContainerBase; |
41 |
import org.apache.catalina.realm.GenericPrincipal; |
42 |
import org.apache.catalina.realm.RealmBase; |
43 |
import org.junit.Assert; |
44 |
import org.junit.Test; |
33 |
|
45 |
|
34 |
public class TestJaspicCallbackHandlerInAuthenticator { |
46 |
public class TestJaspicCallbackHandlerInAuthenticator { |
35 |
|
47 |
|
Lines 44-62
Link Here
|
44 |
testCallbackHandlerCreation(null, CallbackHandlerImpl.class); |
56 |
testCallbackHandlerCreation(null, CallbackHandlerImpl.class); |
45 |
} |
57 |
} |
46 |
|
58 |
|
|
|
59 |
@Test |
60 |
public void testCallerPrincipalCallback() throws Exception { |
61 |
CallbackHandler callbackHandler = createCallbackHandler(null); |
62 |
Subject clientSubject = new Subject(); |
63 |
CallerPrincipalCallback cpc1 = new CallerPrincipalCallback(clientSubject, "name1"); |
64 |
callbackHandler.handle(new Callback[] { cpc1 }); |
65 |
CallerPrincipalCallback cpc2 = new CallerPrincipalCallback(clientSubject, new Principal() { |
66 |
@Override |
67 |
public String getName() { |
68 |
return "name2"; |
69 |
} |
70 |
}); |
71 |
callbackHandler.handle(new Callback[] { cpc2 }); |
72 |
Set<Object> credentials = clientSubject.getPrivateCredentials(); |
73 |
Assert.assertTrue(credentials.size() == 2); |
74 |
Set<String> names = new HashSet<String>(Arrays.asList(new String[] { "name1", "name2" })); |
75 |
for (Object o : credentials) |
76 |
names.remove(((GenericPrincipal) o).getName()); |
77 |
Assert.assertTrue(names.isEmpty()); |
78 |
} |
79 |
|
80 |
@Test |
81 |
public void testGroupPrincipalCallback() throws Exception { |
82 |
CallbackHandler callbackHandler = createCallbackHandler(null); |
83 |
Subject clientSubject = new Subject(); |
84 |
CallerPrincipalCallback cpc = new CallerPrincipalCallback(clientSubject, "name"); |
85 |
GroupPrincipalCallback gpc = new GroupPrincipalCallback(clientSubject, |
86 |
new String[] { "group1", "group2" }); |
87 |
callbackHandler.handle(new Callback[] { cpc, gpc }); |
88 |
Set<Object> credentials = clientSubject.getPrivateCredentials(); |
89 |
Assert.assertTrue(credentials.size() == 1); |
90 |
GenericPrincipal gp = (GenericPrincipal) credentials.iterator().next(); |
91 |
Assert.assertEquals("name", gp.getName()); |
92 |
Assert.assertTrue(gp.hasRole("group1")); |
93 |
Assert.assertTrue(gp.hasRole("group2")); |
94 |
} |
95 |
|
96 |
@Test |
97 |
public void testPasswordValidationCallback() throws Exception { |
98 |
CallbackHandler callbackHandler = createCallbackHandler(null); |
99 |
Container container = new TestContainer(); |
100 |
container.setRealm(new TestRealm()); |
101 |
((Contained) callbackHandler).setContainer(container); |
102 |
Subject clientSubject = new Subject(); |
103 |
PasswordValidationCallback pvc1 = new PasswordValidationCallback(clientSubject, "name1", |
104 |
"password".toCharArray()); |
105 |
callbackHandler.handle(new Callback[] { pvc1 }); |
106 |
PasswordValidationCallback pvc2 = new PasswordValidationCallback(clientSubject, "name2", |
107 |
"invalid".toCharArray()); |
108 |
callbackHandler.handle(new Callback[] { pvc2 }); |
109 |
Set<Object> credentials = clientSubject.getPrivateCredentials(); |
110 |
Assert.assertTrue(credentials.size() == 1); |
111 |
GenericPrincipal gp = (GenericPrincipal) credentials.iterator().next(); |
112 |
Assert.assertEquals("name1", gp.getName()); |
113 |
} |
47 |
|
114 |
|
48 |
private void testCallbackHandlerCreation(String callbackHandlerImplClassName, |
115 |
private void testCallbackHandlerCreation(String callbackHandlerImplClassName, |
49 |
Class<?> callbackHandlerImplClass) |
116 |
Class<?> callbackHandlerImplClass) throws NoSuchMethodException, SecurityException, |
|
|
117 |
IllegalAccessException, IllegalArgumentException, InvocationTargetException { |
118 |
CallbackHandler callbackHandler = createCallbackHandler(callbackHandlerImplClassName); |
119 |
Assert.assertTrue(callbackHandlerImplClass.isInstance(callbackHandler)); |
120 |
} |
121 |
|
122 |
private CallbackHandler createCallbackHandler(String callbackHandlerImplClassName) |
50 |
throws NoSuchMethodException, SecurityException, IllegalAccessException, |
123 |
throws NoSuchMethodException, SecurityException, IllegalAccessException, |
51 |
IllegalArgumentException, InvocationTargetException { |
124 |
IllegalArgumentException, InvocationTargetException { |
52 |
TestAuthenticator authenticator = new TestAuthenticator(); |
125 |
TestAuthenticator authenticator = new TestAuthenticator(); |
53 |
authenticator.setJaspicCallbackHandlerClass(callbackHandlerImplClassName); |
126 |
if (callbackHandlerImplClassName != null) |
54 |
Method createCallbackHandlerMethod = |
127 |
authenticator.setJaspicCallbackHandlerClass(callbackHandlerImplClassName); |
55 |
AuthenticatorBase.class.getDeclaredMethod("createCallbackHandler"); |
128 |
Method createCallbackHandlerMethod = AuthenticatorBase.class |
|
|
129 |
.getDeclaredMethod("createCallbackHandler"); |
56 |
createCallbackHandlerMethod.setAccessible(true); |
130 |
createCallbackHandlerMethod.setAccessible(true); |
57 |
CallbackHandler callbackHandler = |
131 |
return (CallbackHandler) createCallbackHandlerMethod.invoke(authenticator); |
58 |
(CallbackHandler) createCallbackHandlerMethod.invoke(authenticator); |
|
|
59 |
Assert.assertTrue(callbackHandlerImplClass.isInstance(callbackHandler)); |
60 |
} |
132 |
} |
61 |
|
133 |
|
62 |
private static class TestAuthenticator extends AuthenticatorBase { |
134 |
private static class TestAuthenticator extends AuthenticatorBase { |
Lines 71-77
Link Here
|
71 |
protected String getAuthMethod() { |
143 |
protected String getAuthMethod() { |
72 |
return null; |
144 |
return null; |
73 |
} |
145 |
} |
|
|
146 |
} |
74 |
|
147 |
|
|
|
148 |
private static class TestContainer extends ContainerBase { |
149 |
|
150 |
@Override |
151 |
protected String getObjectNameKeyProperties() { |
152 |
return null; |
153 |
} |
154 |
} |
155 |
|
156 |
private static class TestRealm extends RealmBase { |
157 |
|
158 |
@Override |
159 |
public Principal authenticate(String username, String password) { |
160 |
if (getPassword(username).equals(password)) |
161 |
return getPrincipal(username); |
162 |
return null; |
163 |
} |
164 |
|
165 |
@Override |
166 |
protected String getPassword(String username) { |
167 |
return "password"; |
168 |
} |
169 |
|
170 |
@Override |
171 |
protected Principal getPrincipal(String username) { |
172 |
return new GenericPrincipal(username, null, null); |
173 |
} |
75 |
} |
174 |
} |
76 |
} |
175 |
} |
77 |
|
176 |
|
Lines 81-87
Link Here
|
81 |
// Default constructor required by reflection |
180 |
// Default constructor required by reflection |
82 |
} |
181 |
} |
83 |
|
182 |
|
84 |
|
|
|
85 |
@Override |
183 |
@Override |
86 |
public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException { |
184 |
public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException { |
87 |
// don't have to do anything; needed only for instantiation |
185 |
// don't have to do anything; needed only for instantiation |