KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > jforum > sso > LDAPAuthenticator


1 /*
2  * Copyright (c) Rafael Steil
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms,
6  * with or without modification, are permitted provided
7  * that the following conditions are met:
8  *
9  * 1) Redistributions of source code must retain the above
10  * copyright notice, this list of conditions and the
11  * following disclaimer.
12  * 2) Redistributions in binary form must reproduce the
13  * above copyright notice, this list of conditions and
14  * the following disclaimer in the documentation and/or
15  * other materials provided with the distribution.
16  * 3) Neither the name of "Rafael Steil" nor
17  * the names of its contributors may be used to endorse
18  * or promote products derived from this software without
19  * specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT
22  * HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
23  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
24  * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
25  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR
26  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
27  * THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
29  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES
30  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
32  * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
33  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
34  * IN CONTRACT, STRICT LIABILITY, OR TORT
35  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
36  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
37  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE
38  *
39  * Created on Jun 2, 2005 5:41:11 PM
40  * The JForum Project
41  * http://www.jforum.net
42  */

43 package net.jforum.sso;
44
45 import java.util.Hashtable JavaDoc;
46 import java.util.Map JavaDoc;
47
48 import javax.naming.AuthenticationException JavaDoc;
49 import javax.naming.Context JavaDoc;
50 import javax.naming.directory.Attribute JavaDoc;
51 import javax.naming.directory.DirContext JavaDoc;
52 import javax.naming.directory.InitialDirContext JavaDoc;
53
54 import net.jforum.dao.UserDAO;
55 import net.jforum.entities.User;
56 import net.jforum.util.preferences.ConfigKeys;
57 import net.jforum.util.preferences.SystemGlobals;
58
59 /**
60  * Authenticate users against a LDAP server.
61  *
62  * @author Rafael Steil
63  * @version $Id: LDAPAuthenticator.java,v 1.5 2005/10/14 00:30:31 rafaelsteil Exp $
64  */

65 public class LDAPAuthenticator implements LoginAuthenticator
66 {
67     private Hashtable JavaDoc prepareEnvironment()
68     {
69         Hashtable JavaDoc h = new Hashtable JavaDoc();
70         
71         h.put(Context.INITIAL_CONTEXT_FACTORY, SystemGlobals.getValue(ConfigKeys.LDAP_FACTORY));
72         h.put(Context.PROVIDER_URL, SystemGlobals.getValue(ConfigKeys.LDAP_SERVER_URL));
73         
74         String JavaDoc protocol = SystemGlobals.getValue(ConfigKeys.LDAP_SECURITY_PROTOCOL);
75         
76         if (protocol != null && !"".equals(protocol.trim())) {
77             h.put(Context.SECURITY_PROTOCOL, protocol);
78         }
79
80         String JavaDoc authentication = SystemGlobals.getValue(ConfigKeys.LDAP_AUTHENTICATION);
81
82         if (authentication != null && !"".equals(authentication.trim())) {
83             h.put(Context.SECURITY_AUTHENTICATION, authentication);
84         }
85         
86         return h;
87     }
88     
89     /**
90      * @see net.jforum.sso.LoginAuthenticator#validateLogin(java.lang.String, java.lang.String, java.util.Map)
91      */

92     public User validateLogin(String JavaDoc username, String JavaDoc password, Map JavaDoc extraParams) throws Exception JavaDoc
93     {
94         Hashtable JavaDoc environment = this.prepareEnvironment();
95         
96         String JavaDoc principal = SystemGlobals.getValue(ConfigKeys.LDAP_LOGIN_PREFIX)
97             + username
98             + ","
99             + SystemGlobals.getValue(ConfigKeys.LDAP_LOGIN_SUFFIX);
100         
101         environment.put(Context.SECURITY_PRINCIPAL, principal);
102         environment.put(Context.SECURITY_CREDENTIALS, password);
103         
104         DirContext JavaDoc dir = null;
105         
106         try {
107             dir = new InitialDirContext JavaDoc(environment);
108             
109             String JavaDoc lookupPrefix = SystemGlobals.getValue(ConfigKeys.LDAP_LOOKUP_PREFIX);
110             String JavaDoc lookupSuffix = SystemGlobals.getValue(ConfigKeys.LDAP_LOOKUP_SUFFIX);
111             
112             if (lookupPrefix == null || lookupPrefix.length() == 0) {
113                 lookupPrefix = SystemGlobals.getValue(ConfigKeys.LDAP_LOGIN_PREFIX);
114             }
115             
116             if (lookupSuffix == null || lookupSuffix .length() == 0) {
117                 lookupSuffix = SystemGlobals.getValue(ConfigKeys.LDAP_LOGIN_SUFFIX);
118             }
119
120             String JavaDoc lookupPrincipal = lookupPrefix + username + "," + lookupSuffix ;
121
122             Attribute JavaDoc att = dir.getAttributes(lookupPrincipal ).get(SystemGlobals.getValue(ConfigKeys.LDAP_FIELD_EMAIL));
123             
124             SSOUtils utils = new SSOUtils();
125             
126             if (!utils.userExists(username)) {
127                 String JavaDoc email = att != null ? (String JavaDoc)att.get() : "noemail";
128                 utils.register("ldap", email);
129             }
130             
131             return utils.getUser();
132         }
133         catch (AuthenticationException JavaDoc e) {
134             return null;
135         }
136         finally {
137             if (dir != null) {
138                 dir.close();
139             }
140         }
141     }
142
143     /**
144      * @see net.jforum.sso.LoginAuthenticator#setUserModel(net.jforum.dao.UserDAO)
145      */

146     public void setUserModel(UserDAO dao)
147     {
148     }
149 }
150
Popular Tags