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.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
61 public class CASAutoLogin implements AutoLogin {
62
63 public String[] login(HttpServletRequest req, HttpServletResponse res)
64 throws AutoLoginException {
65
66 try {
67 String[] credentials = null;
68
69 long companyId = PortalUtil.getCompanyId(req);
70
71 if (!PrefsPropsUtil.getBoolean(
72 companyId, PropsUtil.CAS_AUTH_ENABLED,
73 PropsValues.CAS_AUTH_ENABLED)) {
74
75 return credentials;
76 }
77
78 HttpSession ses = req.getSession();
79
80 String screenName =
81 (String)ses.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, PropsUtil.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 PortalException, SystemException {
117
118 try {
119 String baseDN = PrefsPropsUtil.getString(
120 companyId, PropsUtil.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, PropsUtil.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.hasMore()) {
155 if (_log.isDebugEnabled()) {
156 _log.debug("Search filter returned at least one result");
157 }
158
159 Binding binding = enu.next();
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 }