Skip to content

Commit

Permalink
[fix][broker] Fix authenticate order in AuthenticationProviderList (a…
Browse files Browse the repository at this point in the history
  • Loading branch information
coderzc authored Aug 5, 2024
1 parent 76f16e8 commit 0e66547
Showing 1 changed file with 8 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,8 @@ public CompletableFuture<AuthData> authenticateAsync(AuthData authData) {
if (log.isDebugEnabled()) {
log.debug("Authentication failed for auth provider " + authState.getClass() + ": ", ex);
}
authenticateRemainingAuthStates(authChallengeFuture, authData, ex, states.size() - 1);
authenticateRemainingAuthStates(authChallengeFuture, authData, ex,
states.isEmpty() ? -1 : 0);
}
});
return authChallengeFuture;
Expand All @@ -130,7 +131,7 @@ private void authenticateRemainingAuthStates(CompletableFuture<AuthData> authCha
AuthData clientAuthData,
Throwable previousException,
int index) {
if (index < 0) {
if (index < 0 || index >= states.size()) {
if (previousException == null) {
previousException = new AuthenticationException("Authentication required");
}
Expand All @@ -142,7 +143,7 @@ private void authenticateRemainingAuthStates(CompletableFuture<AuthData> authCha
AuthenticationState state = states.get(index);
if (state == authState) {
// Skip the current auth state
authenticateRemainingAuthStates(authChallengeFuture, clientAuthData, null, index - 1);
authenticateRemainingAuthStates(authChallengeFuture, clientAuthData, null, index + 1);
} else {
state.authenticateAsync(clientAuthData)
.whenComplete((authChallenge, ex) -> {
Expand All @@ -155,7 +156,7 @@ private void authenticateRemainingAuthStates(CompletableFuture<AuthData> authCha
log.debug("Authentication failed for auth provider "
+ authState.getClass() + ": ", ex);
}
authenticateRemainingAuthStates(authChallengeFuture, clientAuthData, ex, index - 1);
authenticateRemainingAuthStates(authChallengeFuture, clientAuthData, ex, index + 1);
}
});
}
Expand Down Expand Up @@ -228,15 +229,15 @@ public String getAuthMethodName() {
@Override
public CompletableFuture<String> authenticateAsync(AuthenticationDataSource authData) {
CompletableFuture<String> roleFuture = new CompletableFuture<>();
authenticateRemainingAuthProviders(roleFuture, authData, null, providers.size() - 1);
authenticateRemainingAuthProviders(roleFuture, authData, null, providers.isEmpty() ? -1 : 0);
return roleFuture;
}

private void authenticateRemainingAuthProviders(CompletableFuture<String> roleFuture,
AuthenticationDataSource authData,
Throwable previousException,
int index) {
if (index < 0) {
if (index < 0 || index >= providers.size()) {
if (previousException == null) {
previousException = new AuthenticationException("Authentication required");
}
Expand All @@ -254,7 +255,7 @@ private void authenticateRemainingAuthProviders(CompletableFuture<String> roleFu
if (log.isDebugEnabled()) {
log.debug("Authentication failed for auth provider " + provider.getClass() + ": ", ex);
}
authenticateRemainingAuthProviders(roleFuture, authData, ex, index - 1);
authenticateRemainingAuthProviders(roleFuture, authData, ex, index + 1);
}
});
}
Expand Down

0 comments on commit 0e66547

Please sign in to comment.