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