Skip to content

The Unified and Autonomous Development Platform

Update GitPlex password hash algorithm to use BCrypt

robin shineced68c70fb7d
diff --git a/commons.hibernate/src/main/java/com/gitplex/commons/hibernate/command/DefaultUpgradeCommand.java b/commons.hibernate/src/main/java/com/gitplex/commons/hibernate/command/DefaultUpgradeCommand.java
index 0aada5b8cd..3add0e7790 100644
--- a/commons.hibernate/src/main/java/com/gitplex/commons/hibernate/command/DefaultUpgradeCommand.java
+++ b/commons.hibernate/src/main/java/com/gitplex/commons/hibernate/command/DefaultUpgradeCommand.java
@@ -288,6 +288,17 @@ public class DefaultUpgradeCommand extends DefaultPersistManager {
System.exit(1);
} else {
logger.info("Successfully upgraded {}", upgradeDir.getAbsolutePath());
+
+ if (Integer.parseInt(oldDataVersion.get()) <= 5) {
+ logger.warn("\n"
+ + "************************* IMPORTANT NOTICE *************************\n"
+ + "* GitPlex password hash algorithm has been changed for security *\n"
+ + "* reason. Please reset administrator password with the *\n"
+ + "* reset_admin_password command. Other users' password will be *\n"
+ + "* reset and sent to their mail box automatically when they logs *\n"
+ + "* into GitPlex. *\n"
+ + "********************************************************************");
+ }
System.exit(0);
}
}
diff --git a/commons.shiro/pom.xml b/commons.shiro/pom.xml
index 60ad31345e..7b24f2f6f4 100644
--- a/commons.shiro/pom.xml
+++ b/commons.shiro/pom.xml
@@ -42,6 +42,11 @@
<artifactId>commons.hibernate</artifactId>
<version>1.0-EAP-build20</version>
</dependency>
+ <dependency>
+ <groupId>com.pmease.security.shiro</groupId>
+ <artifactId>shiro-bcrypt</artifactId>
+ <version>1.0.2</version>
+ </dependency>
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-guice</artifactId>
diff --git a/commons.shiro/src/main/java/com/gitplex/commons/shiro/AbstractUser.java b/commons.shiro/src/main/java/com/gitplex/commons/shiro/AbstractUser.java
index c691dd2b2a..59f2db275f 100644
--- a/commons.shiro/src/main/java/com/gitplex/commons/shiro/AbstractUser.java
+++ b/commons.shiro/src/main/java/com/gitplex/commons/shiro/AbstractUser.java
@@ -11,7 +11,6 @@ import org.apache.shiro.subject.SimplePrincipalCollection;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.web.mgt.WebSecurityManager;
-import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.gitplex.commons.hibernate.AbstractEntity;
import com.gitplex.commons.loader.AppLoader;
import com.google.common.base.Preconditions;
@@ -20,13 +19,10 @@ import com.google.common.base.Preconditions;
@MappedSuperclass
public abstract class AbstractUser extends AbstractEntity implements AuthenticationInfo {
- public static final String HASH_PREFIX = "@hash^prefix@";
-
@Column(unique=true, nullable=false)
private String name;
@Column(length=1024)
- @JsonDeserialize(using=PasswordDeserializer.class)
private String password;
public String getName() {
@@ -58,12 +54,9 @@ public abstract class AbstractUser extends AbstractEntity implements Authenticat
*/
public void setPassword(String password) {
if (password != null) {
- if (password.startsWith(HASH_PREFIX))
- this.password = password;
- else
- this.password = HASH_PREFIX + AppLoader.getInstance(PasswordService.class).encryptPassword(password);
+ this.password = AppLoader.getInstance(PasswordService.class).encryptPassword(password);
} else {
- this.password = "";
+ this.password = null;
}
}
@@ -74,10 +67,7 @@ public abstract class AbstractUser extends AbstractEntity implements Authenticat
@Override
public Object getCredentials() {
- if (password != null && password.startsWith(HASH_PREFIX))
- return password.substring(HASH_PREFIX.length());
- else
- return password;
+ return password;
}
public Subject asSubject() {
diff --git a/commons.shiro/src/main/java/com/gitplex/commons/shiro/PasswordDeserializer.java b/commons.shiro/src/main/java/com/gitplex/commons/shiro/PasswordDeserializer.java
deleted file mode 100644
index 782d22e658..0000000000
--- a/commons.shiro/src/main/java/com/gitplex/commons/shiro/PasswordDeserializer.java
+++ /dev/null
@@ -1,30 +0,0 @@
-package com.gitplex.commons.shiro;
-
-import java.io.IOException;
-
-import org.apache.shiro.authc.credential.PasswordService;
-
-import com.fasterxml.jackson.core.JsonParser;
-import com.fasterxml.jackson.core.JsonProcessingException;
-import com.fasterxml.jackson.databind.DeserializationContext;
-import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
-import com.gitplex.commons.loader.AppLoader;
-
-@SuppressWarnings("serial")
-public class PasswordDeserializer extends StdDeserializer<String> {
-
- public PasswordDeserializer() {
- super(String.class);
- }
-
- @Override
- public String deserialize(JsonParser jp, DeserializationContext ctxt)
- throws IOException, JsonProcessingException {
- String password = jp.readValueAs(String.class);
- if (password == null || password.startsWith(AbstractUser.HASH_PREFIX))
- return password;
- else
- return AppLoader.getInstance(PasswordService.class).encryptPassword(password);
- }
-
-}
diff --git a/commons.shiro/src/main/java/com/gitplex/commons/shiro/ShiroModule.java b/commons.shiro/src/main/java/com/gitplex/commons/shiro/ShiroModule.java
index 0e3c8c8682..0692cff976 100644
--- a/commons.shiro/src/main/java/com/gitplex/commons/shiro/ShiroModule.java
+++ b/commons.shiro/src/main/java/com/gitplex/commons/shiro/ShiroModule.java
@@ -2,7 +2,6 @@ package com.gitplex.commons.shiro;
import javax.inject.Singleton;
-import org.apache.shiro.authc.credential.DefaultPasswordService;
import org.apache.shiro.authc.credential.PasswordService;
import org.apache.shiro.guice.aop.ShiroAopModule;
import org.apache.shiro.web.filter.mgt.FilterChainManager;
@@ -10,8 +9,9 @@ import org.apache.shiro.web.filter.mgt.FilterChainResolver;
import org.apache.shiro.web.mgt.WebSecurityManager;
import org.apache.shiro.web.servlet.ShiroFilter;
-import com.gitplex.commons.loader.AbstractPluginModule;
import com.gitplex.commons.jetty.ServletConfigurator;
+import com.gitplex.commons.loader.AbstractPluginModule;
+import com.pmease.security.shiro.bcrypt.BCryptPasswordService;
/**
* NOTE: Do not forget to rename moduleClass property defined in the pom if you've renamed this class.
@@ -27,7 +27,7 @@ public class ShiroModule extends AbstractPluginModule {
bind(WebSecurityManager.class).to(DefaultWebSecurityManager.class);
bind(FilterChainResolver.class).to(DefaultFilterChainResolver.class);
bind(BasicAuthenticationFilter.class);
- bind(PasswordService.class).to(DefaultPasswordService.class).in(Singleton.class);
+ bind(PasswordService.class).to(BCryptPasswordService.class).in(Singleton.class);
bind(ShiroFilter.class);
install(new ShiroAopModule());
diff --git a/server.core/src/main/java/com/gitplex/server/core/migration/DatabaseMigrator.java b/server.core/src/main/java/com/gitplex/server/core/migration/DatabaseMigrator.java
index 7fe92d0080..9b31b24ec7 100644
--- a/server.core/src/main/java/com/gitplex/server/core/migration/DatabaseMigrator.java
+++ b/server.core/src/main/java/com/gitplex/server/core/migration/DatabaseMigrator.java
@@ -99,5 +99,8 @@ public class DatabaseMigrator implements Migrator {
}
}
}
+
+ private void migrate6(File dataDir, Stack<Integer> versions) {
+ }
}
diff --git a/server.web/src/main/java/com/gitplex/server/web/page/security/LoginPage.java b/server.web/src/main/java/com/gitplex/server/web/page/security/LoginPage.java
index 916d055800..5c6806b0e7 100644
--- a/server.web/src/main/java/com/gitplex/server/web/page/security/LoginPage.java
+++ b/server.web/src/main/java/com/gitplex/server/web/page/security/LoginPage.java
@@ -1,5 +1,8 @@
package com.gitplex.server.web.page.security;
+import java.util.Arrays;
+
+import org.apache.commons.lang3.RandomStringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.wicket.RestartResponseException;
@@ -11,9 +14,14 @@ import org.apache.wicket.markup.html.form.PasswordTextField;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.link.BookmarkablePageLink;
import org.apache.wicket.model.IModel;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
import com.gitplex.server.core.GitPlex;
+import com.gitplex.server.core.entity.Account;
+import com.gitplex.server.core.manager.AccountManager;
import com.gitplex.server.core.manager.ConfigManager;
+import com.gitplex.server.core.manager.MailManager;
import com.gitplex.server.web.WebSession;
import com.gitplex.server.web.page.base.BasePage;
@@ -22,6 +30,8 @@ import de.agilecoders.wicket.core.markup.html.bootstrap.common.NotificationPanel
@SuppressWarnings("serial")
public class LoginPage extends BasePage {
+ private static final Logger logger = LoggerFactory.getLogger(LoginPage.class);
+
private String userName;
private String password;
@@ -45,12 +55,64 @@ public class LoginPage extends BasePage {
protected void onSubmit() {
super.onSubmit();
- try {
- WebSession.get().login(userName, password, rememberMe);
- continueToOriginalDestination();
- setResponsePage(getApplication().getHomePage());
- } catch (AuthenticationException ae) {
- error("Invalid user name and/or password.");
+ String feedback = null;
+
+ AccountManager accountManager = GitPlex.getInstance(AccountManager.class);
+ if (userName != null) {
+ Account user = accountManager.findByName(userName);
+ if (user != null && user.getPassword() != null && user.getPassword().startsWith("@hash^prefix@")) {
+ String hashAlgorithmChangeMessage = "GitPlex password hash algorithm has been changed for security reason.";
+ if (user.isAdministrator()) {
+ feedback = hashAlgorithmChangeMessage + " Please reset administrator password by running "
+ + "reset_admin_password command from GitPlex bin directory";
+ } else {
+ String password = RandomStringUtils.random(10, true, true);
+ user.setPassword(password);
+ accountManager.save(user);
+
+ ConfigManager configManager = GitPlex.getInstance(ConfigManager.class);
+ if (configManager.getMailSetting() != null) {
+
+ MailManager mailManager = GitPlex.getInstance(MailManager.class);
+ try {
+ String mailBody = String.format("Dear %s, "
+ + "<p style='margin: 16px 0;'>"
+ + hashAlgorithmChangeMessage
+ + " As a result of this, password of your account \"%s\" has been reset to:<br>"
+ + "%s<br><br>"
+ + "-- Sent by GitPlex",
+ user.getDisplayName(), user.getName(), password);
+
+ mailManager.sendMail(configManager.getMailSetting(), Arrays.asList(user.getEmail()),
+ "Your GitPlex password has been reset", mailBody);
+ feedback = hashAlgorithmChangeMessage
+ + " As a result of this, password of your account has been reset and sent to "
+ + "address " + user.getEmail();
+ } catch (Exception e) {
+ logger.error("Error sending password reset email", e);
+ feedback = hashAlgorithmChangeMessage
+ + " Since the reset password can not be been sent to your mail box "
+ + "due to mail error (check server log for details). Please "
+ + "contact GitPlex administrator to reset your password manually";
+ }
+ } else {
+ feedback = hashAlgorithmChangeMessage
+ + " The reset password can not be sent as mail setting is not defined. "
+ + "Please contact GitPlex administrator to reset your password manually";
+ }
+ }
+ }
+ }
+ if (feedback != null) {
+ error(feedback);
+ } else {
+ try {
+ WebSession.get().login(userName, password, rememberMe);
+ continueToOriginalDestination();
+ setResponsePage(getApplication().getHomePage());
+ } catch (AuthenticationException ae) {
+ error("Invalid user name and/or password.");
+ }
}
}