1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
13   */
14  
15  package com.liferay.portal.security.auth;
16  
17  import com.liferay.portal.NoSuchUserException;
18  import com.liferay.portal.kernel.log.Log;
19  import com.liferay.portal.kernel.log.LogFactoryUtil;
20  import com.liferay.portal.kernel.util.LocaleUtil;
21  import com.liferay.portal.kernel.util.ParamUtil;
22  import com.liferay.portal.kernel.util.PropsKeys;
23  import com.liferay.portal.kernel.util.StringPool;
24  import com.liferay.portal.kernel.util.Validator;
25  import com.liferay.portal.kernel.util.WebKeys;
26  import com.liferay.portal.model.User;
27  import com.liferay.portal.service.ServiceContext;
28  import com.liferay.portal.service.UserLocalServiceUtil;
29  import com.liferay.portal.servlet.filters.sso.opensso.OpenSSOUtil;
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.PropsValues;
34  import com.liferay.util.PwdGenerator;
35  
36  import java.util.Calendar;
37  import java.util.Locale;
38  import java.util.Map;
39  
40  import javax.servlet.http.HttpServletRequest;
41  import javax.servlet.http.HttpServletResponse;
42  
43  /**
44   * <a href="OpenSSOAutoLogin.java.html"><b><i>View Source</i></b></a>
45   *
46   * @author Brian Wing Shun Chan
47   * @author Prashant Dighe
48   */
49  public class OpenSSOAutoLogin implements AutoLogin {
50  
51      public String[] login(
52          HttpServletRequest request, HttpServletResponse response) {
53  
54          String[] credentials = null;
55  
56          try {
57              long companyId = PortalUtil.getCompanyId(request);
58  
59              if (!PrefsPropsUtil.getBoolean(
60                      companyId, PropsKeys.OPEN_SSO_AUTH_ENABLED,
61                      PropsValues.OPEN_SSO_AUTH_ENABLED)) {
62  
63                  return credentials;
64              }
65  
66              String serviceUrl = PrefsPropsUtil.getString(
67                  companyId, PropsKeys.OPEN_SSO_SERVICE_URL);
68  
69              if (!OpenSSOUtil.isAuthenticated(request, serviceUrl)) {
70                  return credentials;
71              }
72  
73              String screenNameAttr = PrefsPropsUtil.getString(
74                  companyId, PropsKeys.OPEN_SSO_SCREEN_NAME_ATTR,
75                  PropsValues.OPEN_SSO_SCREEN_NAME_ATTR);
76              String emailAddressAttr = PrefsPropsUtil.getString(
77                  companyId, PropsKeys.OPEN_SSO_EMAIL_ADDRESS_ATTR,
78                  PropsValues.OPEN_SSO_EMAIL_ADDRESS_ATTR);
79              String firstNameAttr = PrefsPropsUtil.getString(
80                  companyId, PropsKeys.OPEN_SSO_FIRST_NAME_ATTR,
81                  PropsValues.OPEN_SSO_FIRST_NAME_ATTR);
82              String lastNameAttr = PrefsPropsUtil.getString(
83                  companyId, PropsKeys.OPEN_SSO_LAST_NAME_ATTR,
84                  PropsValues.OPEN_SSO_LAST_NAME_ATTR);
85  
86              Map<String, String> nameValues = OpenSSOUtil.getAttributes(
87                  request, serviceUrl);
88  
89              String screenName = nameValues.get(screenNameAttr);
90              String emailAddress = nameValues.get(emailAddressAttr);
91              String firstName = nameValues.get(firstNameAttr);
92              String lastName = nameValues.get(lastNameAttr);
93  
94              if (_log.isDebugEnabled()) {
95                  _log.debug(
96                      "Validating user information for " + firstName + " " +
97                          lastName + " with screen name " + screenName +
98                          " and email address " + emailAddress);
99              }
100 
101             if (Validator.isNull(emailAddress)) {
102                 throw new AutoLoginException("Email address is null");
103             }
104 
105             User user = null;
106 
107             try {
108                 user = UserLocalServiceUtil.getUserByScreenName(
109                     companyId, screenName);
110             }
111             catch (NoSuchUserException nsue) {
112                 ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
113                     WebKeys.THEME_DISPLAY);
114 
115                 Locale locale = LocaleUtil.getDefault();
116 
117                 if (themeDisplay != null) {
118 
119                     // ThemeDisplay should never be null, but some users
120                     // complain of this error. Cause is unknown.
121 
122                     locale = themeDisplay.getLocale();
123                 }
124 
125                 if (_log.isDebugEnabled()) {
126                     _log.debug("Adding user " + screenName);
127                 }
128 
129                 user = addUser(
130                     companyId, firstName, lastName, emailAddress, screenName,
131                     locale);
132             }
133 
134             String redirect = ParamUtil.getString(request, "redirect");
135 
136             if (Validator.isNotNull(redirect)) {
137                 request.setAttribute(AutoLogin.AUTO_LOGIN_REDIRECT, redirect);
138             }
139 
140             credentials = new String[3];
141 
142             credentials[0] = String.valueOf(user.getUserId());
143             credentials[1] = user.getPassword();
144             credentials[2] = Boolean.TRUE.toString();
145         }
146         catch (Exception e) {
147             _log.error(e, e);
148         }
149 
150         return credentials;
151     }
152 
153     protected User addUser(
154             long companyId, String firstName, String lastName,
155             String emailAddress, String screenName, Locale locale)
156         throws Exception {
157 
158         long creatorUserId = 0;
159         boolean autoPassword = false;
160         String password1 = PwdGenerator.getPassword();
161         String password2 = password1;
162         boolean autoScreenName = false;
163         String openId = StringPool.BLANK;
164         String middleName = StringPool.BLANK;
165         int prefixId = 0;
166         int suffixId = 0;
167         boolean male = true;
168         int birthdayMonth = Calendar.JANUARY;
169         int birthdayDay = 1;
170         int birthdayYear = 1970;
171         String jobTitle = StringPool.BLANK;
172         long[] groupIds = null;
173         long[] organizationIds = null;
174         long[] roleIds = null;
175         long[] userGroupIds = null;
176         boolean sendEmail = false;
177         ServiceContext serviceContext = new ServiceContext();
178 
179         return UserLocalServiceUtil.addUser(
180             creatorUserId, companyId, autoPassword, password1, password2,
181             autoScreenName, screenName, emailAddress, openId, locale, firstName,
182             middleName, lastName, prefixId, suffixId, male, birthdayMonth,
183             birthdayDay, birthdayYear, jobTitle, groupIds, organizationIds,
184             roleIds, userGroupIds, sendEmail, serviceContext);
185     }
186 
187     private static Log _log = LogFactoryUtil.getLog(OpenSSOAutoLogin.class);
188 
189 }