Rename CompromisedPasswordCheckResult to CompromisedPasswordDecision

Issue gh-7395
This commit is contained in:
Marcus Hert Da Coregio 2024-04-30 08:38:03 -03:00
parent 1ca381c364
commit b3c7f3ff19
11 changed files with 34 additions and 34 deletions

View File

@ -47,8 +47,8 @@ import org.springframework.security.authentication.TestingAuthenticationToken;
import org.springframework.security.authentication.event.AbstractAuthenticationEvent; import org.springframework.security.authentication.event.AbstractAuthenticationEvent;
import org.springframework.security.authentication.event.AbstractAuthenticationFailureEvent; import org.springframework.security.authentication.event.AbstractAuthenticationFailureEvent;
import org.springframework.security.authentication.event.AuthenticationSuccessEvent; import org.springframework.security.authentication.event.AuthenticationSuccessEvent;
import org.springframework.security.authentication.password.CompromisedPasswordCheckResult;
import org.springframework.security.authentication.password.CompromisedPasswordChecker; import org.springframework.security.authentication.password.CompromisedPasswordChecker;
import org.springframework.security.authentication.password.CompromisedPasswordDecision;
import org.springframework.security.authentication.password.CompromisedPasswordException; import org.springframework.security.authentication.password.CompromisedPasswordException;
import org.springframework.security.config.Customizer; import org.springframework.security.config.Customizer;
import org.springframework.security.config.annotation.SecurityContextChangedListenerConfig; import org.springframework.security.config.annotation.SecurityContextChangedListenerConfig;
@ -809,11 +809,11 @@ public class HttpSecurityConfigurationTests {
private static class TestCompromisedPasswordChecker implements CompromisedPasswordChecker { private static class TestCompromisedPasswordChecker implements CompromisedPasswordChecker {
@Override @Override
public CompromisedPasswordCheckResult check(String password) { public CompromisedPasswordDecision check(String password) {
if ("password".equals(password)) { if ("password".equals(password)) {
return new CompromisedPasswordCheckResult(true); return new CompromisedPasswordDecision(true);
} }
return new CompromisedPasswordCheckResult(false); return new CompromisedPasswordDecision(false);
} }
} }

View File

@ -26,7 +26,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.password.CompromisedPasswordCheckResult; import org.springframework.security.authentication.password.CompromisedPasswordDecision;
import org.springframework.security.authentication.password.CompromisedPasswordException; import org.springframework.security.authentication.password.CompromisedPasswordException;
import org.springframework.security.authentication.password.ReactiveCompromisedPasswordChecker; import org.springframework.security.authentication.password.ReactiveCompromisedPasswordChecker;
import org.springframework.security.config.Customizer; import org.springframework.security.config.Customizer;
@ -228,11 +228,11 @@ public class ServerHttpSecurityConfigurationTests {
static class TestReactivePasswordChecker implements ReactiveCompromisedPasswordChecker { static class TestReactivePasswordChecker implements ReactiveCompromisedPasswordChecker {
@Override @Override
public Mono<CompromisedPasswordCheckResult> check(String password) { public Mono<CompromisedPasswordDecision> check(String password) {
if ("password".equals(password)) { if ("password".equals(password)) {
return Mono.just(new CompromisedPasswordCheckResult(true)); return Mono.just(new CompromisedPasswordDecision(true));
} }
return Mono.just(new CompromisedPasswordCheckResult(false)); return Mono.just(new CompromisedPasswordDecision(false));
} }
} }

View File

@ -25,8 +25,8 @@ import reactor.core.scheduler.Schedulers;
import org.springframework.context.MessageSource; import org.springframework.context.MessageSource;
import org.springframework.context.MessageSourceAware; import org.springframework.context.MessageSourceAware;
import org.springframework.context.support.MessageSourceAccessor; import org.springframework.context.support.MessageSourceAccessor;
import org.springframework.security.authentication.password.CompromisedPasswordCheckResult;
import org.springframework.security.authentication.password.CompromisedPasswordChecker; import org.springframework.security.authentication.password.CompromisedPasswordChecker;
import org.springframework.security.authentication.password.CompromisedPasswordDecision;
import org.springframework.security.authentication.password.CompromisedPasswordException; import org.springframework.security.authentication.password.CompromisedPasswordException;
import org.springframework.security.authentication.password.ReactiveCompromisedPasswordChecker; import org.springframework.security.authentication.password.ReactiveCompromisedPasswordChecker;
import org.springframework.security.core.Authentication; import org.springframework.security.core.Authentication;
@ -118,7 +118,7 @@ public abstract class AbstractUserDetailsReactiveAuthenticationManager
return Mono.empty(); return Mono.empty();
} }
return this.compromisedPasswordChecker.check(password) return this.compromisedPasswordChecker.check(password)
.filter(CompromisedPasswordCheckResult::isCompromised) .filter(CompromisedPasswordDecision::isCompromised)
.flatMap((compromised) -> Mono.error(new CompromisedPasswordException( .flatMap((compromised) -> Mono.error(new CompromisedPasswordException(
"The provided password is compromised, please change your password"))); "The provided password is compromised, please change your password")));
} }

