Skip to content

Commit

Permalink
fix: smsRedisService -> Provider로 rename && 타입별 Service 분리
Browse files Browse the repository at this point in the history
  • Loading branch information
psychology50 committed Dec 24, 2023
1 parent e2c2d8f commit fd00d56
Show file tree
Hide file tree
Showing 25 changed files with 428 additions and 171 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,12 @@
import com.kcy.fitapet.domain.member.dto.account.AccountSearchReq;
import com.kcy.fitapet.domain.member.dto.account.ProfilePatchReq;
import com.kcy.fitapet.domain.member.dto.account.UidRes;
import com.kcy.fitapet.domain.member.exception.AccountErrorCode;
import com.kcy.fitapet.domain.member.service.component.MemberAccountService;
import com.kcy.fitapet.domain.member.type.MemberAttrType;
import com.kcy.fitapet.domain.notification.type.NotificationType;
import com.kcy.fitapet.global.common.response.SuccessResponse;
import com.kcy.fitapet.global.common.response.code.ErrorCode;
import com.kcy.fitapet.global.common.response.code.StatusCode;
import com.kcy.fitapet.global.common.response.exception.GlobalErrorException;
import com.kcy.fitapet.global.common.security.authentication.CustomUserDetails;
import com.kcy.fitapet.global.common.redis.sms.SmsPrefix;
import com.kcy.fitapet.global.common.redis.sms.type.SmsPrefix;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
Expand All @@ -22,7 +18,6 @@
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PostAuthorize;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import com.kcy.fitapet.domain.member.dto.auth.SignInReq;
import com.kcy.fitapet.domain.member.dto.auth.SignUpReq;
import com.kcy.fitapet.global.common.redis.sms.SmsPrefix;
import com.kcy.fitapet.global.common.redis.sms.type.SmsPrefix;
import com.kcy.fitapet.global.common.util.sms.dto.SmsReq;
import com.kcy.fitapet.global.common.util.sms.dto.SmsRes;
import com.kcy.fitapet.domain.member.exception.SmsErrorCode;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
import com.kcy.fitapet.domain.member.service.module.MemberSearchService;
import com.kcy.fitapet.domain.member.type.MemberAttrType;
import com.kcy.fitapet.domain.notification.type.NotificationType;
import com.kcy.fitapet.global.common.redis.sms.SmsCertificationService;
import com.kcy.fitapet.global.common.redis.sms.SmsPrefix;
import com.kcy.fitapet.global.common.redis.sms.provider.SmsRedisProvider;
import com.kcy.fitapet.global.common.redis.sms.type.SmsPrefix;
import com.kcy.fitapet.global.common.response.code.StatusCode;
import com.kcy.fitapet.global.common.response.exception.GlobalErrorException;
import lombok.RequiredArgsConstructor;
Expand All @@ -26,7 +26,7 @@
@Slf4j
public class MemberAccountService {
private final MemberSearchService memberSearchService;
private final SmsCertificationService smsCertificationService;
private final SmsRedisProvider smsRedisProvider;

private final PasswordEncoder bCryptPasswordEncoder;

Expand Down Expand Up @@ -125,13 +125,13 @@ private void validatePassword(Member member, String prePassword, String newPassw
* @param prefix : 인증번호 타입
*/
private void validatePhone(String phone, String code, SmsPrefix prefix) {
if (!smsCertificationService.existsCode(phone, prefix)) {
if (!smsRedisProvider.isExistsCode(phone, prefix)) {
StatusCode errorCode = SmsErrorCode.EXPIRED_AUTH_CODE;
log.warn("인증번호 유효성 검사 실패: {}", errorCode);
throw new GlobalErrorException(errorCode);
}

if (!smsCertificationService.isCorrectCode(phone, code, prefix)) {
if (!smsRedisProvider.isCorrectCode(phone, code, prefix)) {
StatusCode errorCode = SmsErrorCode.INVALID_AUTH_CODE;
log.warn("인증번호 유효성 검사 실패: {}", errorCode);
throw new GlobalErrorException(errorCode);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
import com.kcy.fitapet.global.common.redis.forbidden.ForbiddenTokenService;
import com.kcy.fitapet.global.common.redis.refresh.RefreshToken;
import com.kcy.fitapet.global.common.redis.refresh.RefreshTokenService;
import com.kcy.fitapet.global.common.redis.sms.SmsCertificationService;
import com.kcy.fitapet.global.common.redis.sms.SmsPrefix;
import com.kcy.fitapet.global.common.redis.sms.provider.SmsRedisProvider;
import com.kcy.fitapet.global.common.redis.sms.type.SmsPrefix;
import com.kcy.fitapet.global.common.util.sms.SmsProvider;
import com.kcy.fitapet.global.common.util.sms.dto.SensInfo;
import com.kcy.fitapet.global.common.util.sms.dto.SmsReq;
Expand All @@ -44,7 +44,7 @@ public class MemberAuthService {

private final RefreshTokenService refreshTokenService;
private final ForbiddenTokenService forbiddenTokenService;
private final SmsCertificationService smsCertificationService;
private final SmsRedisProvider smsRedisProvider;

private final SmsProvider smsProvider;
private final JwtUtil jwtUtil;
Expand All @@ -56,7 +56,7 @@ public Map<String, String> register(String requestAccessToken, SignUpReq dto) {
String accessToken = jwtUtil.resolveToken(requestAccessToken);

String authenticatedPhone = jwtUtil.getPhoneNumberFromToken(accessToken);
smsCertificationService.removeCode(authenticatedPhone, SmsPrefix.REGISTER);
smsRedisProvider.removeCode(authenticatedPhone, SmsPrefix.REGISTER);

Member requestMember = dto.toEntity(authenticatedPhone);
requestMember.encodePassword(bCryptPasswordEncoder);
Expand Down Expand Up @@ -100,34 +100,34 @@ public SmsRes sendCode(SmsReq dto, SmsPrefix prefix) {
validateForSms(prefix, dto);
SensInfo smsInfo = smsProvider.sendCodeByPhoneNumber(dto);

smsCertificationService.saveSmsAuthToken(dto.to(), smsInfo.code(), prefix);
LocalDateTime expireTime = smsCertificationService.getExpiredTime(dto.to(), prefix);
smsRedisProvider.saveSmsAuthToken(dto.to(), smsInfo.code(), prefix);
LocalDateTime expireTime = smsRedisProvider.getExpiredTime(dto.to(), prefix);
log.info("인증번호 만료 시간: {}", expireTime);
return SmsRes.of(dto.to(), smsInfo.requestTime(), expireTime);
}

@Transactional
public String checkCodeForRegister(SmsReq smsReq, String requestCode) {
if (!smsCertificationService.isCorrectCode(smsReq.to(), requestCode, SmsPrefix.REGISTER)) {
if (!smsRedisProvider.isCorrectCode(smsReq.to(), requestCode, SmsPrefix.REGISTER)) {
log.warn("인증번호 불일치 -> 사용자 입력 인증 번호 : {}", requestCode);
throw new GlobalErrorException(SmsErrorCode.INVALID_AUTH_CODE);
}

String token = jwtUtil.generateSmsAuthToken(SmsAuthInfo.of(1L, smsReq.to()));
smsCertificationService.saveSmsAuthToken(smsReq.to(), token, SmsPrefix.REGISTER);
smsRedisProvider.saveSmsAuthToken(smsReq.to(), token, SmsPrefix.REGISTER);

return token;
}

@Transactional(readOnly = true)
public void checkCodeForSearch(SmsReq req, String code, SmsPrefix prefix) {
if (!smsCertificationService.existsCode(req.to(), prefix)) {
if (!smsRedisProvider.isExistsCode(req.to(), prefix)) {
StatusCode errorCode = SmsErrorCode.EXPIRED_AUTH_CODE;
log.warn("인증번호 유효성 검사 실패: {}", errorCode);
throw new GlobalErrorException(errorCode);
}

if (!smsCertificationService.isCorrectCode(req.to(), code, prefix)) {
if (!smsRedisProvider.isCorrectCode(req.to(), code, prefix)) {
StatusCode errorCode = SmsErrorCode.INVALID_AUTH_CODE;
log.warn("인증번호 유효성 검사 실패: {}", errorCode);
throw new GlobalErrorException(errorCode);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import com.kcy.fitapet.domain.oauth.dto.OauthSignUpReq;
import com.kcy.fitapet.domain.oauth.service.component.OauthService;
import com.kcy.fitapet.domain.oauth.type.ProviderType;
import com.kcy.fitapet.global.common.redis.sms.SmsPrefix;
import com.kcy.fitapet.global.common.redis.sms.type.SmsPrefix;
import com.kcy.fitapet.global.common.response.SuccessResponse;
import com.kcy.fitapet.global.common.security.jwt.dto.Jwt;
import com.kcy.fitapet.global.common.util.cookie.CookieUtil;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
import com.kcy.fitapet.domain.oauth.service.module.OauthSearchService;
import com.kcy.fitapet.domain.oauth.type.ProviderType;
import com.kcy.fitapet.global.common.redis.oauth.OIDCTokenService;
import com.kcy.fitapet.global.common.redis.sms.SmsCertificationService;
import com.kcy.fitapet.global.common.redis.sms.SmsPrefix;
import com.kcy.fitapet.global.common.redis.sms.provider.SmsRedisProvider;
import com.kcy.fitapet.global.common.redis.sms.type.SmsPrefix;
import com.kcy.fitapet.global.common.response.exception.GlobalErrorException;
import com.kcy.fitapet.global.common.security.jwt.JwtUtil;
import com.kcy.fitapet.global.common.security.jwt.dto.Jwt;
Expand Down Expand Up @@ -51,7 +51,7 @@ public class OauthService {
private final JwtUtil jwtUtil;
private final OIDCTokenService oidcTokenService;
private final SmsProvider smsProvider;
private final SmsCertificationService smsCertificationService;
private final SmsRedisProvider smsRedisProvider;

@Transactional
public Jwt signInByOIDC(Long id, String idToken, ProviderType provider, String nonce) {
Expand Down Expand Up @@ -88,21 +88,21 @@ public Jwt signUpByOIDC(Long id, ProviderType provider, OauthSignUpReq req) {
public SmsRes sendCode(SmsReq dto, Long id, ProviderType provider, SmsPrefix prefix) {
SensInfo smsInfo = smsProvider.sendCodeByPhoneNumber(dto);

smsCertificationService.saveSmsAuthToken(dto.to(), smsInfo.code(), prefix);
LocalDateTime expireTime = smsCertificationService.getExpiredTime(dto.to(), prefix);
smsRedisProvider.saveSmsAuthToken(dto.to(), smsInfo.code(), prefix);
LocalDateTime expireTime = smsRedisProvider.getExpiredTime(dto.to(), prefix);
log.info("인증번호 만료 시간: {}", expireTime);
return SmsRes.of(dto.to(), smsInfo.requestTime(), expireTime);
}

@Transactional
public String checkCertificationNumber(SmsReq req, Long id, String code) {
if (!smsCertificationService.isCorrectCode(req.to(), code, SmsPrefix.REGISTER)) {
if (!smsRedisProvider.isCorrectCode(req.to(), code, SmsPrefix.REGISTER)) {
log.warn("인증번호 불일치 -> 사용자 입력 인증 번호 : {}", code);
throw new GlobalErrorException(SmsErrorCode.INVALID_AUTH_CODE);
}

String token = jwtUtil.generateSmsOauthToken(SmsAuthInfo.of(id, req.to()));
smsCertificationService.saveSmsAuthToken(req.to(), token, SmsPrefix.OAUTH);
smsRedisProvider.saveSmsAuthToken(req.to(), token, SmsPrefix.OAUTH);

return token;
}
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package com.kcy.fitapet.global.common.redis.sms.provider;

import com.kcy.fitapet.domain.member.exception.SmsErrorCode;
import com.kcy.fitapet.global.common.redis.sms.dao.*;
import com.kcy.fitapet.global.common.redis.sms.domain.*;
import com.kcy.fitapet.global.common.redis.sms.type.SmsPrefix;
import com.kcy.fitapet.global.common.response.exception.GlobalErrorException;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Primary;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Optional;

@Slf4j
@Service
@Primary
@RequiredArgsConstructor
public class SmsOauthProvider implements SmsRedisProvider {
private final SmsOauthRepository smsOauthRepository;

private final RedisTemplate<String, SmsOauth> redisTemplate;

@Override
public void saveSmsAuthToken(String phone, String code, SmsPrefix prefix) {
smsOauthRepository.save(SmsOauth.of(phone, code));
}

@Override
public boolean isCorrectCode(String phoneNumber, String code, SmsPrefix prefix) {
Optional<SmsOauth> smsOauth = smsOauthRepository.findById(phoneNumber);
return smsOauth.map(oauth -> oauth.getCode().equals(code)).orElse(false);
}

@Override
public boolean isExistsCode(String phoneNumber, SmsPrefix prefix) {
return smsOauthRepository.existsById(phoneNumber);
}

@Override
public void removeCode(String phoneNumber, SmsPrefix prefix) {
smsOauthRepository.deleteById(phoneNumber);
}

@Override
public LocalDateTime getExpiredTime(String phoneNumber, SmsPrefix prefix) {
Long ttl = redisTemplate.getExpire(getTopic(phoneNumber, prefix));
log.info("ttl: {}", ttl);

if (ttl == null || ttl < 0L)
throw new GlobalErrorException(SmsErrorCode.EXPIRED_AUTH_CODE);

return LocalDateTime.now().plusSeconds(ttl);
}
}
Loading

0 comments on commit fd00d56

Please sign in to comment.