Skip to content
Merged
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 @@ -17,9 +17,11 @@

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


import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.roller.weblogger.config.WebloggerConfig;
Expand Down Expand Up @@ -95,4 +97,14 @@ public static DelegatingPasswordEncoder create() {

return new DelegatingPasswordEncoder(algorithm, encoders);
}

/**
* A random password for users who log in through OpenID Connect and never
* type one. It is 64 characters, so every encoder accepts it (BCrypt takes
* at most 72 bytes), and comes from a secure random source, since it still
* guards the account's database login.
*/
public static String randomUnusedPassword() {
return RandomStringUtils.secure().nextAlphanumeric(64);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@
import java.util.TimeZone;

import org.apache.commons.lang3.CharSetUtils;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.roller.weblogger.WebloggerException;
import org.apache.roller.weblogger.ui.core.security.RollerPasswordEncoders;
import org.apache.roller.weblogger.business.WebloggerFactory;
import org.apache.roller.weblogger.business.UserManager;
import org.apache.roller.weblogger.config.AuthMethod;
Expand Down Expand Up @@ -161,7 +161,7 @@ public String save() {
// User.password does not allow null, so generate one
if (authMethod.equals(AuthMethod.OIDC) ||
(authMethod.equals(AuthMethod.DB_OIDC) && !StringUtils.isEmpty(bean.getOpenIdUrl()))) {
String randomString = RandomStringUtils.randomAlphanumeric(255);
String randomString = RollerPasswordEncoders.randomUnusedPassword();
user.resetPassword(randomString);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@
*/
package org.apache.roller.weblogger.ui.struts2.core;

import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.roller.weblogger.WebloggerException;
import org.apache.roller.weblogger.ui.core.security.RollerPasswordEncoders;
import org.apache.roller.weblogger.business.UserManager;
import org.apache.roller.weblogger.business.WebloggerFactory;
import org.apache.roller.weblogger.config.AuthMethod;
Expand Down Expand Up @@ -109,7 +109,7 @@ public String save() {
// User.password does not allow null, so generate one
if (authMethod.equals(AuthMethod.OIDC) ||
(authMethod.equals(AuthMethod.DB_OIDC) && !StringUtils.isEmpty(bean.getOpenIdUrl()))) {
String randomString = RandomStringUtils.randomAlphanumeric(255);
String randomString = RollerPasswordEncoders.randomUnusedPassword();
existingUser.resetPassword(randomString);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@
import java.util.UUID;
import jakarta.servlet.http.HttpServletRequest;
import org.apache.commons.lang3.CharSetUtils;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.roller.weblogger.WebloggerException;
import org.apache.roller.weblogger.ui.core.security.RollerPasswordEncoders;
import org.apache.roller.weblogger.business.WebloggerFactory;
import org.apache.roller.weblogger.business.UserManager;
import org.apache.roller.weblogger.config.AuthMethod;
Expand Down Expand Up @@ -384,7 +384,7 @@ public void myValidate() {
// User.password does not allow null, so generate one for OIDC users
if (getAuthMethod().equals(AuthMethod.OIDC.name()) ||
(getAuthMethod().equals(AuthMethod.DB_OIDC.name()) && !StringUtils.isEmpty(getBean().getOpenIdUrl()))) {
String randomString = RandomStringUtils.randomAlphanumeric(255);
String randomString = RollerPasswordEncoders.randomUnusedPassword();
getBean().setPasswordText(randomString);
getBean().setPasswordConfirm(randomString);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

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

import java.nio.file.Files;
import java.nio.file.Paths;

import org.junit.jupiter.api.Test;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

class RandomUnusedPasswordTest {

@Test
void everyEncoderAcceptsIt() {
String password = RollerPasswordEncoders.randomUnusedPassword();
assertEquals(64, password.length());
BCryptPasswordEncoder bcrypt = new BCryptPasswordEncoder();
assertTrue(bcrypt.matches(password, bcrypt.encode(password)));
assertNotEquals(password, RollerPasswordEncoders.randomUnusedPassword());
}

@Test
void oidcAccountFormsUseIt() throws Exception {
for (String action : new String[] {"core/Register", "core/Profile", "admin/UserEdit"}) {
String source = new String(Files.readAllBytes(Paths.get(
"src/main/java/org/apache/roller/weblogger/ui/struts2/" + action + ".java")));
assertFalse(source.contains("randomAlphanumeric(255)"), action);
assertTrue(source.contains("RollerPasswordEncoders.randomUnusedPassword()"), action);
}
}
}
Loading