[pwd] close method visibility
authorAsier Lostalé <asier.lostale@openbravo.com>
Wed, 02 Oct 2019 14:56:58 +0200
changeset 36616 dee41e082133
parent 36615 ac52852c65a1
child 36617 4e2c05b7b614
[pwd] close method visibility
src-test/src/org/openbravo/authentication/hashing/PasswordHashing.java
src-test/src/org/openbravo/test/AllAntTaskTests.java
src-test/src/org/openbravo/test/system/PasswordHashing.java
src/org/openbravo/authentication/hashing/HashingAlgorithm.java
src/org/openbravo/authentication/hashing/PasswordHash.java
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src-test/src/org/openbravo/authentication/hashing/PasswordHashing.java	Wed Oct 02 14:56:58 2019 +0200
@@ -0,0 +1,125 @@
+/*
+ *************************************************************************
+ * The contents of this file are subject to the Openbravo  Public  License
+ * Version  1.1  (the  "License"),  being   the  Mozilla   Public  License
+ * Version 1.1  with a permitted attribution clause; you may not  use this
+ * file except in compliance with the License. You  may  obtain  a copy of
+ * the License at http://www.openbravo.com/legal/license.html 
+ * Software distributed under the License  is  distributed  on  an "AS IS"
+ * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
+ * License for the specific  language  governing  rights  and  limitations
+ * under the License. 
+ * The Original Code is Openbravo ERP. 
+ * The Initial Developer of the Original Code is Openbravo SLU 
+ * All portions are Copyright (C) 2019 Openbravo SLU 
+ * All Rights Reserved. 
+ * Contributor(s):  ______________________________________.
+ ************************************************************************
+ */
+
+package org.openbravo.authentication.hashing;
+
+import static org.hamcrest.Matchers.equalTo;
+import static org.hamcrest.Matchers.is;
+import static org.hamcrest.Matchers.not;
+import static org.junit.Assert.assertThat;
+
+import java.util.Optional;
+
+import org.junit.Test;
+import org.openbravo.authentication.hashing.PasswordHash;
+import org.openbravo.dal.service.OBDal;
+import org.openbravo.model.ad.access.User;
+import org.openbravo.test.base.OBBaseTest;
+import org.openbravo.test.base.TestConstants;
+
+/** Tests password hashing with different algorithms */
+public class PasswordHashing extends OBBaseTest {
+
+  private static final String SHA1_OPENBRAVO = "PwOd6SgWF74HY4u51bfrUxjtB9g=";
+  private static final String SHA512SALT_OPENBRAVO = "1$anySalt$iyWvhlUpOrXFPPeRVzWXXR/B4hQ5qs8ZjCLUPoncJIKHRy5HZeXm9/r20qXg8tRgKcfC8bp/u5fPPQ9qA/hheQ==";
+
+  @Test
+  public void sha1IsAKnownAlgorithm() {
+    assertThat(PasswordHash.getAlgorithm("whatever").getClass().getSimpleName(), is("SHA1"));
+  }
+
+  @Test
+  public void sha512SaltIsAKnownAlgorithm() {
+    assertThat(PasswordHash.getAlgorithm("1$salt$hash").getClass().getSimpleName(),
+        is("SHA512Salt"));
+  }
+
+  @Test(expected = IllegalStateException.class)
+  public void unknownAlgorithmsThrowException() {
+    PasswordHash.getAlgorithm("2$salt$hash");
+  }
+
+  @Test
+  public void oldHashesWork() {
+    assertThat(PasswordHash.matches("openbravo", SHA1_OPENBRAVO), is(true));
+  }
+
+  @Test
+  public void newHashesWork() {
+    assertThat(PasswordHash.matches("openbravo", SHA512SALT_OPENBRAVO), is(true));
+  }
+
+  @Test
+  public void saltPrventCollission() {
+    assertThat("same password should generate different salted hashes",
+        PasswordHash.generateHash("mySecret"), not(equalTo(PasswordHash.generateHash("mySecret"))));
+  }
+
+  @Test
+  public void validUserNameAndPasswordReturnAUser() {
+    Optional<User> user = PasswordHash.getUserWithPassword("Openbravo", "openbravo");
+    assertThat("Openbravo user is found", user.isPresent(), is(true));
+  }
+
+  @Test
+  public void invalidPasswordDoesNotReturnAUser() {
+    Optional<User> user = PasswordHash.getUserWithPassword("Openbravo", "wrongPassword");
+    assertThat("Openbravo user is found", user.isPresent(), is(false));
+  }
+
+  @Test
+  public void invalidUserDoesNotReturnAUser() {
+    Optional<User> user = PasswordHash.getUserWithPassword("wrongUser", "wrongPassword");
+    assertThat("User is found", user.isPresent(), is(false));
+  }
+
+  @Test
+  public void oldAlgorithmsGetPromoted() {
+    setSystemAdministratorContext();
+
+    // Given a user with a password hashed with old algorithm
+    User obUser = OBDal.getInstance().get(User.class, TestConstants.Users.OPENBRAVO);
+    obUser.setPassword(SHA1_OPENBRAVO);
+    OBDal.getInstance().flush();
+
+    // when credentials are checked first time
+    Optional<User> user = PasswordHash.getUserWithPassword("Openbravo", "openbravo");
+
+    // then password gets promoted to new algorithm
+    assertThat("password is promoted",
+        PasswordHash.getAlgorithm(user.get().getPassword()).getClass().getSimpleName(),
+        is("SHA512Salt"));
+  }
+
+  @Test
+  public void newAlgorithmsRemainUntouched() {
+    setSystemAdministratorContext();
+
+    // Given a user with a password hashed with old algorithm
+    User obUser = OBDal.getInstance().get(User.class, TestConstants.Users.OPENBRAVO);
+    obUser.setPassword(SHA512SALT_OPENBRAVO);
+    OBDal.getInstance().flush();
+
+    // when credentials are checked first time
+    Optional<User> user = PasswordHash.getUserWithPassword("Openbravo", "openbravo");
+
+    // then password gets promoted to new algorithm
+    assertThat("password is not changed", user.get().getPassword(), is(SHA512SALT_OPENBRAVO));
+  }
+}
--- a/src-test/src/org/openbravo/test/AllAntTaskTests.java	Wed Oct 02 13:08:14 2019 +0200
+++ b/src-test/src/org/openbravo/test/AllAntTaskTests.java	Wed Oct 02 14:56:58 2019 +0200
@@ -22,6 +22,7 @@
 import org.junit.runner.RunWith;
 import org.junit.runners.Suite;
 import org.openbravo.advpaymentmngt.test.DocumentNumberGeneration;