View File

@ -29,9 +29,9 @@ public interface CompromisedPasswordChecker {
/** /**
* Check whether the password is compromised * Check whether the password is compromised
* @param password the password to check * @param password the password to check
* @return a non-null {@link CompromisedPasswordCheckResult} * @return a non-null {@link CompromisedPasswordDecision}
*/ */
@NonNull @NonNull
CompromisedPasswordCheckResult check(String password); CompromisedPasswordDecision check(String password);
} }

View File

@ -16,11 +16,11 @@
package org.springframework.security.authentication.password; package org.springframework.security.authentication.password;
public class CompromisedPasswordCheckResult { public class CompromisedPasswordDecision {
private final boolean compromised; private final boolean compromised;
public CompromisedPasswordCheckResult(boolean compromised) { public CompromisedPasswordDecision(boolean compromised) {
this.compromised = compromised; this.compromised = compromised;
} }

View File

@ -29,8 +29,8 @@ public interface ReactiveCompromisedPasswordChecker {
/** /**
* Check whether the password is compromised * Check whether the password is compromised
* @param password the password to check * @param password the password to check
* @return a {@link Mono} containing the {@link CompromisedPasswordCheckResult} * @return a {@link Mono} containing the {@link CompromisedPasswordDecision}
*/ */
Mono<CompromisedPasswordCheckResult> check(String password); Mono<CompromisedPasswordDecision> check(String password);
} }

View File

@ -27,7 +27,7 @@ import reactor.core.scheduler.Schedulers;
import reactor.test.StepVerifier; import reactor.test.StepVerifier;
import org.springframework.context.MessageSource; import org.springframework.context.MessageSource;
import org.springframework.security.authentication.password.CompromisedPasswordCheckResult; import org.springframework.security.authentication.password.CompromisedPasswordDecision;
import org.springframework.security.authentication.password.CompromisedPasswordException; import org.springframework.security.authentication.password.CompromisedPasswordException;
import org.springframework.security.authentication.password.ReactiveCompromisedPasswordChecker; import org.springframework.security.authentication.password.ReactiveCompromisedPasswordChecker;
import org.springframework.security.core.Authentication; import org.springframework.security.core.Authentication;
@ -276,11 +276,11 @@ public class UserDetailsRepositoryReactiveAuthenticationManagerTests {
static class TestReactivePasswordChecker implements ReactiveCompromisedPasswordChecker { static class TestReactivePasswordChecker implements ReactiveCompromisedPasswordChecker {
@Override @Override
public Mono<CompromisedPasswordCheckResult> check(String password) { public Mono<CompromisedPasswordDecision> check(String password) {
if ("password".equals(password)) { if ("password".equals(password)) {
return Mono.just(new CompromisedPasswordCheckResult(true)); return Mono.just(new CompromisedPasswordDecision(true));
} }
return Mono.just(new CompromisedPasswordCheckResult(false)); return Mono.just(new CompromisedPasswordDecision(false));
} }
} }

View File

@ -33,8 +33,8 @@ import org.springframework.security.authentication.InternalAuthenticationService
import org.springframework.security.authentication.LockedException; import org.springframework.security.authentication.LockedException;
import org.springframework.security.authentication.TestingAuthenticationToken; import org.springframework.security.authentication.TestingAuthenticationToken;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.authentication.password.CompromisedPasswordCheckResult;
import org.springframework.security.authentication.password.CompromisedPasswordChecker; import org.springframework.security.authentication.password.CompromisedPasswordChecker;
import org.springframework.security.authentication.password.CompromisedPasswordDecision;
import org.springframework.security.authentication.password.CompromisedPasswordException; import org.springframework.security.authentication.password.CompromisedPasswordException;
import org.springframework.security.core.Authentication; import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority; import org.springframework.security.core.GrantedAuthority;
@ -637,11 +637,11 @@ public class DaoAuthenticationProviderTests {
private static class TestCompromisedPasswordChecker implements CompromisedPasswordChecker { private static class TestCompromisedPasswordChecker implements CompromisedPasswordChecker {
@Override @Override
public CompromisedPasswordCheckResult check(String password) { public CompromisedPasswordDecision check(String password) {
if ("password".equals(password)) { if ("password".equals(password)) {
return new CompromisedPasswordCheckResult(true); return new CompromisedPasswordDecision(true);
} }
return new CompromisedPasswordCheckResult(false); return new CompromisedPasswordDecision(false);
} }
} }

View File

@ -26,8 +26,8 @@ import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import org.springframework.lang.NonNull; import org.springframework.lang.NonNull;
import org.springframework.security.authentication.password.CompromisedPasswordCheckResult;
import org.springframework.security.authentication.password.CompromisedPasswordChecker; import org.springframework.security.authentication.password.CompromisedPasswordChecker;
import org.springframework.security.authentication.password.CompromisedPasswordDecision;
import org.springframework.security.crypto.codec.Hex; import org.springframework.security.crypto.codec.Hex;
import org.springframework.util.Assert; import org.springframework.util.Assert;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
@ -61,7 +61,7 @@ public final class HaveIBeenPwnedRestApiPasswordChecker implements CompromisedPa
@Override @Override
@NonNull @NonNull
public CompromisedPasswordCheckResult check(String password) { public CompromisedPasswordDecision check(String password) {
byte[] hash = this.sha1Digest.digest(password.getBytes(StandardCharsets.UTF_8)); byte[] hash = this.sha1Digest.digest(password.getBytes(StandardCharsets.UTF_8));
String encoded = new String(Hex.encode(hash)).toUpperCase(); String encoded = new String(Hex.encode(hash)).toUpperCase();
String prefix = encoded.substring(0, PREFIX_LENGTH); String prefix = encoded.substring(0, PREFIX_LENGTH);
@ -69,7 +69,7 @@ public final class HaveIBeenPwnedRestApiPasswordChecker implements CompromisedPa
List<String> passwords = getLeakedPasswordsForPrefix(prefix); List<String> passwords = getLeakedPasswordsForPrefix(prefix);
boolean isLeaked = findLeakedPassword(passwords, suffix); boolean isLeaked = findLeakedPassword(passwords, suffix);
return new CompromisedPasswordCheckResult(isLeaked); return new CompromisedPasswordDecision(isLeaked);
} }
/** /**

View File

@ -26,7 +26,7 @@ import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono; import reactor.core.publisher.Mono;
import reactor.core.scheduler.Schedulers; import reactor.core.scheduler.Schedulers;
import org.springframework.security.authentication.password.CompromisedPasswordCheckResult; import org.springframework.security.authentication.password.CompromisedPasswordDecision;
import org.springframework.security.authentication.password.ReactiveCompromisedPasswordChecker; import org.springframework.security.authentication.password.ReactiveCompromisedPasswordChecker;
import org.springframework.security.crypto.codec.Hex; import org.springframework.security.crypto.codec.Hex;
import org.springframework.util.Assert; import org.springframework.util.Assert;
@ -60,10 +60,10 @@ public class HaveIBeenPwnedRestApiReactivePasswordChecker implements ReactiveCom
} }
@Override @Override
public Mono<CompromisedPasswordCheckResult> check(String password) { public Mono<CompromisedPasswordDecision> check(String password) {
return getHash(password).map((hash) -> new String(Hex.encode(hash))) return getHash(password).map((hash) -> new String(Hex.encode(hash)))
.flatMap(this::findLeakedPassword) .flatMap(this::findLeakedPassword)
.map(CompromisedPasswordCheckResult::new); .map(CompromisedPasswordDecision::new);
} }
private Mono<Boolean> findLeakedPassword(String encodedPassword) { private Mono<Boolean> findLeakedPassword(String encodedPassword) {

View File

@ -25,7 +25,7 @@ import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.springframework.security.authentication.password.CompromisedPasswordCheckResult; import org.springframework.security.authentication.password.CompromisedPasswordDecision;
import org.springframework.web.client.RestClient; import org.springframework.web.client.RestClient;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
@ -69,7 +69,7 @@ class HaveIBeenPwnedRestApiPasswordCheckerTests {
@Test @Test
void checkWhenPasswordIsLeakedThenIsCompromised() throws InterruptedException { void checkWhenPasswordIsLeakedThenIsCompromised() throws InterruptedException {
this.server.enqueue(new MockResponse().setBody(this.pwnedPasswords).setResponseCode(200)); this.server.enqueue(new MockResponse().setBody(this.pwnedPasswords).setResponseCode(200));
CompromisedPasswordCheckResult check = this.passwordChecker.check("P@ssw0rd"); CompromisedPasswordDecision check = this.passwordChecker.check("P@ssw0rd");
assertThat(check.isCompromised()).isTrue(); assertThat(check.isCompromised()).isTrue();
assertThat(this.server.takeRequest().getPath()).isEqualTo("/range/21BD1"); assertThat(this.server.takeRequest().getPath()).isEqualTo("/range/21BD1");
} }
@ -77,14 +77,14 @@ class HaveIBeenPwnedRestApiPasswordCheckerTests {
@Test @Test
void checkWhenPasswordNotLeakedThenNotCompromised() { void checkWhenPasswordNotLeakedThenNotCompromised() {
this.server.enqueue(new MockResponse().setBody(this.pwnedPasswords).setResponseCode(200)); this.server.enqueue(new MockResponse().setBody(this.pwnedPasswords).setResponseCode(200));
CompromisedPasswordCheckResult check = this.passwordChecker.check("My1nCr3d!bL3P@SS0W0RD"); CompromisedPasswordDecision check = this.passwordChecker.check("My1nCr3d!bL3P@SS0W0RD");
assertThat(check.isCompromised()).isFalse(); assertThat(check.isCompromised()).isFalse();
} }
@Test @Test
void checkWhenNoPasswordsReturnedFromApiCallThenNotCompromised() { void checkWhenNoPasswordsReturnedFromApiCallThenNotCompromised() {
this.server.enqueue(new MockResponse().setResponseCode(200)); this.server.enqueue(new MockResponse().setResponseCode(200));
CompromisedPasswordCheckResult check = this.passwordChecker.check("123456"); CompromisedPasswordDecision check = this.passwordChecker.check("123456");
assertThat(check.isCompromised()).isFalse(); assertThat(check.isCompromised()).isFalse();
} }