1   /**
2    * Copyright (c) 2000-2009 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   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portal.security.auth;
21  
22  import com.liferay.portal.NoSuchUserException;
23  import com.liferay.portal.kernel.log.Log;
24  import com.liferay.portal.kernel.log.LogFactoryUtil;
25  import com.liferay.portal.kernel.util.LocaleUtil;
26  import com.liferay.portal.kernel.util.StringPool;
27  import com.liferay.portal.kernel.util.Validator;
28  import com.liferay.portal.kernel.util.WebKeys;
29  import com.liferay.portal.model.User;
30  import com.liferay.portal.service.UserLocalServiceUtil;
31  import com.liferay.portal.servlet.filters.sso.opensso.OpenSSOUtil;
32  import com.liferay.portal.theme.ThemeDisplay;
33  import com.liferay.portal.util.PortalUtil;
34  import com.liferay.portal.util.PrefsPropsUtil;
35  import com.liferay.portal.util.PropsKeys;
36  import com.liferay.portal.util.PropsValues;
37  import com.liferay.util.PwdGenerator;
38  
39  import java.util.Calendar;
40  import java.util.Locale;
41  import java.util.Map;
42  
43  import javax.servlet.http.HttpServletRequest;
44  import javax.servlet.http.HttpServletResponse;
45  
46  /**
47   * <a href="OpenSSOAutoLogin.java.html"><b><i>View Source</i></b></a>
48   *
49   * @author Brian Wing Shun Chan
50   * @author Prashant Dighe
51   *
52   */
53  public class OpenSSOAutoLogin implements AutoLogin {
54  
55      public String[] login(
56          HttpServletRequest request, HttpServletResponse response) {
57  
58          String[] credentials = null;
59  
60          try {
61              long companyId = PortalUtil.getCompanyId(request);
62  
63              if (!PrefsPropsUtil.getBoolean(
64                      companyId, PropsKeys.OPEN_SSO_AUTH_ENABLED,
65                      PropsValues.OPEN_SSO_AUTH_ENABLED)) {
66  
67                  return credentials;
68              }
69  
70              String serviceUrl = PrefsPropsUtil.getString(
71                  companyId, PropsKeys.OPEN_SSO_SERVICE_URL);
72  
73              if (!OpenSSOUtil.isAuthenticated(request, serviceUrl)) {
74                  return credentials;
75              }
76  
77              String screenNameAttr = PrefsPropsUtil.getString(
78                  companyId, PropsKeys.OPEN_SSO_SCREEN_NAME_ATTR,
79                  PropsValues.OPEN_SSO_SCREEN_NAME_ATTR);
80              String emailAddressAttr = PrefsPropsUtil.getString(
81                  companyId, PropsKeys.OPEN_SSO_EMAIL_ADDRESS_ATTR,
82                  PropsValues.OPEN_SSO_EMAIL_ADDRESS_ATTR);
83              String firstNameAttr = PrefsPropsUtil.getString(
84                  companyId, PropsKeys.OPEN_SSO_FIRST_NAME_ATTR,
85                  PropsValues.OPEN_SSO_FIRST_NAME_ATTR);
86              String lastNameAttr = PrefsPropsUtil.getString(
87                  companyId, PropsKeys.OPEN_SSO_LAST_NAME_ATTR,
88                  PropsValues.OPEN_SSO_LAST_NAME_ATTR);
89  
90              Map<String, String> nameValues = OpenSSOUtil.getAttributes(
91                  request, serviceUrl);
92  
93              String screenName = nameValues.get(screenNameAttr);
94              String emailAddress = nameValues.get(emailAddressAttr);
95              String firstName = nameValues.get(firstNameAttr);
96              String lastName = nameValues.get(lastNameAttr);
97  
98              if (Validator.isNull(emailAddress)) {
99                  throw new AutoLoginException("Email address is null");
100             }
101 
102             User user = null;
103 
104             try {
105                 user = UserLocalServiceUtil.getUserByScreenName(
106                     companyId, screenName);
107             }
108             catch (NoSuchUserException nsue) {
109                 ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
110                     WebKeys.THEME_DISPLAY);
111 
112                 Locale locale = LocaleUtil.getDefault();
113 
114                 if (themeDisplay != null) {
115 
116                     // ThemeDisplay should never be null, but some users
117                     // complain of this error. Cause is unknown.
118 
119                     locale = themeDisplay.getLocale();
120                 }
121 
122                 user = addUser(
123                     companyId, firstName, lastName, emailAddress, screenName,
124                     locale);
125             }
126 
127             credentials = new String[3];
128 
129             credentials[0] = String.valueOf(user.getUserId());
130             credentials[1] = user.getPassword();
131             credentials[2] = Boolean.TRUE.toString();
132         }
133         catch (Exception e) {
134             _log.error(e, e);
135         }
136 
137         return credentials;
138     }
139 
140     protected User addUser(
141             long companyId, String firstName, String lastName,
142             String emailAddress, String screenName, Locale locale)
143         throws Exception {
144 
145         long creatorUserId = 0;
146         boolean autoPassword = false;
147         String password1 = PwdGenerator.getPassword();
148         String password2 = password1;
149         boolean autoScreenName = false;
150         String middleName = StringPool.BLANK;
151         int prefixId = 0;
152         int suffixId = 0;
153         boolean male = true;
154         int birthdayMonth = Calendar.JANUARY;
155         int birthdayDay = 1;
156         int birthdayYear = 1970;
157         String jobTitle = StringPool.BLANK;
158         long[] organizationIds = new long[0];
159         boolean sendEmail = false;
160 
161         return UserLocalServiceUtil.addUser(
162             creatorUserId, companyId, autoPassword, password1, password2,
163             autoScreenName, screenName, emailAddress, locale, firstName,
164             middleName, lastName, prefixId, suffixId, male, birthdayMonth,
165             birthdayDay, birthdayYear, jobTitle, organizationIds, sendEmail);
166     }
167 
168     private static Log _log = LogFactoryUtil.getLog(OpenSSOAutoLogin.class);
169 
170 }