Skip to content

Commit 4916b81

Browse files
committed
chore: Override OAuth2AuthenticationException to differentiate the errors thrown by Appsmith (#35160)
## Description > Extend OAuth2AuthenticationException so that we can differentiate between AppsmithException and exceptions thrown by Spring Library. > There is not going to be any change to the Authentication flows here, as the we are just inheriting the OAuth2AuthenticationException. Fixes #`Issue Number` _or_ Fixes `Issue URL` > [!WARNING] > _If no issue exists, please create an issue first, and check with the maintainers if the issue is valid._ ## Automation /ok-to-test tags="@tag.All" ### 🔍 Cypress test results <!-- This is an auto-generated comment: Cypress test results --> > [!TIP] > 🟢 🟢 🟢 All cypress tests have passed! 🎉 🎉 🎉 > Workflow run: <https://github.com/appsmithorg/appsmith/actions/runs/10092949232> > Commit: bc2f204 > <a href="https://internal.appsmith.com/app/cypress-dashboard/rundetails-65890b3c81d7400d08fa9ee5?branch=master&workflowId=10092949232&attempt=2" target="_blank">Cypress dashboard</a>. > Tags: `@tag.All` > Spec: > <hr>Thu, 25 Jul 2024 13:13:00 UTC <!-- end of auto-generated comment: Cypress test results --> ## Communication Should the DevRel and Marketing teams inform users about this change? - [ ] Yes - [ ] No <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit - **New Features** - Introduced a new custom exception for improved handling of OAuth 2.0 authentication errors, enhancing the clarity and robustness of the authentication process. - **Bug Fixes** - Enhanced error categorization in the authentication process by refining the error handling logic, allowing for better management of exceptions related to OAuth 2.0. <!-- end of auto-generated comment: release notes by coderabbit.ai --> --------- Co-authored-by: Nilesh Sarupriya <[email protected]>
1 parent f4073fc commit 4916b81

File tree

3 files changed

+43
-2
lines changed

3 files changed

+43
-2
lines changed

app/server/appsmith-server/src/main/java/com/appsmith/server/authentication/handlers/ce/CustomOAuth2UserServiceCEImpl.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,16 @@
33
import com.appsmith.server.domains.LoginSource;
44
import com.appsmith.server.domains.User;
55
import com.appsmith.server.domains.UserState;
6+
import com.appsmith.server.exceptions.AppsmithException;
7+
import com.appsmith.server.exceptions.AppsmithOAuth2AuthenticationException;
68
import com.appsmith.server.repositories.UserRepository;
79
import com.appsmith.server.services.UserService;
810
import lombok.extern.slf4j.Slf4j;
911
import org.springframework.beans.factory.annotation.Autowired;
1012
import org.springframework.security.oauth2.client.userinfo.DefaultReactiveOAuth2UserService;
1113
import org.springframework.security.oauth2.client.userinfo.OAuth2UserRequest;
1214
import org.springframework.security.oauth2.core.OAuth2AuthenticationException;
15+
import org.springframework.security.oauth2.core.OAuth2Error;
1316
import org.springframework.security.oauth2.core.user.OAuth2User;
1417
import reactor.core.publisher.Mono;
1518

@@ -65,6 +68,12 @@ private Mono<User> checkAndCreateUser(OAuth2User oAuth2User, OAuth2UserRequest u
6568
return repository.save(user);
6669
}
6770
return Mono.just(user);
68-
});
71+
})
72+
.onErrorMap(
73+
AppsmithException.class,
74+
// Throwing an AppsmithOAuth2AuthenticationException in case of an AppsmithException
75+
// This is to differentiate between Appsmith exceptions and OAuth2 exceptions
76+
error -> new AppsmithOAuth2AuthenticationException(
77+
new OAuth2Error(error.getAppErrorCode().toString(), error.getMessage(), "")));
6978
}
7079
}

app/server/appsmith-server/src/main/java/com/appsmith/server/authentication/handlers/ce/CustomOidcUserServiceCEImpl.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import com.appsmith.server.domains.User;
55
import com.appsmith.server.domains.UserState;
66
import com.appsmith.server.exceptions.AppsmithException;
7+
import com.appsmith.server.exceptions.AppsmithOAuth2AuthenticationException;
78
import com.appsmith.server.repositories.UserRepository;
89
import com.appsmith.server.services.UserService;
910
import lombok.extern.slf4j.Slf4j;
@@ -76,7 +77,9 @@ public Mono<User> checkAndCreateUser(OidcUser oidcUser, OidcUserRequest userRequ
7677
})
7778
.onErrorMap(
7879
AppsmithException.class,
79-
error -> new OAuth2AuthenticationException(
80+
// Throwing an AppsmithOAuth2AuthenticationException in case of an AppsmithException
81+
// This is to differentiate between Appsmith exceptions and OAuth2 exceptions
82+
error -> new AppsmithOAuth2AuthenticationException(
8083
new OAuth2Error(error.getAppErrorCode().toString(), error.getMessage(), "")));
8184
}
8285
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.appsmith.server.exceptions;
2+
3+
import lombok.Getter;
4+
import org.springframework.security.oauth2.core.OAuth2AuthenticationException;
5+
import org.springframework.security.oauth2.core.OAuth2Error;
6+
7+
@Getter
8+
public class AppsmithOAuth2AuthenticationException extends OAuth2AuthenticationException {
9+
10+
private final OAuth2Error error;
11+
/**
12+
* Constructs an {@code AppsmithOAuth2AuthenticationException} using the provided parameters.
13+
* @param error the {@link OAuth2Error OAuth 2.0 Error}
14+
*/
15+
public AppsmithOAuth2AuthenticationException(OAuth2Error error) {
16+
this(error, error.getDescription(), null);
17+
}
18+
19+
/**
20+
* Constructs an {@code AppsmithOAuth2AuthenticationException} using the provided parameters.
21+
* @param error the {@link OAuth2Error OAuth 2.0 Error}
22+
* @param message the detail message
23+
* @param cause the root cause
24+
*/
25+
public AppsmithOAuth2AuthenticationException(OAuth2Error error, String message, Throwable cause) {
26+
super(error, message, cause);
27+
this.error = error;
28+
}
29+
}

0 commit comments

Comments
 (0)