1 package org.codehaus.redback.integration.filter.authentication;
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 org.apache.archiva.redback.authentication.AuthenticationException;
23 import org.apache.archiva.redback.policy.MustChangePasswordException;
24 import org.apache.archiva.redback.users.User;
25 import org.apache.archiva.redback.users.UserNotFoundException;
26 import org.apache.archiva.redback.authentication.AuthenticationDataSource;
27 import org.apache.archiva.redback.authentication.AuthenticationResult;
28 import org.apache.archiva.redback.policy.AccountLockedException;
29 import org.apache.archiva.redback.system.SecuritySession;
30 import org.apache.archiva.redback.system.SecuritySystem;
31 import org.apache.archiva.redback.system.SecuritySystemConstants;
32 import org.codehaus.plexus.util.StringUtils;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
36 import javax.inject.Inject;
37 import javax.servlet.http.HttpServletRequest;
38 import javax.servlet.http.HttpServletResponse;
39 import javax.servlet.http.HttpSession;
40 import java.io.IOException;
45 * @author <a href="mailto:joakim@erdfelt.com">Joakim Erdfelt</a>
48 public abstract class HttpAuthenticator
50 protected Logger log = LoggerFactory.getLogger( getClass() );
53 protected SecuritySystem securitySystem;
56 * The Public Face of the Authenticator.
58 * @throws MustChangePasswordException
59 * @throws AccountLockedException
61 public AuthenticationResult authenticate( AuthenticationDataSource ds, HttpSession httpSession )
62 throws AuthenticationException, AccountLockedException, MustChangePasswordException
66 SecuritySession securitySession = securitySystem.authenticate( ds );
68 setSecuritySession( securitySession, httpSession );
70 return securitySession.getAuthenticationResult();
72 catch ( AuthenticationException e )
74 String msg = "Unable to authenticate user: " + ds;
76 throw new HttpAuthenticationException( msg, e );
78 catch ( UserNotFoundException e )
80 log.info( "Login attempt against unknown user: {}", ds );
81 throw new HttpAuthenticationException( "User name or password invalid." );
86 * Entry point for a Filter.
90 * @throws AuthenticationException
92 public void authenticate( HttpServletRequest request, HttpServletResponse response )
93 throws AuthenticationException
97 AuthenticationResult result = getAuthenticationResult( request, response );
99 if ( ( result == null ) || ( !result.isAuthenticated() ) )
101 throw new HttpAuthenticationException( "You are not authenticated." );
104 catch ( AccountLockedException e )
106 throw new HttpAuthenticationException( "Your account is locked.", e );
108 catch ( MustChangePasswordException e )
110 throw new HttpAuthenticationException( "You must change your password.", e );
116 * Issue a Challenge Response back to the HTTP Client.
122 * @throws IOException
124 public abstract void challenge( HttpServletRequest request, HttpServletResponse response, String realmName,
125 AuthenticationException exception )
129 * Parse the incoming request and return an AuthenticationResult.
133 * @return null if no http auth credentials, or the actual authentication result based on the credentials.
134 * @throws AuthenticationException
135 * @throws MustChangePasswordException
136 * @throws AccountLockedException
138 public abstract AuthenticationResult getAuthenticationResult( HttpServletRequest request,
139 HttpServletResponse response )
140 throws AuthenticationException, AccountLockedException, MustChangePasswordException;
143 public User getSessionUser( HttpSession httpSession )
145 return (User) httpSession.getAttribute( SecuritySession.USERKEY );
148 public boolean isAlreadyAuthenticated( HttpSession httpSession )
150 User user = getSessionUser( httpSession );
152 return ( ( user != null ) && !user.isLocked() && !user.isPasswordChangeRequired() );
155 public SecuritySession getSecuritySession( HttpSession httpSession )
157 SecuritySession securitySession = (SecuritySession) httpSession.getAttribute( SecuritySession.SESSION_KEY );
158 if ( securitySession != null )
160 return securitySession;
162 return (SecuritySession) httpSession.getAttribute( SecuritySystemConstants.SECURITY_SESSION_KEY );
167 public void setSecuritySession( SecuritySession session, HttpSession httpSession )
169 httpSession.setAttribute( SecuritySession.SESSION_KEY, session );
170 httpSession.setAttribute( SecuritySession.USERKEY, session.getUser() );
173 public void setSessionUser( User user, HttpSession httpSession )
175 httpSession.setAttribute( SecuritySession.SESSION_KEY, null );
176 httpSession.setAttribute( SecuritySession.USERKEY, user );
179 public String storeDefaultUser( String principal, HttpSession httpSession )
181 httpSession.setAttribute( SecuritySession.SESSION_KEY, null );
182 httpSession.setAttribute( SecuritySession.USERKEY, null );
184 if ( StringUtils.isEmpty( principal ) )
191 User user = securitySystem.getUserManager().findUser( principal );
192 httpSession.setAttribute( SecuritySession.USERKEY, user );
194 return user.getPrincipal().toString();
197 catch ( UserNotFoundException e )
199 log.warn( "Default User '" + principal + "' not found.", e );