1 package org.apache.archiva.redback.struts2.interceptor;
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
22 import com.opensymphony.xwork2.ActionContext;
23 import com.opensymphony.xwork2.ActionInvocation;
24 import com.opensymphony.xwork2.interceptor.Interceptor;
25 import org.apache.archiva.redback.keys.AuthenticationKey;
26 import org.apache.archiva.redback.policy.AccountLockedException;
27 import org.apache.archiva.redback.policy.MustChangePasswordException;
28 import org.apache.struts2.ServletActionContext;
29 import org.apache.archiva.redback.authentication.AuthenticationException;
30 import org.apache.archiva.redback.authentication.AuthenticationResult;
31 import org.apache.archiva.redback.authentication.TokenBasedAuthenticationDataSource;
32 import org.apache.archiva.redback.system.SecuritySession;
33 import org.apache.archiva.redback.system.SecuritySystem;
34 import org.apache.archiva.redback.system.SecuritySystemConstants;
35 import org.apache.archiva.redback.users.UserNotFoundException;
36 import org.apache.archiva.redback.integration.util.AutoLoginCookies;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
39 import org.springframework.context.annotation.Scope;
40 import org.springframework.stereotype.Controller;
42 import javax.inject.Inject;
43 import javax.servlet.http.HttpSession;
46 * AutoLoginInterceptor
48 * @author <a href="mailto:joakim@erdfelt.com">Joakim Erdfelt</a>
50 @Controller( "redbackAutoLoginInterceptor" )
52 public class AutoLoginInterceptor
53 implements Interceptor
55 private Logger log = LoggerFactory.getLogger( AutoLoginInterceptor.class );
57 static final String PASSWORD_CHANGE = "security-must-change-password";
59 static final String ACCOUNT_LOCKED = "security-login-locked";
65 private SecuritySystem securitySystem;
71 private AutoLoginCookies autologinCookies;
84 * @noinspection ProhibitedExceptionDeclared
86 public String intercept( ActionInvocation invocation )
89 SecuritySession securitySession = getSecuritySession();
91 if ( securitySession != null && securitySession.isAuthenticated() )
93 // User already authenticated.
94 log.debug( "User already authenticated." );
96 if ( !checkCookieConsistency( securitySession ) )
98 // update single sign on cookie
99 autologinCookies.setSignonCookie( securitySession.getUser().getUsername(),
100 ServletActionContext.getResponse(),
101 ServletActionContext.getRequest() );
106 AuthenticationKey authkey =
107 autologinCookies.getSignonKey( ServletActionContext.getResponse(), ServletActionContext.getRequest() );
109 if ( authkey != null )
113 securitySession = checkAuthentication( authkey, invocation.getInvocationContext().getName().equals(
116 if ( securitySession != null && securitySession.isAuthenticated() )
118 ActionContext.getContext().getSession().put( SecuritySystemConstants.SECURITY_SESSION_KEY,
120 checkCookieConsistency( securitySession );
124 autologinCookies.removeSignonCookie( ServletActionContext.getResponse(),
125 ServletActionContext.getRequest() );
126 autologinCookies.removeRememberMeCookie( ServletActionContext.getResponse(),
127 ServletActionContext.getRequest() );
130 catch ( AccountLockedException e )
132 log.info( "Account Locked : Username [{}]", e.getUser().getUsername(), e );
133 autologinCookies.removeSignonCookie( ServletActionContext.getResponse(),
134 ServletActionContext.getRequest() );
135 autologinCookies.removeRememberMeCookie( ServletActionContext.getResponse(),
136 ServletActionContext.getRequest() );
137 return ACCOUNT_LOCKED;
139 catch ( MustChangePasswordException e )
141 return PASSWORD_CHANGE;
144 else if ( autologinCookies.isRememberMeEnabled() )
146 authkey = autologinCookies.getRememberMeKey( ServletActionContext.getResponse(),
147 ServletActionContext.getRequest() );
149 if ( authkey != null )
153 securitySession = checkAuthentication( authkey, false );
155 if ( securitySession == null || !securitySession.isAuthenticated() )
157 autologinCookies.removeRememberMeCookie( ServletActionContext.getResponse(),
158 ServletActionContext.getRequest() );
161 catch ( AccountLockedException e )
163 log.info( "Account Locked : Username [{}]", e.getUser().getUsername(), e );
164 autologinCookies.removeRememberMeCookie( ServletActionContext.getResponse(),
165 ServletActionContext.getRequest() );
166 return ACCOUNT_LOCKED;
168 catch ( MustChangePasswordException e )
170 return PASSWORD_CHANGE;
176 return invocation.invoke();
179 private boolean checkCookieConsistency( SecuritySession securitySession )
181 String username = securitySession.getUser().getUsername();
183 boolean failed = false;
185 AuthenticationKey key =
186 autologinCookies.getRememberMeKey( ServletActionContext.getResponse(), ServletActionContext.getRequest() );
189 if ( !key.getForPrincipal().equals( username ) )
191 log.debug( "Login invalidated: remember me cookie was for{}; but session was for {}",
192 key.getForPrincipal(), username );
200 autologinCookies.getSignonKey( ServletActionContext.getResponse(), ServletActionContext.getRequest() );
203 if ( !key.getForPrincipal().equals( username ) )
205 log.debug( "Login invalidated: signon cookie was for {}; but session was for {}",
206 key.getForPrincipal(), username );
212 log.debug( "Login invalidated: signon cookie was removed" );
219 removeCookiesAndSession();
225 private SecuritySession checkAuthentication( AuthenticationKey authkey, boolean enforcePasswordChange )
226 throws AccountLockedException, MustChangePasswordException
228 SecuritySession securitySession = null;
229 log.debug( "Logging in with an authentication key: {}", authkey.getForPrincipal() );
230 TokenBasedAuthenticationDataSource authsource = new TokenBasedAuthenticationDataSource();
231 authsource.setPrincipal( authkey.getForPrincipal() );
232 authsource.setToken( authkey.getKey() );
233 authsource.setEnforcePasswordChange( enforcePasswordChange );
237 securitySession = securitySystem.authenticate( authsource );
239 if ( securitySession.isAuthenticated() )
241 // TODO: this should not happen if there is a password change required - but the password change action needs to log the user in on success to swap them
242 log.debug( "Login success." );
244 HttpSession session = ServletActionContext.getRequest().getSession( true );
245 session.setAttribute( SecuritySystemConstants.SECURITY_SESSION_KEY, securitySession );
246 log.debug( "Setting session:{} to {}", SecuritySystemConstants.SECURITY_SESSION_KEY, securitySession );
248 autologinCookies.setSignonCookie( authkey.getForPrincipal(), ServletActionContext.getResponse(),
249 ServletActionContext.getRequest() );
253 AuthenticationResult result = securitySession.getAuthenticationResult();
254 log.info( "Login interceptor failed against principal : {}", result.getPrincipal(),
255 result.getException() );
259 catch ( AuthenticationException e )
261 log.info( "Authentication Exception.", e );
263 catch ( UserNotFoundException e )
265 log.info( "User Not Found: {}", authkey.getForPrincipal(), e );
267 return securitySession;
270 private void removeCookiesAndSession()
272 autologinCookies.removeRememberMeCookie( ServletActionContext.getResponse(),
273 ServletActionContext.getRequest() );
274 autologinCookies.removeSignonCookie( ServletActionContext.getResponse(), ServletActionContext.getRequest() );
276 HttpSession session = ServletActionContext.getRequest().getSession();
277 if ( session != null )
279 session.removeAttribute( SecuritySystemConstants.SECURITY_SESSION_KEY );
283 private SecuritySession getSecuritySession()
285 HttpSession session = ServletActionContext.getRequest().getSession();
286 if ( session == null )
288 log.debug( "No HTTP Session exists." );
292 SecuritySession secSession =
293 (SecuritySession) session.getAttribute( SecuritySystemConstants.SECURITY_SESSION_KEY );
294 log.debug( "Returning Security Session: {}", secSession );