Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: enhance OAuth2 error handling by adding user-friendly exception messages #58

Merged
merged 1 commit into from
Aug 2, 2024
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
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,5 @@ test {

halo {
version = '2.17'
debug = true
}
35 changes: 35 additions & 0 deletions src/main/java/run/halo/oauth/Oauth2LoginConfiguration.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package run.halo.oauth;

import com.google.common.base.Throwables;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.DelegatingReactiveAuthenticationManager;
import org.springframework.security.authentication.ReactiveAuthenticationManager;
Expand Down Expand Up @@ -40,6 +43,7 @@
import org.springframework.security.web.server.util.matcher.ServerWebExchangeMatcher;
import org.springframework.stereotype.Component;
import org.springframework.util.ClassUtils;
import org.springframework.util.MultiValueMap;
import reactor.core.publisher.Mono;
import run.halo.app.extension.ReactiveExtensionClient;
import run.halo.app.security.LoginHandlerEnhancer;
Expand All @@ -50,6 +54,7 @@
* @author guqing
* @since 1.0.0
*/
@Slf4j
@Getter
@Component
public final class Oauth2LoginConfiguration {
Expand Down Expand Up @@ -108,12 +113,42 @@ ServerAuthenticationFailureHandler getAuthenticationFailureHandler() {
@Override
public Mono<Void> onAuthenticationFailure(WebFilterExchange webFilterExchange,
AuthenticationException exception) {
var queryParams = webFilterExchange.getExchange().getRequest().getQueryParams();
var response = new OAuth2ErrorResponse(queryParams);
log.error("An error occurred while attempting to oauth2 authenticate: \n{}",
response, Throwables.getRootCause(exception));
return loginHandlerEnhancer.onLoginFailure(webFilterExchange.getExchange(), exception)
.then(super.onAuthenticationFailure(webFilterExchange, exception));
}
};
}

@RequiredArgsConstructor
static class OAuth2ErrorResponse {
private final MultiValueMap<String, String> queryParams;

public String error() {
return queryParams.getFirst("error");
}

public String errorDescription() {
return queryParams.getFirst("error_description");
}

public String errorUri() {
return queryParams.getFirst("error_uri");
}

@Override
public String toString() {
return """
error: %s
error_description: %s
error_uri: %s
""".formatted(error(), errorDescription(), errorUri());
}
}

GrantedAuthoritiesMapper getAuthoritiesMapper() {
return new SimpleAuthorityMapper();
}
Expand Down