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.Validator;
26  import com.liferay.portal.model.User;
27  import com.liferay.portal.security.ldap.PortalLDAPUtil;
28  import com.liferay.portal.service.UserLocalServiceUtil;
29  import com.liferay.portal.util.PortalUtil;
30  import com.liferay.portal.util.PrefsPropsUtil;
31  import com.liferay.portal.util.PropsKeys;
32  import com.liferay.portal.util.PropsValues;
33  
34  import javax.servlet.http.HttpServletRequest;
35  import javax.servlet.http.HttpServletResponse;
36  
37  /**
38   * <a href="SiteMinderAutoLogin.java.html"><b><i>View Source</i></b></a>
39   *
40   * @author Mika Koivisto
41   *
42   */
43  public class SiteMinderAutoLogin extends CASAutoLogin {
44  
45      public String[] login(
46              HttpServletRequest request, HttpServletResponse response)
47          throws AutoLoginException {
48  
49          String[] credentials = null;
50  
51          try {
52              long companyId = PortalUtil.getCompanyId(request);
53  
54              if (!PortalLDAPUtil.isSiteMinderEnabled(companyId)) {
55                  return credentials;
56              }
57  
58              String screenName = request.getHeader(
59                  PrefsPropsUtil.getString(
60                      companyId, PropsKeys.SITEMINDER_USER_HEADER,
61                      PropsValues.SITEMINDER_USER_HEADER));
62  
63              if (Validator.isNull(screenName)) {
64                  return credentials;
65              }
66  
67              User user = null;
68  
69              try {
70                  user = UserLocalServiceUtil.getUserByScreenName(
71                      companyId, screenName);
72              }
73              catch (NoSuchUserException nsue) {
74                  if (PrefsPropsUtil.getBoolean(
75                          companyId, PropsKeys.SITEMINDER_IMPORT_FROM_LDAP,
76                          PropsValues.SITEMINDER_IMPORT_FROM_LDAP)) {
77  
78                      user = addUser(companyId, screenName);
79                  }
80                  else {
81                      throw nsue;
82                  }
83              }
84  
85              credentials = new String[3];
86  
87              credentials[0] = String.valueOf(user.getUserId());
88              credentials[1] = user.getPassword();
89              credentials[2] = Boolean.TRUE.toString();
90  
91              return credentials;
92          }
93          catch (Exception e) {
94              _log.error(e, e);
95          }
96  
97          return credentials;
98      }
99  
100     private static Log _log = LogFactoryUtil.getLog(SiteMinderAutoLogin.class);
101 
102 }