Skip to content

Commit

Permalink
feat: Reflect pr review
Browse files Browse the repository at this point in the history
  • Loading branch information
devmizz committed Jun 3, 2024
1 parent d81d745 commit f54b5be
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 21 deletions.
8 changes: 4 additions & 4 deletions app/api/src/main/java/org/example/filter/JWTFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import org.example.security.dto.AuthenticatedUser;
import org.example.security.dto.TokenParam;
import org.example.security.dto.UserParam;
import org.example.security.token.JWTProcessor;
import org.example.security.token.JWTHandler;
import org.example.security.token.RefreshTokenProcessor;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
Expand All @@ -23,7 +23,7 @@
@Component
public class JWTFilter extends OncePerRequestFilter {

private final JWTProcessor jwtProcessor;
private final JWTHandler jwtHandler;
private final RefreshTokenProcessor refreshTokenProcessor;

@Override
Expand All @@ -46,8 +46,8 @@ protected void doFilterInternal(
}

private void handleAccessToken(HttpServletRequest request) {
String accessToken = jwtProcessor.extractAccessToken(request);
UserParam userParam = jwtProcessor.extractUserFrom(accessToken);
String accessToken = jwtHandler.extractAccessToken(request);
UserParam userParam = jwtHandler.extractUserFrom(accessToken);
saveOnSecurityContextHolder(userParam);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

@Component
@RequiredArgsConstructor
public class JWTProcessor {
public class JWTHandler {

private final TokenProperty tokenProperty;

Expand Down Expand Up @@ -53,13 +53,6 @@ public UUID getUserIdFromExpiredToken(String token) {
throw new BusinessException(TokenError.UNEXPIRED_TOKEN);
}

private Jwt<?, ?> parseToken(String token) {
return Jwts.parser()
.verifyWith(tokenProperty.getBASE64URLSecretKey())
.build()
.parse(token);
}

private Object parsePayload(String token) {
try {
return parseToken(token)
Expand All @@ -71,6 +64,13 @@ private Object parsePayload(String token) {
}
}

private Jwt<?, ?> parseToken(String token) {
return Jwts.parser()
.verifyWith(tokenProperty.getBASE64URLSecretKey())
.build()
.parse(token);
}

private UserParam convertPayloadToUserParam(Object payload) {
try {
return UserParam.fromPayload(payload);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@
@RequiredArgsConstructor
public class RefreshTokenProcessor {

private final JWTProcessor jwtProcessor;
private final JWTHandler jwtHandler;
private final JWTGenerator jwtGenerator;

public TokenParam process(HttpServletRequest request, HttpServletResponse response) {
String accessToken = jwtProcessor.extractAccessToken(request);
String refreshToken = jwtProcessor.extractRefreshToken(request);
String accessToken = jwtHandler.extractAccessToken(request);
String refreshToken = jwtHandler.extractRefreshToken(request);

return new TokenParam(accessToken, refreshToken);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

@DisplayName("JWT 생성 테스트")
class JWTGeneratorTest {

long hour = 3600000L;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,15 @@
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

class JWTProcessorTest {
@DisplayName("JWT 처리 테스트")
class JWTHandlerTest {

TokenProperty tokenProperty = new TokenProperty(
"ahRhwlglftmrkwkfehlaussksmsdjraksrmadmfqjfrjtdlrhdkwnwlflsmswlqdptjgodqhrgkrptkftndlTDmfrjtdlek",
3600000L,
1209600000L
);
JWTProcessor jwtProcessor = new JWTProcessor(tokenProperty);
JWTHandler jwtHandler = new JWTHandler(tokenProperty);
JWTGenerator jwtGenerator = new JWTGenerator(tokenProperty);
UserParam userParam = new UserParam(
UUID.randomUUID(),
Expand All @@ -32,7 +33,7 @@ class JWTProcessorTest {
@DisplayName("토큰의 claim을 추출하면 유저 정보가 반환된다.")
void extractClaim() {
TokenParam token = jwtGenerator.generate(userParam, new Date());
UserParam parsedUserParam = jwtProcessor.extractUserFrom(token.accessToken());
UserParam parsedUserParam = jwtHandler.extractUserFrom(token.accessToken());

assertThat(parsedUserParam.userId()).isEqualTo(userParam.userId());
}
Expand All @@ -45,7 +46,7 @@ void makeExceptionParsingExpiredToken() {
new Date(new Date().getTime() - tokenProperty.accessTokenExpirationSeconds())
).accessToken();

assertThatThrownBy(() -> jwtProcessor.extractUserFrom(expiredAccessToken))
assertThatThrownBy(() -> jwtHandler.extractUserFrom(expiredAccessToken))
.isInstanceOf(BusinessException.class)
.hasMessage(TokenError.EXPIRED_TOKEN.getClientMessage());
}
Expand All @@ -55,7 +56,7 @@ void makeExceptionParsingExpiredToken() {
void makeExceptionParsingInvalidToken() {
String invalidAccessToken = "invalidToken";

assertThatThrownBy(() -> jwtProcessor.extractUserFrom(invalidAccessToken))
assertThatThrownBy(() -> jwtHandler.extractUserFrom(invalidAccessToken))
.isInstanceOf(BusinessException.class)
.hasMessage(TokenError.INVALID_TOKEN.getClientMessage());
}
Expand All @@ -68,7 +69,7 @@ void extractUserIdFromExpiredToken() {
new Date(new Date().getTime() - tokenProperty.accessTokenExpirationSeconds())
).accessToken();

UUID extractedUserIdFromExpiredToken = jwtProcessor.getUserIdFromExpiredToken(expiredAccessToken);
UUID extractedUserIdFromExpiredToken = jwtHandler.getUserIdFromExpiredToken(expiredAccessToken);
assertThat(extractedUserIdFromExpiredToken).isEqualTo(userParam.userId());
}
}

0 comments on commit f54b5be

Please sign in to comment.