From d47d159d745a80599b6af7dcc27c6c94f0c0aae9 Mon Sep 17 00:00:00 2001 From: HongGeun Date: Sun, 3 Dec 2023 03:14:22 +0900 Subject: [PATCH 1/5] =?UTF-8?q?key=20generator=20=ED=81=B4=EB=9E=98?= =?UTF-8?q?=EC=8A=A4=20=EC=83=9D=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/catcher/utils/KeyGenerator.java | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 src/main/java/com/catcher/utils/KeyGenerator.java diff --git a/src/main/java/com/catcher/utils/KeyGenerator.java b/src/main/java/com/catcher/utils/KeyGenerator.java new file mode 100644 index 00000000..d8bb2fd7 --- /dev/null +++ b/src/main/java/com/catcher/utils/KeyGenerator.java @@ -0,0 +1,23 @@ +package com.catcher.utils; + +import lombok.AccessLevel; +import lombok.NoArgsConstructor; + +@NoArgsConstructor(access = AccessLevel.PRIVATE) +public final class KeyGenerator { + + public static String generateKey(Object obj, AuthType type) { + return String.format("%s:%s", type.name(), obj); + } + + public enum AuthType { + BLACK_LIST_ACCESS_TOKEN, + REFRESH_TOKEN, + FIND_ID, + FIND_PASSWORD, + FIND_PASSWORD_SUCCESS, + CAPTCHA_ID, + CAPTCHA_PASSWORD, + + } +} From 7cc80bbc55a58d1cd9018b6318f65e25875f06d8 Mon Sep 17 00:00:00 2001 From: HongGeun Date: Sun, 3 Dec 2023 03:15:25 +0900 Subject: [PATCH 2/5] =?UTF-8?q?generate=20key=20=EC=A0=81=EC=9A=A9=20?= =?UTF-8?q?=EB=B0=8F=20=EC=BA=A1=EC=B1=A0,=20=EC=9D=B4=EB=A9=94=EC=9D=BC?= =?UTF-8?q?=20=EC=9D=B8=EC=A6=9D=20=EC=84=B1=EA=B3=B5=EC=8B=9C=20=EB=8D=B0?= =?UTF-8?q?=EC=9D=B4=ED=84=B0=EA=B0=92=20=EC=A0=9C=EA=B1=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/catcher/config/JwtFilter.java | 6 ++--- .../core/port/KeyValueDataStorePort.java | 4 +++- .../catcher/core/service/AuthCodeService.java | 23 +++++++++++------- .../catcher/core/service/CaptchaService.java | 24 +++++++++++-------- .../catcher/core/service/OAuthService.java | 4 +++- .../com/catcher/core/service/UserService.java | 4 +++- .../adaptor/KeyValueDataStoreAdapter.java | 9 +++++-- .../adaptor/RefreshTokenAdaptor.java | 23 ++++++++++-------- .../com/catcher/resource/UserController.java | 20 ++++++++-------- src/main/java/com/catcher/utils/JwtUtils.java | 4 ---- 10 files changed, 70 insertions(+), 51 deletions(-) diff --git a/src/main/java/com/catcher/config/JwtFilter.java b/src/main/java/com/catcher/config/JwtFilter.java index 0fc7bde0..3f9ca7f9 100644 --- a/src/main/java/com/catcher/config/JwtFilter.java +++ b/src/main/java/com/catcher/config/JwtFilter.java @@ -2,7 +2,6 @@ import com.catcher.common.exception.BaseException; import com.catcher.core.database.DBManager; -import com.catcher.utils.JwtUtils; import jakarta.servlet.FilterChain; import jakarta.servlet.ServletException; import jakarta.servlet.http.HttpServletRequest; @@ -17,6 +16,8 @@ import static com.catcher.common.BaseResponseStatus.REDIS_ERROR; import static com.catcher.utils.HttpServletUtils.getHeader; +import static com.catcher.utils.KeyGenerator.AuthType.BLACK_LIST_ACCESS_TOKEN; +import static com.catcher.utils.KeyGenerator.generateKey; import static org.apache.http.HttpHeaders.AUTHORIZATION; /** @@ -51,8 +52,7 @@ protected void doFilterInternal( } private boolean isBlackList(String accessToken) { - String blackListToken = JwtUtils.generateBlackListToken(accessToken); - return dbManager.getValue(blackListToken).isPresent(); + return dbManager.getValue(generateKey(accessToken, BLACK_LIST_ACCESS_TOKEN)).isPresent(); } private String getAccessToken(HttpServletRequest request) { diff --git a/src/main/java/com/catcher/core/port/KeyValueDataStorePort.java b/src/main/java/com/catcher/core/port/KeyValueDataStorePort.java index 02bb0c56..91df4dd9 100644 --- a/src/main/java/com/catcher/core/port/KeyValueDataStorePort.java +++ b/src/main/java/com/catcher/core/port/KeyValueDataStorePort.java @@ -2,8 +2,10 @@ public interface KeyValueDataStorePort { - void saveValidationCodeWithUserId(String userId, String value); + void saveValidationCodeWithUserId(String key, String value); String findValidationCodeWithKey(String key); + void deleteKey(String key); + } diff --git a/src/main/java/com/catcher/core/service/AuthCodeService.java b/src/main/java/com/catcher/core/service/AuthCodeService.java index 2b251b37..b347bb10 100644 --- a/src/main/java/com/catcher/core/service/AuthCodeService.java +++ b/src/main/java/com/catcher/core/service/AuthCodeService.java @@ -9,6 +9,9 @@ import java.util.Random; +import static com.catcher.utils.KeyGenerator.AuthType; +import static com.catcher.utils.KeyGenerator.generateKey; + @Service @RequiredArgsConstructor public class AuthCodeService { @@ -24,24 +27,26 @@ public int generateSixDigitsRandomCode() { return random.nextInt(max - min + 1) + min; } - public String generateAndSaveRandomKey(final String email) { + public String generateAndSaveRandomKey(final String email, final AuthType authType) { final var user = userRepository.findByEmail(email).orElseThrow(() -> new BaseException(BaseResponseStatus.USERS_NOT_EXISTS)); final var generatedKey = String.valueOf(generateSixDigitsRandomCode()); - final var generatedDataStoreKey = generateDataStoreKey(user.getId()); + final var generatedDataStoreKey = generateKey(user.getId(), authType); keyValueDataStorePort.saveValidationCodeWithUserId(generatedDataStoreKey, generatedKey); return generatedKey; } - public boolean verifyAuthCode(final String email, String authCode) { + public boolean verifyAuthCode(final String email, String authCode, AuthType authType) { final var user = userRepository.findByEmail(email).orElseThrow(() -> new BaseException(BaseResponseStatus.USERS_NOT_EXISTS)); - final var generatedDataStoreKey = generateDataStoreKey(user.getId()); + final var generatedDataStoreKey = generateKey(user.getId(), authType); final String storedAuthCode = keyValueDataStorePort.findValidationCodeWithKey(generatedDataStoreKey); - return authCode.equals(storedAuthCode); - } - private String generateDataStoreKey(final Long userId) { - return String.format("%s_%s", userId, "AUTHCODE"); - } + boolean isSuccess = authCode.equals(storedAuthCode); + if(isSuccess) { + keyValueDataStorePort.deleteKey(generatedDataStoreKey); + } + + return isSuccess; + } } diff --git a/src/main/java/com/catcher/core/service/CaptchaService.java b/src/main/java/com/catcher/core/service/CaptchaService.java index 98c627a1..8b699dab 100644 --- a/src/main/java/com/catcher/core/service/CaptchaService.java +++ b/src/main/java/com/catcher/core/service/CaptchaService.java @@ -11,6 +11,9 @@ import java.awt.image.BufferedImage; import java.util.Objects; +import static com.catcher.utils.KeyGenerator.AuthType; +import static com.catcher.utils.KeyGenerator.generateKey; + @Service @RequiredArgsConstructor public class CaptchaService { @@ -22,13 +25,12 @@ public class CaptchaService { private final KeyValueDataStorePort keyValueDataStorePort; - public Captcha generateCaptchaAndSaveAnswer(final String email) { - + public Captcha generateCaptchaAndSaveAnswer(final String email, AuthType authType) { final var user = userRepository.findByEmail(email).orElseThrow(() -> new BaseException(BaseResponseStatus.USERS_NOT_EXISTS)); Captcha captcha = generateCaptcha(); - final String generatedUserKey = generateCaptchaUserKey(user.getId()); + final String generatedUserKey = generateKey(user.getId(), authType); keyValueDataStorePort.saveValidationCodeWithUserId(generatedUserKey, captcha.getAnswer()); return captcha; @@ -47,17 +49,19 @@ public BufferedImage getImage(Captcha captcha) { return captcha.getImage(); } - public boolean validateCaptcha(String userEmail, String userAnswer) { + public boolean validateCaptcha(String userEmail, String userAnswer, AuthType authType) { final var user = userRepository.findByEmail(userEmail).orElseThrow(() -> new BaseException(BaseResponseStatus.USERS_NOT_EXISTS)); - final String generatedUserKEy = generateCaptchaUserKey(user.getId()); - final String answer = keyValueDataStorePort.findValidationCodeWithKey(generatedUserKEy); + final String generatedUserKey = generateKey(user.getId(), authType); + final String answer = keyValueDataStorePort.findValidationCodeWithKey(generatedUserKey); - return Objects.equals(answer, userAnswer); - } + boolean isSuccess = Objects.equals(answer, userAnswer); + + if(isSuccess) { + keyValueDataStorePort.deleteKey(generatedUserKey); + } - private String generateCaptchaUserKey(final Long userId) { - return String.format("%s_%s", userId, "CAPTCHA"); + return isSuccess; } } diff --git a/src/main/java/com/catcher/core/service/OAuthService.java b/src/main/java/com/catcher/core/service/OAuthService.java index e8a6557f..4a2d292f 100644 --- a/src/main/java/com/catcher/core/service/OAuthService.java +++ b/src/main/java/com/catcher/core/service/OAuthService.java @@ -34,6 +34,8 @@ import static com.catcher.common.BaseResponseStatus.*; import static com.catcher.utils.JwtUtils.REFRESH_TOKEN_EXPIRATION_MILLIS; +import static com.catcher.utils.KeyGenerator.AuthType.REFRESH_TOKEN; +import static com.catcher.utils.KeyGenerator.generateKey; @Service @Slf4j @@ -132,7 +134,7 @@ private TokenDto checkAuthenticationAndGetTokenDto(String username) { String accessToken = jwtTokenProvider.createAccessToken(authentication); String refreshToken = jwtTokenProvider.createRefreshToken(authentication); - dbManager.putValue(username, refreshToken, REFRESH_TOKEN_EXPIRATION_MILLIS); + dbManager.putValue(generateKey(username, REFRESH_TOKEN), refreshToken, REFRESH_TOKEN_EXPIRATION_MILLIS); return new TokenDto(accessToken, refreshToken); } catch (BadCredentialsException e) { diff --git a/src/main/java/com/catcher/core/service/UserService.java b/src/main/java/com/catcher/core/service/UserService.java index e4029ba5..0a1dc850 100644 --- a/src/main/java/com/catcher/core/service/UserService.java +++ b/src/main/java/com/catcher/core/service/UserService.java @@ -10,6 +10,7 @@ import com.catcher.core.dto.user.UserCreateRequest; import com.catcher.core.dto.user.UserLoginRequest; import com.catcher.security.CatcherUser; +import com.catcher.utils.KeyGenerator; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.security.authentication.AuthenticationManager; @@ -28,6 +29,7 @@ import static com.catcher.core.domain.entity.enums.UserProvider.CATCHER; import static com.catcher.core.domain.entity.enums.UserRole.USER; import static com.catcher.utils.JwtUtils.REFRESH_TOKEN_EXPIRATION_MILLIS; +import static com.catcher.utils.KeyGenerator.AuthType.*; @RequiredArgsConstructor @Transactional(readOnly = true) @@ -76,7 +78,7 @@ private TokenDto checkAuthenticationAndGetTokenDto(String username, String passw String accessToken = jwtTokenProvider.createAccessToken(authentication); String refreshToken = jwtTokenProvider.createRefreshToken(authentication); - dbManager.putValue(username, refreshToken, REFRESH_TOKEN_EXPIRATION_MILLIS); + dbManager.putValue(KeyGenerator.generateKey(username, REFRESH_TOKEN), refreshToken, REFRESH_TOKEN_EXPIRATION_MILLIS); return new TokenDto(accessToken, refreshToken); } catch (BadCredentialsException e) { diff --git a/src/main/java/com/catcher/infrastructure/adaptor/KeyValueDataStoreAdapter.java b/src/main/java/com/catcher/infrastructure/adaptor/KeyValueDataStoreAdapter.java index f3b4191c..77b0adf0 100644 --- a/src/main/java/com/catcher/infrastructure/adaptor/KeyValueDataStoreAdapter.java +++ b/src/main/java/com/catcher/infrastructure/adaptor/KeyValueDataStoreAdapter.java @@ -16,8 +16,8 @@ public class KeyValueDataStoreAdapter implements KeyValueDataStorePort { private static final long THREE_MINUTES_AS_MILLISECONDS = 180000L; @Override - public void saveValidationCodeWithUserId(final String userId, final String authCode) { - redisManager.putValue(userId, authCode, THREE_MINUTES_AS_MILLISECONDS); + public void saveValidationCodeWithUserId(final String key, final String authCode) { + redisManager.putValue(key, authCode, THREE_MINUTES_AS_MILLISECONDS); } @Override @@ -25,4 +25,9 @@ public String findValidationCodeWithKey(final String key) { return redisManager.getValue(key).orElseThrow(() -> new BaseException(BaseResponseStatus.AUTH_CODE_NOT_FOUND)); } + @Override + public void deleteKey(String key) { + redisManager.deleteKey(key); + } + } diff --git a/src/main/java/com/catcher/infrastructure/adaptor/RefreshTokenAdaptor.java b/src/main/java/com/catcher/infrastructure/adaptor/RefreshTokenAdaptor.java index 5fbafcdb..abbb5ef2 100644 --- a/src/main/java/com/catcher/infrastructure/adaptor/RefreshTokenAdaptor.java +++ b/src/main/java/com/catcher/infrastructure/adaptor/RefreshTokenAdaptor.java @@ -13,7 +13,11 @@ import java.util.Optional; import static com.catcher.common.BaseResponseStatus.NOT_EXIST_REFRESH_JWT; -import static com.catcher.utils.JwtUtils.*; +import static com.catcher.utils.JwtUtils.ACCESS_TOKEN_EXPIRATION_MILLIS; +import static com.catcher.utils.JwtUtils.REFRESH_TOKEN_EXPIRATION_MILLIS; +import static com.catcher.utils.KeyGenerator.AuthType.BLACK_LIST_ACCESS_TOKEN; +import static com.catcher.utils.KeyGenerator.AuthType.REFRESH_TOKEN; +import static com.catcher.utils.KeyGenerator.generateKey; @Slf4j @Component @@ -28,15 +32,15 @@ public TokenDto reissueRefreshToken(String refreshToken) throws BaseException { Authentication authentication = jwtTokenProvider.getAuthentication(refreshToken); - String redisRefreshToken = getRefreshToken(authentication.getName()); + String redisRefreshToken = getRefreshToken(generateKey(authentication.getName(), REFRESH_TOKEN)); compareRefreshToken(refreshToken, redisRefreshToken); String newAccessToken = jwtTokenProvider.createAccessToken(authentication); String newRefreshToken = jwtTokenProvider.createRefreshToken(authentication); - dbManager.deleteKey(refreshToken); - dbManager.putValue(authentication.getName(), newRefreshToken, REFRESH_TOKEN_EXPIRATION_MILLIS); + dbManager.deleteKey(generateKey(refreshToken, REFRESH_TOKEN)); + dbManager.putValue(generateKey(authentication.getName(), REFRESH_TOKEN), newRefreshToken, REFRESH_TOKEN_EXPIRATION_MILLIS); return new TokenDto(newAccessToken, newRefreshToken); } @@ -46,11 +50,11 @@ public void discardRefreshToken(String refreshToken) { try { jwtTokenProvider.validateToken(refreshToken); Authentication authentication = jwtTokenProvider.getAuthentication(refreshToken); - Optional refreshTokenOptional = dbManager.getValue(refreshToken); + Optional refreshTokenOptional = dbManager.getValue(generateKey(refreshToken, REFRESH_TOKEN)); if (refreshTokenOptional.isPresent()) { compareRefreshToken(refreshToken, refreshTokenOptional.get()); } - dbManager.deleteKey(authentication.getName()); + dbManager.deleteKey(generateKey(authentication.getName(), REFRESH_TOKEN)); } catch (BaseException e) { log.warn("ErrorCode = {}, Message = {}", e.getStatus().getCode(), e.getStatus().getMessage()); } @@ -61,8 +65,7 @@ public void discardAccessToken(String accessToken) { try { accessToken = getAccessToken(accessToken); jwtTokenProvider.validateToken(accessToken); - String key = generateBlackListToken(accessToken); - dbManager.putValue(key, "", ACCESS_TOKEN_EXPIRATION_MILLIS); + dbManager.putValue(generateKey(accessToken, BLACK_LIST_ACCESS_TOKEN), "", ACCESS_TOKEN_EXPIRATION_MILLIS); } catch (BaseException e) { log.warn("ErrorCode = {}, Message = {}", e.getStatus().getCode(), e.getStatus().getMessage()); } @@ -75,8 +78,8 @@ private String getAccessToken(String accessToken) { return null; } - private String getRefreshToken(String name) { - return dbManager.getValue(name) + private String getRefreshToken(String key) { + return dbManager.getValue(key) .orElseThrow(() -> new BaseException(NOT_EXIST_REFRESH_JWT)); } diff --git a/src/main/java/com/catcher/resource/UserController.java b/src/main/java/com/catcher/resource/UserController.java index 3c3a5c6e..d365f27e 100644 --- a/src/main/java/com/catcher/resource/UserController.java +++ b/src/main/java/com/catcher/resource/UserController.java @@ -31,6 +31,7 @@ import static com.catcher.config.JwtTokenProvider.setRefreshCookie; import static com.catcher.utils.HttpServletUtils.deleteCookie; import static com.catcher.utils.JwtUtils.REFRESH_TOKEN_NAME; +import static com.catcher.utils.KeyGenerator.AuthType.FIND_ID; import static org.springframework.http.HttpHeaders.AUTHORIZATION; @RequiredArgsConstructor @@ -70,29 +71,28 @@ public CommonResponse logout(HttpServletRequest request, return success(); } - // TODO: 제목 교체 - @Operation(summary = "이메일 인증코드 발송") + @Operation(summary = "ID 찾기 이메일 인증코드 발송") @PostMapping("/create-authcode/email") public CommonResponse sendEmailWithAuthCode(final AuthCodeSendRequest authCodeSendRequest) { - - final var key = authCodeService.generateAndSaveRandomKey(authCodeSendRequest.getEmail()); + final var key = authCodeService.generateAndSaveRandomKey(authCodeSendRequest.getEmail(), FIND_ID); emailService.sendEmail(authCodeSendRequest.getEmail(), "title", key); + return success(); } // TODO: 응답 타입은 따로 생각해보기 - @Operation(summary = "인증 코드가 맞는지 검증") + @Operation(summary = "ID 찾기 인증 코드가 맞는지 검증") @PostMapping("/check-authcode/email") public CommonResponse verifyAuthCode(final AuthCodeVerifyRequest authCodeVerifyRequest) { - final boolean isVerified = authCodeService.verifyAuthCode(authCodeVerifyRequest.getEmail(), authCodeVerifyRequest.getAuthCode()); + final boolean isVerified = authCodeService.verifyAuthCode(authCodeVerifyRequest.getEmail(), authCodeVerifyRequest.getAuthCode(), FIND_ID); return success(new AuthCodeVerifyResponse(isVerified)); } - @Operation(summary = "캡챠 이미지 생성 및 정답 임시 저장") + @Operation(summary = "ID 찾기 캡챠 이미지 생성 및 정답 임시 저장") @PostMapping("/captcha/email") public void captchaGenerate(final CaptchaGenerateRequest captchaGenerateRequest, HttpServletResponse response) throws IOException { - Captcha captcha = captchaService.generateCaptchaAndSaveAnswer(captchaGenerateRequest.getEmail()); + Captcha captcha = captchaService.generateCaptchaAndSaveAnswer(captchaGenerateRequest.getEmail(), FIND_ID); BufferedImage image = captchaService.getImage(captcha); response.setHeader("Cache-Control", "no-store"); @@ -101,10 +101,10 @@ public void captchaGenerate(final CaptchaGenerateRequest captchaGenerateRequest, ImageIO.write(image, "png", response.getOutputStream()); } - @Operation(summary = "캡챠 이미지 정답 검증") + @Operation(summary = "ID 찾기 캡챠 이미지 정답 검증") @PostMapping("/captcha/validate/email") public CommonResponse validateCaptcha(final CaptchaValidateRequest captchaValidateRequest) { - final boolean isValidated = captchaService.validateCaptcha(captchaValidateRequest.getEmail(), captchaValidateRequest.getUserAnswer()); + final boolean isValidated = captchaService.validateCaptcha(captchaValidateRequest.getEmail(), captchaValidateRequest.getUserAnswer(), FIND_ID); return success(new CaptchaValidateResponse(isValidated)); diff --git a/src/main/java/com/catcher/utils/JwtUtils.java b/src/main/java/com/catcher/utils/JwtUtils.java index 1a5a056a..21d9e403 100644 --- a/src/main/java/com/catcher/utils/JwtUtils.java +++ b/src/main/java/com/catcher/utils/JwtUtils.java @@ -5,9 +5,5 @@ public class JwtUtils { public static long REFRESH_TOKEN_EXPIRATION_MILLIS = 1000L * 60 * 60 * 24 * 7; // 7Days public final static String REFRESH_TOKEN_NAME = "RefreshToken"; - public final static String BLACK_LIST_TOKEN = "BlackListToken"; - public final static String generateBlackListToken(String accessToken) { - return BLACK_LIST_TOKEN + "[" + accessToken + "]"; - } } From 09dc4b2df1349490e7b28d9f1780d7791be256c1 Mon Sep 17 00:00:00 2001 From: HongGeun Date: Sun, 3 Dec 2023 03:15:29 +0900 Subject: [PATCH 3/5] =?UTF-8?q?test=20code=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/catcher/core/service/AuthServiceTest.java | 5 +++-- .../java/com/catcher/core/service/UserServiceTest.java | 5 +++-- src/test/java/com/catcher/resource/UserControllerTest.java | 7 ++++--- 3 files changed, 10 insertions(+), 7 deletions(-) diff --git a/src/test/java/com/catcher/core/service/AuthServiceTest.java b/src/test/java/com/catcher/core/service/AuthServiceTest.java index 3ad8a89d..1fe7aedb 100644 --- a/src/test/java/com/catcher/core/service/AuthServiceTest.java +++ b/src/test/java/com/catcher/core/service/AuthServiceTest.java @@ -6,6 +6,7 @@ import com.catcher.core.dto.TokenDto; import com.catcher.core.dto.user.UserCreateRequest; import com.catcher.testconfiguriation.EmbeddedRedisConfiguration; +import com.catcher.utils.KeyGenerator; import jakarta.persistence.EntityManager; import jakarta.persistence.PersistenceContext; import org.junit.jupiter.api.DisplayName; @@ -19,7 +20,7 @@ import java.util.Optional; import java.util.UUID; -import static com.catcher.utils.JwtUtils.generateBlackListToken; +import static com.catcher.utils.KeyGenerator.AuthType.*; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatThrownBy; @@ -89,7 +90,7 @@ void discard_access_token() { //when authService.discardAccessToken("Bearer " + tokenDto.getAccessToken()); //then - Optional value = dbManager.getValue(generateBlackListToken(tokenDto.getAccessToken())); + Optional value = dbManager.getValue(KeyGenerator.generateKey(tokenDto.getAccessToken(), BLACK_LIST_ACCESS_TOKEN)); assertThat(value).isPresent(); } diff --git a/src/test/java/com/catcher/core/service/UserServiceTest.java b/src/test/java/com/catcher/core/service/UserServiceTest.java index be9bf862..f9880d4e 100644 --- a/src/test/java/com/catcher/core/service/UserServiceTest.java +++ b/src/test/java/com/catcher/core/service/UserServiceTest.java @@ -10,6 +10,7 @@ import com.catcher.core.dto.user.UserCreateRequest; import com.catcher.core.dto.user.UserLoginRequest; import com.catcher.testconfiguriation.EmbeddedRedisConfiguration; +import com.catcher.utils.KeyGenerator; import jakarta.persistence.EntityManager; import jakarta.persistence.PersistenceContext; import org.junit.jupiter.api.DisplayName; @@ -25,7 +26,7 @@ import java.util.UUID; import static com.catcher.core.domain.entity.enums.UserProvider.*; -import static com.catcher.utils.JwtUtils.generateBlackListToken; +import static com.catcher.utils.KeyGenerator.AuthType.*; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatThrownBy; @@ -279,7 +280,7 @@ void valid_logout() { //when userService.logout("Bearer " + tokenDto.getAccessToken(), tokenDto.getRefreshToken()); //then - Optional value = dbManager.getValue(generateBlackListToken(tokenDto.getAccessToken())); + Optional value = dbManager.getValue(KeyGenerator.generateKey(tokenDto.getAccessToken(), BLACK_LIST_ACCESS_TOKEN)); assertThat(value).isPresent(); } diff --git a/src/test/java/com/catcher/resource/UserControllerTest.java b/src/test/java/com/catcher/resource/UserControllerTest.java index c6798da0..7ed105d3 100644 --- a/src/test/java/com/catcher/resource/UserControllerTest.java +++ b/src/test/java/com/catcher/resource/UserControllerTest.java @@ -11,6 +11,7 @@ import com.catcher.core.dto.user.UserCreateRequest; import com.catcher.core.dto.user.UserLoginRequest; import com.catcher.testconfiguriation.EmbeddedRedisConfiguration; +import com.catcher.utils.KeyGenerator; import com.fasterxml.jackson.databind.ObjectMapper; import jakarta.persistence.EntityManager; import jakarta.persistence.PersistenceContext; @@ -37,7 +38,7 @@ import static com.catcher.core.domain.entity.enums.UserProvider.CATCHER; import static com.catcher.utils.JwtUtils.REFRESH_TOKEN_NAME; -import static com.catcher.utils.JwtUtils.generateBlackListToken; +import static com.catcher.utils.KeyGenerator.AuthType.*; import static org.assertj.core.api.Assertions.assertThat; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; @@ -351,7 +352,7 @@ void valid_logout() throws Exception { ).andExpect(status().isOk()); Set keys = redisTemplate.keys("*"); //then - assertThat(dbManager.getValue(generateBlackListToken(accessToken))).isPresent(); + assertThat(dbManager.getValue(KeyGenerator.generateKey(accessToken, BLACK_LIST_ACCESS_TOKEN))).isPresent(); } @Autowired RedisTemplate redisTemplate; @@ -368,7 +369,7 @@ void invalid_logout() throws Exception { .content(objectMapper.writeValueAsString(userLoginRequest)) ).andExpect(status().isOk()); //then - assertThat(dbManager.getValue(generateBlackListToken(invalidAccessToken))).isEmpty(); + assertThat(dbManager.getValue(KeyGenerator.generateKey(invalidAccessToken, BLACK_LIST_ACCESS_TOKEN))).isEmpty(); } private UserCreateRequest userCreateRequest(String username, String nickname, String phone, String email) { From 153849bfc5c1431bdd425b72d38bb549791523a8 Mon Sep 17 00:00:00 2001 From: HongGeun Date: Sun, 3 Dec 2023 15:58:00 +0900 Subject: [PATCH 4/5] resolve conversation --- src/main/java/com/catcher/core/port/KeyValueDataStorePort.java | 2 +- src/main/java/com/catcher/core/service/AuthCodeService.java | 2 +- src/main/java/com/catcher/core/service/CaptchaService.java | 2 +- .../infrastructure/adaptor/KeyValueDataStoreAdapter.java | 2 +- src/main/java/com/catcher/utils/KeyGenerator.java | 1 - 5 files changed, 4 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/catcher/core/port/KeyValueDataStorePort.java b/src/main/java/com/catcher/core/port/KeyValueDataStorePort.java index 91df4dd9..55bafcf1 100644 --- a/src/main/java/com/catcher/core/port/KeyValueDataStorePort.java +++ b/src/main/java/com/catcher/core/port/KeyValueDataStorePort.java @@ -2,7 +2,7 @@ public interface KeyValueDataStorePort { - void saveValidationCodeWithUserId(String key, String value); + void saveValidationCodeWithKey(String key, String value); String findValidationCodeWithKey(String key); diff --git a/src/main/java/com/catcher/core/service/AuthCodeService.java b/src/main/java/com/catcher/core/service/AuthCodeService.java index b347bb10..f6f3bf12 100644 --- a/src/main/java/com/catcher/core/service/AuthCodeService.java +++ b/src/main/java/com/catcher/core/service/AuthCodeService.java @@ -31,7 +31,7 @@ public String generateAndSaveRandomKey(final String email, final AuthType authTy final var user = userRepository.findByEmail(email).orElseThrow(() -> new BaseException(BaseResponseStatus.USERS_NOT_EXISTS)); final var generatedKey = String.valueOf(generateSixDigitsRandomCode()); final var generatedDataStoreKey = generateKey(user.getId(), authType); - keyValueDataStorePort.saveValidationCodeWithUserId(generatedDataStoreKey, generatedKey); + keyValueDataStorePort.saveValidationCodeWithKey(generatedDataStoreKey, generatedKey); return generatedKey; } diff --git a/src/main/java/com/catcher/core/service/CaptchaService.java b/src/main/java/com/catcher/core/service/CaptchaService.java index 8b699dab..cf5862a6 100644 --- a/src/main/java/com/catcher/core/service/CaptchaService.java +++ b/src/main/java/com/catcher/core/service/CaptchaService.java @@ -31,7 +31,7 @@ public Captcha generateCaptchaAndSaveAnswer(final String email, AuthType authTyp Captcha captcha = generateCaptcha(); final String generatedUserKey = generateKey(user.getId(), authType); - keyValueDataStorePort.saveValidationCodeWithUserId(generatedUserKey, captcha.getAnswer()); + keyValueDataStorePort.saveValidationCodeWithKey(generatedUserKey, captcha.getAnswer()); return captcha; } diff --git a/src/main/java/com/catcher/infrastructure/adaptor/KeyValueDataStoreAdapter.java b/src/main/java/com/catcher/infrastructure/adaptor/KeyValueDataStoreAdapter.java index 77b0adf0..22b1053c 100644 --- a/src/main/java/com/catcher/infrastructure/adaptor/KeyValueDataStoreAdapter.java +++ b/src/main/java/com/catcher/infrastructure/adaptor/KeyValueDataStoreAdapter.java @@ -16,7 +16,7 @@ public class KeyValueDataStoreAdapter implements KeyValueDataStorePort { private static final long THREE_MINUTES_AS_MILLISECONDS = 180000L; @Override - public void saveValidationCodeWithUserId(final String key, final String authCode) { + public void saveValidationCodeWithKey(final String key, final String authCode) { redisManager.putValue(key, authCode, THREE_MINUTES_AS_MILLISECONDS); } diff --git a/src/main/java/com/catcher/utils/KeyGenerator.java b/src/main/java/com/catcher/utils/KeyGenerator.java index d8bb2fd7..e1a95348 100644 --- a/src/main/java/com/catcher/utils/KeyGenerator.java +++ b/src/main/java/com/catcher/utils/KeyGenerator.java @@ -18,6 +18,5 @@ public enum AuthType { FIND_PASSWORD_SUCCESS, CAPTCHA_ID, CAPTCHA_PASSWORD, - } } From b989006c04cb7529019b4fe2120912ff60b4058c Mon Sep 17 00:00:00 2001 From: HongGeun Date: Sun, 3 Dec 2023 15:59:09 +0900 Subject: [PATCH 5/5] remove unused field --- src/test/java/com/catcher/resource/UserControllerTest.java | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/test/java/com/catcher/resource/UserControllerTest.java b/src/test/java/com/catcher/resource/UserControllerTest.java index 7ed105d3..8a089258 100644 --- a/src/test/java/com/catcher/resource/UserControllerTest.java +++ b/src/test/java/com/catcher/resource/UserControllerTest.java @@ -33,12 +33,11 @@ import org.springframework.web.filter.CharacterEncodingFilter; import java.time.ZonedDateTime; -import java.util.Set; import java.util.UUID; import static com.catcher.core.domain.entity.enums.UserProvider.CATCHER; import static com.catcher.utils.JwtUtils.REFRESH_TOKEN_NAME; -import static com.catcher.utils.KeyGenerator.AuthType.*; +import static com.catcher.utils.KeyGenerator.AuthType.BLACK_LIST_ACCESS_TOKEN; import static org.assertj.core.api.Assertions.assertThat; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; @@ -350,7 +349,7 @@ void valid_logout() throws Exception { .header(HttpHeaders.AUTHORIZATION, "Bearer " + accessToken) .content(objectMapper.writeValueAsString(userLoginRequest)) ).andExpect(status().isOk()); - Set keys = redisTemplate.keys("*"); + //then assertThat(dbManager.getValue(KeyGenerator.generateKey(accessToken, BLACK_LIST_ACCESS_TOKEN))).isPresent(); }