mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-07 05:55:44 +01:00
improve authentication system
This commit is contained in:
@@ -46,7 +46,8 @@ import sonia.scm.plugin.ext.Extension;
|
||||
import sonia.scm.plugin.ext.ExtensionProcessor;
|
||||
import sonia.scm.repository.RepositoryHandler;
|
||||
import sonia.scm.security.EncryptionHandler;
|
||||
import sonia.scm.web.security.Authenticator;
|
||||
import sonia.scm.web.security.AuthenticationManager;
|
||||
import sonia.scm.web.security.XmlAuthenticationManager;
|
||||
|
||||
//~--- JDK imports ------------------------------------------------------------
|
||||
|
||||
@@ -90,6 +91,10 @@ public class BindingExtensionProcessor implements ExtensionProcessor
|
||||
{
|
||||
Multibinder<RepositoryHandler> repositoryHandlers =
|
||||
Multibinder.newSetBinder(binder, RepositoryHandler.class);
|
||||
Multibinder<AuthenticationManager> authenticators =
|
||||
Multibinder.newSetBinder(binder, AuthenticationManager.class);
|
||||
|
||||
authenticators.addBinding().to(XmlAuthenticationManager.class);
|
||||
|
||||
for (Class extensionClass : extensions)
|
||||
{
|
||||
@@ -103,17 +108,21 @@ public class BindingExtensionProcessor implements ExtensionProcessor
|
||||
}
|
||||
|
||||
binder.bind(extensionClass);
|
||||
|
||||
repositoryHandlers.addBinding().to(extensionClass);
|
||||
|
||||
}
|
||||
else if (EncryptionHandler.class.isAssignableFrom(extensionClass))
|
||||
{
|
||||
bind(binder, EncryptionHandler.class, extensionClass);
|
||||
}
|
||||
else if (Authenticator.class.isAssignableFrom(extensionClass))
|
||||
else if (AuthenticationManager.class.isAssignableFrom(extensionClass))
|
||||
{
|
||||
bind(binder, Authenticator.class, extensionClass);
|
||||
if (logger.isInfoEnabled())
|
||||
{
|
||||
logger.info("bind Authenticator {}", extensionClass.getName());
|
||||
}
|
||||
|
||||
binder.bind(extensionClass);
|
||||
authenticators.addBinding().to(extensionClass);
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
@@ -44,13 +44,16 @@ import sonia.scm.plugin.DefaultPluginManager;
|
||||
import sonia.scm.plugin.PluginManager;
|
||||
import sonia.scm.repository.RepositoryManager;
|
||||
import sonia.scm.user.UserManager;
|
||||
import sonia.scm.web.security.Authenticator;
|
||||
import sonia.scm.util.IOUtil;
|
||||
import sonia.scm.web.security.AuthenticationManager;
|
||||
|
||||
//~--- JDK imports ------------------------------------------------------------
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import javax.servlet.ServletContextEvent;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Sebastian Sdorra
|
||||
@@ -58,6 +61,33 @@ import java.util.List;
|
||||
public class ContextListener extends GuiceServletContextListener
|
||||
{
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @param servletContextEvent
|
||||
*/
|
||||
@Override
|
||||
public void contextDestroyed(ServletContextEvent servletContextEvent)
|
||||
{
|
||||
if (injector != null)
|
||||
{
|
||||
|
||||
// close RepositoryManager
|
||||
IOUtil.close(injector.getInstance(RepositoryManager.class));
|
||||
|
||||
// close Authenticator
|
||||
IOUtil.close(injector.getInstance(AuthenticationManager.class));
|
||||
|
||||
// close UserManager
|
||||
IOUtil.close(injector.getInstance(UserManager.class));
|
||||
}
|
||||
|
||||
super.contextDestroyed(servletContextEvent);
|
||||
}
|
||||
|
||||
//~--- get methods ----------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
@@ -78,8 +108,7 @@ public class ContextListener extends GuiceServletContextListener
|
||||
new ArrayList<Module>(bindExtProcessor.getModuleSet());
|
||||
|
||||
moduleList.add(0, main);
|
||||
|
||||
Injector injector = Guice.createInjector(moduleList);
|
||||
injector = Guice.createInjector(moduleList);
|
||||
|
||||
// init RepositoryManager
|
||||
injector.getInstance(RepositoryManager.class).init(SCMContext.getContext());
|
||||
@@ -88,8 +117,14 @@ public class ContextListener extends GuiceServletContextListener
|
||||
injector.getInstance(UserManager.class).init(SCMContext.getContext());
|
||||
|
||||
// init Authenticator
|
||||
injector.getInstance(Authenticator.class).init(SCMContext.getContext());
|
||||
injector.getInstance(AuthenticationManager.class).init(
|
||||
SCMContext.getContext());
|
||||
|
||||
return injector;
|
||||
}
|
||||
|
||||
//~--- fields ---------------------------------------------------------------
|
||||
|
||||
/** Field description */
|
||||
private Injector injector;
|
||||
}
|
||||
|
||||
@@ -56,10 +56,10 @@ import sonia.scm.security.SecurityContext;
|
||||
import sonia.scm.user.UserManager;
|
||||
import sonia.scm.user.xml.XmlUserManager;
|
||||
import sonia.scm.util.DebugServlet;
|
||||
import sonia.scm.web.security.Authenticator;
|
||||
import sonia.scm.web.security.AuthenticationManager;
|
||||
import sonia.scm.web.security.BasicSecurityContext;
|
||||
import sonia.scm.web.security.ChainAuthenticatonManager;
|
||||
import sonia.scm.web.security.WebSecurityContext;
|
||||
import sonia.scm.web.security.XmlAuthenticator;
|
||||
|
||||
//~--- JDK imports ------------------------------------------------------------
|
||||
|
||||
@@ -155,10 +155,14 @@ public class ScmServletModule extends ServletModule
|
||||
bind(ScmConfiguration.class).toInstance(config);
|
||||
bind(PluginManager.class).toInstance(pluginManager);
|
||||
bind(EncryptionHandler.class).to(MessageDigestEncryptionHandler.class);
|
||||
bind(Authenticator.class).to(XmlAuthenticator.class);
|
||||
bindExtProcessor.bindExtensions(binder());
|
||||
|
||||
// bind security stuff
|
||||
bind(AuthenticationManager.class).to(ChainAuthenticatonManager.class);
|
||||
bind(SecurityContext.class).to(BasicSecurityContext.class);
|
||||
bind(WebSecurityContext.class).to(BasicSecurityContext.class);
|
||||
bindExtProcessor.bindExtensions(binder());
|
||||
|
||||
// bind security cache
|
||||
bind(CacheManager.class).to(EhCacheManager.class);
|
||||
|
||||
// bind(RepositoryManager.class).annotatedWith(Undecorated.class).to(
|
||||
|
||||
@@ -1,157 +0,0 @@
|
||||
/**
|
||||
* 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 com.google.inject.Inject;
|
||||
import com.google.inject.Singleton;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import sonia.scm.SCMContextProvider;
|
||||
import sonia.scm.security.EncryptionHandler;
|
||||
import sonia.scm.user.User;
|
||||
|
||||
//~--- JDK imports ------------------------------------------------------------
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import sonia.scm.user.UserManager;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Sebastian Sdorra
|
||||
*/
|
||||
@Singleton
|
||||
public class XmlAuthenticator implements Authenticator
|
||||
{
|
||||
|
||||
/** Field description */
|
||||
public static final String NAME_DIRECTORY = "users";
|
||||
|
||||
/** the logger for XmlAuthenticator */
|
||||
private static final Logger logger =
|
||||
LoggerFactory.getLogger(XmlAuthenticator.class);
|
||||
|
||||
//~--- methods --------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @param request
|
||||
* @param response
|
||||
* @param username
|
||||
* @param password
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public User authenticate(HttpServletRequest request,
|
||||
HttpServletResponse response, String username,
|
||||
String password)
|
||||
{
|
||||
User user = userManager.get(username);
|
||||
|
||||
if (user != null)
|
||||
{
|
||||
String encryptedPassword = encryptionHandler.encrypt(password);
|
||||
|
||||
if (!encryptedPassword.equalsIgnoreCase(user.getPassword()))
|
||||
{
|
||||
user = null;
|
||||
|
||||
if (logger.isDebugEnabled())
|
||||
{
|
||||
logger.debug("password for user {} is wrong", username);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (logger.isDebugEnabled())
|
||||
{
|
||||
logger.debug("user {} logged in successfully", username);
|
||||
}
|
||||
|
||||
user.setPassword(null);
|
||||
}
|
||||
}
|
||||
else if (logger.isDebugEnabled())
|
||||
{
|
||||
logger.debug("could not find user {}", username);
|
||||
}
|
||||
|
||||
return user;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @throws IOException
|
||||
*/
|
||||
@Override
|
||||
public void close() throws IOException
|
||||
{
|
||||
|
||||
// do nothing
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @param provider
|
||||
*/
|
||||
@Override
|
||||
public void init(SCMContextProvider provider)
|
||||
{
|
||||
|
||||
// do nothing
|
||||
}
|
||||
|
||||
//~--- fields ---------------------------------------------------------------
|
||||
|
||||
/** Field description */
|
||||
@Inject
|
||||
private EncryptionHandler encryptionHandler;
|
||||
|
||||
/** Field description */
|
||||
@Inject
|
||||
private UserManager userManager;
|
||||
}
|
||||
Reference in New Issue
Block a user