feat: admin-set passwords for staff, and a secure temporary-password generator
Creating a user takes the first password from the admin, and a new
POST /api/admin/users/{id}/password lets an admin set a temporary password
for another active user. Both store the bcrypt hash with login = N, so the
user must choose their own password at the next sign-in. No email is sent.
The password must match its confirmation and pass the admin password rules.
The emailed-password generator now draws from SecureRandom.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EFUzfj5akPxyikzGc2iAHM
This commit is contained in:
1 parent
b49c9f1bc3
commit
2c0a847b68
12 files changed
+386
-15
No files matched your search
+108
@@ -0,0 +1,108 @@
|
||||
package com.solvway.services.system.controller;
|
||||
|
||||
import com.solvway.services.system.constant.SystemConstants.LoginStatus;
|
||||
import com.solvway.core.enums.UserEnum.UserType;
|
||||
import com.solvway.services.system.model.PasswordRuleProperty;
|
||||
import com.solvway.services.system.model.User;
|
||||
import com.solvway.services.system.repo.PasswordRuleRepository;
|
||||
import com.solvway.services.system.service.EmailService;
|
||||
import com.solvway.services.system.service.UserService;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
import org.mockito.InjectMocks;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.Spy;
|
||||
import org.mockito.junit.jupiter.MockitoExtension;
|
||||
import org.mockito.junit.jupiter.MockitoSettings;
|
||||
import org.mockito.quality.Strictness;
|
||||
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.anyLong;
|
||||
import static org.mockito.ArgumentMatchers.anyString;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
/** Admin-set passwords: validated against ADMIN_PWD_FORMAT, stored bcrypt-encoded with login = N, never emailed. */
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
@MockitoSettings(strictness = Strictness.LENIENT)
|
||||
class UserControllerSetAdminPasswordTest {
|
||||
|
||||
@Mock private UserService userService;
|
||||
@Mock private EmailService emailService;
|
||||
@Mock private PasswordRuleRepository passwordRuleRepository;
|
||||
@Spy private BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder(4);
|
||||
@InjectMocks private UserController controller;
|
||||
|
||||
@BeforeEach
|
||||
void minLengthEightRule() {
|
||||
PasswordRuleProperty rule = new PasswordRuleProperty();
|
||||
rule.setParam("INPUT_LENGTH_MIN");
|
||||
rule.setValue("8");
|
||||
rule.setStatus("Y");
|
||||
when(passwordRuleRepository.findByType("ADMIN_PWD_FORMAT")).thenReturn(List.of(rule));
|
||||
when(userService.resetPassword(anyLong(), anyString(), any())).thenReturn(1);
|
||||
}
|
||||
|
||||
private User admin(boolean enabled, UserType type) {
|
||||
User u = new User();
|
||||
u.setId(5L);
|
||||
u.setUsername("sales1");
|
||||
u.setType(type);
|
||||
u.setEnabled(enabled);
|
||||
when(userService.findById(5L)).thenReturn(Optional.of(u));
|
||||
return u;
|
||||
}
|
||||
|
||||
private static HashMap<String, Object> body(Object id, String password) {
|
||||
return new HashMap<>(Map.of("id", id, "password", password));
|
||||
}
|
||||
|
||||
@Test
|
||||
void validateReturnsFailingAdminRules() {
|
||||
assertThat(controller.validatePasswordFromRulesForAdmin(new HashMap<>(Map.of("password", "short")))).hasSize(1);
|
||||
assertThat(controller.validatePasswordFromRulesForAdmin(new HashMap<>(Map.of("password", "LongEnough1")))).isEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
void storesBcryptHashWithLoginN() {
|
||||
admin(true, UserType.A);
|
||||
|
||||
int updated = controller.setAdminPassword(body(5, "LongEnough1"));
|
||||
|
||||
assertThat(updated).isEqualTo(1);
|
||||
ArgumentCaptor<String> hash = ArgumentCaptor.forClass(String.class);
|
||||
verify(userService).resetPassword(eq(5L), hash.capture(), eq(LoginStatus.N));
|
||||
assertThat(hash.getValue()).isNotEqualTo("LongEnough1");
|
||||
assertThat(bCryptPasswordEncoder.matches("LongEnough1", hash.getValue())).isTrue();
|
||||
verify(emailService, never()).send(any());
|
||||
}
|
||||
|
||||
@Test
|
||||
void refusesDisabledUnknownNonAdminAndWeakPasswords() {
|
||||
admin(false, UserType.A);
|
||||
assertThat(controller.setAdminPassword(body(5, "LongEnough1"))).isZero();
|
||||
|
||||
admin(true, UserType.M);
|
||||
assertThat(controller.setAdminPassword(body(5, "LongEnough1"))).isZero();
|
||||
|
||||
admin(true, UserType.A);
|
||||
assertThat(controller.setAdminPassword(body(5, "short"))).isZero();
|
||||
|
||||
when(userService.findById(9L)).thenReturn(Optional.empty());
|
||||
assertThat(controller.setAdminPassword(body(9, "LongEnough1"))).isZero();
|
||||
|
||||
verify(userService, never()).resetPassword(anyLong(), anyString(), any());
|
||||
verify(emailService, never()).send(any());
|
||||
}
|
||||
}
|
||||
+40
@@ -0,0 +1,40 @@
|
||||
package com.solvway.services.system.util;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import com.solvway.core.utils.ValidateUtil;
|
||||
|
||||
/** The shared temporary-password generator (lives in solvway-core-libs, which has no test setup). */
|
||||
class ValidateUtilPasswordTest {
|
||||
|
||||
private static final String ALPHABET = "ABCDEFGHJKLMNPRSTUVWXYZabcdefghjkmnpqrstuvwyz23456789";
|
||||
private final ValidateUtil util = new ValidateUtil();
|
||||
|
||||
@Test
|
||||
void lengthMatchesArgumentAndCharactersComeFromTheAlphabet() {
|
||||
for (int len : new int[] {1, 8, 12, 32}) {
|
||||
String p = util.getRandomPasswordString(len);
|
||||
assertThat(p).hasSize(len);
|
||||
assertThat(p.chars()).allMatch(c -> ALPHABET.indexOf(c) >= 0);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void thousandPasswordsOfTwelveAreDistinct() {
|
||||
Set<String> seen = new HashSet<>();
|
||||
for (int i = 0; i < 1000; i++) seen.add(util.getRandomPasswordString(12));
|
||||
assertThat(seen).hasSize(1000);
|
||||
}
|
||||
|
||||
@Test
|
||||
void generatorUsesSecureRandom() throws Exception {
|
||||
// java.util.Random would also pass the tests above; this pins the secure source the spec requires.
|
||||
var field = ValidateUtil.class.getDeclaredField("PASSWORD_RANDOM");
|
||||
assertThat(field.getType()).isEqualTo(java.security.SecureRandom.class);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user