Skip to content

Commit

Permalink
Add ContinueOnError Support For Failed Authentications
Browse files Browse the repository at this point in the history
Closes gh-14521
  • Loading branch information
ruabtmh authored and ruabtmh committed Feb 22, 2024
1 parent e771267 commit 9e2f34b
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2018 the original author or authors.
* Copyright 2002-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -18,7 +18,10 @@

import java.util.Arrays;
import java.util.List;
import java.util.function.Function;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

Expand All @@ -28,7 +31,7 @@
/**
* A {@link ReactiveAuthenticationManager} that delegates to other
* {@link ReactiveAuthenticationManager} instances using the result from the first non
* empty result.
* empty result. Errors from delegates will be ignored if continueOnError is true.
*
* @author Rob Winch
* @since 5.1
Expand All @@ -37,6 +40,10 @@ public class DelegatingReactiveAuthenticationManager implements ReactiveAuthenti

private final List<ReactiveAuthenticationManager> delegates;

private boolean continueOnError = false;

private final Log logger = LogFactory.getLog(getClass());

public DelegatingReactiveAuthenticationManager(ReactiveAuthenticationManager... entryPoints) {
this(Arrays.asList(entryPoints));
}
Expand All @@ -48,11 +55,15 @@ public DelegatingReactiveAuthenticationManager(List<ReactiveAuthenticationManage

@Override
public Mono<Authentication> authenticate(Authentication authentication) {
// @formatter:off
return Flux.fromIterable(this.delegates)
.concatMap((m) -> m.authenticate(authentication))
.next();
// @formatter:on
Flux<ReactiveAuthenticationManager> result = Flux.fromIterable(this.delegates);
Function<ReactiveAuthenticationManager, Mono<Authentication>> logging = (m) -> m.authenticate(authentication)
.doOnError(logger::debug);

return ((this.continueOnError) ? result.concatMapDelayError(logging) : result.concatMap(logging)).next();
}

public void setContinueOnError(boolean continueOnError) {
this.continueOnError = continueOnError;
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2018 the original author or authors.
* Copyright 2002-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -77,4 +77,43 @@ public void authenticateWhenBadCredentialsThenDelegate2NotInvokedAndError() {
.verify();
}

@Test
public void authenticateWhenContinueOnErrorAndFirstBadCredentialsThenTriesSecond() {
given(this.delegate1.authenticate(any())).willReturn(Mono.error(new BadCredentialsException("Test")));
given(this.delegate2.authenticate(any())).willReturn(Mono.just(this.authentication));

DelegatingReactiveAuthenticationManager manager = managerWithContinueOnError();

assertThat(manager.authenticate(this.authentication).block()).isEqualTo(this.authentication);
}

@Test
public void authenticateWhenContinueOnErrorAndBothDelegatesBadCredentialsThenError() {
given(this.delegate1.authenticate(any())).willReturn(Mono.error(new BadCredentialsException("Test")));
given(this.delegate2.authenticate(any())).willReturn(Mono.error(new BadCredentialsException("Test")));

DelegatingReactiveAuthenticationManager manager = managerWithContinueOnError();

StepVerifier.create(manager.authenticate(this.authentication))
.expectError(BadCredentialsException.class)
.verify();
}

@Test
public void authenticateWhenContinueOnErrorAndDelegate1NotEmptyThenReturnsNotEmpty() {
given(this.delegate1.authenticate(any())).willReturn(Mono.just(this.authentication));

DelegatingReactiveAuthenticationManager manager = managerWithContinueOnError();

assertThat(manager.authenticate(this.authentication).block()).isEqualTo(this.authentication);
}

private DelegatingReactiveAuthenticationManager managerWithContinueOnError() {
DelegatingReactiveAuthenticationManager manager = new DelegatingReactiveAuthenticationManager(this.delegate1,
this.delegate2);
manager.setContinueOnError(true);

return manager;
}

}

0 comments on commit 9e2f34b

Please sign in to comment.