1
22
23 package com.liferay.portal.security.auth;
24
25 import com.liferay.portal.NoSuchUserException;
26 import com.liferay.portal.kernel.util.HttpUtil;
27 import com.liferay.portal.kernel.util.StringPool;
28 import com.liferay.portal.model.User;
29 import com.liferay.portal.service.UserLocalServiceUtil;
30 import com.liferay.portal.theme.ThemeDisplay;
31 import com.liferay.portal.util.PortalUtil;
32 import com.liferay.portal.util.PrefsPropsUtil;
33 import com.liferay.portal.util.PropsUtil;
34 import com.liferay.portal.util.PropsValues;
35 import com.liferay.portal.util.WebKeys;
36 import com.liferay.util.PwdGenerator;
37
38 import java.io.BufferedReader;
39 import java.io.InputStream;
40 import java.io.InputStreamReader;
41
42 import java.net.URL;
43 import java.net.URLConnection;
44
45 import java.util.Calendar;
46 import java.util.HashMap;
47 import java.util.Locale;
48 import java.util.Map;
49
50 import javax.servlet.http.HttpServletRequest;
51 import javax.servlet.http.HttpServletResponse;
52 import javax.servlet.http.HttpSession;
53
54 import org.apache.commons.logging.Log;
55 import org.apache.commons.logging.LogFactory;
56
57
63 public class OpenSSOAutoLogin implements AutoLogin {
64
65 public String[] login(HttpServletRequest req, HttpServletResponse res)
66 throws AutoLoginException {
67
68 String[] credentials = null;
69
70 try {
71 long companyId = PortalUtil.getCompanyId(req);
72
73 if (!PrefsPropsUtil.getBoolean(
74 companyId, PropsUtil.OPEN_SSO_AUTH_ENABLED,
75 PropsValues.OPEN_SSO_AUTH_ENABLED)) {
76
77 return credentials;
78 }
79
80 HttpSession ses = req.getSession();
81
82 String subjectId = (String)ses.getAttribute(WebKeys.OPEN_SSO_LOGIN);
83
84 if (subjectId == null) {
85 return credentials;
86 }
87
88 Map<String, String> nameValues = new HashMap<String, String>();
89
90 String serviceUrl = PrefsPropsUtil.getString(
91 companyId, PropsUtil.OPEN_SSO_SERVICE_URL);
92
93 String url =
94 serviceUrl + "/attributes?subjectid=" +
95 HttpUtil.encodeURL(subjectId);
96
97 URL urlObj = new URL(url);
98
99 URLConnection con = urlObj.openConnection();
100
101 BufferedReader reader = new BufferedReader(
102 new InputStreamReader((InputStream)con.getContent()));
103
104 String line = null;
105
106 while ((line = reader.readLine()) != null) {
107 String[] parts = line.split("=");
108
109 if ((parts == null) || (parts.length != 2)) {
110 continue;
111 }
112
113 String attrName = null;
114 String attrValue = null;
115
116 if (parts[0].endsWith("name")) {
117 attrName = parts[1];
118
119 line = reader.readLine();
120
121 if (line == null) {
122
123
125 throw new AutoLoginException(
126 "Error reading user attributes");
127 }
128
129 parts = line.split("=");
130
131 if ((parts == null) || (parts.length != 2) ||
132 (!parts[0].endsWith("value"))) {
133
134 attrValue = null;
135 }
136 else {
137 attrValue = parts[1];
138 }
139
140 nameValues.put(attrName, attrValue);
141 }
142 }
143
144 String firstName = nameValues.get("cn");
145 String lastName = nameValues.get("sn");
146 String screenName = nameValues.get("givenname");
147 String emailAddress = nameValues.get("mail");
148
149 User user = null;
150
151 try {
152 user = UserLocalServiceUtil.getUserByEmailAddress(
153 companyId, emailAddress);
154 }
155 catch (NoSuchUserException nsue) {
156 ThemeDisplay themeDisplay =
157 (ThemeDisplay)req.getAttribute(WebKeys.THEME_DISPLAY);
158
159 user = addUser(
160 companyId, firstName, lastName, emailAddress, screenName,
161 themeDisplay.getLocale());
162 }
163
164 credentials = new String[3];
165
166 credentials[0] = String.valueOf(user.getUserId());
167 credentials[1] = user.getPassword();
168 credentials[2] = Boolean.TRUE.toString();
169 }
170 catch (Exception e) {
171 _log.error(e.getMessage());
172 }
173
174 return credentials;
175 }
176
177 protected User addUser(
178 long companyId, String firstName, String lastName,
179 String emailAddress, String screenName, Locale locale)
180 throws Exception {
181
182 long creatorUserId = 0;
183 boolean autoPassword = false;
184 String password1 = PwdGenerator.getPassword();
185 String password2 = password1;
186 boolean autoScreenName = false;
187 String middleName = StringPool.BLANK;
188 int prefixId = 0;
189 int suffixId = 0;
190 boolean male = true;
191 int birthdayMonth = Calendar.JANUARY;
192 int birthdayDay = 1;
193 int birthdayYear = 1970;
194 String jobTitle = StringPool.BLANK;
195 long[] organizationIds = new long[0];
196 boolean sendEmail = false;
197
198 return UserLocalServiceUtil.addUser(
199 creatorUserId, companyId, autoPassword, password1, password2,
200 autoScreenName, screenName, emailAddress, locale, firstName,
201 middleName, lastName, prefixId, suffixId, male, birthdayMonth,
202 birthdayDay, birthdayYear, jobTitle, organizationIds, sendEmail);
203 }
204
205 private static Log _log = LogFactory.getLog(OpenSSOAutoLogin.class);
206
207 }