merge changes from branch 1.x

This commit is contained in:
Sebastian Sdorra
2014-02-18 21:25:29 +01:00
16 changed files with 306 additions and 43 deletions

View File

@@ -0,0 +1,64 @@
/**
* 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.api.rest;
//~--- JDK imports ------------------------------------------------------------
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;
import javax.ws.rs.ext.ExceptionMapper;
import javax.ws.rs.ext.Provider;
/**
*
* @author Sebastian Sdorra
* @since 1.36
*/
@Provider
public class IllegalArgumentExceptionMapper
implements ExceptionMapper<IllegalArgumentException>
{
/**
* Method description
*
*
* @param exception
*
* @return
*/
@Override
public Response toResponse(IllegalArgumentException exception)
{
return Response.status(Status.BAD_REQUEST).build();
}
}

View File

@@ -35,6 +35,8 @@ package sonia.scm.api.rest.resources;
//~--- non-JDK imports --------------------------------------------------------
import com.google.common.base.Preconditions;
import com.google.common.base.Strings;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableList.Builder;
import com.google.inject.Inject;
@@ -153,6 +155,7 @@ public class AuthenticationResource
* <br />
* <ul>
* <li>200 success</li>
* <li>400 bad request, required parameter is missing.</li>
* <li>401 unauthorized, the specified username or password is wrong</li>
* <li>500 internal server error</li>
* </ul>
@@ -172,6 +175,11 @@ public class AuthenticationResource
@FormParam("password") String password, @FormParam("rememberMe")
@DefaultValue("false") boolean rememberMe)
{
Preconditions.checkArgument(!Strings.isNullOrEmpty(username),
"username parameter is required");
Preconditions.checkArgument(!Strings.isNullOrEmpty(password),
"password parameter is required");
Response response;
Subject subject = SecurityUtils.getSubject();

View File

@@ -30,6 +30,7 @@
*/
package sonia.scm.security;
//~--- non-JDK imports --------------------------------------------------------
@@ -68,6 +69,17 @@ public class DefaultKeyGenerator implements KeyGenerator
//~--- methods --------------------------------------------------------------
/**
* Method description
*
*
* @param args
*/
public static void main(String[] args)
{
System.out.println(new DefaultKeyGenerator().createKey());
}
/**
* Method description
*
@@ -107,8 +119,8 @@ public class DefaultKeyGenerator implements KeyGenerator
//~--- fields ---------------------------------------------------------------
/** Field description */
private AtomicLong sessionKey = new AtomicLong();
private final AtomicLong sessionKey = new AtomicLong();
/** Field description */
private Random random = new Random();
private final Random random = new Random();
}

View File

@@ -361,7 +361,10 @@ public class ScmRealm extends AuthorizingRealm
// modify existing user, copy properties except password and admin
if (user.copyProperties(dbUser, false))
{
userManager.modify(dbUser);
user.setLastModified(System.currentTimeMillis());
UserEventHack.fireEvent(userManager, HandlerEventType.BEFORE_MODIFY, user);
userDAO.modify(user);
UserEventHack.fireEvent(userManager, HandlerEventType.MODIFY, user);
}
}

View File

@@ -47,6 +47,7 @@ import org.slf4j.LoggerFactory;
import sonia.scm.SCMContextProvider;
import sonia.scm.cache.Cache;
import sonia.scm.cache.CacheManager;
import sonia.scm.config.ScmConfiguration;
import sonia.scm.security.EncryptionHandler;
import sonia.scm.user.User;
import sonia.scm.user.UserManager;
@@ -85,19 +86,21 @@ public class ChainAuthenticatonManager extends AbstractAuthenticationManager
* Constructs ...
*
*
*
* @param configuration
* @param userManager
* @param authenticationHandlerSet
* @param encryptionHandler
* @param cacheManager
*/
@Inject
public ChainAuthenticatonManager(UserManager userManager,
public ChainAuthenticatonManager(ScmConfiguration configuration,
UserManager userManager,
Set<AuthenticationHandler> authenticationHandlerSet,
EncryptionHandler encryptionHandler, CacheManager cacheManager)
{
AssertUtil.assertIsNotEmpty(authenticationHandlerSet);
AssertUtil.assertIsNotNull(cacheManager);
this.configuration = configuration;
this.authenticationHandlers = sort(userManager, authenticationHandlerSet);
this.encryptionHandler = encryptionHandler;
this.cache = cacheManager.getCache(CACHE_NAME);
@@ -190,6 +193,22 @@ public class ChainAuthenticatonManager extends AbstractAuthenticationManager
}
}
/**
* Method description
*
*
* @param result
*
* @return
*/
boolean stopChain(AuthenticationResult result)
{
return (result != null) && (result.getState() != null)
&& (result.getState().isSuccessfully()
|| ((result.getState() == AuthenticationState.FAILED)
&&!configuration.isSkipFailedAuthenticators()));
}
/**
* Method description
*
@@ -230,9 +249,7 @@ public class ChainAuthenticatonManager extends AbstractAuthenticationManager
authenticator.getClass().getName(), result);
}
if ((result != null) && (result.getState() != null)
&& (result.getState().isSuccessfully()
|| (result.getState() == AuthenticationState.FAILED)))
if (stopChain(result))
{
if (result.getState().isSuccessfully() && (result.getUser() != null))
{
@@ -372,7 +389,10 @@ public class ChainAuthenticatonManager extends AbstractAuthenticationManager
/** authentication cache */
private final Cache<String, AuthenticationCacheValue> cache;
/** Field description */
private final ScmConfiguration configuration;
/** encryption handler */
private final EncryptionHandler encryptionHandler;
}

