Skip to content

Commit

Permalink
Set minimum oauth linked account (#137)
Browse files Browse the repository at this point in the history
  • Loading branch information
jinlee1703 authored Oct 24, 2023
2 parents 00d3b92 + 9a44cfb commit 405b00f
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 20 deletions.
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

0 comments on commit 405b00f

Please sign in to comment.