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

chore: Override OAuth2AuthenticationException to differentiate the errors thrown by Appsmith #35160

Merged
merged 2 commits into from
Jul 25, 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
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@
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;
import org.springframework.beans.factory.annotation.Autowired;
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;

Expand Down Expand Up @@ -65,6 +68,12 @@ private Mono<User> 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(), "")));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -76,7 +77,9 @@ public Mono<User> 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(), "")));
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
Loading