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

Set minimum oauth linked account #137

Merged
merged 3 commits into from
Oct 24, 2023
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 @@ -94,16 +94,7 @@ public ResponseEntity<Message> readMember(HttpServletRequest request, @PathVaria
})
public ResponseEntity<Message> 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);
Expand All @@ -119,16 +110,7 @@ public ResponseEntity<Message> createOAuthInfo(HttpServletRequest request, @Path
@ApiResponse(responseCode = "400", description = "OAuth 연동 계정 삭제 실패")
})
public ResponseEntity<Message> 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);
Expand All @@ -151,4 +133,19 @@ public ResponseEntity<Message> 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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,6 @@ public interface OAuthRepository extends JpaRepository<OAuth, Long> {
Optional<OAuth> findByPlatformAndPlatformIdAndDeletedAtIsNull(OAuthPlatform platform, String platformId);

List<OAuth> findAllByUserAndDeletedAtIsNull(User user);

int countByUserAndDeletedAtIsNull(User user);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -164,6 +165,9 @@ public OAuth createOAuthInfo(User user, OAuthPlatform oAuthPlatform, OAuthTokenD
}

public OAuth deleteOAuthInfo(User user, OAuthPlatform oAuthPlatform) {
if (UUID_REGEX.matcher(user.getUsername()).matches() && oAuthService.count(user) <= 1) {
throw new BusinessException("최소 하나 이상의 OAuth 연동 계정이 존재해야 합니다.", StatusEnum.BAD_REQUEST);
}
return oAuthService.delete(user, oAuthPlatform);
}

Expand Down