Skip to content

Commit d93e009

Browse files
committed
MOSU-341 refactor: OAuth 관련 에러 공통화
1 parent fcaecf1 commit d93e009

4 files changed

Lines changed: 43 additions & 7 deletions

File tree

src/main/java/life/mosu/mosuserver/global/handler/OAuth2LoginFailureHandler.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import jakarta.servlet.http.HttpServletResponse;
77
import java.io.IOException;
88
import java.nio.charset.StandardCharsets;
9-
import life.mosu.mosuserver.presentation.auth.dto.request.LoginResponse;
109
import lombok.RequiredArgsConstructor;
1110
import lombok.extern.slf4j.Slf4j;
1211
import org.springframework.security.core.AuthenticationException;
@@ -27,8 +26,11 @@ public class OAuth2LoginFailureHandler implements
2726
public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response,
2827
AuthenticationException exception) throws IOException, ServletException {
2928

30-
LoginResponse loginResponse = LoginResponse.from();
31-
String jsonResponse = UriUtils.encode(objectMapper.writeValueAsString(loginResponse),
29+
OAuthErrorType errorType = OAuthErrorType.from(exception.getMessage());
30+
OAuthFailureResponse oAuthFailureResponse = OAuthFailureResponse.from(
31+
errorType.getMessage());
32+
33+
String jsonResponse = UriUtils.encode(objectMapper.writeValueAsString(oAuthFailureResponse),
3234
StandardCharsets.UTF_8);
3335

3436
final String redirectWithAccessToken = UriComponentsBuilder.fromUriString(
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package life.mosu.mosuserver.global.handler;
2+
3+
import lombok.Getter;
4+
5+
@Getter
6+
public enum OAuthErrorType {
7+
CANCELED("CANCELED"),
8+
DUPLICATE("DUPLICATE"),
9+
UNKNOWN("UNKNOWN");
10+
11+
private final String message;
12+
13+
OAuthErrorType(String message) {
14+
this.message = message;
15+
}
16+
17+
public static OAuthErrorType from(String text) {
18+
if (text == null) {
19+
return UNKNOWN;
20+
}
21+
return switch (text) {
22+
case "DUPLICATE" -> DUPLICATE;
23+
case "[access_denied] User denied access" -> CANCELED;
24+
default -> UNKNOWN;
25+
};
26+
}
27+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package life.mosu.mosuserver.global.handler;
2+
3+
public record OAuthFailureResponse(
4+
Boolean isProfileRegistered,
5+
String errorCode
6+
) {
7+
8+
public static OAuthFailureResponse from(String errorCode) {
9+
return new OAuthFailureResponse(null, errorCode);
10+
}
11+
}

src/main/java/life/mosu/mosuserver/presentation/auth/dto/request/LoginResponse.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,4 @@ public static LoginResponse from(Boolean isProfileRegistered, final UserJpaEntit
2121
}
2222
return new LoginResponse(false, LoginUserResponse.from(user));
2323
}
24-
25-
public static LoginResponse from() {
26-
return new LoginResponse(null, null);
27-
}
2824
}

0 commit comments

Comments
 (0)