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

Refactor/QFEED-167-Redis-refactor #104

Merged
merged 2 commits into from
Jan 15, 2025
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
@@ -0,0 +1,19 @@
package com.wsws.moduleinfra.authcontext.auth.dto;

import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;

import java.time.LocalDateTime;

@Getter
@NoArgsConstructor
@AllArgsConstructor
public class RefreshTokenData {

private String token;

@JsonFormat(pattern = "yyyy-MM-dd'T'HH:mm:ss")
private LocalDateTime expiryDate;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.wsws.moduleinfra.authcontext.auth.exception;

import com.wsws.modulecommon.exception.BaseErrorCode;
import com.wsws.modulecommon.exception.ErrorInfo;
import lombok.RequiredArgsConstructor;

import static com.wsws.modulecommon.constants.ErrorCodeConstants.INTERNAL_SERVER;

@RequiredArgsConstructor
public enum AuthInfraErrorCode implements BaseErrorCode {

INVALID_REFRESH_TOKEN_DATA(INTERNAL_SERVER, "AUTH_INFRA_500_1", "RefreshTokenData๊ฐ€ ์œ ํšจํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.");

private final Integer status;
private final String errorCode;
private final String message;

@Override
public ErrorInfo getErrorInfo() {
return ErrorInfo.of(status, errorCode, message);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.wsws.moduleinfra.authcontext.auth.exception;

import com.wsws.modulecommon.exception.InfraException;

public class InvalidRefreshTokenDataException extends InfraException {

public static final InvalidRefreshTokenDataException EXCEPTION = new InvalidRefreshTokenDataException();

private InvalidRefreshTokenDataException() {
super(AuthInfraErrorCode.INVALID_REFRESH_TOKEN_DATA);
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
package com.wsws.moduleinfra.authcontext.auth.repository;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.wsws.moduledomain.authcontext.auth.RefreshToken;
import com.wsws.moduledomain.authcontext.auth.repo.AuthRepository;
import com.wsws.moduleinfra.authcontext.auth.dto.RefreshTokenData;
import com.wsws.moduleinfra.authcontext.auth.exception.InvalidRefreshTokenDataException;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Repository;


import java.io.IOException;
import java.util.Optional;
import java.util.concurrent.TimeUnit;

Expand All @@ -17,8 +21,12 @@ public class RedisAuthRepository implements AuthRepository {

private final RedisTemplate<String, String> redisTemplate;

public RedisAuthRepository(@Qualifier("customRedisTemplateString") RedisTemplate<String, String> redisTemplate) {
private final ObjectMapper objectMapper;


public RedisAuthRepository(@Qualifier("customRedisTemplateString") RedisTemplate<String, String> redisTemplate, ObjectMapper objectMapper) {
this.redisTemplate = redisTemplate;
this.objectMapper = objectMapper;
}

private String createRedisKey(String token) {
Expand All @@ -29,17 +37,36 @@ private String createRedisKey(String token) {
@Override
public Optional<RefreshToken> findByToken(String token) {
String key = createRedisKey(token);
String storedToken = redisTemplate.opsForValue().get(key);
if (storedToken == null) {
String storedValue = redisTemplate.opsForValue().get(key);
if (storedValue == null) {
return Optional.empty();
}
return Optional.of(RefreshToken.create(storedToken, null)); // Expiry๋Š” AuthService์—์„œ ๊ด€๋ฆฌ


try {
RefreshTokenData data = objectMapper.readValue(storedValue, RefreshTokenData.class);
RefreshToken refreshToken = RefreshToken.create(data.getToken(), data.getExpiryDate());
return Optional.of(refreshToken);
} catch (IOException e) {
throw InvalidRefreshTokenDataException.EXCEPTION;
}
}

@Override
public void save(RefreshToken refreshToken) {
String key = createRedisKey(refreshToken.getToken());
redisTemplate.opsForValue().set(key, refreshToken.getToken(), 7, TimeUnit.DAYS);

RefreshTokenData data = new RefreshTokenData(
refreshToken.getToken(),
refreshToken.getExpiryDate()
);

try {
String json = objectMapper.writeValueAsString(data);
redisTemplate.opsForValue().set(key, json, 7, TimeUnit.DAYS);
} catch (IOException e) {
throw InvalidRefreshTokenDataException.EXCEPTION;
}
}

@Override
Expand Down
Loading