001: /**
002: * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE, version 2.1, dated February 1999.
003: *
004: * This program is free software; you can redistribute it and/or modify
005: * it under the terms of the latest version of the GNU Lesser General
006: * Public License as published by the Free Software Foundation;
007: *
008: * This program is distributed in the hope that it will be useful,
009: * but WITHOUT ANY WARRANTY; without even the implied warranty of
010: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
011: * GNU Lesser General Public License for more details.
012: *
013: * You should have received a copy of the GNU Lesser General Public License
014: * along with this program (LICENSE.txt); if not, write to the Free Software
015: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
016: */package org.jamwiki.ldap;
017:
018: import java.util.Hashtable;
019: import javax.naming.Context;
020: import javax.naming.NamingEnumeration;
021: import javax.naming.directory.Attributes;
022: import javax.naming.directory.BasicAttribute;
023: import javax.naming.directory.BasicAttributes;
024: import javax.naming.directory.InitialDirContext;
025: import javax.naming.directory.SearchResult;
026: import org.apache.commons.lang.StringUtils;
027: import org.jamwiki.Environment;
028: import org.jamwiki.UserHandler;
029: import org.jamwiki.model.WikiUserInfo;
030: import org.jamwiki.utils.Encryption;
031: import org.jamwiki.utils.WikiLogger;
032:
033: /**
034: * Implementation of the {@link org.jamwiki.UserHandler} interface that uses
035: * an LDAP database for storing user login, password and other basic user
036: * information.
037: */
038: public class LdapUserHandler implements UserHandler {
039:
040: private static final WikiLogger logger = WikiLogger
041: .getLogger(LdapUserHandler.class.getName());
042:
043: private static String[] SEARCH_ATTRIBUTES = new String[4];
044:
045: static {
046: SEARCH_ATTRIBUTES[0] = Environment
047: .getValue(Environment.PROP_LDAP_FIELD_EMAIL);
048: SEARCH_ATTRIBUTES[1] = Environment
049: .getValue(Environment.PROP_LDAP_FIELD_FIRST_NAME);
050: SEARCH_ATTRIBUTES[2] = Environment
051: .getValue(Environment.PROP_LDAP_FIELD_LAST_NAME);
052: SEARCH_ATTRIBUTES[3] = Environment
053: .getValue(Environment.PROP_LDAP_FIELD_USERID);
054: }
055:
056: /**
057: *
058: */
059: public void addWikiUserInfo(WikiUserInfo userInfo,
060: Object transactionObject) throws Exception {
061: throw new UnsupportedOperationException();
062: }
063:
064: /**
065: *
066: */
067: public boolean authenticate(String username, String password)
068: throws Exception {
069: InitialDirContext ctx = null;
070: try {
071: username = this .fullDirectoryPath(username);
072: ctx = getContext(username, password);
073: return true;
074: } catch (Exception e) {
075: // could not authenticate, return false
076: return false;
077: } finally {
078: try {
079: ctx.close();
080: } catch (Exception e) {
081: }
082: }
083: }
084:
085: /**
086: *
087: */
088: private String fullDirectoryPath(String value) {
089: // convert single user value to full path
090: value = Environment
091: .getValue(Environment.PROP_LDAP_FIELD_USERID)
092: + "=" + value;
093: String context = Environment
094: .getValue(Environment.PROP_LDAP_CONTEXT);
095: if (!StringUtils.isBlank(context)) {
096: // add context
097: value += "," + context;
098: }
099: return value;
100: }
101:
102: /**
103: * Connect to the LDAP server and return a context.
104: *
105: * @return The LDAP context to use when retrieving user information.
106: */
107: private InitialDirContext getContext(String username,
108: String password) throws Exception {
109: // Set up the environment for creating the initial context
110: Hashtable env = new Hashtable();
111: env.put(Context.INITIAL_CONTEXT_FACTORY, Environment
112: .getValue(Environment.PROP_LDAP_FACTORY_CLASS));
113: env.put(Context.PROVIDER_URL, Environment
114: .getValue(Environment.PROP_LDAP_URL));
115: if (!StringUtils.isBlank(username)) {
116: // "simple" "DIGEST-MD5"
117: env
118: .put(
119: Context.SECURITY_AUTHENTICATION,
120: Environment
121: .getValue(Environment.PROP_LDAP_SECURITY_AUTHENTICATION));
122: // cn=login, ou=NewHires, o=JNDITutorial
123: env.put(Context.SECURITY_PRINCIPAL, username);
124: env.put(Context.SECURITY_CREDENTIALS, password);
125: }
126: InitialDirContext ctx = new InitialDirContext(env);
127: return ctx;
128: }
129:
130: /**
131: *
132: */
133: private WikiUserInfo initWikiUserInfo(NamingEnumeration answer)
134: throws Exception {
135: WikiUserInfo userInfo = new WikiUserInfo();
136: SearchResult sr = (SearchResult) answer.next();
137: Attributes attributes = sr.getAttributes();
138: userInfo
139: .setEmail((String) attributes
140: .get(
141: Environment
142: .getValue(Environment.PROP_LDAP_FIELD_EMAIL))
143: .get());
144: userInfo
145: .setFirstName((String) attributes
146: .get(
147: Environment
148: .getValue(Environment.PROP_LDAP_FIELD_FIRST_NAME))
149: .get());
150: userInfo
151: .setLastName((String) attributes
152: .get(
153: Environment
154: .getValue(Environment.PROP_LDAP_FIELD_LAST_NAME))
155: .get());
156: return userInfo;
157: }
158:
159: /**
160: *
161: */
162: public boolean isWriteable() {
163: return false;
164: }
165:
166: /**
167: *
168: */
169: public WikiUserInfo lookupWikiUserInfo(String username)
170: throws Exception {
171: InitialDirContext ctx = null;
172: try {
173: ctx = getContext(Environment
174: .getValue(Environment.PROP_LDAP_LOGIN), Encryption
175: .getEncryptedProperty(
176: Environment.PROP_LDAP_PASSWORD, null));
177: BasicAttributes matchAttrs = new BasicAttributes(true);
178: matchAttrs.put(new BasicAttribute(Environment
179: .getValue(Environment.PROP_LDAP_FIELD_USERID),
180: username));
181: NamingEnumeration answer = ctx.search(Environment
182: .getValue(Environment.PROP_LDAP_CONTEXT),
183: matchAttrs, SEARCH_ATTRIBUTES);
184: return (!answer.hasMore()) ? null : this
185: .initWikiUserInfo(answer);
186: } finally {
187: try {
188: ctx.close();
189: } catch (Exception e) {
190: }
191: }
192: }
193:
194: /**
195: *
196: */
197: public void updateWikiUserInfo(WikiUserInfo userInfo,
198: Object transactionObject) throws Exception {
199: throw new UnsupportedOperationException();
200: }
201: }
|