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