Skip to content

Commit

Permalink
Merge pull request #655 from tukcomCD2024/fix/phone_duplicate_validat…
Browse files Browse the repository at this point in the history
…ion-B-core-#652

fix : 전화번호 중복 검증 추가 #652
  • Loading branch information
seokho-1116 authored Aug 29, 2024
2 parents d11248a + 019635c commit e7c384e
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package site.timecapsulearchive.core.domain.auth.exception;

import site.timecapsulearchive.core.global.error.ErrorCode;
import site.timecapsulearchive.core.global.error.exception.BusinessException;

public class PhoneDuplicationException extends BusinessException {

public PhoneDuplicationException() {
super(ErrorCode.PHONE_DUPLICATION_ERROR);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
import site.timecapsulearchive.core.domain.auth.data.dto.VerificationMessageSendDto;
import site.timecapsulearchive.core.domain.auth.exception.CertificationNumberNotFoundException;
import site.timecapsulearchive.core.domain.auth.exception.CertificationNumberNotMatchException;
import site.timecapsulearchive.core.domain.auth.exception.PhoneDuplicationException;
import site.timecapsulearchive.core.domain.auth.repository.MessageAuthenticationCacheRepository;
import site.timecapsulearchive.core.domain.member.repository.MemberRepository;
import site.timecapsulearchive.core.global.security.encryption.HashEncryptionManager;
import site.timecapsulearchive.core.infra.sms.data.response.SmsApiResponse;
import site.timecapsulearchive.core.infra.sms.manager.SmsApiManager;
Expand All @@ -27,6 +29,7 @@ public class MessageVerificationService {
private final MessageAuthenticationCacheRepository messageAuthenticationCacheRepository;
private final SmsApiManager smsApiManager;
private final HashEncryptionManager hashEncryptionManager;
private final MemberRepository memberRepository;

/**
* 사용자 아이디와 수신자 핸드폰을 받아서 인증번호를 발송한다.
Expand All @@ -40,13 +43,18 @@ public VerificationMessageSendDto sendVerificationMessage(
final String receiver,
final String appHashKey
) {
final byte[] plain = receiver.getBytes(StandardCharsets.UTF_8);
byte[] encrypt = hashEncryptionManager.encrypt(plain);

boolean isDuplicated = memberRepository.checkPhoneHashDuplication(encrypt);
if (isDuplicated) {
throw new PhoneDuplicationException();
}

final String code = generateRandomCode();
final String message = generateMessage(code, appHashKey);
final SmsApiResponse apiResponse = smsApiManager.sendMessage(receiver, message);

final byte[] plain = receiver.getBytes(StandardCharsets.UTF_8);
byte[] encrypt = hashEncryptionManager.encrypt(plain);

messageAuthenticationCacheRepository.save(memberId, encrypt, code);

return VerificationMessageSendDto.success(apiResponse.resultCode(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,5 @@ Optional<VerifiedCheckDto> findVerifiedCheckDtoByAuthIdAndSocialType(

Optional<ByteArrayWrapper> findMemberPhoneHash(final Long memberId);

boolean checkPhoneHashDuplication(byte[] encrypt);
}
Original file line number Diff line number Diff line change
Expand Up @@ -151,4 +151,14 @@ public Optional<ByteArrayWrapper> findMemberPhoneHash(final Long memberId) {

return Optional.empty();
}

@Override
public boolean checkPhoneHashDuplication(final byte[] encrypt) {
final Integer count = jpaQueryFactory.selectOne()
.from(member)
.where(member.phoneHash.eq(encrypt))
.fetchFirst();

return count != null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public enum ErrorCode {
AUTHENTICATION_ERROR(401, "AUTH-003", "인증에 실패했습니다. 인증 수단이 유효한지 확인하세요."),
AUTHORIZATION_ERROR(403, "AUTH-004", "권한이 존재하지 않습니다."),
CREDENTIALS_NOT_MATCHED_ERROR(401, "AUTH-005", "이메일과 비밀번호 인증에 실패했습니다."),
PHONE_DUPLICATION_ERROR(400, "AUTH-006", "중복된 전화번호입니다."),

//message
TOO_MANY_REQUEST_ERROR(429, "MESSAGE-001", "너무 많은 인증 메시지를 요청했습니다. 24시간 후 요청해주세요."),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
import site.timecapsulearchive.core.domain.auth.data.dto.VerificationMessageSendDto;
import site.timecapsulearchive.core.domain.auth.exception.CertificationNumberNotFoundException;
import site.timecapsulearchive.core.domain.auth.exception.CertificationNumberNotMatchException;
import site.timecapsulearchive.core.domain.auth.exception.PhoneDuplicationException;
import site.timecapsulearchive.core.domain.auth.repository.MessageAuthenticationCacheRepository;
import site.timecapsulearchive.core.domain.member.repository.MemberRepository;
import site.timecapsulearchive.core.infra.sms.manager.SmsApiManager;

class MessageVerificationServiceTest {
Expand All @@ -26,16 +28,33 @@ class MessageVerificationServiceTest {
private final MessageAuthenticationCacheRepository messageAuthenticationCacheRepository = mock(
MessageAuthenticationCacheRepository.class);
private final SmsApiManager smsApiManager = UnitTestDependency.smsApiManager();
private final MemberRepository memberRepository = mock(MemberRepository.class);

private final MessageVerificationService messageVerificationService = new MessageVerificationService(
messageAuthenticationCacheRepository,
smsApiManager,
UnitTestDependency.hashEncryptionManager()
UnitTestDependency.hashEncryptionManager(),
memberRepository
);

@Test
void 중복된_번호가_있으면_예외가_발생한다() {
// given
given(memberRepository.checkPhoneHashDuplication(any())).willReturn(true);

// when
// then
assertThatThrownBy(
() -> messageVerificationService.sendVerificationMessage(MEMBER_ID, RECEIVER,
APP_HASH_KEY))
.isInstanceOf(PhoneDuplicationException.class);
}

@Test
void 인증번호를_전송하면_성공한다() {
//given
given(memberRepository.checkPhoneHashDuplication(any())).willReturn(false);

//when
VerificationMessageSendDto verificationMessageSendDto = messageVerificationService.sendVerificationMessage(
MEMBER_ID, RECEIVER, APP_HASH_KEY);
Expand Down

0 comments on commit e7c384e

Please sign in to comment.