Skip to content

Commit 7221a98

Browse files
committed
Adapt to super.encode() becoming final in Spring 7
Adds an encode2() method with a default implementation to call encode().
1 parent b5488d8 commit 7221a98

File tree

3 files changed

+17
-6
lines changed

3 files changed

+17
-6
lines changed

core/src/main/java/hudson/security/HudsonPrivateSecurityRealm.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -456,7 +456,7 @@ private SignupInfo validateAccountCreationForm(StaplerRequest2 req, boolean vali
456456
}
457457

458458
try {
459-
PASSWORD_HASH_ENCODER.encode(si.password1);
459+
PASSWORD_HASH_ENCODER.encode2(si.password1);
460460
} catch (RuntimeException ex) {
461461
si.errors.put("password1", ex.getMessage());
462462
}
@@ -853,7 +853,7 @@ public Details newInstance(StaplerRequest2 req, JSONObject formData) throws Form
853853

854854
// The password is being changed
855855
try {
856-
PASSWORD_HASH_ENCODER.encode(pwd);
856+
PASSWORD_HASH_ENCODER.encode2(pwd);
857857
} catch (RuntimeException ex) {
858858
throw new FormException(ex.getMessage(), "user.password");
859859
}
@@ -934,9 +934,9 @@ static class JBCryptEncoder extends BCryptPasswordEncoder implements PasswordHas
934934
private static final Pattern BCRYPT_PATTERN = Pattern.compile("^\\$2a\\$([0-9]{2})\\$.{53}$");
935935

936936
@Override
937-
public String encode(CharSequence rawPassword) {
937+
public String encode2(CharSequence rawPassword) {
938938
try {
939-
return super.encode(rawPassword);
939+
return encode(rawPassword);
940940
} catch (IllegalArgumentException ex) {
941941
if (ex.getMessage().equals("password cannot be more than 72 bytes")) {
942942
if (rawPassword.toString().matches("\\A\\p{ASCII}+\\z")) {

core/src/main/java/hudson/security/PasswordHashEncoder.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,8 @@
2727

2828
interface PasswordHashEncoder extends PasswordEncoder {
2929
boolean isHashValid(String hash);
30+
31+
default String encode2(CharSequence rawPassword) {
32+
return encode(rawPassword);
33+
}
3034
}

core/src/test/java/hudson/security/HudsonPrivateSecurityRealmTest.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,17 +160,24 @@ void testJBCryptPasswordMatching() {
160160
@Issue("JENKINS-75533")
161161
@Test
162162
void ensureExpectedMessageAscii() {
163-
final IllegalArgumentException ex = assertThrows(IllegalArgumentException.class, () -> HudsonPrivateSecurityRealm.PASSWORD_HASH_ENCODER.encode(
163+
final IllegalArgumentException ex = assertThrows(IllegalArgumentException.class, () -> HudsonPrivateSecurityRealm.PASSWORD_HASH_ENCODER.encode2(
164164
"1234567890123456789012345678901234567890123456789012345678901234567890123"));
165165
assertThat(ex.getMessage(), is(Messages.HudsonPrivateSecurityRealm_CreateAccount_BCrypt_PasswordTooLong_ASCII()));
166166
}
167167

168168
@Issue("JENKINS-75533")
169169
@Test
170170
void ensureExpectedMessageEmoji() {
171-
final IllegalArgumentException ex = assertThrows(IllegalArgumentException.class, () -> HudsonPrivateSecurityRealm.PASSWORD_HASH_ENCODER.encode(
171+
final IllegalArgumentException ex = assertThrows(IllegalArgumentException.class, () -> HudsonPrivateSecurityRealm.PASSWORD_HASH_ENCODER.encode2(
172172
"\uD83E\uDD20\uD83E\uDD20\uD83E\uDD20\uD83E\uDD20\uD83E\uDD20\uD83E\uDD20\uD83E\uDD20\uD83E\uDD20\uD83E\uDD20" +
173173
"\uD83E\uDD20\uD83E\uDD20\uD83E\uDD20\uD83E\uDD20\uD83E\uDD20\uD83E\uDD20\uD83E\uDD20\uD83E\uDD20\uD83E\uDD20\uD83E\uDD20")); // 🤠
174174
assertThat(ex.getMessage(), is(Messages.HudsonPrivateSecurityRealm_CreateAccount_BCrypt_PasswordTooLong()));
175175
}
176+
177+
@Test
178+
void ensureExpectedMessageFromEncode() {
179+
final IllegalArgumentException ex = assertThrows(IllegalArgumentException.class, () -> HudsonPrivateSecurityRealm.PASSWORD_HASH_ENCODER.encode(
180+
"1234567890123456789012345678901234567890123456789012345678901234567890123"));
181+
assertThat(ex.getMessage(), is("password cannot be more than 72 bytes"));
182+
}
176183
}

0 commit comments

Comments
 (0)