1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.directory.server.core.operations.bind;
21
22
23 import java.util.Hashtable;
24
25 import javax.naming.Context;
26 import javax.naming.InvalidNameException;
27 import javax.naming.NamingEnumeration;
28 import javax.naming.NamingException;
29 import javax.naming.OperationNotSupportedException;
30 import javax.naming.directory.DirContext;
31 import javax.naming.directory.InitialDirContext;
32 import javax.naming.directory.SearchControls;
33 import javax.naming.directory.SearchResult;
34
35 import org.apache.directory.server.core.DirectoryService;
36 import org.apache.directory.server.core.integ.CiRunner;
37 import org.apache.directory.server.core.jndi.CoreContextFactory;
38 import org.apache.directory.shared.ldap.constants.JndiPropertyConstants;
39 import org.apache.directory.shared.ldap.exception.LdapAuthenticationException;
40 import org.apache.directory.shared.ldap.exception.LdapNameNotFoundException;
41 import org.apache.directory.shared.ldap.message.AliasDerefMode;
42 import org.junit.Test;
43 import org.junit.runner.RunWith;
44 import static org.junit.Assert.assertEquals;
45 import static org.junit.Assert.assertFalse;
46 import static org.junit.Assert.assertNotNull;
47 import static org.junit.Assert.assertTrue;
48 import static org.junit.Assert.fail;
49
50
51
52
53
54
55
56
57 @RunWith ( CiRunner.class )
58 public class SimpleBindIT
59 {
60
61 public static DirectoryService service;
62
63
64
65
66 private NamingEnumeration<SearchResult> search( DirContext ctx, String baseDn, String filter, int scope ) throws NamingException
67 {
68 SearchControls controls = new SearchControls();
69 controls.setSearchScope( scope );
70 controls.setDerefLinkFlag( false );
71 controls.setReturningAttributes( new String[]{ "*", "+"} );
72 ctx.addToEnvironment( JndiPropertyConstants.JNDI_LDAP_DAP_DEREF_ALIASES,
73 AliasDerefMode.NEVER_DEREF_ALIASES.getJndiValue() );
74
75 NamingEnumeration<SearchResult> list = ctx.search( baseDn, filter, controls );
76 return list;
77 }
78
79
80
81
82
83
84
85 @Test
86 public void testSimpleBindUserPassword()
87 {
88
89
90 Hashtable<String, Object> env = new Hashtable<String, Object>();
91 env.put( DirectoryService.JNDI_KEY, service );
92 env.put( Context.INITIAL_CONTEXT_FACTORY, CoreContextFactory.class.getName() );
93 env.put( Context.PROVIDER_URL, "ou=system" );
94
95
96 env.put(Context.SECURITY_AUTHENTICATION, "simple");
97 env.put(Context.SECURITY_PRINCIPAL, "uid=admin,ou=system");
98 env.put(Context.SECURITY_CREDENTIALS, "secret");
99
100 DirContext ctx = null;
101
102
103 try
104 {
105 ctx = new InitialDirContext(env);
106 }
107 catch ( NamingException ne )
108 {
109 fail();
110 }
111
112 try
113 {
114 ctx.close();
115 }
116 catch ( NamingException ne )
117 {
118 fail();
119 }
120 }
121
122
123
124
125
126
127
128 @Test
129 public void testSimpleBindUserBadPassword()
130 {
131
132
133 Hashtable<String, Object> env = new Hashtable<String, Object>();
134 env.put( DirectoryService.JNDI_KEY, service );
135 env.put( Context.INITIAL_CONTEXT_FACTORY, CoreContextFactory.class.getName() );
136 env.put( Context.PROVIDER_URL, "ou=system" );
137
138
139 env.put(Context.SECURITY_AUTHENTICATION, "simple");
140 env.put(Context.SECURITY_PRINCIPAL, "uid=admin,ou=system");
141 env.put(Context.SECURITY_CREDENTIALS, "badsecret");
142
143
144 try
145 {
146 new InitialDirContext(env);
147
148
149 fail();
150 }
151 catch ( LdapAuthenticationException lae )
152 {
153 assertTrue( true );
154 }
155 catch ( NamingException ne )
156 {
157 fail();
158 }
159 }
160
161
162
163
164
165
166
167 @Test
168 public void testSimpleBindBadUserPassword()
169 {
170
171
172 Hashtable<String, Object> env = new Hashtable<String, Object>();
173 env.put( DirectoryService.JNDI_KEY, service );
174 env.put( Context.INITIAL_CONTEXT_FACTORY, CoreContextFactory.class.getName() );
175 env.put( Context.PROVIDER_URL, "ou=system" );
176
177
178 env.put(Context.SECURITY_AUTHENTICATION, "simple");
179 env.put(Context.SECURITY_PRINCIPAL, "admin");
180 env.put(Context.SECURITY_CREDENTIALS, "secret");
181
182
183 try
184 {
185 new InitialDirContext(env);
186
187
188 fail();
189 }
190 catch ( InvalidNameException ine )
191 {
192 assertEquals( "Bad DN : admin", ine.getMessage() );
193 }
194 catch ( NamingException ne )
195 {
196 fail();
197 }
198 }
199
200
201
202
203
204
205
206 @Test
207 public void testSimpleBindUnknowUserPassword()
208 {
209
210
211 Hashtable<String, Object> env = new Hashtable<String, Object>();
212 env.put( DirectoryService.JNDI_KEY, service );
213 env.put( Context.INITIAL_CONTEXT_FACTORY, CoreContextFactory.class.getName() );
214 env.put( Context.PROVIDER_URL, "ou=system" );
215
216
217 env.put(Context.SECURITY_AUTHENTICATION, "simple");
218 env.put(Context.SECURITY_PRINCIPAL, "uid=unknown,ou=system");
219 env.put(Context.SECURITY_CREDENTIALS, "secret");
220
221
222 try
223 {
224 new InitialDirContext(env);
225
226
227 fail();
228 }
229 catch ( LdapAuthenticationException lae )
230 {
231 assertEquals( "Cannot authenticate user uid=unknown,ou=system", lae.getMessage() );
232 }
233 catch ( NamingException ne )
234 {
235 fail();
236 }
237 }
238
239
240
241
242
243
244
245 @Test
246 public void testSimpleBindNoUserNoPassword()
247 {
248
249
250 Hashtable<String, Object> env = new Hashtable<String, Object>();
251 env.put( DirectoryService.JNDI_KEY, service );
252 env.put( Context.INITIAL_CONTEXT_FACTORY, CoreContextFactory.class.getName() );
253
254
255 env.put( Context.PROVIDER_URL, "" );
256
257
258 env.put(Context.SECURITY_AUTHENTICATION, "simple");
259 env.put(Context.SECURITY_PRINCIPAL, "");
260 env.put(Context.SECURITY_CREDENTIALS, "");
261
262 DirContext ctx = null;
263
264
265 try
266 {
267 ctx = new InitialDirContext(env);
268 }
269 catch ( NamingException ne )
270 {
271 fail();
272 }
273
274
275
276 try
277 {
278 NamingEnumeration<SearchResult> list = search( ctx, "", "(ObjectClass=*)", SearchControls.OBJECT_SCOPE );
279
280 assertNotNull( list );
281
282 while ( list.hasMore() )
283 {
284 SearchResult result = list.next();
285 assertNotNull( result );
286 }
287 }
288 catch ( NamingException ne )
289 {
290 fail();
291 }
292
293
294 try
295 {
296 NamingEnumeration<SearchResult> list = search( ctx, "uid=admin, ou=system", "(ObjectClass=*)", SearchControls.OBJECT_SCOPE );
297
298 assertNotNull( list );
299 assertFalse( list.hasMore() );
300 }
301 catch ( NamingException ne )
302 {
303 fail();
304 }
305
306 try
307 {
308 ctx.close();
309 }
310 catch ( NamingException ne )
311 {
312 fail();
313 }
314 }
315
316
317
318
319
320
321
322 @Test
323 public void testSimpleBindUserNoPassword()
324 {
325
326
327 Hashtable<String, Object> env = new Hashtable<String, Object>();
328 env.put( DirectoryService.JNDI_KEY, service );
329 env.put( Context.INITIAL_CONTEXT_FACTORY, CoreContextFactory.class.getName() );
330
331
332 env.put( Context.PROVIDER_URL, "" );
333
334
335 env.put(Context.SECURITY_AUTHENTICATION, "simple");
336 env.put(Context.SECURITY_PRINCIPAL, "uid=admin,ou=system");
337 env.put(Context.SECURITY_CREDENTIALS, "");
338
339
340 try
341 {
342 new InitialDirContext(env);
343 }
344 catch ( OperationNotSupportedException onse )
345 {
346 assertEquals( "Cannot Bind for DN uid=admin,ou=system", onse.getMessage() );
347 }
348 catch ( NamingException ne )
349 {
350 fail();
351 }
352 }
353
354
355
356
357
358
359
360 @Test
361 public void testSimpleBindNoUserPassword() throws Exception
362 {
363
364
365 Hashtable<String, Object> env = new Hashtable<String, Object>();
366 env.put( DirectoryService.JNDI_KEY, service );
367 env.put( Context.INITIAL_CONTEXT_FACTORY, CoreContextFactory.class.getName() );
368
369
370 env.put( Context.PROVIDER_URL, "" );
371
372
373 env.put(Context.SECURITY_AUTHENTICATION, "simple");
374 env.put(Context.SECURITY_PRINCIPAL, "");
375 env.put(Context.SECURITY_CREDENTIALS, "secret");
376
377
378 try
379 {
380 new InitialDirContext(env);
381 }
382 catch ( LdapNameNotFoundException lnnfe )
383 {
384 assertTrue( true );
385 }
386 catch ( NamingException ne )
387 {
388 fail();
389 }
390 }
391 }