mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-14 01:15:44 +01:00
improve check for legacy passwords
This commit is contained in:
@@ -34,6 +34,7 @@ package sonia.scm.legacy;
|
|||||||
//~--- non-JDK imports --------------------------------------------------------
|
//~--- non-JDK imports --------------------------------------------------------
|
||||||
|
|
||||||
import com.google.common.annotations.VisibleForTesting;
|
import com.google.common.annotations.VisibleForTesting;
|
||||||
|
import com.google.common.base.CharMatcher;
|
||||||
import com.google.common.base.Preconditions;
|
import com.google.common.base.Preconditions;
|
||||||
|
|
||||||
import org.apache.shiro.authc.AuthenticationException;
|
import org.apache.shiro.authc.AuthenticationException;
|
||||||
@@ -44,6 +45,9 @@ import org.apache.shiro.authc.credential.HashedCredentialsMatcher;
|
|||||||
import org.apache.shiro.crypto.hash.Sha1Hash;
|
import org.apache.shiro.crypto.hash.Sha1Hash;
|
||||||
import org.apache.shiro.realm.AuthenticatingRealm;
|
import org.apache.shiro.realm.AuthenticatingRealm;
|
||||||
|
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
import sonia.scm.group.GroupDAO;
|
import sonia.scm.group.GroupDAO;
|
||||||
import sonia.scm.plugin.Extension;
|
import sonia.scm.plugin.Extension;
|
||||||
import sonia.scm.security.DAORealmHelper;
|
import sonia.scm.security.DAORealmHelper;
|
||||||
@@ -69,6 +73,19 @@ public class LegacyRealm extends AuthenticatingRealm
|
|||||||
@VisibleForTesting
|
@VisibleForTesting
|
||||||
static final String REALM = "LegacyRealm";
|
static final String REALM = "LegacyRealm";
|
||||||
|
|
||||||
|
/** Field description */
|
||||||
|
//J-
|
||||||
|
private static final CharMatcher HEX_MATCHER = CharMatcher.inRange('0', '9')
|
||||||
|
.or(CharMatcher.inRange('a', 'f'))
|
||||||
|
.or(CharMatcher.inRange('A', 'F'));
|
||||||
|
//J+
|
||||||
|
|
||||||
|
/**
|
||||||
|
* the logger for LegacyRealm
|
||||||
|
*/
|
||||||
|
private static final Logger logger =
|
||||||
|
LoggerFactory.getLogger(LegacyRealm.class);
|
||||||
|
|
||||||
//~--- constructors ---------------------------------------------------------
|
//~--- constructors ---------------------------------------------------------
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -112,15 +129,37 @@ public class LegacyRealm extends AuthenticatingRealm
|
|||||||
Preconditions.checkArgument(token instanceof UsernamePasswordToken,
|
Preconditions.checkArgument(token instanceof UsernamePasswordToken,
|
||||||
"unsupported token");
|
"unsupported token");
|
||||||
|
|
||||||
AuthenticationInfo info = null;
|
return returnOnHexCredentials(helper.getAuthenticationInfo(token));
|
||||||
char[] password = ((UsernamePasswordToken) token).getPassword();
|
|
||||||
|
|
||||||
if ((password != null) && (password[0] != '$'))
|
|
||||||
{
|
|
||||||
info = helper.getAuthenticationInfo(token);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return info;
|
private AuthenticationInfo returnOnHexCredentials(AuthenticationInfo info)
|
||||||
|
{
|
||||||
|
AuthenticationInfo result = null;
|
||||||
|
|
||||||
|
if (info != null)
|
||||||
|
{
|
||||||
|
Object credentials = info.getCredentials();
|
||||||
|
|
||||||
|
if (credentials instanceof String)
|
||||||
|
{
|
||||||
|
String password = (String) credentials;
|
||||||
|
|
||||||
|
if (HEX_MATCHER.matchesAllOf(password))
|
||||||
|
{
|
||||||
|
result = info;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
logger.debug("hash contains non hex chars");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
logger.debug("non string crendentials found");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
//~--- fields ---------------------------------------------------------------
|
//~--- fields ---------------------------------------------------------------
|
||||||
|
|||||||
@@ -100,12 +100,29 @@ public class LegacyRealmTest
|
|||||||
@Test
|
@Test
|
||||||
public void testDoGetAuthenticationInfoWithNewPasswords()
|
public void testDoGetAuthenticationInfoWithNewPasswords()
|
||||||
{
|
{
|
||||||
|
User user = UserTestData.createTrillian();
|
||||||
|
user.setPassword(NEW_PASSWORD);
|
||||||
|
when(userDAO.get("tricia")).thenReturn(user);
|
||||||
|
|
||||||
AuthenticationToken token = new UsernamePasswordToken("tricia",
|
AuthenticationToken token = new UsernamePasswordToken("tricia",
|
||||||
NEW_PASSWORD);
|
NEW_PASSWORD);
|
||||||
|
|
||||||
assertNull(realm.doGetAuthenticationInfo(token));
|
assertNull(realm.doGetAuthenticationInfo(token));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Method description
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
public void testDoGetAuthenticationInfoWithNullPassword()
|
||||||
|
{
|
||||||
|
when(userDAO.get("tricia")).thenReturn(UserTestData.createTrillian());
|
||||||
|
AuthenticationToken token = new UsernamePasswordToken("tricia", "secret");
|
||||||
|
|
||||||
|
assertNull(realm.doGetAuthenticationInfo(token));
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Method description
|
* Method description
|
||||||
*
|
*
|
||||||
|
|||||||
Reference in New Issue
Block a user