MOSU refactor: enhance OAuth2 login failure handling with redirect#305
MOSU refactor: enhance OAuth2 login failure handling with redirect#305wlgns12370 merged 1 commit intoprodfrom
Conversation
|
Important Review skippedAuto reviews are disabled on base/target branches other than the default branch. Please check the settings in the CodeRabbit UI or the You can disable this status message by setting the ✨ Finishing Touches🧪 Generate unit tests
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
SupportNeed help? Create a ticket on our support page for assistance with any issues or questions. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
There was a problem hiding this comment.
Summary of Changes
Hello @wlgns12370, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!
I've refactored the OAuth2 login failure handling to improve API integration. Instead of returning a JSON error response, the system will now redirect the user to a predefined URL, passing serialized failure data as a query parameter. This change streamlines the client-side handling of authentication failures by providing a consistent redirect mechanism.
Highlights
- OAuth2 Failure Redirection: Modified OAuth2LoginFailureHandler to perform a client-side redirect upon authentication failure, moving away from direct JSON error responses.
- Dynamic Redirect Data: The LoginResponse object is now serialized to JSON, URL-encoded, and included as a data query parameter in the redirect URL, allowing the client to parse failure details.
- LoginResponse Utility: Added a new static from() method to LoginResponse to facilitate the creation of an empty response object specifically for OAuth2 login failure scenarios.
- Simplified Error Reporting: Removed the previous verbose error response generation logic, simplifying the failure handling flow within the backend.
Using Gemini Code Assist
The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.
Invoking Gemini
You can request assistance from Gemini at any point in your pull request via creating an issue comment (i.e. comment on the pull request page) using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands.
| Feature | Command | Description |
|---|---|---|
| Code Review | /gemini review |
Performs a code review for the current pull request in its current state. |
| Pull Request Summary | /gemini summary |
Provides a summary of the current pull request in its current state. |
| Comment | @gemini-code-assist | Responds in comments when explicitly tagged, both in issue comments and review comments. |
| Help | /gemini help |
Displays a list of available commands. |
Customization
To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.
Limitations & Feedback
Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments or fill out our survey to provide feedback.
You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.
Footnotes
-
Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution. ↩
There was a problem hiding this comment.
Code Review
This pull request refactors the OAuth2 login failure handling to redirect to the frontend instead of returning a 401 error. The changes are in the right direction, but I've identified a couple of important issues in the new OAuth2LoginFailureHandler. Specifically, it's missing crucial error logging and uses a hardcoded redirect URL, which harms maintainability and flexibility. I've provided a detailed comment with a code suggestion to address these high-severity issues. Additionally, I've suggested a minor improvement to rename a factory method for better code clarity. Overall, the changes are good but require these fixes to be robust and production-ready.
| LoginResponse loginResponse = LoginResponse.from(); | ||
| String jsonResponse = UriUtils.encode(objectMapper.writeValueAsString(loginResponse), | ||
| StandardCharsets.UTF_8); | ||
|
|
||
| if (exception instanceof OAuth2AuthenticationException oAuth2Ex) { | ||
| OAuth2Error error = oAuth2Ex.getError(); | ||
| errorCode = error.getErrorCode(); | ||
| errorMessage = error.getDescription() != null ? error.getDescription() : errorCode; | ||
| final String redirectWithAccessToken = UriComponentsBuilder.fromUriString( | ||
| "https://www.mosuedu.com/auth/kakao/redirect") | ||
| .queryParam("data", jsonResponse) | ||
| .build() | ||
| .toUriString(); | ||
|
|
||
| log.info("OAuth2 Error: {}, Description: {}", error.getErrorCode(), | ||
| error.getDescription()); | ||
| } | ||
|
|
||
| ErrorResponse errorResponse = ErrorResponse.builder() | ||
| .status(HttpStatus.UNAUTHORIZED.value()) | ||
| .message("인증에 실패했습니다") | ||
| .errors(errorMessage) | ||
| .code(errorCode) | ||
| .build(); | ||
|
|
||
| response.setStatus(HttpServletResponse.SC_UNAUTHORIZED); | ||
| response.setContentType("application/json;charset=UTF-8"); | ||
| response.setContentType(MediaType.APPLICATION_JSON_VALUE); | ||
|
|
||
| objectMapper.writeValue(response.getWriter(), errorResponse); | ||
| response.sendRedirect(redirectWithAccessToken); |
There was a problem hiding this comment.
This method can be improved in several ways:
- Missing Failure Logging: The
AuthenticationExceptionis ignored. It's crucial to log authentication failures for security monitoring and debugging. - Hardcoded Redirect URL: The redirect URL is hardcoded, which is inflexible for different environments or clients. It should be derived from the
stateparameter in the request, similar toOAuth2LoginSuccessHandler.
I've included a suggested implementation that addresses these points. This suggestion also anticipates renaming LoginResponse.from() to LoginResponse.createForFailure() for better clarity (see my other comment on LoginResponse.java).
To apply this suggestion, you will also need to:
- Copy the
parseStateprivate method fromOAuth2LoginSuccessHandlerinto this class. - Add the necessary imports for
Map,HashMap, andREDIRECT_PARAM_KEY.
log.warn("OAuth2 Authentication failed: {}", exception.getMessage());
LoginResponse loginResponse = LoginResponse.createForFailure();
String jsonResponse = UriUtils.encode(objectMapper.writeValueAsString(loginResponse),
StandardCharsets.UTF_8);
final String state = request.getParameter("state");
final Map<String, String> stateParams = parseState(state);
final String redirectUri = stateParams.getOrDefault(REDIRECT_PARAM_KEY, "/");
final String redirectUrl = UriComponentsBuilder.fromUriString(redirectUri)
.queryParam("data", jsonResponse)
.build()
.toUriString();
response.sendRedirect(redirectUrl);| public static LoginResponse from() { | ||
| return new LoginResponse(null, null); | ||
| } |
There was a problem hiding this comment.
The method name from() is ambiguous for creating a failure response, especially since other from methods handle success cases. To improve code clarity and make the method's purpose self-documenting, consider renaming it to something more descriptive, like createForFailure().
| public static LoginResponse from() { | |
| return new LoginResponse(null, null); | |
| } | |
| public static LoginResponse createForFailure() { | |
| return new LoginResponse(null, null); | |
| } |
✨ 구현한 기능