From 7ef0d1b61a4675cdf42b489a5b93bb2781eb8950 Mon Sep 17 00:00:00 2001 From: Jinwoo Lee Date: Tue, 24 Oct 2023 12:37:38 +0900 Subject: [PATCH 1/3] refactor: implement string-to-platform method --- .../auth/controller/UserController.java | 37 +++++++++---------- 1 file changed, 17 insertions(+), 20 deletions(-) diff --git a/src/main/java/org/swmaestro/repl/gifthub/auth/controller/UserController.java b/src/main/java/org/swmaestro/repl/gifthub/auth/controller/UserController.java index 49efce3e..83309287 100644 --- a/src/main/java/org/swmaestro/repl/gifthub/auth/controller/UserController.java +++ b/src/main/java/org/swmaestro/repl/gifthub/auth/controller/UserController.java @@ -94,16 +94,7 @@ public ResponseEntity readMember(HttpServletRequest request, @PathVaria }) public ResponseEntity createOAuthInfo(HttpServletRequest request, @PathVariable String platform, @RequestBody OAuthTokenDto oAuthTokenDto) { - OAuthPlatform oAuthPlatform; - - switch (platform) { - case "naver" -> oAuthPlatform = OAuthPlatform.NAVER; - case "kakao" -> oAuthPlatform = OAuthPlatform.KAKAO; - case "google" -> oAuthPlatform = OAuthPlatform.GOOGLE; - case "apple" -> oAuthPlatform = OAuthPlatform.APPLE; - default -> throw new BusinessException("올바르지 않은 플랫폼에 대한 요청입니다.", StatusEnum.BAD_REQUEST); - } - + OAuthPlatform oAuthPlatform = stringToPlatform(platform); String username = jwtProvider.getUsername(jwtProvider.resolveToken(request).substring(7)); User user = userService.read(username); userService.createOAuthInfo(user, oAuthPlatform, oAuthTokenDto); @@ -119,16 +110,7 @@ public ResponseEntity createOAuthInfo(HttpServletRequest request, @Path @ApiResponse(responseCode = "400", description = "OAuth 연동 계정 삭제 실패") }) public ResponseEntity deleteOAuthInfo(HttpServletRequest request, @PathVariable String platform) { - OAuthPlatform oAuthPlatform; - - switch (platform) { - case "naver" -> oAuthPlatform = OAuthPlatform.NAVER; - case "kakao" -> oAuthPlatform = OAuthPlatform.KAKAO; - case "google" -> oAuthPlatform = OAuthPlatform.GOOGLE; - case "apple" -> oAuthPlatform = OAuthPlatform.APPLE; - default -> throw new BusinessException("올바르지 않은 플랫폼에 대한 요청입니다.", StatusEnum.BAD_REQUEST); - } - + OAuthPlatform oAuthPlatform = stringToPlatform(platform); String username = jwtProvider.getUsername(jwtProvider.resolveToken(request).substring(7)); User user = userService.read(username); userService.deleteOAuthInfo(user, oAuthPlatform); @@ -151,4 +133,19 @@ public ResponseEntity readMyInfo(HttpServletRequest request) { .data(userService.readInfo(username)) .build()); } + + private OAuthPlatform stringToPlatform(String platform) { + switch (platform) { + case "naver": + return OAuthPlatform.NAVER; + case "kakao": + return OAuthPlatform.KAKAO; + case "google": + return OAuthPlatform.GOOGLE; + case "apple": + return OAuthPlatform.APPLE; + default: + throw new BusinessException("올바르지 않은 플랫폼에 대한 요청입니다.", StatusEnum.BAD_REQUEST); + } + } } From 19d6519d00f3706fb9c89a78e974547f9f086458 Mon Sep 17 00:00:00 2001 From: Jinwoo Lee Date: Tue, 24 Oct 2023 12:46:08 +0900 Subject: [PATCH 2/3] fix: set minimum oauth linked account --- .../repl/gifthub/auth/repository/OAuthRepository.java | 2 ++ .../org/swmaestro/repl/gifthub/auth/service/OAuthService.java | 4 ++++ .../org/swmaestro/repl/gifthub/auth/service/UserService.java | 3 +++ 3 files changed, 9 insertions(+) diff --git a/src/main/java/org/swmaestro/repl/gifthub/auth/repository/OAuthRepository.java b/src/main/java/org/swmaestro/repl/gifthub/auth/repository/OAuthRepository.java index 72af923c..dfada4d3 100644 --- a/src/main/java/org/swmaestro/repl/gifthub/auth/repository/OAuthRepository.java +++ b/src/main/java/org/swmaestro/repl/gifthub/auth/repository/OAuthRepository.java @@ -20,4 +20,6 @@ public interface OAuthRepository extends JpaRepository { Optional findByPlatformAndPlatformIdAndDeletedAtIsNull(OAuthPlatform platform, String platformId); List findAllByUserAndDeletedAtIsNull(User user); + + int countByUserAndDeletedAtIsNull(User user); } diff --git a/src/main/java/org/swmaestro/repl/gifthub/auth/service/OAuthService.java b/src/main/java/org/swmaestro/repl/gifthub/auth/service/OAuthService.java index 76b8087d..c478cbaa 100644 --- a/src/main/java/org/swmaestro/repl/gifthub/auth/service/OAuthService.java +++ b/src/main/java/org/swmaestro/repl/gifthub/auth/service/OAuthService.java @@ -40,6 +40,10 @@ public OAuth delete(User user, OAuthPlatform platform) { return platformToService(platform).delete(user); } + public int count(User user) { + return oAuthRepository.countByUserAndDeletedAtIsNull(user); + } + public OAuth read(OAuthUserInfoDto oAuthUserInfoDto, OAuthPlatform platform) { return platformToService(platform).read(oAuthUserInfoDto); } diff --git a/src/main/java/org/swmaestro/repl/gifthub/auth/service/UserService.java b/src/main/java/org/swmaestro/repl/gifthub/auth/service/UserService.java index 36c7a3db..dc7c8d9e 100644 --- a/src/main/java/org/swmaestro/repl/gifthub/auth/service/UserService.java +++ b/src/main/java/org/swmaestro/repl/gifthub/auth/service/UserService.java @@ -164,6 +164,9 @@ public OAuth createOAuthInfo(User user, OAuthPlatform oAuthPlatform, OAuthTokenD } public OAuth deleteOAuthInfo(User user, OAuthPlatform oAuthPlatform) { + if (oAuthService.count(user) <= 1) { + throw new BusinessException("최소 하나 이상의 OAuth 연동 계정이 존재해야 합니다.", StatusEnum.BAD_REQUEST); + } return oAuthService.delete(user, oAuthPlatform); } From 9a44cfb24727ff186e67135ff1d876006b47b373 Mon Sep 17 00:00:00 2001 From: Jinwoo Lee Date: Tue, 24 Oct 2023 12:49:50 +0900 Subject: [PATCH 3/3] fix: add uuid check logic --- .../org/swmaestro/repl/gifthub/auth/service/UserService.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/swmaestro/repl/gifthub/auth/service/UserService.java b/src/main/java/org/swmaestro/repl/gifthub/auth/service/UserService.java index dc7c8d9e..069595ab 100644 --- a/src/main/java/org/swmaestro/repl/gifthub/auth/service/UserService.java +++ b/src/main/java/org/swmaestro/repl/gifthub/auth/service/UserService.java @@ -36,6 +36,7 @@ public class UserService implements UserDetailsService { private final PasswordEncoder passwordEncoder; private final OAuthService oAuthService; private final DeviceTokenRepository deviceTokenRepository; + private final Pattern UUID_REGEX = Pattern.compile("^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$"); public User passwordEncryption(User user) { return User.builder() @@ -164,7 +165,7 @@ public OAuth createOAuthInfo(User user, OAuthPlatform oAuthPlatform, OAuthTokenD } public OAuth deleteOAuthInfo(User user, OAuthPlatform oAuthPlatform) { - if (oAuthService.count(user) <= 1) { + if (UUID_REGEX.matcher(user.getUsername()).matches() && oAuthService.count(user) <= 1) { throw new BusinessException("최소 하나 이상의 OAuth 연동 계정이 존재해야 합니다.", StatusEnum.BAD_REQUEST); } return oAuthService.delete(user, oAuthPlatform);