Skip to content

Commit

Permalink
feat/#507: 중복 코드 제거 및 reissueToken 메소드 리팩토링
Browse files Browse the repository at this point in the history
  • Loading branch information
LJH098 authored and hwangdaesun committed Oct 9, 2024
1 parent b03065e commit 99bb59d
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 32 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.gaebaljip.exceed.application.service.auth;

import com.gaebaljip.exceed.common.EatCeedStaticMessage;
import com.gaebaljip.exceed.adapter.out.redis.RedisAdapter;
import com.gaebaljip.exceed.common.exception.auth.NotFoundRefreshTokenException;
import com.gaebaljip.exceed.common.security.domain.JwtResolver;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
Expand All @@ -9,6 +11,7 @@
import com.gaebaljip.exceed.application.domain.member.MemberEntity;
import com.gaebaljip.exceed.application.port.in.auth.AuthUsecase;
import com.gaebaljip.exceed.application.port.out.member.MemberPort;
import com.gaebaljip.exceed.common.EatCeedStaticMessage;
import com.gaebaljip.exceed.common.dto.HttpRequestDTO;
import com.gaebaljip.exceed.common.dto.TokenDTO;
import com.gaebaljip.exceed.common.exception.auth.PasswordMismatchException;
Expand All @@ -25,6 +28,8 @@ public class AuthService implements AuthUsecase {
private final MemberPort memberPort;
private final BCryptPasswordEncoder bCryptPasswordEncoder;
private final JwtManager jwtManager;
private final JwtResolver jwtResolver;
private final RedisAdapter redisAdapter;

@Override
public TokenDTO execute(LoginRequest request) {
Expand All @@ -37,15 +42,35 @@ public TokenDTO execute(LoginRequest request) {
.accessToken(jwtManager.generateAccessToken(member.getId()))
.refreshToken(jwtManager.generateRefreshToken(member.getId()))
.build();
jwtManager.saveRefreshToken(EatCeedStaticMessage.REDIS_REFRESH_TOKEN_KEY + member.getId().toString(), tokenDTO.refreshToken());
jwtManager.saveRefreshToken(
EatCeedStaticMessage.REDIS_REFRESH_TOKEN_KEY + member.getId().toString(),
tokenDTO.refreshToken());
return tokenDTO;
}

@Override
public TokenDTO reIssueToken(
String accessToken, String refreshToken, HttpRequestDTO requestDTO) {
if (jwtManager.validateRefreshToken(refreshToken, requestDTO)) {
return jwtManager.reissueToken(accessToken);
return reissueToken(accessToken);
}
throw InvalidJwtException.EXECPTION;
}

private TokenDTO reissueToken(String requestAccessToken) {
String accessToken = jwtResolver.extractToken(requestAccessToken);
String accessTokenMemberId = jwtResolver.getMemberIdFromToken(accessToken);
String refreshToken =
redisAdapter
.query(EatCeedStaticMessage.REDIS_REFRESH_TOKEN_KEY + accessTokenMemberId)
.orElseThrow(() -> NotFoundRefreshTokenException.EXECPTION);
String refreshTokenMemberId = jwtResolver.getMemberIdFromToken(refreshToken);

if (accessTokenMemberId.equals(refreshTokenMemberId)) {
return TokenDTO.builder()
.accessToken(jwtManager.generateAccessToken(Long.parseLong(accessTokenMemberId)))
.refreshToken(jwtManager.generateRefreshToken(Long.parseLong(refreshTokenMemberId)))
.build();
}
throw InvalidJwtException.EXECPTION;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@

import javax.servlet.http.HttpServletRequest;

import com.gaebaljip.exceed.common.EatCeedStaticMessage;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.core.AuthenticationException;
import org.springframework.stereotype.Component;

import com.gaebaljip.exceed.adapter.out.redis.RedisAdapter;
import com.gaebaljip.exceed.common.EatCeedStaticMessage;
import com.gaebaljip.exceed.common.dto.HttpRequestDTO;
import com.gaebaljip.exceed.common.dto.TokenDTO;
import com.gaebaljip.exceed.common.exception.auth.NotFoundRefreshTokenException;
Expand All @@ -27,7 +27,7 @@
@Component
@Slf4j
public class JwtManager {
private static final long ACCESS_TOKEN_EXPIRE_TIME = 1000 * 60; // 3일
private static final long ACCESS_TOKEN_EXPIRE_TIME = 1000 * 60 * 30; // 30분
private static final long REFRESH_TOKEN_EXPIRE_TIME = 1000 * 60 * 60 * 24 * 7; // 7일
private final Key key;
private RedisAdapter redisAdapter;
Expand Down Expand Up @@ -163,29 +163,4 @@ public boolean validateRefreshToken(String refreshToken, HttpRequestDTO requestD
public void saveRefreshToken(String memberId, String refreshToken) {
redisAdapter.saveWithExpiration(memberId, refreshToken, REFRESH_TOKEN_EXPIRE_TIME);
}

public Claims parseClaims(String Token) {
try {
return Jwts.parserBuilder().setSigningKey(key).build().parseClaimsJws(Token).getBody();
} catch (io.jsonwebtoken.ExpiredJwtException e) {
return e.getClaims();
}
}

public TokenDTO reissueToken(String accessToken) {
String accessTokenMemberId = parseClaims(accessToken).getSubject();
String refreshToken =
redisAdapter
.query(EatCeedStaticMessage.REDIS_REFRESH_TOKEN_KEY + accessTokenMemberId)
.orElseThrow(() -> NotFoundRefreshTokenException.EXECPTION);
String refreshTokenMemberId = parseClaims(refreshToken).getSubject();

if (accessTokenMemberId.equals(refreshTokenMemberId)) {
return TokenDTO.builder()
.accessToken(generateAccessToken(Long.parseLong(accessTokenMemberId)))
.refreshToken(generateRefreshToken(Long.parseLong(refreshTokenMemberId)))
.build();
}
throw InvalidJwtException.EXECPTION;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,12 @@ public String getMemberIdFromToken(String token) {
return parseClaims(token).getSubject();
}

private Claims parseClaims(String Token) {
return Jwts.parserBuilder().setSigningKey(key).build().parseClaimsJws(Token).getBody();
public Claims parseClaims(String Token) {
try {
return Jwts.parserBuilder().setSigningKey(key).build().parseClaimsJws(Token).getBody();
} catch (io.jsonwebtoken.ExpiredJwtException e) {
return e.getClaims();
}
}

public String extractToken(String bearerToken) {
Expand Down

0 comments on commit 99bb59d

Please sign in to comment.