KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > jaas > LDAPLoginModuleTest


1 /**
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one or more
4  * contributor license agreements. See the NOTICE file distributed with
5  * this work for additional information regarding copyright ownership.
6  * The ASF licenses this file to You under the Apache License, Version 2.0
7  * (the "License"); you may not use this file except in compliance with
8  * the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18 package org.apache.activemq.jaas;
19
20 import java.io.File JavaDoc;
21 import java.io.IOException JavaDoc;
22 import java.net.InetAddress JavaDoc;
23 import java.util.HashSet JavaDoc;
24 import java.util.Hashtable JavaDoc;
25 import java.util.Properties JavaDoc;
26 import javax.naming.Context JavaDoc;
27 import javax.naming.NameClassPair JavaDoc;
28 import javax.naming.NamingEnumeration JavaDoc;
29 import javax.naming.directory.DirContext JavaDoc;
30 import javax.naming.directory.InitialDirContext JavaDoc;
31 import javax.security.auth.callback.Callback JavaDoc;
32 import javax.security.auth.callback.CallbackHandler JavaDoc;
33 import javax.security.auth.callback.NameCallback JavaDoc;
34 import javax.security.auth.callback.PasswordCallback JavaDoc;
35 import javax.security.auth.callback.UnsupportedCallbackException JavaDoc;
36 import javax.security.auth.login.LoginContext JavaDoc;
37 import javax.security.auth.login.LoginException JavaDoc;
38
39 import junit.framework.TestCase;
40
41 import org.apache.activemq.jaas.ldap.MutableServerStartupConfiguration;
42 import org.apache.activemq.jaas.ldap.ServerContextFactory;
43 import org.apache.ldap.server.configuration.ShutdownConfiguration;
44 import org.apache.ldap.server.jndi.CoreContextFactory;
45
46
47
48 /**
49  * @version $Rev: $ $Date: $
50  */

51 public class LDAPLoginModuleTest extends TestCase {
52
53     private static final String JavaDoc PRINCIPAL = "uid=admin,ou=system";
54     private static final String JavaDoc CREDENTIALS = "secret";
55
56     public void testNothing() {
57     }
58
59     public void testRunning() throws Exception JavaDoc {
60
61         Hashtable JavaDoc env = new Hashtable JavaDoc();
62         env.put(Context.PROVIDER_URL, "ldap://localhost:9389");
63         env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
64         env.put(Context.SECURITY_AUTHENTICATION, "simple");
65         env.put(Context.SECURITY_PRINCIPAL, PRINCIPAL);
66         env.put(Context.SECURITY_CREDENTIALS, CREDENTIALS);
67         DirContext JavaDoc ctx = new InitialDirContext JavaDoc(env);
68
69         // Perform search using URL
70
// NamingEnumeration answer = ctx.search(
71
// "ldap://localhost:389/ou=system", "(uid=admin)", null);
72
HashSet JavaDoc set = new HashSet JavaDoc();
73
74         NamingEnumeration JavaDoc list = ctx.list("ou=system");
75
76         while (list.hasMore()) {
77             NameClassPair JavaDoc ncp = (NameClassPair JavaDoc) list.next();
78             set.add(ncp.getName());
79         }
80
81         assertTrue(set.contains("uid=admin"));
82         assertTrue(set.contains("ou=users"));
83         assertTrue(set.contains("ou=groups"));
84         assertTrue(set.contains("ou=configuration"));
85         assertTrue(set.contains("prefNodeName=sysPrefRoot"));
86
87     }
88
89     public void XtestLogin() throws LoginException JavaDoc {
90         LoginContext JavaDoc context = new LoginContext JavaDoc("LDAPLogin", new CallbackHandler JavaDoc() {
91             public void handle(Callback JavaDoc[] callbacks) throws IOException JavaDoc, UnsupportedCallbackException JavaDoc {
92                 for (int i = 0; i < callbacks.length; i++) {
93                     if (callbacks[i] instanceof NameCallback JavaDoc) {
94                         ((NameCallback JavaDoc) callbacks[i]).setName("first");
95                     } else if (callbacks[i] instanceof PasswordCallback JavaDoc) {
96                         ((PasswordCallback JavaDoc) callbacks[i]).setPassword("secret".toCharArray());
97                     } else {
98                         throw new UnsupportedCallbackException JavaDoc(callbacks[i]);
99                     }
100                 }
101             }
102         });
103         context.login();
104         context.logout();
105     }
106
107     public void setUp() throws Exception JavaDoc {
108         MutableServerStartupConfiguration startup = new MutableServerStartupConfiguration();
109         // put some mandatory JNDI properties here
110
startup.setWorkingDirectory(new File JavaDoc("target/ldap"));
111         startup.setAllowAnonymousAccess(true);
112         startup.setLdapPort(9389);
113         startup.setEnableNetworking(true);
114         startup.setHost(InetAddress.getByName("localhost"));
115
116         Properties JavaDoc env = new Properties JavaDoc();
117         env.putAll(startup.toJndiEnvironment());
118         env.put(Context.INITIAL_CONTEXT_FACTORY, ServerContextFactory.class.getName());
119         env.put(Context.PROVIDER_URL, "ou=system");
120         env.put(Context.SECURITY_AUTHENTICATION, "simple");
121         env.put(Context.SECURITY_PRINCIPAL, PRINCIPAL);
122         env.put(Context.SECURITY_CREDENTIALS, CREDENTIALS);
123
124         //Fire it up
125
new InitialDirContext JavaDoc(env);
126     }
127
128     public void tearDown() throws Exception JavaDoc {
129         Properties JavaDoc env = new Properties JavaDoc();
130         env.putAll(new ShutdownConfiguration().toJndiEnvironment());
131         env.put(Context.INITIAL_CONTEXT_FACTORY, CoreContextFactory.class.getName());
132         env.put(Context.PROVIDER_URL, "ou=system");
133         env.put(Context.SECURITY_AUTHENTICATION, "simple");
134         env.put(Context.SECURITY_PRINCIPAL, PRINCIPAL);
135         env.put(Context.SECURITY_CREDENTIALS, CREDENTIALS);
136
137         //Shut it down
138
new InitialDirContext JavaDoc(env);
139     }
140 }
141
Popular Tags