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

[FEAT] Validator 구현 - #130 #131

Merged
merged 3 commits into from
Jul 17, 2024
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 @@ -23,13 +23,6 @@ public Token issueToken(final long userId) {
);
}

//refreshToken 재발급할 때 검증
public void validateRefreshToken(LocalDateTime expireDate) {
if (expireDate.isBefore(LocalDateTime.now())) {
throw new UnauthorizedException(FailureCode.EXPIRED_REFRESH_TOKEN);
}
}

public long getUserIdFromSubject(String token) {
Jws<Claims> jws = jwtGenerator.parseToken(token);
String subject = jws.getBody().getSubject();
Expand Down
29 changes: 29 additions & 0 deletions dateroad-api/src/main/java/org/dateroad/common/ValidatorUtil.java
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Utility 지렸다 ... 화장실다녀오겠습니다.

Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package org.dateroad.common;

import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import org.dateroad.code.FailureCode;
import org.dateroad.exception.InvalidValueException;
import org.dateroad.exception.UnauthorizedException;
import org.dateroad.tag.domain.DateTagType;

import java.time.LocalDateTime;
import java.util.List;

@NoArgsConstructor(access = AccessLevel.PRIVATE)
public final class ValidatorUtil {

//태그 리스트 사이즈 검증
public static void validateUserTagSize(final List<DateTagType> userTags) {
if (userTags.isEmpty() || userTags.size() > 3) {
throw new InvalidValueException((FailureCode.WRONG_USER_TAG_SIZE));
}
}

//refreshToken 재발급할 때 검증
public static void validateRefreshToken(LocalDateTime expireDate) {
if (expireDate.isBefore(LocalDateTime.now())) {
throw new UnauthorizedException(FailureCode.EXPIRED_REFRESH_TOKEN);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import org.dateroad.auth.jwt.JwtProvider;
import org.dateroad.auth.jwt.Token;
import org.dateroad.code.FailureCode;
import org.dateroad.common.ValidatorUtil;
import org.dateroad.exception.*;
import org.dateroad.feign.apple.AppleFeignProvider;
import org.dateroad.feign.kakao.KakaoFeignProvider;
Expand Down Expand Up @@ -33,6 +34,9 @@
import java.io.IOException;
import java.util.List;

import static org.dateroad.common.ValidatorUtil.validateRefreshToken;
import static org.dateroad.common.ValidatorUtil.validateUserTagSize;

@RequiredArgsConstructor
@Transactional(readOnly = true)
@Service
Expand Down Expand Up @@ -74,7 +78,7 @@ public UserJwtInfoRes signIn(final String token, final UserSignInReq userSignInR
@Transactional
public UserJwtInfoRes reissue(final String refreshToken) {
RefreshToken foundRefreshToken = getRefreshTokenByToken(refreshToken);
jwtProvider.validateRefreshToken(foundRefreshToken.getExpiredAt());
validateRefreshToken(foundRefreshToken.getExpiredAt());
Long userId = foundRefreshToken.getUserId();
deleteRefreshToken(userId);
Token newToken = issueToken(userId);
Expand Down Expand Up @@ -156,13 +160,6 @@ private User getUserByUserId(final long userId) {
);
}

//태그 리스트 사이즈 검증
public void validateUserTagSize(final List<DateTagType> userTags) {
if (userTags.isEmpty() || userTags.size() > 3) {
throw new InvalidValueException((FailureCode.WRONG_USER_TAG_SIZE));
}
}

private RefreshToken getRefreshTokenByToken(final String refreshToken) {
try {
return refreshTokenRepository.findRefreshTokenByToken(refreshToken)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
import java.util.List;
import java.util.concurrent.ExecutionException;

import static org.dateroad.common.ValidatorUtil.validateUserTagSize;

@Service
@Transactional(readOnly = true)
@RequiredArgsConstructor
Expand Down Expand Up @@ -60,7 +62,7 @@ public void editUserProfile(final Long userId,

//tag 변경
userTagRepository.deleteAllByUserId(foundUser.getId());
authService.validateUserTagSize(tags);
validateUserTagSize(tags);
saveUserTag(foundUser, tags);

//이미지 변경
Expand Down
Loading