Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/develop' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
imenuuu committed Mar 6, 2024
2 parents 423f88e + 72c5622 commit 6f65b39
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ public static User toUser(AppleMember appleMember) {
.password(staticPasswordEncoder.encode(appleMember.getSocialId()))
.socialId(appleMember.getSocialId())
.nickName(nickName)
.username(createUserName(SocialType.GOOGLE, appleMember.getSocialId()))
.username(createUserName(SocialType.APPLE, appleMember.getSocialId()))
.socialType(SocialType.APPLE)
.level(1)
.status(Status.INACTIVE)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.example.wineyapi.user.service.context.SocialLoginContext;
import com.example.wineyapi.user.service.context.SocialLoginContextFactory;
import com.example.wineyapi.wineBadge.service.WineBadgeService;
import com.example.wineycommon.constants.WineyStatic;
import com.example.wineycommon.exception.MessageException;
import com.example.wineycommon.exception.NotFoundException;
import com.example.wineycommon.exception.UserException;
Expand Down Expand Up @@ -41,6 +42,7 @@
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.PostConstruct;
import java.time.Duration;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;
Expand Down Expand Up @@ -160,6 +162,23 @@ private String numberGen(int len) {
public VerificationMessage sendCode(Long userId, UserRequest.SendCodeDTO request) {

Optional<User> optionalUser = userRepository.findByPhoneNumber(request.getPhoneNumber());
Optional<VerificationMessage> optionalVerificationMessage = verificationMessageRepository.findByPhoneNumber(request.getPhoneNumber());

// 요청 횟수가 3회(이면 이번 요청이 4회째)이상 & updatedAt이 5분 이내이면 에러 응답
if(optionalVerificationMessage.isPresent()) {
VerificationMessage verificationMessage = optionalVerificationMessage.get();
LocalDateTime updatedAt = verificationMessage.getUpdatedAt();
LocalDateTime now = LocalDateTime.now();

// updatedAt과 현재 시간(now) 사이의 차이를 계산
Duration duration = Duration.between(updatedAt, now);

// 차이가 5분 이내인지 확인
if(verificationMessage.getRequestCount() >= 3 && duration.toMinutes() < 5) {
// 에러 응답 로직
throw new UserException(CommonResponseStatus.REQUEST_RATE_LIMIT_EXCEEDED);
}
}

if(optionalUser.isPresent() && optionalUser.get().getStatus() == Status.ACTIVE) {
// 0. 1~2를 수행한 소셜로그인 계정 hard delete & 안내문구전송
Expand All @@ -170,14 +189,14 @@ public VerificationMessage sendCode(Long userId, UserRequest.SendCodeDTO request
}

try {
// 1. 4자리 인증 번호 생성
String verificationNumber = numberGen(6);
// 1. 6자리 인증 번호 생성
String verificationNumber = numberGen(WineyStatic.VERIFICATION_MESSAGE_NUMBER_LENGTH);

// 2. 발송할 메시지 객체 준비
Message message = new Message();
message.setFrom(coolSmsProperties.getFromNumber());
message.setTo(request.getPhoneNumber());
message.setText("[WINEY]\n인증번호 : " + verificationNumber);
message.setText(WineyStatic.VERIFICATION_MESSAGE_PREFIX + verificationNumber);

// 3. 서비스를 이용하여 메시지 발송
coolSmsService.sendOne(new SingleMessageSendingRequest(message));
Expand All @@ -186,8 +205,9 @@ public VerificationMessage sendCode(Long userId, UserRequest.SendCodeDTO request
VerificationMessage verificationMessage = verificationMessageRepository.findByPhoneNumber(request.getPhoneNumber())
.orElseGet(() -> UserConverter.toVerificationMessage(request, verificationNumber));

verificationMessage.setRequestCount(verificationMessage.getRequestCount() + 1);
verificationMessage.setVerificationNumber(verificationNumber);
verificationMessage.setExpireAt(LocalDateTime.now().plusMinutes(5));
verificationMessage.setExpireAt(LocalDateTime.now().plusMinutes(WineyStatic.VERIFICATION_MESSAGE_EXPIRE_AT));
verificationMessage.setStatus(VerifyMessageStatus.PENDING);

// 5. 데이터베이스에 검증 메시지 저장
Expand All @@ -214,6 +234,7 @@ public VerificationMessage verifyCode(Long userId, UserRequest.VerifyCodeDTO req
// 3. 제공된 인증 번호가 검증 번호와 일치하는지 확인
if(verificationMessage.getVerificationNumber().equals(request.getVerificationCode())) {
verificationMessage.setStatus(VerifyMessageStatus.VERIFIED);
verificationMessage.setVerifiedAt(LocalDateTime.now());
} else {
verificationMessage.setStatus(VerifyMessageStatus.FAILED);
verificationMessage.setMismatchAttempts(verificationMessage.getMismatchAttempts() + 1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,10 @@ public class WineyStatic {
public static final List<String> IGNORE_METHODS = List.of(new String[]{"healthCheck", ""});
public static final String BOOKMARK_CANCEL_MESSAGE = "북마크 취소";
public static final String BOOKMARK_REGISTER_MESSAGE = "북마크 등록";

public static final String VERIFICATION_MESSAGE_PREFIX = "[WINEY]\n인증번호 : ";

public static final int VERIFICATION_MESSAGE_NUMBER_LENGTH = 6;

public static final int VERIFICATION_MESSAGE_EXPIRE_AT = 5;
}
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,8 @@ public enum CommonResponseStatus implements BaseErrorCode {
MESSAGE_SEND_FAILED(BAD_REQUEST, "M001", "메시지 전송이 실패했습니다. 올바른 번호인지 확인하세요."),
MESSAGE_NOT_FOUND(NOT_FOUND, "M002", "인증번호 전송 기록이 존재하지 않습니다."),
MESSAGE_VERIFICATION_TIMEOUT(BAD_REQUEST, "M003","인증 번호가 만료되었습니다."),
VERIFICATION_DID_NOT_MATCH(BAD_REQUEST, "M004", "인증 번호가 일치하지 않습니다.");
VERIFICATION_DID_NOT_MATCH(BAD_REQUEST, "M004", "인증 번호가 일치하지 않습니다."),
REQUEST_RATE_LIMIT_EXCEEDED(BAD_REQUEST, "M005", "인증번호 요청 횟수가 초과되었습니다. 잠시 후 다시 시도하세요.");

private final HttpStatus httpStatus;
private final String code;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ public class QVerificationMessage extends EntityPathBase<VerificationMessage> {

public final StringPath phoneNumber = createString("phoneNumber");

public final NumberPath<Integer> requestCount = createNumber("requestCount", Integer.class);

public final DateTimePath<java.time.LocalDateTime> requestedAt = createDateTime("requestedAt", java.time.LocalDateTime.class);

public final EnumPath<com.example.wineydomain.common.model.VerifyMessageStatus> status = createEnum("status", com.example.wineydomain.common.model.VerifyMessageStatus.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,8 @@ public class VerificationMessage extends BaseEntity {

@Column(name = "mismatchAttempts")
private Integer mismatchAttempts;

@Column(name = "requestCount")
@Builder.Default
private Integer requestCount = 0;
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,6 @@

public interface VerificationMessageRepository extends JpaRepository<VerificationMessage, Long> {
Optional<VerificationMessage> findByPhoneNumber(String phoneNumber);

boolean existsByPhoneNumber(String phoneNumber);
}

0 comments on commit 6f65b39

Please sign in to comment.