1   /**
2    * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.security.auth;
24  
25  import com.liferay.portal.NoSuchUserException;
26  import com.liferay.portal.SystemException;
27  import com.liferay.portal.kernel.util.StringPool;
28  import com.liferay.portal.kernel.util.StringUtil;
29  import com.liferay.portal.kernel.util.Validator;
30  import com.liferay.portal.model.User;
31  import com.liferay.portal.security.ldap.PortalLDAPUtil;
32  import com.liferay.portal.service.UserLocalServiceUtil;
33  import com.liferay.portal.util.PortalUtil;
34  import com.liferay.portal.util.PrefsPropsUtil;
35  import com.liferay.portal.util.PropsKeys;
36  import com.liferay.portal.util.PropsValues;
37  
38  import edu.yale.its.tp.cas.client.filter.CASFilter;
39  
40  import javax.naming.Binding;
41  import javax.naming.NamingEnumeration;
42  import javax.naming.directory.Attributes;
43  import javax.naming.directory.SearchControls;
44  import javax.naming.directory.SearchResult;
45  import javax.naming.ldap.LdapContext;
46  
47  import javax.servlet.http.HttpServletRequest;
48  import javax.servlet.http.HttpServletResponse;
49  import javax.servlet.http.HttpSession;
50  
51  import org.apache.commons.logging.Log;
52  import org.apache.commons.logging.LogFactory;
53  
54  /**
55   * <a href="CASAutoLogin.java.html"><b><i>View Source</i></b></a>
56   *
57   * @author Brian Wing Shun Chan
58   * @author Jorge Ferrer
59   *
60   */
61  public class CASAutoLogin implements AutoLogin {
62  
63      public String[] login(
64              HttpServletRequest request, HttpServletResponse response)
65          throws AutoLoginException {
66  
67          String[] credentials = null;
68  
69          try {
70              long companyId = PortalUtil.getCompanyId(request);
71  
72              if (!PrefsPropsUtil.getBoolean(
73                      companyId, PropsKeys.CAS_AUTH_ENABLED,
74                      PropsValues.CAS_AUTH_ENABLED)) {
75  
76                  return credentials;
77              }
78  
79              HttpSession session = request.getSession();
80  
81              String screenName = (String)session.getAttribute(
82                  CASFilter.CAS_FILTER_USER);
83  
84              if (Validator.isNull(screenName)) {
85                  return credentials;
86              }
87  
88              User user = null;
89  
90              try {
91                  user = UserLocalServiceUtil.getUserByScreenName(
92                      companyId, screenName);
93              }
94              catch (NoSuchUserException nsue) {
95                  if (PrefsPropsUtil.getBoolean(
96                          companyId, PropsKeys.CAS_IMPORT_FROM_LDAP,
97                          PropsValues.CAS_IMPORT_FROM_LDAP)) {
98  
99                      user = addUser(companyId, screenName);
100                 }
101                 else {
102                     throw nsue;
103                 }
104             }
105 
106             credentials = new String[3];
107 
108             credentials[0] = String.valueOf(user.getUserId());
109             credentials[1] = user.getPassword();
110             credentials[2] = Boolean.TRUE.toString();
111 
112             return credentials;
113         }
114         catch (Exception e) {
115             _log.error(e, e);
116         }
117 
118         return credentials;
119     }
120 
121     protected User addUser(long companyId, String screenName)
122         throws SystemException {
123 
124         try {
125             String baseDN = PrefsPropsUtil.getString(
126                 companyId, PropsKeys.LDAP_BASE_DN);
127 
128             LdapContext ctx = PortalLDAPUtil.getContext(companyId);
129 
130             if (ctx == null) {
131                 throw new SystemException("Failed to bind to the LDAP server");
132             }
133 
134             String filter = PrefsPropsUtil.getString(
135                 companyId, PropsKeys.LDAP_AUTH_SEARCH_FILTER);
136 
137             if (_log.isDebugEnabled()) {
138                 _log.debug("Search filter before transformation " + filter);
139             }
140 
141             filter = StringUtil.replace(
142                 filter,
143                 new String[] {
144                     "@company_id@", "@email_address@", "@screen_name@"
145                 },
146                 new String[] {
147                     String.valueOf(companyId), StringPool.BLANK, screenName
148                 });
149 
150             if (_log.isDebugEnabled()) {
151                 _log.debug("Search filter after transformation " + filter);
152             }
153 
154             SearchControls cons = new SearchControls(
155                 SearchControls.SUBTREE_SCOPE, 1, 0, null, false, false);
156 
157             NamingEnumeration<SearchResult> enu = ctx.search(
158                 baseDN, filter, cons);
159 
160             if (enu.hasMoreElements()) {
161                 if (_log.isDebugEnabled()) {
162                     _log.debug("Search filter returned at least one result");
163                 }
164 
165                 Binding binding = enu.nextElement();
166 
167                 Attributes attrs = PortalLDAPUtil.getUserAttributes(
168                     companyId, ctx,
169                     PortalLDAPUtil.getNameInNamespace(companyId, binding));
170 
171                 return PortalLDAPUtil.importLDAPUser(
172                     companyId, ctx, attrs, StringPool.BLANK, true);
173             }
174             else {
175                 throw new NoSuchUserException(
176                     "User " + screenName + " was not found in the LDAP server");
177             }
178         }
179         catch (Exception e) {
180             _log.error("Problem accessing LDAP server ", e);
181 
182             throw new SystemException(
183                 "Problem accessign LDAP server " + e.getMessage());
184         }
185     }
186 
187     private static Log _log = LogFactory.getLog(CASAutoLogin.class);
188 
189 }