From 40071eb56ad5a7571d6b79d5fd2c208a3fab0315 Mon Sep 17 00:00:00 2001 From: Nilesh Sarupriya <20905988+nsarupr@users.noreply.github.com> Date: Wed, 24 Jul 2024 18:55:30 +0530 Subject: [PATCH 1/2] Override OAuth2AuthenticationException to differentiate the errors thrown by Appsmith --- .../ce/CustomOidcUserServiceCEImpl.java | 5 +++- ...AppsmithOAuth2AuthenticationException.java | 29 +++++++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) create mode 100644 app/server/appsmith-server/src/main/java/com/appsmith/server/exceptions/AppsmithOAuth2AuthenticationException.java diff --git a/app/server/appsmith-server/src/main/java/com/appsmith/server/authentication/handlers/ce/CustomOidcUserServiceCEImpl.java b/app/server/appsmith-server/src/main/java/com/appsmith/server/authentication/handlers/ce/CustomOidcUserServiceCEImpl.java index 5d3326036d76..cbfc782c5e2f 100644 --- a/app/server/appsmith-server/src/main/java/com/appsmith/server/authentication/handlers/ce/CustomOidcUserServiceCEImpl.java +++ b/app/server/appsmith-server/src/main/java/com/appsmith/server/authentication/handlers/ce/CustomOidcUserServiceCEImpl.java @@ -4,6 +4,7 @@ import com.appsmith.server.domains.User; import com.appsmith.server.domains.UserState; import com.appsmith.server.exceptions.AppsmithException; +import com.appsmith.server.exceptions.AppsmithOAuth2AuthenticationException; import com.appsmith.server.repositories.UserRepository; import com.appsmith.server.services.UserService; import lombok.extern.slf4j.Slf4j; @@ -76,7 +77,9 @@ public Mono checkAndCreateUser(OidcUser oidcUser, OidcUserRequest userRequ }) .onErrorMap( AppsmithException.class, - error -> new OAuth2AuthenticationException( + // Throwing an AppsmithOAuth2AuthenticationException in case of an AppsmithException + // This is to differentiate between Appsmith exceptions and OAuth2 exceptions + error -> new AppsmithOAuth2AuthenticationException( new OAuth2Error(error.getAppErrorCode().toString(), error.getMessage(), ""))); } } diff --git a/app/server/appsmith-server/src/main/java/com/appsmith/server/exceptions/AppsmithOAuth2AuthenticationException.java b/app/server/appsmith-server/src/main/java/com/appsmith/server/exceptions/AppsmithOAuth2AuthenticationException.java new file mode 100644 index 000000000000..9641864a0282 --- /dev/null +++ b/app/server/appsmith-server/src/main/java/com/appsmith/server/exceptions/AppsmithOAuth2AuthenticationException.java @@ -0,0 +1,29 @@ +package com.appsmith.server.exceptions; + +import lombok.Getter; +import org.springframework.security.oauth2.core.OAuth2AuthenticationException; +import org.springframework.security.oauth2.core.OAuth2Error; + +@Getter +public class AppsmithOAuth2AuthenticationException extends OAuth2AuthenticationException { + + private final OAuth2Error error; + /** + * Constructs an {@code AppsmithOAuth2AuthenticationException} using the provided parameters. + * @param error the {@link OAuth2Error OAuth 2.0 Error} + */ + public AppsmithOAuth2AuthenticationException(OAuth2Error error) { + this(error, error.getDescription(), null); + } + + /** + * Constructs an {@code AppsmithOAuth2AuthenticationException} using the provided parameters. + * @param error the {@link OAuth2Error OAuth 2.0 Error} + * @param message the detail message + * @param cause the root cause + */ + public AppsmithOAuth2AuthenticationException(OAuth2Error error, String message, Throwable cause) { + super(error, message, cause); + this.error = error; + } +} From bc2f204a6516fd527775daafb4829254d19251eb Mon Sep 17 00:00:00 2001 From: Nilesh Sarupriya <20905988+nsarupr@users.noreply.github.com> Date: Thu, 25 Jul 2024 13:21:40 +0530 Subject: [PATCH 2/2] fix: throw Authentication exception when erroring out on checkAndCreateUser --- .../handlers/ce/CustomOAuth2UserServiceCEImpl.java | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/app/server/appsmith-server/src/main/java/com/appsmith/server/authentication/handlers/ce/CustomOAuth2UserServiceCEImpl.java b/app/server/appsmith-server/src/main/java/com/appsmith/server/authentication/handlers/ce/CustomOAuth2UserServiceCEImpl.java index 469726318226..33cced2fd3f6 100644 --- a/app/server/appsmith-server/src/main/java/com/appsmith/server/authentication/handlers/ce/CustomOAuth2UserServiceCEImpl.java +++ b/app/server/appsmith-server/src/main/java/com/appsmith/server/authentication/handlers/ce/CustomOAuth2UserServiceCEImpl.java @@ -3,6 +3,8 @@ import com.appsmith.server.domains.LoginSource; import com.appsmith.server.domains.User; import com.appsmith.server.domains.UserState; +import com.appsmith.server.exceptions.AppsmithException; +import com.appsmith.server.exceptions.AppsmithOAuth2AuthenticationException; import com.appsmith.server.repositories.UserRepository; import com.appsmith.server.services.UserService; import lombok.extern.slf4j.Slf4j; @@ -10,6 +12,7 @@ import org.springframework.security.oauth2.client.userinfo.DefaultReactiveOAuth2UserService; import org.springframework.security.oauth2.client.userinfo.OAuth2UserRequest; import org.springframework.security.oauth2.core.OAuth2AuthenticationException; +import org.springframework.security.oauth2.core.OAuth2Error; import org.springframework.security.oauth2.core.user.OAuth2User; import reactor.core.publisher.Mono; @@ -65,6 +68,12 @@ private Mono checkAndCreateUser(OAuth2User oAuth2User, OAuth2UserRequest u return repository.save(user); } return Mono.just(user); - }); + }) + .onErrorMap( + AppsmithException.class, + // Throwing an AppsmithOAuth2AuthenticationException in case of an AppsmithException + // This is to differentiate between Appsmith exceptions and OAuth2 exceptions + error -> new AppsmithOAuth2AuthenticationException( + new OAuth2Error(error.getAppErrorCode().toString(), error.getMessage(), ""))); } }