improve authentication system

This commit is contained in:
Sebastian Sdorra
2010-12-04 16:01:27 +01:00
parent d506310e29
commit 8e49f50ced
7 changed files with 146 additions and 41 deletions

View File

@@ -0,0 +1,68 @@
/**
* Copyright (c) 2010, Sebastian Sdorra
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* 3. Neither the name of SCM-Manager; nor the names of its
* contributors may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* http://bitbucket.org/sdorra/scm-manager
*
*/
package sonia.scm.web.security;
//~--- non-JDK imports --------------------------------------------------------
import sonia.scm.Initable;
import sonia.scm.TypedObject;
//~--- JDK imports ------------------------------------------------------------
import java.io.Closeable;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
*
* @author Sebastian Sdorra
*/
public interface AuthenticationHandler extends Initable, Closeable, TypedObject
{
/**
* Method description
*
*
* @param request
* @param response
* @param username
* @param password
*
* @return
*/
public AuthenticationResult authenticate(HttpServletRequest request,
HttpServletResponse response, String username, String password);
}

View File

@@ -36,6 +36,7 @@ package sonia.scm.web.security;
//~--- non-JDK imports -------------------------------------------------------- //~--- non-JDK imports --------------------------------------------------------
import sonia.scm.Initable; import sonia.scm.Initable;
import sonia.scm.user.User;
//~--- JDK imports ------------------------------------------------------------ //~--- JDK imports ------------------------------------------------------------
@@ -62,6 +63,7 @@ public interface AuthenticationManager extends Initable, Closeable
* *
* @return * @return
*/ */
public AuthenticationResult authenticate(HttpServletRequest request, public User authenticate(HttpServletRequest request,
HttpServletResponse response, String username, String password); HttpServletResponse response, String username,
String password);
} }

View File

@@ -45,8 +45,8 @@ public class AuthenticationResult
{ {
/** Field description */ /** Field description */
public static final AuthenticationResult NEXT = public static final AuthenticationResult NOT_FOUND =
new AuthenticationResult(AuthenticationState.NEXT); new AuthenticationResult(AuthenticationState.NOT_FOUND);
/** Field description */ /** Field description */
public static final AuthenticationResult FAILED = public static final AuthenticationResult FAILED =
@@ -65,6 +65,18 @@ public class AuthenticationResult
this.state = state; this.state = state;
} }
/**
* Constructs ...
*
*
*
* @param user
*/
public AuthenticationResult(User user)
{
this.state = AuthenticationState.SUCCESS;
}
/** /**
* Constructs ... * Constructs ...
* *

View File

@@ -39,8 +39,7 @@ package sonia.scm.web.security;
*/ */
public enum AuthenticationState public enum AuthenticationState
{ {
CREATE_USER(true), MODIFY_USER(true), SUCCESS(true), NEXT(false), SUCCESS(true), NOT_FOUND(false), FAILED(false);
FAILED(false);
/** /**
* Constructs ... * Constructs ...

View File

@@ -96,28 +96,26 @@ public class BasicSecurityContext implements WebSecurityContext
HttpServletResponse response, String username, HttpServletResponse response, String username,
String password) String password)
{ {
AuthenticationResult result = authenticator.authenticate(request, response, user = authenticator.authenticate(request, response, username, password);
username, password);
if (result.getState().isSuccessfully()) if (user != null)
{ {
user = result.getUser();
try try
{ {
switch (result.getState()) user.setLastLogin(System.currentTimeMillis());
if (userManager.contains(username))
{ {
case CREATE_USER : userManager.modify(user);
userManager.create(user); }
else
break; {
userManager.create(user);
case MODIFY_USER :
userManager.modify(user);
} }
} }
catch (Exception ex) catch (Exception ex)
{ {
user = null;
logger.error(ex.getMessage(), ex); logger.error(ex.getMessage(), ex);
} }
} }

View File

@@ -42,6 +42,7 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import sonia.scm.SCMContextProvider; import sonia.scm.SCMContextProvider;
import sonia.scm.user.User;
import sonia.scm.util.AssertUtil; import sonia.scm.util.AssertUtil;
import sonia.scm.util.IOUtil; import sonia.scm.util.IOUtil;
@@ -72,13 +73,14 @@ public class ChainAuthenticatonManager implements AuthenticationManager
* Constructs ... * Constructs ...
* *
* *
* @param authenticatorSet * @param authenticationHandlerSet
*/ */
@Inject @Inject
public ChainAuthenticatonManager(Set<AuthenticationManager> authenticatorSet) public ChainAuthenticatonManager(
Set<AuthenticationHandler> authenticationHandlerSet)
{ {
AssertUtil.assertIsNotEmpty(authenticatorSet); AssertUtil.assertIsNotEmpty(authenticationHandlerSet);
this.authenticatorSet = authenticatorSet; this.authenticationHandlerSet = authenticationHandlerSet;
} }
//~--- methods -------------------------------------------------------------- //~--- methods --------------------------------------------------------------
@@ -95,17 +97,18 @@ public class ChainAuthenticatonManager implements AuthenticationManager
* @return * @return
*/ */
@Override @Override
public AuthenticationResult authenticate(HttpServletRequest request, public User authenticate(HttpServletRequest request,
HttpServletResponse response, String username, String password) HttpServletResponse response, String username,
String password)
{ {
AuthenticationResult result = null; User user = null;
for (AuthenticationManager authenticator : authenticatorSet) for (AuthenticationHandler authenticator : authenticationHandlerSet)
{ {
try try
{ {
result = authenticator.authenticate(request, response, username, AuthenticationResult result = authenticator.authenticate(request,
password); response, username, password);
if (logger.isDebugEnabled()) if (logger.isDebugEnabled())
{ {
@@ -113,9 +116,16 @@ public class ChainAuthenticatonManager implements AuthenticationManager
authenticator.getClass().getName(), result); authenticator.getClass().getName(), result);
} }
if (((result != null) && result.getState().isSuccessfully()) if ((result != null) && (result.getState() != null)
|| (result.getState() == AuthenticationState.FAILED)) && (result.getState().isSuccessfully()
|| (result.getState() == AuthenticationState.FAILED)))
{ {
if (result.getState().isSuccessfully() && (result.getUser() != null))
{
user = result.getUser();
user.setType(authenticator.getType());
}
break; break;
} }
} }
@@ -125,7 +135,7 @@ public class ChainAuthenticatonManager implements AuthenticationManager
} }
} }
return result; return user;
} }
/** /**
@@ -137,7 +147,7 @@ public class ChainAuthenticatonManager implements AuthenticationManager
@Override @Override
public void close() throws IOException public void close() throws IOException
{ {
for (AuthenticationManager authenticator : authenticatorSet) for (AuthenticationHandler authenticator : authenticationHandlerSet)
{ {
IOUtil.close(authenticator); IOUtil.close(authenticator);
} }
@@ -152,7 +162,7 @@ public class ChainAuthenticatonManager implements AuthenticationManager
@Override @Override
public void init(SCMContextProvider context) public void init(SCMContextProvider context)
{ {
for (AuthenticationManager authenticator : authenticatorSet) for (AuthenticationHandler authenticator : authenticationHandlerSet)
{ {
authenticator.init(context); authenticator.init(context);
} }
@@ -161,5 +171,5 @@ public class ChainAuthenticatonManager implements AuthenticationManager
//~--- fields --------------------------------------------------------------- //~--- fields ---------------------------------------------------------------
/** Field description */ /** Field description */
private Set<AuthenticationManager> authenticatorSet; private Set<AuthenticationHandler> authenticationHandlerSet;
} }

