Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ else if(userDetails instanceof LdapUserDetails) {

// for LDAP we don't store its password in the roller_users table,
// just an string indicating external auth method being used.
unusedPassword = WebloggerConfig.getProperty("users.passwords.externalAuthValue","<externalAuth>");
unusedPassword = RollerPasswordEncoders.externalLoginMarker();
ud.setPassword(unusedPassword);
ud.setEnabled(enabled ? Boolean.TRUE : Boolean.FALSE);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

package org.apache.roller.weblogger.ui.core.security;


import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;

Expand All @@ -40,11 +40,35 @@ public final class RollerPasswordEncoders {

private static final Log log = LogFactory.getLog(RollerPasswordEncoders.class);

/** Stored in place of a password when none is configured. */
public static final String DEFAULT_EXTERNAL_LOGIN_MARKER = "<externalAuth>";

private RollerPasswordEncoders() {
}

/**
* The value stored in place of a Roller password for users who log in
* through LDAP or the servlet container: users.passwords.externalAuthValue,
* or the default when that setting is empty.
*/
public static String externalLoginMarker() {
String marker = WebloggerConfig.getProperty("users.passwords.externalAuthValue");
return marker == null || marker.isEmpty() ? DEFAULT_EXTERNAL_LOGIN_MARKER : marker;
}

/**
* Whether a password is the configured external-login marker or the
* default one, which accounts may still have stored from before the
* setting changed. Neither matches at login, so neither can be chosen as a
* Roller password.
*/
public static boolean isExternalLoginMarker(CharSequence password) {
return password != null && (externalLoginMarker().contentEquals(password)
|| DEFAULT_EXTERNAL_LOGIN_MARKER.contentEquals(password));
}

@SuppressWarnings("deprecation")
public static DelegatingPasswordEncoder create() {
public static PasswordEncoder create() {

Map<String, PasswordEncoder> encoders = new HashMap<>();

Expand Down Expand Up @@ -95,7 +119,50 @@ public static DelegatingPasswordEncoder create() {

log.info("Password Encryption Algorithm set to '" + algorithm + "'");

return new DelegatingPasswordEncoder(algorithm, encoders);
return new ExternalLoginAware(new DelegatingPasswordEncoder(algorithm, encoders));
}

/**
* Users who log in through LDAP or the servlet container have no Roller
* password; Roller stores users.passwords.externalAuthValue in its place.
* That placeholder is not a password, so it never matches, and neither do
* the default placeholder and an empty password.
*/
private record ExternalLoginAware(PasswordEncoder delegate) implements PasswordEncoder {

/**
* Returns null for a password that BCrypt refuses as too long. A
* login re-encodes the password it has just checked against an older
* encoding, and for such a password that login has to succeed and
* leave the stored password as it is.
*/
@Override
public String encode(CharSequence rawPassword) {
try {
return delegate.encode(rawPassword);
} catch (IllegalArgumentException e) {
if (rawPassword != null && isTooLong(rawPassword.toString())) {
return null;
}
throw e;
}
}

@Override
public boolean matches(CharSequence rawPassword, String encodedPassword) {
// a stored marker is checked before the delegate sees it, since a
// marker such as {noop}secret would otherwise match its own suffix
if (rawPassword == null || rawPassword.length() == 0 || isExternalLoginMarker(rawPassword)
|| isExternalLoginMarker(encodedPassword)) {
return false;
}
return delegate.matches(rawPassword, encodedPassword);
}

@Override
public boolean upgradeEncoding(String encodedPassword) {
return delegate.upgradeEncoding(encodedPassword);
}
}

/**
Expand All @@ -107,4 +174,21 @@ public static DelegatingPasswordEncoder create() {
public static String randomUnusedPassword() {
return RandomStringUtils.secure().nextAlphanumeric(64);
}

/**
* The longest password, in UTF-8 bytes, that BCrypt accepts. PBKDF2,
* SCrypt and Argon2 take longer ones, but the limit applies whatever the
* algorithm, so that a site can switch to BCrypt, the default, without
* leaving passwords it can't re-encode.
*/
public static final int MAX_PASSWORD_BYTES = 72;

/**
* Returns true when a typed password is longer than an encoder accepts, so
* that a form can say so instead of failing when the password is encoded.
*/
public static boolean isTooLong(String password) {
return password != null
&& password.getBytes(StandardCharsets.UTF_8).length > MAX_PASSWORD_BYTES;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ protected String makeTokenSignature(long tokenExpiryTime, String username, Strin

// for LDAP we don't store its password in the roller_users table,
// just an string indicating external auth method being used.
password = WebloggerConfig.getProperty("users.passwords.externalAuthValue","<externalAuth>");
password = RollerPasswordEncoders.externalLoginMarker();
}

String data = username + ":" + tokenExpiryTime + ":" + password + ":" + getKey();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,14 @@ private boolean isAdd() {
return actionName.equals("createUser");
}

private void myValidate() {
void myValidate() {
if (RollerPasswordEncoders.isTooLong(getBean().getPassword())) {
addError("userRegister.error.passwordTooLong");
}
// the external-login marker never matches at login
if (RollerPasswordEncoders.isExternalLoginMarker(getBean().getPassword())) {
addError("error.add.user.reservedPassword");
}
if (isAdd()) {
String allowed = WebloggerConfig.getProperty("username.allowedChars");
if(allowed == null || allowed.isBlank()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,13 @@ public String save() {
}

public void myValidate() {
if (RollerPasswordEncoders.isTooLong(getBean().getPasswordText())) {
addError("userRegister.error.passwordTooLong");
}
// the external-login marker never matches at login
if (RollerPasswordEncoders.isExternalLoginMarker(getBean().getPasswordText())) {
addError("error.add.user.reservedPassword");
}
if (StringUtils.isEmpty(getBean().getOpenIdUrl())) {
// check that passwords match if they were specified (w/StringUtils.equals, null == null)
if (!StringUtils.equals(getBean().getPasswordText(), getBean().getPasswordConfirm())) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ public class Register extends UIAction {
private String activationCode = null;
private ProfileBean bean = new ProfileBean();

// generated for OIDC users, kept out of the bean so that the form never
// renders it
private String unusedPassword = null;

public Register() {
this.pageTitle = "newUser.addNewUser";
}
Expand Down Expand Up @@ -210,7 +214,9 @@ public String save() {
ud.setEnabled(Boolean.TRUE);

// If user set both password and passwordConfirm then reset password
if (!StringUtils.isEmpty(getBean().getPasswordText()) &&
if (unusedPassword != null) {
ud.resetPassword(unusedPassword);
} else if (!StringUtils.isEmpty(getBean().getPasswordText()) &&
!StringUtils.isEmpty(getBean().getPasswordConfirm())) {
ud.resetPassword(getBean().getPasswordText());
}
Expand Down Expand Up @@ -357,7 +363,7 @@ public void myValidate() {
if (usingSSO) {
// store an unused marker in the Roller DB for the passphrase in
// the LDAP or CMA cases, as actual passwords are stored externally
String unusedPassword = WebloggerConfig.getProperty("users.passwords.externalAuthValue", "<externalAuth>");
String unusedPassword = RollerPasswordEncoders.externalLoginMarker();

// Preserve username and password, Spring Security case
preserveUsernameAndPassword(unusedPassword);
Expand All @@ -375,23 +381,35 @@ public void myValidate() {
}

// check password, it is required if OpenID and SSO are disabled
if (AuthMethod.ROLLERDB.name().equals(getAuthMethod())
if ((AuthMethod.ROLLERDB.name().equals(getAuthMethod())
|| (AuthMethod.DB_OIDC.name().equals(getAuthMethod())
&& StringUtils.isEmpty(getBean().getOpenIdUrl())))
&& StringUtils.isEmpty(getBean().getPasswordText())) {
addError("error.add.user.passwordEmpty");
addError("error.add.user.missingPassword");
return;
}

// User.password does not allow null, so generate one for OIDC users
unusedPassword = null;
if (getAuthMethod().equals(AuthMethod.OIDC.name()) ||
(getAuthMethod().equals(AuthMethod.DB_OIDC.name()) && !StringUtils.isEmpty(getBean().getOpenIdUrl()))) {
String randomString = RollerPasswordEncoders.randomUnusedPassword();
getBean().setPasswordText(randomString);
getBean().setPasswordConfirm(randomString);
}

// check that passwords match
if (!getBean().getPasswordText().equals(getBean().getPasswordConfirm())) {
addError("userRegister.error.mismatchedPasswords");
unusedPassword = RollerPasswordEncoders.randomUnusedPassword();
} else {
// (with LDAP or CMA the bean holds the marker, not a typed password)
if (!usingSSO && RollerPasswordEncoders.isTooLong(getBean().getPasswordText())) {
addError("userRegister.error.passwordTooLong");
}

// the external-login marker is stored for users who log in
// elsewhere and never matches at login
if (!usingSSO && RollerPasswordEncoders.isExternalLoginMarker(getBean().getPasswordText())) {
addError("error.add.user.reservedPassword");
}

// check that passwords match
if (!StringUtils.equals(getBean().getPasswordText(), getBean().getPasswordConfirm())) {
addError("userRegister.error.mismatchedPasswords");
}
}

// check that username is not taken
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/resources/ApplicationResources.properties
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,7 @@ error.add.user.openIdInUse=Open ID already in use with another account.
error.add.user.missingUserName=You must specify a username.
error.add.user.badUserName=Invalid user name (must be alpha-numerics only).
error.add.user.missingPassword=You must specify a password.
error.add.user.reservedPassword=That password is reserved for accounts that log in elsewhere. Please choose a different password.
error.add.user.invalid.timezone=Invalid timezone.
error.add.user.invalid.locale=Invalid locale.
error.upload.dirmax=You cannot exceed the maximum directory size of {0} MB.
Expand Down Expand Up @@ -1521,6 +1522,7 @@ userRegister.button.save=Sign Up

userRegister.success.ready=Yes, form appears to be complete. Click the button below.
userRegister.error.mismatchedPasswords=Your passwords do not match!
userRegister.error.passwordTooLong=Your password is too long. Accented letters, non-Latin characters and emoji take up more room than other characters, so use fewer of them or a shorter password.
userRegister.error.missingOpenID=Open ID URL must be provided.
userRegister.error.missingOpenIDOrPassword=Either an Open ID URL or a password must be provided.
userRegister.error.bothOpenIDAndPassword=Please supply just a password or an OpenID (not both).
Expand Down
Loading
Loading