1/*2 * Licensed to the Apache Software Foundation (ASF) under one or more3 * contributor license agreements. See the NOTICE file distributed with4 * this work for additional information regarding copyright ownership.5 * The ASF licenses this file to You under the Apache License, Version 2.06 * (the "License"); you may not use this file except in compliance with7 * the License. You may obtain a copy of the License at8 * 9 * http://www.apache.org/licenses/LICENSE-2.010 * 11 * Unless required by applicable law or agreed to in writing, software12 * distributed under the License is distributed on an "AS IS" BASIS,13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.14 * See the License for the specific language governing permissions and15 * limitations under the License.16 */17packageorg.apache.jetspeed.security.impl;
1819import java.util.Iterator;
20import java.util.LinkedList;
21import java.util.List;
2223import org.apache.commons.logging.Log;
24import org.apache.commons.logging.LogFactory;
25import org.apache.jetspeed.Jetspeed;
26import org.apache.jetspeed.login.LoginConstants;
27import org.apache.jetspeed.pipeline.PipelineException;
28import org.apache.jetspeed.pipeline.valve.AbstractValve;
29import org.apache.jetspeed.pipeline.valve.ValveContext;
30import org.apache.jetspeed.request.RequestContext;
31import org.apache.jetspeed.security.PasswordCredential;
32import org.apache.jetspeed.security.SecurityException;
33import org.apache.jetspeed.security.SecurityHelper;
34import org.apache.jetspeed.security.User;
35import org.apache.jetspeed.security.UserManager;
36import org.apache.jetspeed.security.UserPrincipal;
3738/***39 * LoginValidationValve40 *41 * @author <a href="mailto:ate@apache.org">Ate Douma</a>42 * @version $Id: LoginValidationValveImpl.java 544402 2007-06-05 06:20:00Z taylor $43 */44publicclassLoginValidationValveImplextendsAbstractValve implements org.apache.jetspeed.pipeline.valve.LoginValidationValve
45 {
46privatestaticfinal Log log = LogFactory.getLog(LoginValidationValveImpl.class);
4748privateint maxNumberOfAuthenticationFailures;
49private List sessionAttributes;
5051/***52 * Creates a LoginValidationValveImpl instance which doesn't evaluate the maxNumberOfAuthenticationFailures 53 * for LoginConstant.ERROR_FINAL_LOGIN_ATTEMPT error reporting.54 */55publicLoginValidationValveImpl(List sessionAttributes)
56 {
57this.sessionAttributes = sessionAttributes;
58 }
5960/***61 * <p>62 * Creates a LoginValidationValveImpl instance which can evaluate {@link PasswordCredential#getAuthenticationFailures()}63 * to determine if a user only has one login attempt available before the maxNumberOfAuthenticationFailures parameter64 * value is reached and the credential will be disabled.</p>65 * <p>66 * The provided maxNumberOfAuthenticationFailures value should be equal to the value configured for the67 * MaxPasswordAuthenticationFailuresInterceptor (and > 2 to be useful).</p>68 */69publicLoginValidationValveImpl(int maxNumberOfAuthenticationFailures)
70 {
71this.maxNumberOfAuthenticationFailures = maxNumberOfAuthenticationFailures;
72this.sessionAttributes = new LinkedList();
73 }
7475/***76 * <p>77 * Creates a LoginValidationValveImpl instance which can evaluate {@link PasswordCredential#getAuthenticationFailures()}78 * to determine if a user only has one login attempt available before the maxNumberOfAuthenticationFailures parameter79 * value is reached and the credential will be disabled.</p>80 * <p>81 * The provided maxNumberOfAuthenticationFailures value should be equal to the value configured for the82 * MaxPasswordAuthenticationFailuresInterceptor (and > 2 to be useful).</p>83 */84publicLoginValidationValveImpl(int maxNumberOfAuthenticationFailures, List sessionAttributes)
85 {
86this.maxNumberOfAuthenticationFailures = maxNumberOfAuthenticationFailures;
87this.sessionAttributes = sessionAttributes;
88 }
8990/***91 * @see org.apache.jetspeed.pipeline.valve.Valve#invoke(org.apache.jetspeed.request.RequestContext, org.apache.jetspeed.pipeline.valve.ValveContext)92 */93publicvoid invoke(RequestContext request, ValveContext context) throws PipelineException
94 {
95try96 {
97if ( request.getRequest().getUserPrincipal() == null )
98 {
99if ( request.getSessionAttribute(LoginConstants.RETRYCOUNT) != null )
100 {
101// we have a login attempt failure102 String userName = (String)request.getSessionAttribute(LoginConstants.USERNAME);
103if ( userName != null && !userName.equals(""))
104 {
105 UserManager um = (UserManager)Jetspeed.getComponentManager().getComponent(UserManager.class);
106if ( um != null )
107 {
108 User user = null;
109try110 {
111 user = um.getUser(userName);
112 UserPrincipal userPrincipal = (UserPrincipal)SecurityHelper.getPrincipal(user.getSubject(), UserPrincipal.class);
113if ( !userPrincipal.isEnabled() )
114 {
115 request.setSessionAttribute(LoginConstants.ERRORCODE, LoginConstants.ERROR_USER_DISABLED);
116 }
117else118 {
119 PasswordCredential pwdCredential = SecurityHelper.getPasswordCredential(user.getSubject());
120if ( pwdCredential == null || !pwdCredential.isEnabled() )
121 {
122 request.setSessionAttribute(LoginConstants.ERRORCODE, LoginConstants.ERROR_CREDENTIAL_DISABLED);
123 }
124elseif ( pwdCredential.isExpired() )
125 {
126 request.setSessionAttribute(LoginConstants.ERRORCODE, LoginConstants.ERROR_CREDENTIAL_EXPIRED);
127 }
128elseif ( maxNumberOfAuthenticationFailures > 1 && pwdCredential.getAuthenticationFailures() == maxNumberOfAuthenticationFailures -1 )
129 {
130 request.setSessionAttribute(LoginConstants.ERRORCODE, LoginConstants.ERROR_FINAL_LOGIN_ATTEMPT);
131 }
132else133 {
134 request.setSessionAttribute(LoginConstants.ERRORCODE, LoginConstants.ERROR_INVALID_PASSWORD);
135 }
136 }
137 }
138catch (SecurityException sex)
139 {
140 request.setSessionAttribute(LoginConstants.ERRORCODE, LoginConstants.ERROR_UNKNOWN_USER);
141 }
142 }
143 }
144else145 {
146 request.setSessionAttribute(LoginConstants.ERRORCODE, LoginConstants.ERROR_UNKNOWN_USER);
147 }
148 }
149 }
150else151 {
152if (request.getSessionAttribute(LoginConstants.LOGIN_CHECK) == null)
153 {
154 clearSessionAttributes(request);
155 request.getRequest().getSession().setAttribute(LoginConstants.LOGIN_CHECK, "true");
156 }
157 }
158159 context.invokeNext(request);
160 }
161catch (Exception e)
162 {
163 log.error("Exception in request pipeline: " + e.getMessage(), e);
164thrownew PipelineException(e.toString(), e);
165 }
166 }
167168privatevoid clearSessionAttributes(RequestContext request)
169 {
170 Iterator attributes = this.sessionAttributes.iterator();
171while (attributes.hasNext())
172 {
173 String attribute = (String)attributes.next();
174 request.getRequest().getSession().removeAttribute(attribute);
175 }
176 }
177178public String toString()
179 {
180return"LoginValidationValve";
181 }
182183 }