1
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.log.Log;
28 import com.liferay.portal.kernel.log.LogFactoryUtil;
29 import com.liferay.portal.kernel.util.ParamUtil;
30 import com.liferay.portal.kernel.util.PropsKeys;
31 import com.liferay.portal.kernel.util.StringPool;
32 import com.liferay.portal.kernel.util.StringUtil;
33 import com.liferay.portal.kernel.util.Validator;
34 import com.liferay.portal.model.User;
35 import com.liferay.portal.security.ldap.PortalLDAPUtil;
36 import com.liferay.portal.service.UserLocalServiceUtil;
37 import com.liferay.portal.util.PortalUtil;
38 import com.liferay.portal.util.PrefsPropsUtil;
39 import com.liferay.portal.util.PropsValues;
40
41 import edu.yale.its.tp.cas.client.filter.CASFilter;
42
43 import javax.naming.Binding;
44 import javax.naming.NamingEnumeration;
45 import javax.naming.directory.Attributes;
46 import javax.naming.directory.SearchControls;
47 import javax.naming.directory.SearchResult;
48 import javax.naming.ldap.LdapContext;
49
50 import javax.servlet.http.HttpServletRequest;
51 import javax.servlet.http.HttpServletResponse;
52 import javax.servlet.http.HttpSession;
53
54
61 public class CASAutoLogin implements AutoLogin {
62
63 public String[] login(
64 HttpServletRequest request, HttpServletResponse response) {
65
66 String[] credentials = null;
67
68 try {
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 = (String)session.getAttribute(
81 CASFilter.CAS_FILTER_USER);
82
83 if (Validator.isNull(screenName)) {
84 return credentials;
85 }
86
87 User user = null;
88
89 if (PrefsPropsUtil.getBoolean(
90 companyId, PropsKeys.CAS_IMPORT_FROM_LDAP,
91 PropsValues.CAS_IMPORT_FROM_LDAP)) {
92
93 try {
94 user = importLDAPUser(companyId, screenName);
95 }
96 catch (SystemException se) {
97 }
98 }
99
100 if (user == null) {
101 user = UserLocalServiceUtil.getUserByScreenName(
102 companyId, screenName);
103 }
104
105 String redirect = ParamUtil.getString(request, "redirect");
106
107 if (Validator.isNotNull(redirect)) {
108 request.setAttribute(AutoLogin.AUTO_LOGIN_REDIRECT, redirect);
109 }
110
111 credentials = new String[3];
112
113 credentials[0] = String.valueOf(user.getUserId());
114 credentials[1] = user.getPassword();
115 credentials[2] = Boolean.TRUE.toString();
116
117 return credentials;
118 }
119 catch (Exception e) {
120 _log.error(e, e);
121 }
122
123 return credentials;
124 }
125
126
129 protected User addUser(long companyId, String screenName)
130 throws Exception {
131
132 return importLDAPUser(companyId, screenName);
133 }
134
135 protected User importLDAPUser(long companyId, String screenName)
136 throws Exception {
137
138 LdapContext ctx = null;
139
140 try {
141 String baseDN = PrefsPropsUtil.getString(
142 companyId, PropsKeys.LDAP_BASE_DN);
143
144 ctx = PortalLDAPUtil.getContext(companyId);
145
146 if (ctx == null) {
147 throw new SystemException("Failed to bind to the LDAP server");
148 }
149
150 String filter = PrefsPropsUtil.getString(
151 companyId, PropsKeys.LDAP_AUTH_SEARCH_FILTER);
152
153 if (_log.isDebugEnabled()) {
154 _log.debug("Search filter before transformation " + filter);
155 }
156
157 filter = StringUtil.replace(
158 filter,
159 new String[] {
160 "@company_id@", "@email_address@", "@screen_name@"
161 },
162 new String[] {
163 String.valueOf(companyId), StringPool.BLANK, screenName
164 });
165
166 if (_log.isDebugEnabled()) {
167 _log.debug("Search filter after transformation " + filter);
168 }
169
170 SearchControls cons = new SearchControls(
171 SearchControls.SUBTREE_SCOPE, 1, 0, null, false, false);
172
173 NamingEnumeration<SearchResult> enu = ctx.search(
174 baseDN, filter, cons);
175
176 if (enu.hasMoreElements()) {
177 if (_log.isDebugEnabled()) {
178 _log.debug("Search filter returned at least one result");
179 }
180
181 Binding binding = enu.nextElement();
182
183 Attributes attrs = PortalLDAPUtil.getUserAttributes(
184 companyId, ctx,
185 PortalLDAPUtil.getNameInNamespace(companyId, binding));
186
187 return PortalLDAPUtil.importLDAPUser(
188 companyId, ctx, attrs, StringPool.BLANK, true);
189 }
190 else {
191 throw new NoSuchUserException(
192 "User " + screenName + " was not found in the LDAP server");
193 }
194 }
195 catch (Exception e) {
196 if (_log.isWarnEnabled()) {
197 _log.warn("Problem accessing LDAP server " + e.getMessage());
198 }
199
200 if (_log.isDebugEnabled()) {
201 _log.debug(e, e);
202 }
203
204 throw new SystemException(
205 "Problem accessing LDAP server " + e.getMessage());
206 }
207 finally {
208 if (ctx != null) {
209 ctx.close();
210 }
211 }
212 }
213
214 private static Log _log = LogFactoryUtil.getLog(CASAutoLogin.class);
215
216 }