001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.portal.security.auth;
022:
023: import com.liferay.portal.kernel.util.GetterUtil;
024: import com.liferay.portal.kernel.util.InstancePool;
025: import com.liferay.portal.kernel.util.Validator;
026: import com.liferay.portal.model.impl.CompanyImpl;
027:
028: import java.util.Map;
029:
030: /**
031: * <a href="AuthPipeline.java.html"><b><i>View Source</i></b></a>
032: *
033: * @author Brian Wing Shun Chan
034: *
035: */
036: public class AuthPipeline {
037:
038: public static int authenticateByEmailAddress(String[] classes,
039: long companyId, String emailAddress, String password,
040: Map headerMap, Map parameterMap) throws AuthException {
041:
042: return _authenticate(classes, companyId, emailAddress,
043: password, CompanyImpl.AUTH_TYPE_EA, headerMap,
044: parameterMap);
045: }
046:
047: public static int authenticateByScreenName(String[] classes,
048: long companyId, String screenName, String password,
049: Map headerMap, Map parameterMap) throws AuthException {
050:
051: return _authenticate(classes, companyId, screenName, password,
052: CompanyImpl.AUTH_TYPE_SN, headerMap, parameterMap);
053: }
054:
055: public static int authenticateByUserId(String[] classes,
056: long companyId, long userId, String password,
057: Map headerMap, Map parameterMap) throws AuthException {
058:
059: return _authenticate(classes, companyId,
060: String.valueOf(userId), password,
061: CompanyImpl.AUTH_TYPE_ID, headerMap, parameterMap);
062: }
063:
064: public static void onFailureByEmailAddress(String[] classes,
065: long companyId, String emailAddress, Map headerMap,
066: Map parameterMap) throws AuthException {
067:
068: _onFailure(classes, companyId, emailAddress,
069: CompanyImpl.AUTH_TYPE_EA, headerMap, parameterMap);
070: }
071:
072: public static void onFailureByScreenName(String[] classes,
073: long companyId, String screenName, Map headerMap,
074: Map parameterMap) throws AuthException {
075:
076: _onFailure(classes, companyId, screenName,
077: CompanyImpl.AUTH_TYPE_SN, headerMap, parameterMap);
078: }
079:
080: public static void onFailureByUserId(String[] classes,
081: long companyId, long userId, Map headerMap, Map parameterMap)
082: throws AuthException {
083:
084: _onFailure(classes, companyId, String.valueOf(userId),
085: CompanyImpl.AUTH_TYPE_ID, headerMap, parameterMap);
086: }
087:
088: public static void onMaxFailuresByEmailAddress(String[] classes,
089: long companyId, String emailAddress, Map headerMap,
090: Map parameterMap) throws AuthException {
091:
092: onFailureByEmailAddress(classes, companyId, emailAddress,
093: headerMap, parameterMap);
094: }
095:
096: public static void onMaxFailuresByScreenName(String[] classes,
097: long companyId, String screenName, Map headerMap,
098: Map parameterMap) throws AuthException {
099:
100: onFailureByScreenName(classes, companyId, screenName,
101: headerMap, parameterMap);
102: }
103:
104: public static void onMaxFailuresByUserId(String[] classes,
105: long companyId, long userId, Map headerMap, Map parameterMap)
106: throws AuthException {
107:
108: onFailureByUserId(classes, companyId, userId, headerMap,
109: parameterMap);
110: }
111:
112: private static int _authenticate(String[] classes, long companyId,
113: String login, String password, String authType,
114: Map headerMap, Map parameterMap) throws AuthException {
115:
116: if ((classes == null) || (classes.length == 0)) {
117: return 1;
118: }
119:
120: for (int i = 0; i < classes.length; i++) {
121: String className = classes[i];
122:
123: if (Validator.isNotNull(className)) {
124: Authenticator auth = (Authenticator) InstancePool
125: .get(classes[i]);
126:
127: try {
128: int authResult = Authenticator.FAILURE;
129:
130: if (authType.equals(CompanyImpl.AUTH_TYPE_EA)) {
131: authResult = auth.authenticateByEmailAddress(
132: companyId, login, password, headerMap,
133: parameterMap);
134: } else if (authType
135: .equals(CompanyImpl.AUTH_TYPE_SN)) {
136: authResult = auth.authenticateByScreenName(
137: companyId, login, password, headerMap,
138: parameterMap);
139: } else if (authType
140: .equals(CompanyImpl.AUTH_TYPE_ID)) {
141: long userId = GetterUtil.getLong(login);
142:
143: authResult = auth.authenticateByUserId(
144: companyId, userId, password, headerMap,
145: parameterMap);
146: }
147:
148: if (authResult != Authenticator.SUCCESS) {
149: return authResult;
150: }
151: } catch (AuthException ae) {
152: throw ae;
153: } catch (Exception e) {
154: throw new AuthException(e);
155: }
156: }
157: }
158:
159: return Authenticator.SUCCESS;
160: }
161:
162: private static void _onFailure(String[] classes, long companyId,
163: String login, String authType, Map headerMap,
164: Map parameterMap) throws AuthException {
165:
166: if ((classes == null) || (classes.length == 0)) {
167: return;
168: }
169:
170: for (int i = 0; i < classes.length; i++) {
171: String className = classes[i];
172:
173: if (Validator.isNotNull(className)) {
174: AuthFailure authFailure = (AuthFailure) InstancePool
175: .get(classes[i]);
176:
177: try {
178: if (authType.equals(CompanyImpl.AUTH_TYPE_EA)) {
179: authFailure.onFailureByEmailAddress(companyId,
180: login, headerMap, parameterMap);
181: } else if (authType
182: .equals(CompanyImpl.AUTH_TYPE_SN)) {
183: authFailure.onFailureByScreenName(companyId,
184: login, headerMap, parameterMap);
185: } else if (authType
186: .equals(CompanyImpl.AUTH_TYPE_ID)) {
187: long userId = GetterUtil.getLong(login);
188:
189: authFailure.onFailureByUserId(companyId,
190: userId, headerMap, parameterMap);
191: }
192: } catch (AuthException ae) {
193: throw ae;
194: } catch (Exception e) {
195: throw new AuthException(e);
196: }
197: }
198: }
199: }
200:
201: }
|