+import org.openbravo.authentication.hashing.PasswordHashing;
 import org.openbravo.base.weld.test.testinfrastructure.CdiInfrastructure;
 import org.openbravo.base.weld.test.testinfrastructure.DalPersistanceEventTest;
 import org.openbravo.base.weld.test.testinfrastructure.ParameterizedCdi;
@@ -124,7 +125,6 @@
 import org.openbravo.test.system.Issue29934Test;
 import org.openbravo.test.system.JSONSerialization;
 import org.openbravo.test.system.OldCallouts;
-import org.openbravo.test.system.PasswordHashing;
 import org.openbravo.test.system.Sessions;
 import org.openbravo.test.system.SystemServiceTest;
 import org.openbravo.test.system.SystemValidatorTest;
--- a/src-test/src/org/openbravo/test/system/PasswordHashing.java	Wed Oct 02 13:08:14 2019 +0200
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,125 +0,0 @@
-/*
- *************************************************************************
- * The contents of this file are subject to the Openbravo  Public  License
- * Version  1.1  (the  "License"),  being   the  Mozilla   Public  License
- * Version 1.1  with a permitted attribution clause; you may not  use this
- * file except in compliance with the License. You  may  obtain  a copy of
- * the License at http://www.openbravo.com/legal/license.html 
- * Software distributed under the License  is  distributed  on  an "AS IS"
- * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
- * License for the specific  language  governing  rights  and  limitations
- * under the License. 
- * The Original Code is Openbravo ERP. 
- * The Initial Developer of the Original Code is Openbravo SLU 
- * All portions are Copyright (C) 2019 Openbravo SLU 
- * All Rights Reserved. 
- * Contributor(s):  ______________________________________.
- ************************************************************************
- */
-
-package org.openbravo.test.system;
-
-import static org.hamcrest.Matchers.equalTo;
-import static org.hamcrest.Matchers.is;
-import static org.hamcrest.Matchers.not;
-import static org.junit.Assert.assertThat;
-
-import java.util.Optional;
-
-import org.junit.Test;
-import org.openbravo.authentication.hashing.PasswordHash;
-import org.openbravo.dal.service.OBDal;
-import org.openbravo.model.ad.access.User;
-import org.openbravo.test.base.OBBaseTest;
-import org.openbravo.test.base.TestConstants;
-
-/** Tests password hashing with different algorithms */
-public class PasswordHashing extends OBBaseTest {
-
-  private static final String SHA1_OPENBRAVO = "PwOd6SgWF74HY4u51bfrUxjtB9g=";
-  private static final String SHA512SALT_OPENBRAVO = "1$anySalt$iyWvhlUpOrXFPPeRVzWXXR/B4hQ5qs8ZjCLUPoncJIKHRy5HZeXm9/r20qXg8tRgKcfC8bp/u5fPPQ9qA/hheQ==";
-
-  @Test
-  public void sha1IsAKnownAlgorithm() {
-    assertThat(PasswordHash.getAlgorithm("whatever").getClass().getSimpleName(), is("SHA1"));
-  }
-
-  @Test
-  public void sha512SaltIsAKnownAlgorithm() {
-    assertThat(PasswordHash.getAlgorithm("1$salt$hash").getClass().getSimpleName(),
-        is("SHA512Salt"));
-  }
-
-  @Test(expected = IllegalStateException.class)
-  public void unknownAlgorithmsThrowException() {
-    PasswordHash.getAlgorithm("2$salt$hash");
-  }
-
-  @Test
-  public void oldHashesWork() {
-    assertThat(PasswordHash.matches("openbravo", SHA1_OPENBRAVO), is(true));
-  }
-
-  @Test
-  public void newHashesWork() {
-    assertThat(PasswordHash.matches("openbravo", SHA512SALT_OPENBRAVO), is(true));
-  }
-
-  @Test
-  public void saltPrventCollission() {
-    assertThat("same password should generate different salted hashes",
-        PasswordHash.generateHash("mySecret"), not(equalTo(PasswordHash.generateHash("mySecret"))));
-  }
-
-  @Test
-  public void validUserNameAndPasswordReturnAUser() {
-    Optional<User> user = PasswordHash.getUserWithPassword("Openbravo", "openbravo");
-    assertThat("Openbravo user is found", user.isPresent(), is(true));
-  }
-
-  @Test
-  public void invalidPasswordDoesNotReturnAUser() {
-    Optional<User> user = PasswordHash.getUserWithPassword("Openbravo", "wrongPassword");
-    assertThat("Openbravo user is found", user.isPresent(), is(false));
-  }
-
-  @Test
-  public void invalidUserDoesNotReturnAUser() {
-    Optional<User> user = PasswordHash.getUserWithPassword("wrongUser", "wrongPassword");
-    assertThat("User is found", user.isPresent(), is(false));
-  }
-
-  @Test
-  public void oldAlgorithmsGetPromoted() {
-    setSystemAdministratorContext();
-
-    // Given a user with a password hashed with old algorithm
-    User obUser = OBDal.getInstance().get(User.class, TestConstants.Users.OPENBRAVO);
-    obUser.setPassword(SHA1_OPENBRAVO);
-    OBDal.getInstance().flush();
-
-    // when credentials are checked first time
-    Optional<User> user = PasswordHash.getUserWithPassword("Openbravo", "openbravo");
-
-    // then password gets promoted to new algorithm
-    assertThat("password is promoted",
-        PasswordHash.getAlgorithm(user.get().getPassword()).getClass().getSimpleName(),
-        is("SHA512Salt"));
-  }
-
-  @Test
-  public void newAlgorithmsRemainUntouched() {
-    setSystemAdministratorContext();
-
-    // Given a user with a password hashed with old algorithm
-    User obUser = OBDal.getInstance().get(User.class, TestConstants.Users.OPENBRAVO);
-    obUser.setPassword(SHA512SALT_OPENBRAVO);
-    OBDal.getInstance().flush();
-
-    // when credentials are checked first time
-    Optional<User> user = PasswordHash.getUserWithPassword("Openbravo", "openbravo");
-
-    // then password gets promoted to new algorithm
-    assertThat("password is not changed", user.get().getPassword(), is(SHA512SALT_OPENBRAVO));
-  }
-}
--- a/src/org/openbravo/authentication/hashing/HashingAlgorithm.java	Wed Oct 02 13:08:14 2019 +0200
+++ b/src/org/openbravo/authentication/hashing/HashingAlgorithm.java	Wed Oct 02 14:56:58 2019 +0200
@@ -24,7 +24,7 @@
 import java.util.Base64;
 
 /** Algorithm used to hash password to store in Database */
-public abstract class HashingAlgorithm {
+abstract class HashingAlgorithm {
 
   /**
    * Generates a hash using current algorithm, hashes should look like: {@code version$salt$hashed}
--- a/src/org/openbravo/authentication/hashing/PasswordHash.java	Wed Oct 02 13:08:14 2019 +0200
+++ b/src/org/openbravo/authentication/hashing/PasswordHash.java	Wed Oct 02 14:56:58 2019 +0200
@@ -118,7 +118,7 @@
   }
 
   /** Determines the algorithm used to hash a given password. */
-  public static HashingAlgorithm getAlgorithm(String hash) {
+  static HashingAlgorithm getAlgorithm(String hash) {
     HashingAlgorithm algorithm = ALGORITHMS.get(getVersion(hash));
 
     if (algorithm == null) {