View File

@@ -34,6 +34,7 @@ Sonia.config.ScmConfigPanel = Ext.extend(Sonia.config.ConfigPanel,{
titleText: 'General Settings',
servnameText: 'Servername',
realmDescriptionText: 'Realm description',
dateFormatText: 'Date format',
enableForwardingText: 'Enable forwarding (mod_proxy)',
forwardPortText: 'Forward Port',
@@ -50,6 +51,7 @@ Sonia.config.ScmConfigPanel = Ext.extend(Sonia.config.ConfigPanel,{
errorSubmitMsgText: 'Could not submit config.',
// TODO i18n
skipFailedAuthenticatorsText: 'Skip failed authenticators',
loginAttemptLimitText: 'Login Attempt Limit',
loginAttemptLimitTimeoutText: 'Login Attempt Limit Timeout',
@@ -67,6 +69,7 @@ Sonia.config.ScmConfigPanel = Ext.extend(Sonia.config.ConfigPanel,{
// help
servernameHelpText: 'The name of this server. This name will be part of the repository url.',
realmDescriptionHelpText: 'Enter authentication realm description',
// TODO i18n
dateFormatHelpText: 'Moments date format. Please have a look at \n\
<a href="http://momentjs.com/docs/#/displaying/format/" target="_blank">http://momentjs.com/docs/#/displaying/format/</a>.<br />\n\
@@ -83,6 +86,8 @@ Sonia.config.ScmConfigPanel = Ext.extend(Sonia.config.ConfigPanel,{
adminUsersHelpText: 'Comma seperated list of users with admin permissions.',
// TODO i18n
skipFailedAuthenticatorsHelpText: 'Do not stop the authentication chain, \n\
if an authenticator finds the user but fails to authenticate the user.',
loginAttemptLimitHelpText: 'Maximum allowed login attempts. Use -1 to disable the login attempt limit.',
loginAttemptLimitTimeoutHelpText: 'Timeout in seconds for users which are temporary disabled,\
because of too many failed login attempts.',
@@ -129,6 +134,12 @@ Sonia.config.ScmConfigPanel = Ext.extend(Sonia.config.ConfigPanel,{
name: 'enableRepositoryArchive',
inputValue: 'true',
helpText: this.enableRepositoryArchiveHelpText
},{
xtype: 'textfield',
fieldLabel: this.realmDescriptionText,
name: 'realmDescription',
allowBlank: false,
helpText: this.realmDescriptionHelpText
},{
xtype: 'textfield',
fieldLabel: this.dateFormatText,
@@ -149,6 +160,12 @@ Sonia.config.ScmConfigPanel = Ext.extend(Sonia.config.ConfigPanel,{
name: 'anonymousAccessEnabled',
inputValue: 'true',
helpText: this.allowAnonymousAccessHelpText
},{
xtype: 'checkbox',
fieldLabel: this.skipFailedAuthenticatorsText,
name: 'skip-failed-authenticators',
inputValue: 'true',
helpText: this.skipFailedAuthenticatorsHelpText
},{
xtype: 'numberfield',
fieldLabel: this.loginAttemptLimitText,

View File

@@ -40,7 +40,7 @@ if (Ext.form.VTypes){
passwordText: 'Die Passwörter stimmen nicht überein!',
nameTest: 'Der Name ist invalid.',
usernameText: 'Der Benutzername ist invalid.',
repositoryNameText: 'Der Name des Repositorys ist ungültig.',
repositoryNameText: 'Der Name des Repositorys ist ungültig.'
});
}
@@ -349,6 +349,10 @@ if (Sonia.config.ScmConfigPanel){
adminGroupsHelpText: 'Komma getrennte Liste von Gruppen mit Administrationsrechten.',
adminUsersHelpText: 'Komma getrennte Liste von Benutzern mit Administrationsrechten.',
skipFailedAuthenticatorsText: 'Überspringe fehlgeschlagene Authentifizierer',
skipFailedAuthenticatorsHelpText: 'Setzt die Authentifizierungs-Kette fort,\n\
auch wenn ein ein Authentifizierer einen Benutzer gefunden hat,\n\
diesen aber nicht Authentifizieren kann.',
loginAttemptLimitText: 'Login Attempt Limit',
loginAttemptLimitTimeoutText: 'Login Attempt Limit Timeout',
loginAttemptLimitHelpText: 'Maximale Anzahl gescheiterte Loginversuche. Der Wert -1 deaktiviert die Begrenzung.',

View File

@@ -99,7 +99,8 @@ Sonia.repository.Grid = Ext.extend(Sonia.rest.Grid, {
},{
name: 'archived'
},{
name: 'healthCheckFailures'
name: 'healthCheckFailures',
defaultValue: null
}]
}),
sortInfo: {

View File

@@ -42,6 +42,7 @@ import org.junit.Test;
import sonia.scm.AbstractTestBase;
import sonia.scm.SCMContextProvider;
import sonia.scm.cache.MapCacheManager;
import sonia.scm.config.ScmConfiguration;
import sonia.scm.security.MessageDigestEncryptionHandler;
import sonia.scm.user.User;
import sonia.scm.user.UserManager;
@@ -136,7 +137,7 @@ public class ChainAuthenticationManagerTest extends AbstractTestBase
SingleUserAuthenticaionHandler a2 =
new SingleUserAuthenticaionHandler("a2", trillian);
manager = createManager("a2", a1, a2);
manager = createManager("a2", false, a1, a2);
AuthenticationResult result = manager.authenticate(request, response,
trillian.getName(), "trillian123");
@@ -146,6 +147,24 @@ public class ChainAuthenticationManagerTest extends AbstractTestBase
assertEquals("a2", result.getUser().getType());
}
/**
* Method description
*
*/
@Test
public void testStopChain()
{
ChainAuthenticatonManager cam = createManager("", false);
assertTrue(cam.stopChain(new AuthenticationResult(perfect)));
assertTrue(cam.stopChain(AuthenticationResult.FAILED));
assertFalse(cam.stopChain(AuthenticationResult.NOT_FOUND));
cam = createManager("", true);
assertTrue(cam.stopChain(new AuthenticationResult(perfect)));
assertFalse(cam.stopChain(AuthenticationResult.FAILED));
assertFalse(cam.stopChain(AuthenticationResult.NOT_FOUND));
}
/**
* Method description
*
@@ -198,7 +217,7 @@ public class ChainAuthenticationManagerTest extends AbstractTestBase
trillian = UserTestData.createTrillian();
trillian.setPassword("trillian123");
return createManager("",
return createManager("", false,
new SingleUserAuthenticaionHandler("perfectsType", perfect),
new SingleUserAuthenticaionHandler("trilliansType", trillian));
}
@@ -208,20 +227,33 @@ public class ChainAuthenticationManagerTest extends AbstractTestBase
*
*
* @param defaultType
* @param skipFailedAuthenticators
* @param handlers
*
* @return
*/
private ChainAuthenticatonManager createManager(String defaultType,
AuthenticationHandler... handlers)
boolean skipFailedAuthenticators, AuthenticationHandler... handlers)
{
if ( handlers == null || handlers.length == 0 ){
//J-
handlers = new AuthenticationHandler[]{
new SingleUserAuthenticaionHandler("perfectsType", perfect),
new SingleUserAuthenticaionHandler("trilliansType", trillian)
};
//J+
}
ScmConfiguration configuration = new ScmConfiguration();
configuration.setSkipFailedAuthenticators(skipFailedAuthenticators);
Set<AuthenticationHandler> handlerSet = ImmutableSet.copyOf(handlers);
UserManager userManager = mock(UserManager.class);
when(userManager.getDefaultType()).thenReturn(defaultType);
manager = new ChainAuthenticatonManager(userManager, handlerSet,
new MessageDigestEncryptionHandler(), new MapCacheManager());
manager = new ChainAuthenticatonManager(configuration, userManager,
handlerSet, new MessageDigestEncryptionHandler(), new MapCacheManager());
manager.init(contextProvider);
return manager;
@@ -326,10 +358,10 @@ public class ChainAuthenticationManagerTest extends AbstractTestBase
//~--- fields -------------------------------------------------------------
/** Field description */
private String type;
private final String type;
/** Field description */
private User user;
private final User user;
}