1
22
23 package com.liferay.portal.security.auth;
24
25 import com.liferay.portal.NoSuchUserException;
26 import com.liferay.portal.PortalException;
27 import com.liferay.portal.SystemException;
28 import com.liferay.portal.kernel.util.StringPool;
29 import com.liferay.portal.kernel.util.StringUtil;
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.PropsUtil;
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.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
60 public class CASAutoLogin implements AutoLogin {
61
62 public String[] login(HttpServletRequest req, HttpServletResponse res)
63 throws AutoLoginException {
64
65 try {
66 String[] credentials = null;
67
68 long companyId = PortalUtil.getCompanyId(req);
69
70 if (!PrefsPropsUtil.getBoolean(
71 companyId, PropsUtil.CAS_AUTH_ENABLED,
72 PropsValues.CAS_AUTH_ENABLED)) {
73
74 return credentials;
75 }
76
77 HttpSession ses = req.getSession();
78
79 String screenName =
80 (String)ses.getAttribute(CASFilter.CAS_FILTER_USER);
81
82 if (screenName != null) {
83 User user = null;
84
85 try {
86 user = UserLocalServiceUtil.getUserByScreenName(
87 companyId, screenName);
88 }
89 catch (NoSuchUserException nsue) {
90 if (PrefsPropsUtil.getBoolean(
91 companyId, PropsUtil.CAS_IMPORT_FROM_LDAP)) {
92
93 user = addUser(companyId, screenName);
94 }
95 else {
96 throw nsue;
97 }
98 }
99
100 credentials = new String[3];
101
102 credentials[0] = String.valueOf(user.getUserId());
103 credentials[1] = user.getPassword();
104 credentials[2] = Boolean.TRUE.toString();
105 }
106
107 return credentials;
108 }
109 catch (Exception e) {
110 throw new AutoLoginException(e);
111 }
112 }
113
114 protected User addUser(long companyId, String screenName)
115 throws PortalException, SystemException {
116
117 try {
118 String baseDN = PrefsPropsUtil.getString(
119 companyId, PropsUtil.LDAP_BASE_DN);
120
121 LdapContext ctx = PortalLDAPUtil.getContext(companyId);
122
123 if (ctx == null) {
124 throw new SystemException("Failed to bind to the LDAP server");
125 }
126
127 String filter = PrefsPropsUtil.getString(
128 companyId, PropsUtil.LDAP_AUTH_SEARCH_FILTER);
129
130 if (_log.isDebugEnabled()) {
131 _log.debug("Search filter before transformation " + filter);
132 }
133
134 filter = StringUtil.replace(
135 filter,
136 new String[] {
137 "@company_id@", "@email_address@", "@screen_name@"
138 },
139 new String[] {
140 String.valueOf(companyId), StringPool.BLANK, screenName
141 });
142
143 if (_log.isDebugEnabled()) {
144 _log.debug("Search filter after transformation " + filter);
145 }
146
147 SearchControls cons = new SearchControls(
148 SearchControls.SUBTREE_SCOPE, 1, 0, null, false, false);
149
150 NamingEnumeration enu = ctx.search(baseDN, filter, cons);
151
152 if (enu.hasMore()) {
153 if (_log.isDebugEnabled()) {
154 _log.debug("Search filter returned at least one result");
155 }
156
157 Binding binding = (Binding)enu.next();
158
159 Attributes attrs = ctx.getAttributes(binding.getName());
160
161 return PortalLDAPUtil.importLDAPUser(
162 companyId, ctx, attrs, StringPool.BLANK, true);
163 }
164 else {
165 throw new NoSuchUserException(
166 "User " + screenName + " was not found in the LDAP server");
167 }
168 }
169 catch (Exception e) {
170 _log.error("Problem accessing LDAP server ", e);
171
172 throw new SystemException(
173 "Problem accessign LDAP server " + e.getMessage());
174 }
175 }
176
177 private static Log _log = LogFactory.getLog(CASAutoLogin.class);
178
179 }