Allow upgrading between different BCrypt encodings
Fixes gh-7042
This commit is contained in:
@@ -169,4 +169,35 @@ public class BCryptPasswordEncoderTests {
|
||||
assertThat(encoder.matches("password", "012345678901234567890123456789")).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void upgradeFromLowerStrength() {
|
||||
BCryptPasswordEncoder weakEncoder = new BCryptPasswordEncoder(5);
|
||||
BCryptPasswordEncoder strongEncoder = new BCryptPasswordEncoder(15);
|
||||
|
||||
String weakPassword = weakEncoder.encode("password");
|
||||
String strongPassword = strongEncoder.encode("password");
|
||||
|
||||
assertThat(weakEncoder.upgradeEncoding(strongPassword)).isFalse();
|
||||
assertThat(strongEncoder.upgradeEncoding(weakPassword)).isTrue();
|
||||
}
|
||||
|
||||
/**
|
||||
* @see <a href="https://github.com/spring-projects/spring-security/pull/7042#issuecomment-506755496">https://github.com/spring-projects/spring-security/pull/7042#issuecomment-506755496</>
|
||||
*/
|
||||
@Test
|
||||
public void upgradeFromNullOrEmpty() {
|
||||
BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
|
||||
assertThat(encoder.upgradeEncoding(null)).isFalse();
|
||||
assertThat(encoder.upgradeEncoding("")).isFalse();
|
||||
}
|
||||
|
||||
/**
|
||||
* @see <a href="https://github.com/spring-projects/spring-security/pull/7042#issuecomment-506755496">https://github.com/spring-projects/spring-security/pull/7042#issuecomment-506755496</>
|
||||
*/
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void upgradeFromNonBCrypt() {
|
||||
BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
|
||||
encoder.upgradeEncoding("not-a-bcrypt-password");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -215,12 +215,16 @@ public class DelegatingPasswordEncoderTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void upgradeEncodingWhenSameIdThenFalse() {
|
||||
assertThat(this.passwordEncoder.upgradeEncoding(this.bcryptEncodedPassword)).isFalse();
|
||||
public void upgradeEncodingWhenSameIdThenEncoderDecides() {
|
||||
this.passwordEncoder.upgradeEncoding(this.bcryptEncodedPassword);
|
||||
|
||||
verify(bcrypt).upgradeEncoding(this.encodedPassword);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void upgradeEncodingWhenDifferentIdThenTrue() {
|
||||
assertThat(this.passwordEncoder.upgradeEncoding(this.noopEncodedPassword)).isTrue();
|
||||
|
||||
verifyZeroInteractions(bcrypt);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user