diff --git a/framework/common/src/main/java/org/apache/ofbiz/common/login/LoginServices.java b/framework/common/src/main/java/org/apache/ofbiz/common/login/LoginServices.java index 877afaf8c4..edb820a10c 100644 --- a/framework/common/src/main/java/org/apache/ofbiz/common/login/LoginServices.java +++ b/framework/common/src/main/java/org/apache/ofbiz/common/login/LoginServices.java @@ -49,6 +49,7 @@ import org.apache.ofbiz.entity.condition.EntityCondition; import org.apache.ofbiz.entity.condition.EntityFunction; import org.apache.ofbiz.entity.condition.EntityOperator; import org.apache.ofbiz.entity.model.ModelEntity; +import org.apache.ofbiz.entity.model.ModelField; import org.apache.ofbiz.entity.transaction.GenericTransactionException; import org.apache.ofbiz.entity.transaction.TransactionUtil; import org.apache.ofbiz.entity.util.EntityListIterator; @@ -345,9 +346,12 @@ public class LoginServices { } // ONLY save the password if it was incorrect + // we will check in the hash size isn't too huge for the store other wise store a fix string if ("N".equals(successfulLogin) && !"false".equals(EntityUtilProperties.getPropertyValue("security", "store.login.history.incorrect.password", delegator))) { - ulhCreateMap.put("passwordUsed", password); + ulhCreateMap.put("passwordUsed", isGivenPasswordCanBeStored(delegator, password) + ? " TOO LONG FOR STORAGE " + : password); } delegator.create("UserLoginHistory", ulhCreateMap); @@ -358,7 +362,6 @@ public class LoginServices { if (doStore) { geeErrMsg += " and updating login status to reset hasLoggedOut, unsuccessful login count, etc."; } - geeErrMsg += ": " + e.toString(); try { TransactionUtil.rollback(beganTransaction, geeErrMsg, e); } catch (GenericTransactionException e2) { @@ -447,6 +450,30 @@ public class LoginServices { return result; } + /** + * To escape an exception when the password store due to limitation size for passwordUsed field, we analyse if it's possible. + * @param delegator + * @param password + * @return + * @throws GenericEntityException + */ + private static boolean isGivenPasswordCanBeStored(Delegator delegator, String password) + throws GenericEntityException { + ModelEntity modelEntityUserLoginHistory = delegator.getModelEntity("UserLoginHistory"); + ModelField passwordUsedField = modelEntityUserLoginHistory.getField("passwordUsed"); + int maxPasswordSize = delegator.getEntityFieldType( + modelEntityUserLoginHistory, + passwordUsedField.getType()).stringLength(); + int passwordUsedCurrentSize = password.length(); + + // if the field is encrypted, we check the size of the hashed result + ModelField.EncryptMethod encryptMethod = passwordUsedField.getEncryptMethod(); + if (encryptMethod.isEncrypted()) { + passwordUsedCurrentSize = delegator.encryptFieldValue("UserLoginHistory", encryptMethod, password).toString().length(); + } + return passwordUsedCurrentSize > maxPasswordSize; + } + /** * Login service to authenticate a username without password, storing history * @return Map of results including (userLogin) GenericValue object