Files
SCM-Manager/scm-webapp/src/main/java/sonia/scm/lifecycle/modules/ScmSecurityModule.java

179 lines
6.0 KiB
Java
Raw Normal View History

/*
* MIT License
2012-08-27 08:04:44 +02:00
*
* Copyright (c) 2020-present Cloudogu GmbH and Contributors
2012-08-27 08:04:44 +02:00
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
2012-08-27 08:04:44 +02:00
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
2012-08-27 08:04:44 +02:00
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
2012-08-27 08:04:44 +02:00
*/
package sonia.scm.lifecycle.modules;
2012-08-27 08:04:44 +02:00
//~--- non-JDK imports --------------------------------------------------------
2013-02-17 13:59:00 +01:00
import com.google.inject.name.Names;
2020-08-20 17:44:36 +02:00
import org.apache.shiro.authc.Authenticator;
import org.apache.shiro.authc.credential.DefaultPasswordService;
import org.apache.shiro.authc.credential.PasswordService;
2020-08-20 17:44:36 +02:00
import org.apache.shiro.authc.pam.AuthenticationStrategy;
import org.apache.shiro.authc.pam.ModularRealmAuthenticator;
import org.apache.shiro.authz.permission.PermissionResolver;
import org.apache.shiro.crypto.hash.DefaultHashService;
2012-08-27 08:04:44 +02:00
import org.apache.shiro.guice.web.ShiroWebModule;
import org.apache.shiro.mgt.DefaultSessionStorageEvaluator;
import org.apache.shiro.mgt.DefaultSubjectDAO;
import org.apache.shiro.mgt.SubjectDAO;
2014-12-19 17:52:44 +01:00
import org.apache.shiro.realm.Realm;
2012-08-27 08:04:44 +02:00
2014-12-19 17:52:44 +01:00
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import sonia.scm.plugin.ExtensionProcessor;
2012-08-27 08:04:44 +02:00
//~--- JDK imports ------------------------------------------------------------
import javax.servlet.ServletContext;
import org.apache.shiro.mgt.RememberMeManager;
import sonia.scm.security.DisabledRememberMeManager;
2020-08-20 17:44:36 +02:00
import sonia.scm.security.ScmAtLeastOneSuccessfulStrategy;
import sonia.scm.security.ScmPermissionResolver;
2012-08-27 08:04:44 +02:00
/**
*
* @author Sebastian Sdorra
*/
public class ScmSecurityModule extends ShiroWebModule
{
/** Field description */
private static final int ITERATIONS = 8192;
2014-12-19 17:52:44 +01:00
/**
* the logger for ScmSecurityModule
*/
private static final Logger logger =
LoggerFactory.getLogger(ScmSecurityModule.class);
//~--- constructors ---------------------------------------------------------
2012-08-27 08:04:44 +02:00
/**
* Constructs ...
*
*
* @param servletContext
2014-12-19 17:52:44 +01:00
* @param extensionProcessor
2012-08-27 08:04:44 +02:00
*/
public ScmSecurityModule(ServletContext servletContext, ExtensionProcessor extensionProcessor)
2012-08-27 08:04:44 +02:00
{
super(servletContext);
2014-12-19 17:52:44 +01:00
this.extensionProcessor = extensionProcessor;
2012-08-27 08:04:44 +02:00
}
//~--- methods --------------------------------------------------------------
/**
* Method description
*
*/
@Override
2014-01-11 15:17:44 +01:00
@SuppressWarnings("unchecked")
2012-08-27 08:04:44 +02:00
protected void configureShiroWeb()
{
bind(PasswordService.class).toInstance(createPasswordService());
// expose password service to global injector
expose(PasswordService.class);
// disable remember me cookie generation
bind(RememberMeManager.class).to(DisabledRememberMeManager.class);
2013-02-17 13:59:00 +01:00
2020-08-20 17:44:36 +02:00
// bind authentication strategy
bind(ModularRealmAuthenticator.class);
bind(Authenticator.class).to(ModularRealmAuthenticator.class);
bind(AuthenticationStrategy.class).to(ScmAtLeastOneSuccessfulStrategy.class);
bind(PermissionResolver.class).to(ScmPermissionResolver.class);
2020-08-20 17:44:36 +02:00
2013-02-17 13:59:00 +01:00
// bind realm
for (Class<? extends Realm> realm : extensionProcessor.byExtensionPoint(Realm.class))
2014-12-19 17:52:44 +01:00
{
logger.info("bind security realm {}", realm);
bindRealm().to(realm);
}
2013-02-17 13:59:00 +01:00
// bind constant
bindConstant().annotatedWith(Names.named("shiro.loginUrl")).to("/index.html");
2013-02-17 13:59:00 +01:00
// do not block non ascii character,
// because this would exclude languages which are non ascii based
bindConstant().annotatedWith(Names.named("shiro.blockNonAscii")).to(false);
2013-02-17 13:59:00 +01:00
// disable access to mustache resources
2016-12-11 21:30:33 +01:00
addFilterChain("/**.mustache", filterConfig(ROLES, "nobody"));
// disable session
disableSession();
}
private void disableSession() {
2015-03-21 16:06:17 +01:00
addFilterChain("/**", NO_SESSION_CREATION);
bindConstant().annotatedWith(Names.named("shiro.sessionStorageEnabled")).to(false);
DefaultSubjectDAO subjectDAO = new DefaultSubjectDAO();
DefaultSessionStorageEvaluator sessionStorageEvaluator = new DefaultSessionStorageEvaluator();
sessionStorageEvaluator.setSessionStorageEnabled(false);
subjectDAO.setSessionStorageEvaluator(sessionStorageEvaluator);
bind(SubjectDAO.class).toInstance(subjectDAO);
2012-08-27 08:04:44 +02:00
}
/**
* Creates a {@link PasswordService} with a smaller size of iteration, because
* large iterations will slow down subversion.
*
* @return instance of {@link PasswordService}
*/
private PasswordService createPasswordService()
{
DefaultPasswordService passwordService = new IdempotentPasswordService();
DefaultHashService hashService = new DefaultHashService();
hashService.setHashIterations(ITERATIONS);
passwordService.setHashService(hashService);
return passwordService;
}
2014-12-19 17:52:44 +01:00
//~--- fields ---------------------------------------------------------------
/** Field description */
private final ExtensionProcessor extensionProcessor;
static class IdempotentPasswordService extends DefaultPasswordService {
private boolean isEncrypted(Object password) {
return password instanceof String && ((String) password).startsWith("$shiro1$SHA-512$");
}
@Override
public String encryptPassword(Object plaintext) {
if (isEncrypted(plaintext)) {
return plaintext.toString();
}
return super.encryptPassword(plaintext);
}
}
2012-08-27 08:04:44 +02:00
}