1   /**
2    * Copyright (c) 2000-2007 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions 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.kernel.util.GetterUtil;
26  import com.liferay.portal.kernel.util.InstancePool;
27  import com.liferay.portal.kernel.util.Validator;
28  import com.liferay.portal.model.impl.CompanyImpl;
29  
30  import java.util.Map;
31  
32  /**
33   * <a href="AuthPipeline.java.html"><b><i>View Source</i></b></a>
34   *
35   * @author Brian Wing Shun Chan
36   *
37   */
38  public class AuthPipeline {
39  
40      public static int authenticateByEmailAddress(
41              String[] classes, long companyId, String emailAddress,
42              String password, Map headerMap, Map parameterMap)
43          throws AuthException {
44  
45          return _authenticate(
46              classes, companyId, emailAddress, password,
47              CompanyImpl.AUTH_TYPE_EA, headerMap, parameterMap);
48      }
49  
50      public static int authenticateByScreenName(
51              String[] classes, long companyId, String screenName,
52              String password, Map headerMap, Map parameterMap)
53          throws AuthException {
54  
55          return _authenticate(
56              classes, companyId, screenName, password, CompanyImpl.AUTH_TYPE_SN,
57              headerMap, parameterMap);
58      }
59  
60      public static int authenticateByUserId(
61              String[] classes, long companyId, long userId, String password,
62              Map headerMap, Map parameterMap)
63          throws AuthException {
64  
65          return _authenticate(
66              classes, companyId, String.valueOf(userId), password,
67              CompanyImpl.AUTH_TYPE_ID, headerMap, parameterMap);
68      }
69  
70      public static void onFailureByEmailAddress(
71              String[] classes, long companyId, String emailAddress,
72              Map headerMap, Map parameterMap)
73          throws AuthException {
74  
75          _onFailure(
76              classes, companyId, emailAddress, CompanyImpl.AUTH_TYPE_EA,
77              headerMap, parameterMap);
78      }
79  
80      public static void onFailureByScreenName(
81              String[] classes, long companyId, String screenName,
82              Map headerMap, Map parameterMap)
83          throws AuthException {
84  
85          _onFailure(
86              classes, companyId, screenName, CompanyImpl.AUTH_TYPE_SN, headerMap,
87              parameterMap);
88      }
89  
90      public static void onFailureByUserId(
91              String[] classes, long companyId, long userId, Map headerMap,
92              Map parameterMap)
93          throws AuthException {
94  
95          _onFailure(
96              classes, companyId, String.valueOf(userId),
97              CompanyImpl.AUTH_TYPE_ID, headerMap, parameterMap);
98      }
99  
100     public static void onMaxFailuresByEmailAddress(
101             String[] classes, long companyId, String emailAddress,
102             Map headerMap, Map parameterMap)
103         throws AuthException {
104 
105         onFailureByEmailAddress(
106             classes, companyId, emailAddress, headerMap, parameterMap);
107     }
108 
109     public static void onMaxFailuresByScreenName(
110             String[] classes, long companyId, String screenName,
111             Map headerMap, Map parameterMap)
112         throws AuthException {
113 
114         onFailureByScreenName(
115             classes, companyId, screenName, headerMap, parameterMap);
116     }
117 
118     public static void onMaxFailuresByUserId(
119             String[] classes, long companyId, long userId, Map headerMap,
120             Map parameterMap)
121         throws AuthException {
122 
123         onFailureByUserId(classes, companyId, userId, headerMap, parameterMap);
124     }
125 
126     private static int _authenticate(
127             String[] classes, long companyId, String login, String password,
128             String authType, Map headerMap, Map parameterMap)
129         throws AuthException {
130 
131         if ((classes == null) || (classes.length == 0)) {
132             return 1;
133         }
134 
135         for (int i = 0; i < classes.length; i++) {
136             String className = classes[i];
137 
138             if (Validator.isNotNull(className)) {
139                 Authenticator auth =
140                     (Authenticator)InstancePool.get(classes[i]);
141 
142                 try {
143                     int authResult = Authenticator.FAILURE;
144 
145                     if (authType.equals(CompanyImpl.AUTH_TYPE_EA)) {
146                         authResult = auth.authenticateByEmailAddress(
147                             companyId, login, password, headerMap,
148                             parameterMap);
149                     }
150                     else if (authType.equals(CompanyImpl.AUTH_TYPE_SN)) {
151                         authResult = auth.authenticateByScreenName(
152                             companyId, login, password, headerMap,
153                             parameterMap);
154                     }
155                     else if (authType.equals(CompanyImpl.AUTH_TYPE_ID)) {
156                         long userId = GetterUtil.getLong(login);
157 
158                         authResult = auth.authenticateByUserId(
159                             companyId, userId, password, headerMap,
160                             parameterMap);
161                     }
162 
163                     if (authResult != Authenticator.SUCCESS) {
164                         return authResult;
165                     }
166                 }
167                 catch (AuthException ae) {
168                     throw ae;
169                 }
170                 catch (Exception e) {
171                     throw new AuthException(e);
172                 }
173             }
174         }
175 
176         return Authenticator.SUCCESS;
177     }
178 
179     private static void _onFailure(
180             String[] classes, long companyId, String login, String authType,
181             Map headerMap, Map parameterMap)
182         throws AuthException {
183 
184         if ((classes == null) || (classes.length == 0)) {
185             return;
186         }
187 
188         for (int i = 0; i < classes.length; i++) {
189             String className = classes[i];
190 
191             if (Validator.isNotNull(className)) {
192                 AuthFailure authFailure =
193                     (AuthFailure)InstancePool.get(classes[i]);
194 
195                 try {
196                     if (authType.equals(CompanyImpl.AUTH_TYPE_EA)) {
197                         authFailure.onFailureByEmailAddress(
198                             companyId, login, headerMap, parameterMap);
199                     }
200                     else if (authType.equals(CompanyImpl.AUTH_TYPE_SN)) {
201                         authFailure.onFailureByScreenName(
202                             companyId, login, headerMap, parameterMap);
203                     }
204                     else if (authType.equals(CompanyImpl.AUTH_TYPE_ID)) {
205                         long userId = GetterUtil.getLong(login);
206 
207                         authFailure.onFailureByUserId(
208                             companyId, userId, headerMap, parameterMap);
209                     }
210                 }
211                 catch (AuthException ae) {
212                     throw ae;
213                 }
214                 catch (Exception e) {
215                     throw new AuthException(e);
216                 }
217             }
218         }
219     }
220 
221 }