Skip to content

Commit 1e21f1e

Browse files
committed
#398 update password handling logic
1 parent 6444222 commit 1e21f1e

File tree

5 files changed

+34
-26
lines changed

5 files changed

+34
-26
lines changed

saturn-console-api/src/main/java/com/vip/saturn/job/console/controller/gui/AuthenticationController.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ public SuccessResponseEntity login(@RequestParam String username, @RequestParam
3232

3333
User user = authenticationService.authenticate(username, password);
3434
if (user == null) {
35-
throw new SaturnJobConsoleException("Invalid username or password");
35+
throw new SaturnJobConsoleException(SaturnJobConsoleException.ERROR_CODE_AUTHN_FAIL,
36+
"Invalid username or password");
3637
}
3738

3839
request.getSession().setAttribute(SessionAttributeKeys.LOGIN_USER_NAME, user.getUserName());

saturn-console-api/src/main/java/com/vip/saturn/job/console/exception/SaturnJobConsoleException.java

+2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ public class SaturnJobConsoleException extends Exception {
1414

1515
public static final int ERROR_CODE_INTERNAL_ERROR = 0;
1616

17+
public static final int ERROR_CODE_AUTHN_FAIL = 4;
18+
1719
private int errorCode = ERROR_CODE_INTERNAL_ERROR;
1820

1921
public SaturnJobConsoleException() {

saturn-console-api/src/main/java/com/vip/saturn/job/console/service/impl/AuthenticationServiceImpl.java

+2-7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package com.vip.saturn.job.console.service.impl;
22

3-
import com.vip.saturn.job.console.exception.SaturnJobConsoleException;
43
import com.vip.saturn.job.console.mybatis.entity.User;
54
import com.vip.saturn.job.console.mybatis.repository.UserRepository;
65
import com.vip.saturn.job.console.service.AuthenticationService;
@@ -18,7 +17,7 @@ public class AuthenticationServiceImpl implements AuthenticationService {
1817
private String hashMethod;
1918

2019
@Override
21-
public User authenticate(String username, String password) throws SaturnJobConsoleException {
20+
public User authenticate(String username, String password) {
2221
if (StringUtils.isEmpty(password)) {
2322
return null;
2423
}
@@ -28,11 +27,7 @@ public User authenticate(String username, String password) throws SaturnJobConso
2827
return null;
2928
}
3029

31-
try {
32-
return PasswordUtils.validate(password, user.getPassword(), hashMethod) ? user : null;
33-
} catch (Exception e) {
34-
throw new SaturnJobConsoleException(e);
35-
}
30+
return PasswordUtils.validate(password, user.getPassword(), hashMethod) ? user : null;
3631
}
3732

3833
public void setHashMethod(String hashMethod) {

saturn-console-api/src/main/java/com/vip/saturn/job/console/utils/PasswordUtils.java

+21-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package com.vip.saturn.job.console.utils;
22

3-
import org.apache.commons.codec.binary.Base64;
3+
import org.apache.commons.codec.DecoderException;
4+
import org.apache.commons.codec.binary.Hex;
5+
import org.slf4j.Logger;
6+
import org.slf4j.LoggerFactory;
47

58
import javax.crypto.SecretKey;
69
import javax.crypto.SecretKeyFactory;
@@ -15,6 +18,8 @@ public class PasswordUtils {
1518

1619
public static final String HASH_METHOD_PBKDF2 = "PBKDF2WithHmacSHA1";
1720

21+
private static final Logger log = LoggerFactory.getLogger(PasswordUtils.class);
22+
1823
private static final int ITERATIONS = 10 * 1000;
1924

2025
private static final int SALT_LEN = 8;
@@ -31,7 +36,7 @@ public static String genPassword(String password, String hashMethod) throws Exce
3136
}
3237

3338
public static String genPassword(String password, byte[] salt, String hashMethod) throws Exception {
34-
return hash(password, salt, hashMethod) + "$" + Base64.encodeBase64String(salt);
39+
return hash(password, salt, hashMethod) + "$" + Hex.encodeHexString(salt);
3540
}
3641

3742
public static String hash(String password, byte[] salt, String hashMethod) throws NoSuchAlgorithmException, InvalidKeySpecException {
@@ -43,21 +48,31 @@ public static String hash(String password, byte[] salt, String hashMethod) throw
4348
}
4449

4550
SecretKey key = secretKeyFactory.generateSecret(new PBEKeySpec(password.toCharArray(), salt, ITERATIONS, KEY_LEN));
46-
return Base64.encodeBase64String(key.getEncoded());
51+
return Hex.encodeHexString(key.getEncoded());
4752
}
4853

49-
public static boolean validate(String password, String passwordInDB, String hashMethod) throws Exception {
54+
public static boolean validate(String password, String passwordInDB, String hashMethod) {
5055
if (PasswordUtils.HASH_METHOD_PLANTEXT.equals(hashMethod)) {
5156
return password.equals(passwordInDB);
5257
}
5358

5459
String[] saltAndPassword = passwordInDB.split("\\$");
5560
if (saltAndPassword.length != 2) {
56-
throw new IllegalArgumentException("Invalid password stored in DB");
61+
log.debug("malformed password in db");
62+
return false;
5763
}
5864

59-
String hashOfRequestPassword = hash(password, Base64.decodeBase64(saltAndPassword[1]), hashMethod);
65+
String hashOfRequestPassword = null;
66+
try {
67+
hashOfRequestPassword = hash(password, getSalt(saltAndPassword[1]), hashMethod);
68+
} catch (Exception e) {
69+
return false;
70+
}
6071
return hashOfRequestPassword.equals(new String(saltAndPassword[0]));
6172
}
6273

74+
private static byte[] getSalt(String s) throws DecoderException {
75+
return Hex.decodeHex(s.toCharArray());
76+
}
77+
6378
}

saturn-console-api/src/test/java/com/vip/saturn/job/console/utils/PasswordUtilsTest.java

+7-12
Original file line numberDiff line numberDiff line change
@@ -12,27 +12,22 @@ public class PasswordUtilsTest {
1212
@Test
1313
public void testGenSaltedPassword() throws Exception {
1414
String password = PasswordUtils.genPassword("password", "salt".getBytes(), "PBKDF2WithHmacSHA1");
15-
assertEquals("osJkYYaChHS3VFkaVHwY8TLYjXRMFSZVpHAWGhoFITU=$c2FsdA==", password);
15+
assertEquals("a2c2646186828474b754591a547c18f132d88d744c152655a470161a1a052135$73616c74", password);
1616
}
1717

1818
@Test
1919
public void testValidate() throws Exception {
20-
assertTrue(PasswordUtils.validate("password", "osJkYYaChHS3VFkaVHwY8TLYjXRMFSZVpHAWGhoFITU=$c2FsdA==", "PBKDF2WithHmacSHA1"));
21-
assertFalse(PasswordUtils.validate("password1", "osJkYYaChHS3VFkaVHwY8TLYjXRMFSZVpHAWGhoFITU=$c2FsdA==", "PBKDF2WithHmacSHA1"));
20+
String passwordInDB = "a2c2646186828474b754591a547c18f132d88d744c152655a470161a1a052135$73616c74";
21+
22+
assertTrue(PasswordUtils.validate("password", passwordInDB, "PBKDF2WithHmacSHA1"));
23+
assertFalse(PasswordUtils.validate("password1", passwordInDB, "PBKDF2WithHmacSHA1"));
2224
assertTrue(PasswordUtils.validate("password", "password", "plaintext"));
2325
assertFalse(PasswordUtils.validate("password1", "password", "plaintext"));
2426
}
2527

2628
@Test
27-
public void testValidateWherePasswordInDBisMalfomred() {
29+
public void testValidateWherePasswordInDBisMalfomred() throws Exception {
2830
int count = 0;
29-
try {
30-
PasswordUtils.validate("password", "password", "PBKDF2WithHmacSHA1");
31-
} catch (Exception e) {
32-
count++;
33-
assertEquals("Invalid password stored in DB", e.getMessage());
34-
}
35-
36-
assertEquals(1, count);
31+
assertFalse(PasswordUtils.validate("password", "password", "PBKDF2WithHmacSHA1"));
3732
}
3833
}

0 commit comments

Comments
 (0)