Skip to content

Commit

Permalink
[refac] apply code review
Browse files Browse the repository at this point in the history
  • Loading branch information
kgy1008 committed Jul 8, 2024
1 parent cbcb625 commit 674675c
Show file tree
Hide file tree
Showing 14 changed files with 128 additions and 111 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.openfeign.EnableFeignClients;

@SpringBootApplication
@EnableFeignClients
public class HankkiserverApplication {

public static void main(String[] args) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

@RestController
@RequiredArgsConstructor
@RequestMapping("api/v1")
@RequestMapping("/api/v1")
public class AuthController {

private final AuthService authService;
Expand Down Expand Up @@ -46,8 +46,8 @@ public ResponseEntity<BaseResponse<?>> withdraw(

@PostMapping("/auth/reissue")
public ResponseEntity<BaseResponse<?>> reissue(
@RequestHeader(HttpHeaders.AUTHORIZATION) final String refreshtoken) {
final UserReissueResponse response = authService.reissue(refreshtoken);
@RequestHeader(HttpHeaders.AUTHORIZATION) final String refreshToken) {
final UserReissueResponse response = authService.reissue(refreshToken);
return ApiResponse.success(SuccessCode.OK, response);
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package org.hankki.hankkiserver.api.auth.controller.request;

import jakarta.annotation.Nullable;
import org.hankki.hankkiserver.domain.user.model.Platform;

public record UserLoginRequest(
@Nullable
String name,
String platform
Platform platform
) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,29 +13,25 @@
import org.hankki.hankkiserver.common.exception.UnauthorizedException;
import org.hankki.hankkiserver.external.openfeign.apple.AppleOAuthProvider;
import org.hankki.hankkiserver.external.openfeign.dto.SocialInfoDto;
import org.hankki.hankkiserver.domain.user.repository.UserInfoRepository;
import org.hankki.hankkiserver.domain.user.repository.UserRepository;
import org.hankki.hankkiserver.api.auth.controller.request.UserLoginRequest;
import org.hankki.hankkiserver.api.auth.service.response.UserLoginResponse;
import org.hankki.hankkiserver.api.auth.service.response.UserReissueResponse;
import org.hankki.hankkiserver.external.openfeign.kakao.KakaoOAuthProvider;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import static org.hankki.hankkiserver.domain.user.model.MemberStatus.ACTIVE;
import static org.hankki.hankkiserver.domain.user.model.MemberStatus.INACTIVE;
import static org.hankki.hankkiserver.domain.user.model.Platform.*;
import static org.hankki.hankkiserver.domain.user.model.User.createUser;
import static org.hankki.hankkiserver.domain.user.model.UserInfo.createMemberInfo;
import static org.hankki.hankkiserver.domain.user.model.Platform.APPLE;
import static org.hankki.hankkiserver.domain.user.model.Platform.KAKAO;
import static org.hankki.hankkiserver.auth.filter.JwtAuthenticationFilter.BEARER;

@Service
@Transactional
@RequiredArgsConstructor
public class AuthService {

private final UserRepository userRepository;
private final UserInfoRepository userInfoRepository;
private final UserAdapter userAdapter;
private final UserInfoAdapter userInfoAdapter;
private final JwtProvider jwtProvider;
private final JwtValidator jwtValidator;
private final KakaoOAuthProvider kakaoOAuthProvider;
Expand All @@ -44,50 +40,48 @@ public class AuthService {
public UserLoginResponse login(
final String token,
final UserLoginRequest request) {
Platform platform = getEnumPlatformFromStringPlatform(request.platform());
Platform platform = request.platform();
SocialInfoDto socialInfo = getSocialInfo(token, platform, request.name());
boolean isRegistered = isRegisteredUser(platform, socialInfo);
boolean isRegistered = userAdapter.isRegisteredUser(platform, socialInfo);
User findUser = loadOrCreateUser(platform, socialInfo, isRegistered);
Token issuedToken = generateTokens(findUser.getId());
return UserLoginResponse.of(issuedToken, isRegistered);
}

public void logOut(final Long userId) {
UserInfo findUserInfo = getUserInfo(userId);
public void logOut(final long userId) {
UserInfo findUserInfo = userInfoAdapter.getUserInfo(userId);
updateRefreshToken(null, findUserInfo);
}

public void withdraw(final Long userId, final String code) {
User user = getUser(userId);
if (user.getPlatform() == APPLE){
public void withdraw(final long userId, final String code) {
User user = userAdapter.getUser(userId);
if (APPLE == user.getPlatform()){
try {
String refreshToken = appleOAuthProvider.getAppleToken(code);
appleOAuthProvider.requestRevoke(refreshToken);
} catch (Exception e) {
throw new InvalidValueException(ErrorCode.APPLE_REVOKE_FAILED);
}
}

userRepository.softDeleteById(userId, INACTIVE);
userInfoRepository.softDeleteByUserId(userId);
user.softDelete(INACTIVE);
userInfoAdapter.softDelete(userId);
}

@Transactional(noRollbackFor = UnauthorizedException.class)
public UserReissueResponse reissue(final String refreshToken) {
Long userId = jwtProvider.getSubject(refreshToken.substring(BEARER.length()));
long userId = jwtProvider.getSubject(refreshToken.substring(BEARER.length()));
validateRefreshToken(refreshToken, userId);
UserInfo findUserInfo = getUserInfo(userId);
Token issueToken = jwtProvider.issueToken(userId);
updateRefreshToken(issueToken.refreshToken(), findUserInfo);
return UserReissueResponse.of(issueToken);
UserInfo findUserInfo = userInfoAdapter.getUserInfo(userId);
Token issueTokens = jwtProvider.issueTokens(userId);
updateRefreshToken(issueTokens.refreshToken(), findUserInfo);
return UserReissueResponse.of(issueTokens);
}

private Token generateTokens(final Long userId) {
Token issuedToken = jwtProvider.issueToken(userId);
UserInfo findUserInfo = getUserInfo(userId);
updateRefreshToken(issuedToken.refreshToken(), findUserInfo);
return issuedToken;
private Token generateTokens(final long userId) {
Token issuedTokens = jwtProvider.issueTokens(userId);
UserInfo findUserInfo = userInfoAdapter.getUserInfo(userId);
updateRefreshToken(issuedTokens.refreshToken(), findUserInfo);
return issuedTokens;
}

private SocialInfoDto getSocialInfo(
Expand All @@ -99,73 +93,38 @@ private SocialInfoDto getSocialInfo(
} else if (APPLE == platform){
return appleOAuthProvider.getAppleUserInfo(providerToken, name);
}
throw new EntityNotFoundException(ErrorCode.INVALID_PLATFORM_TYPE);
}

private boolean isRegisteredUser(Platform platform, SocialInfoDto socialInfo){
return userRepository.existsByPlatformAndSerialIdAndMemberStatus(
platform,
socialInfo.serialId(),
ACTIVE);
throw new InvalidValueException(ErrorCode.INVALID_PLATFORM_TYPE);
}

private User loadOrCreateUser(Platform platform, SocialInfoDto socialInfo, boolean isRegistered){
if (!isRegistered){
User newUser = createUser(
User newUser = User.createUser(
socialInfo.name(),
socialInfo.email(),
socialInfo.serialId(),
platform);
saveUser(newUser);
return newUser;
}

User findUser = getUser(platform, socialInfo.serialId());
if (INACTIVE == findUser.getMemberStatus()) {
findUser.softDelete(ACTIVE);
userRepository.save(findUser);
}
return findUser;
}

private User getUser(
final Platform platform,
final String serialId) {
return userRepository.findByPlatformAndSerialId(platform, serialId)
.orElseThrow(() -> new EntityNotFoundException(ErrorCode.USER_NOT_FOUND));
}

private User getUser(final Long userId) {
return userRepository.findById(userId)
.orElseThrow(() -> new EntityNotFoundException(ErrorCode.USER_NOT_FOUND));
}

private UserInfo getUserInfo(final Long memberId) {
return userInfoRepository.findByUserId(memberId)
.orElseThrow(() -> new EntityNotFoundException(ErrorCode.USER_INFO_NOT_FOUND));
return userAdapter.getUser(platform, socialInfo.serialId());
}

private String getRefreshToken(final Long userId) {
return userInfoRepository.findByUserId(userId)
.orElseThrow(() -> new EntityNotFoundException(ErrorCode.REFRESH_TOKEN_NOT_FOUND))
return userInfoAdapter.getUserInfo(userId)
.getRefreshToken();
}

private void saveUser(final User user) {
userRepository.save(user);
UserInfo userInfo = createMemberInfo(user, null);
userInfoRepository.save(userInfo);
userAdapter.saveUser(user);
UserInfo userInfo = UserInfo.createMemberInfo(user, null);
userInfoAdapter.saveUserInfo(userInfo);
}

private void updateRefreshToken(
final String refreshToken,
final UserInfo userInfo) {
private void updateRefreshToken(final String refreshToken, final UserInfo userInfo) {
userInfo.updateRefreshToken(refreshToken);
}

private void validateRefreshToken(
final String refreshToken,
final Long userId) {
private void validateRefreshToken(final String refreshToken, final long userId) {
try {
jwtValidator.validateRefreshToken(refreshToken);
String storedRefreshToken = getRefreshToken(userId);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package org.hankki.hankkiserver.api.auth.service;

import lombok.RequiredArgsConstructor;
import org.hankki.hankkiserver.common.code.ErrorCode;
import org.hankki.hankkiserver.common.exception.EntityNotFoundException;
import org.hankki.hankkiserver.domain.user.model.Platform;
import org.hankki.hankkiserver.domain.user.model.User;
import org.hankki.hankkiserver.domain.user.repository.UserRepository;
import org.hankki.hankkiserver.external.openfeign.dto.SocialInfoDto;
import org.springframework.stereotype.Component;

import static org.hankki.hankkiserver.domain.user.model.MemberStatus.ACTIVE;

@Component
@RequiredArgsConstructor
public class UserAdapter {

private final UserRepository userRepository;

public User getUser(final Platform platform, final String serialId) {
return userRepository.findByPlatformAndSerialIdAndMemberStatus(platform, serialId, ACTIVE)
.orElseThrow(() -> new EntityNotFoundException(ErrorCode.USER_NOT_FOUND));
}

public User getUser(final long userId) {
return userRepository.findByIdAndMemberStatus(userId, ACTIVE)
.orElseThrow(() -> new EntityNotFoundException(ErrorCode.USER_NOT_FOUND));
}

public boolean isRegisteredUser(final Platform platform, final SocialInfoDto socialInfo) {
return userRepository.existsByPlatformAndSerialIdAndMemberStatus(
platform,
socialInfo.serialId(),
ACTIVE);
}

public void saveUser(final User user) {
userRepository.save(user);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package org.hankki.hankkiserver.api.auth.service;

import lombok.RequiredArgsConstructor;
import org.hankki.hankkiserver.common.code.ErrorCode;
import org.hankki.hankkiserver.common.exception.EntityNotFoundException;
import org.hankki.hankkiserver.domain.user.model.UserInfo;
import org.hankki.hankkiserver.domain.user.repository.UserInfoRepository;
import org.springframework.stereotype.Component;

@Component
@RequiredArgsConstructor
public class UserInfoAdapter {

private final UserInfoRepository userInfoRepository;

public UserInfo getUserInfo(final long userId) {
return userInfoRepository.findByUserId(userId)
.orElseThrow(() -> new EntityNotFoundException(ErrorCode.USER_INFO_NOT_FOUND));
}

public void saveUserInfo(final UserInfo userInfo) {
userInfoRepository.save(userInfo);
}

public void softDelete(final long userId) {
userInfoRepository.softDeleteByUserId(userId);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class UserIdArgumentResolver implements HandlerMethodArgumentResolver {
@Override
public boolean supportsParameter(MethodParameter parameter) {
boolean hasUserIdAnnotation = parameter.hasParameterAnnotation(UserId.class);
boolean isLongType = Long.class.isAssignableFrom(parameter.getParameterType());
boolean isLongType = parameter.getParameterType().equals(Long.class);
return hasUserIdAnnotation && isLongType;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public class JwtProvider {

private final JwtGenerator jwtGenerator;

public Token issueToken(Long userId) {
public Token issueTokens(Long userId) {
return Token.of(jwtGenerator.generateToken(userId, true),
jwtGenerator.generateToken(userId, false));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@

import lombok.Getter;
import lombok.RequiredArgsConstructor;
import org.hankki.hankkiserver.common.code.ErrorCode;
import org.hankki.hankkiserver.common.exception.InvalidValueException;

import java.util.Arrays;

@Getter
@RequiredArgsConstructor
Expand All @@ -15,11 +11,4 @@ public enum Platform {
APPLE("APPLE");

private final String loginPlatform;

public static Platform getEnumPlatformFromStringPlatform(String loginPlatform) {
return Arrays.stream(values())
.filter(platform -> platform.loginPlatform.equals(loginPlatform))
.findFirst()
.orElseThrow(() -> new InvalidValueException(ErrorCode.INVALID_PLATFORM_TYPE));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,7 @@ public class User extends BaseTimeEntity {
@Enumerated(EnumType.STRING)
private MemberStatus memberStatus;

public static User createUser(
final String name,
final String email,
final String serialId,
final Platform platform) {
public static User createUser(final String name, final String email, final String serialId, final Platform platform) {
return User.builder()
.name(name)
.email(email)
Expand All @@ -60,9 +56,11 @@ public static User createUser(
}

public void softDelete(MemberStatus memberStatus) {
updateStatus(memberStatus);
this.deletedAt = LocalDateTime.now();
}

public void updateStatus(MemberStatus memberStatus) {
this.memberStatus = memberStatus;
if (INACTIVE == memberStatus) {
this.deletedAt = LocalDateTime.now();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
@RequiredArgsConstructor
public enum UserRole {

ADMIN("ADMIN"), USER("USER");
ADMIN("ADMIN"),
USER("USER");

private final String value;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.transaction.annotation.Transactional;

import java.util.Optional;

Expand Down
Loading

0 comments on commit 674675c

Please sign in to comment.