View File

@@ -58,7 +58,7 @@ import javax.servlet.http.HttpServletResponse;
* @author Sebastian Sdorra * @author Sebastian Sdorra
*/ */
@Singleton @Singleton
public class XmlAuthenticationManager implements AuthenticationManager public class XmlAuthenticationHandler implements AuthenticationHandler
{ {
/** Field description */ /** Field description */
@@ -67,9 +67,9 @@ public class XmlAuthenticationManager implements AuthenticationManager
/** Field description */ /** Field description */
public static final String TYPE = "xml"; public static final String TYPE = "xml";
/** the logger for XmlAuthenticationManager */ /** the logger for XmlAuthenticationHandler */
private static final Logger logger = private static final Logger logger =
LoggerFactory.getLogger(XmlAuthenticationManager.class); LoggerFactory.getLogger(XmlAuthenticationHandler.class);
//~--- constructors --------------------------------------------------------- //~--- constructors ---------------------------------------------------------
@@ -81,7 +81,7 @@ public class XmlAuthenticationManager implements AuthenticationManager
* @param encryptionHandler * @param encryptionHandler
*/ */
@Inject @Inject
public XmlAuthenticationManager(UserManager userManager, public XmlAuthenticationHandler(UserManager userManager,
EncryptionHandler encryptionHandler) EncryptionHandler encryptionHandler)
{ {
this.userManager = userManager; this.userManager = userManager;
@@ -121,7 +121,7 @@ public class XmlAuthenticationManager implements AuthenticationManager
logger.debug("{} is not an xml user", username); logger.debug("{} is not an xml user", username);
} }
result = AuthenticationResult.NEXT; result = AuthenticationResult.NOT_FOUND;
} }
} }
else else
@@ -131,7 +131,7 @@ public class XmlAuthenticationManager implements AuthenticationManager
logger.debug("could not find user {}", username); logger.debug("could not find user {}", username);
} }
result = AuthenticationResult.NEXT; result = AuthenticationResult.NOT_FOUND;
} }
return result; return result;
@@ -163,6 +163,22 @@ public class XmlAuthenticationManager implements AuthenticationManager
// do nothing // do nothing
} }
//~--- get methods ----------------------------------------------------------
/**
* Method description
*
*
* @return
*/
@Override
public String getType()
{
return TYPE;
}
//~--- methods --------------------------------------------------------------
/** /**
* Method description * Method description
* *