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.model.User;
30  import com.liferay.portal.security.ldap.PortalLDAPUtil;
31  import com.liferay.portal.service.UserLocalServiceUtil;
32  import com.liferay.portal.util.PortalUtil;
33  import com.liferay.portal.util.PrefsPropsUtil;
34  import com.liferay.portal.util.PropsKeys;
35  import com.liferay.portal.util.PropsValues;
36  
37  import edu.yale.its.tp.cas.client.filter.CASFilter;
38  
39  import javax.naming.Binding;
40  import javax.naming.NamingEnumeration;
41  import javax.naming.directory.Attributes;
42  import javax.naming.directory.SearchControls;
43  import javax.naming.directory.SearchResult;
44  import javax.naming.ldap.LdapContext;
45  
46  import javax.servlet.http.HttpServletRequest;
47  import javax.servlet.http.HttpServletResponse;
48  import javax.servlet.http.HttpSession;
49  
50  import org.apache.commons.logging.Log;
51  import org.apache.commons.logging.LogFactory;
52  
53  /**
54   * <a href="CASAutoLogin.java.html"><b><i>View Source</i></b></a>
55   *
56   * @author Brian Wing Shun Chan
57   * @author Jorge Ferrer
58   *
59   */
60  public class CASAutoLogin implements AutoLogin {
61  
62      public String[] login(
63              HttpServletRequest request, HttpServletResponse response)
64          throws AutoLoginException {
65  
66          try {
67              String[] credentials = null;
68  
69              long companyId = PortalUtil.getCompanyId(request);
70  
71              if (!PrefsPropsUtil.getBoolean(
72                      companyId, PropsKeys.CAS_AUTH_ENABLED,
73                      PropsValues.CAS_AUTH_ENABLED)) {
74  
75                  return credentials;
76              }
77  
78              HttpSession session = request.getSession();
79  
80              String screenName =
81                  (String)session.getAttribute(CASFilter.CAS_FILTER_USER);
82  
83              if (screenName != null) {
84                  User user = null;
85  
86                  try {
87                      user = UserLocalServiceUtil.getUserByScreenName(
88                          companyId, screenName);
89                  }
90                  catch (NoSuchUserException nsue) {
91                      if (PrefsPropsUtil.getBoolean(
92                              companyId, PropsKeys.CAS_IMPORT_FROM_LDAP)) {
93  
94                          user = addUser(companyId, screenName);
95                      }
96                      else {
97                          throw nsue;
98                      }
99                  }
100 
101                 credentials = new String[3];
102 
103                 credentials[0] = String.valueOf(user.getUserId());
104                 credentials[1] = user.getPassword();
105                 credentials[2] = Boolean.TRUE.toString();
106             }
107 
108             return credentials;
109         }
110         catch (Exception e) {
111             throw new AutoLoginException(e);
112         }
113     }
114 
115     protected User addUser(long companyId, String screenName)
116         throws SystemException {
117 
118         try {
119             String baseDN = PrefsPropsUtil.getString(
120                 companyId, PropsKeys.LDAP_BASE_DN);
121 
122             LdapContext ctx = PortalLDAPUtil.getContext(companyId);
123 
124             if (ctx == null) {
125                 throw new SystemException("Failed to bind to the LDAP server");
126             }
127 
128             String filter = PrefsPropsUtil.getString(
129                 companyId, PropsKeys.LDAP_AUTH_SEARCH_FILTER);
130 
131             if (_log.isDebugEnabled()) {
132                 _log.debug("Search filter before transformation " + filter);
133             }
134 
135             filter = StringUtil.replace(
136                 filter,
137                 new String[] {
138                     "@company_id@", "@email_address@", "@screen_name@"
139                 },
140                 new String[] {
141                     String.valueOf(companyId), StringPool.BLANK, screenName
142                 });
143 
144             if (_log.isDebugEnabled()) {
145                 _log.debug("Search filter after transformation " + filter);
146             }
147 
148             SearchControls cons = new SearchControls(
149                 SearchControls.SUBTREE_SCOPE, 1, 0, null, false, false);
150 
151             NamingEnumeration<SearchResult> enu = ctx.search(
152                 baseDN, filter, cons);
153 
154             if (enu.hasMoreElements()) {
155                 if (_log.isDebugEnabled()) {
156                     _log.debug("Search filter returned at least one result");
157                 }
158 
159                 Binding binding = enu.nextElement();
160 
161                 Attributes attrs = ctx.getAttributes(binding.getName());
162 
163                 return PortalLDAPUtil.importLDAPUser(
164                     companyId, ctx, attrs, StringPool.BLANK, true);
165             }
166             else {
167                 throw new NoSuchUserException(
168                     "User " + screenName + " was not found in the LDAP server");
169             }
170         }
171         catch (Exception e) {
172             _log.error("Problem accessing LDAP server ", e);
173 
174             throw new SystemException(
175                 "Problem accessign LDAP server " + e.getMessage());
176         }
177     }
178 
179     private static Log _log = LogFactory.getLog(CASAutoLogin.class);
180 
181 }