|
18 | 18 | import java.nio.charset.Charset;
|
19 | 19 | import java.security.Principal;
|
20 | 20 | import java.text.MessageFormat;
|
21 |
| -import java.util.ArrayList; |
22 |
| -import java.util.HashMap; |
23 |
| -import java.util.List; |
24 |
| -import java.util.Map; |
| 21 | +import java.util.*; |
25 | 22 | import java.util.concurrent.TimeUnit;
|
26 | 23 |
|
27 | 24 | import javax.servlet.http.Cookie;
|
@@ -520,21 +517,33 @@ public UserModel authenticate(String username, char[] password, String remoteIP)
|
520 | 517 | protected UserModel authenticateLocal(UserModel user, char [] password) {
|
521 | 518 | UserModel returnedUser = null;
|
522 | 519 |
|
523 |
| - PasswordHash pwdHash = PasswordHash.instanceFor(user.password); |
524 |
| - if (pwdHash != null) { |
525 |
| - if (pwdHash.matches(user.password, password, user.username)) { |
| 520 | + // Create a copy of the password that we can use to rehash to upgrade to a more secure hashing method. |
| 521 | + // This is done to be independent from the implementation of the PasswordHash, which might already clear out |
| 522 | + // the password it gets passed in. This looks a bit stupid, as we could simply clean up the mess, but this |
| 523 | + // falls under "better safe than sorry". |
| 524 | + char[] pwdToUpgrade = Arrays.copyOf(password, password.length); |
| 525 | + try { |
| 526 | + PasswordHash pwdHash = PasswordHash.instanceFor(user.password); |
| 527 | + if (pwdHash != null) { |
| 528 | + if (pwdHash.matches(user.password, password, user.username)) { |
| 529 | + returnedUser = user; |
| 530 | + } |
| 531 | + } else if (user.password.equals(new String(password))) { |
| 532 | + // plain-text password |
526 | 533 | returnedUser = user;
|
527 | 534 | }
|
528 |
| - } else if (user.password.equals(new String(password))) { |
529 |
| - // plain-text password |
530 |
| - returnedUser = user; |
531 |
| - } |
532 |
| - |
533 |
| - // validate user |
534 |
| - returnedUser = validateAuthentication(returnedUser, AuthenticationType.CREDENTIALS); |
535 |
| - |
536 |
| - // try to upgrade the stored password hash to a stronger hash, if necessary |
537 |
| - upgradeStoredPassword(returnedUser, password, pwdHash); |
| 535 | + |
| 536 | + // validate user |
| 537 | + returnedUser = validateAuthentication(returnedUser, AuthenticationType.CREDENTIALS); |
| 538 | + |
| 539 | + // try to upgrade the stored password hash to a stronger hash, if necessary |
| 540 | + upgradeStoredPassword(returnedUser, pwdToUpgrade, pwdHash); |
| 541 | + } |
| 542 | + finally { |
| 543 | + // Now we make sure that the password is zeroed out in any case. |
| 544 | + Arrays.fill(password, Character.MIN_VALUE); |
| 545 | + Arrays.fill(pwdToUpgrade, Character.MIN_VALUE); |
| 546 | + } |
538 | 547 |
|
539 | 548 | return returnedUser;
|
540 | 549 | }
|
|
0 commit comments