-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
5 changed files
with
101 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 76 additions & 0 deletions
76
src/main/java/com/_119/wepro/member/service/ReissueService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package com._119.wepro.member.service; | ||
|
||
import static com._119.wepro.global.exception.errorcode.CommonErrorCode.EXPIRED_TOKEN; | ||
import static com._119.wepro.global.exception.errorcode.CommonErrorCode.INVALID_TOKEN; | ||
import static com._119.wepro.global.exception.errorcode.CommonErrorCode.NOT_EXIST_BEARER_SUFFIX; | ||
import static com._119.wepro.global.exception.errorcode.CommonErrorCode.REFRESH_DENIED; | ||
import static com._119.wepro.global.security.constant.SecurityConstants.ACCESS_TOKEN_HEADER; | ||
import static com._119.wepro.global.security.constant.SecurityConstants.GRANT_TYPE; | ||
import static com._119.wepro.global.security.constant.SecurityConstants.REFRESH_TOKEN_HEADER; | ||
|
||
import com._119.wepro.global.dto.TokenInfo; | ||
import com._119.wepro.global.exception.RestApiException; | ||
import com._119.wepro.global.exception.errorcode.UserErrorCode; | ||
import com._119.wepro.global.security.JwtTokenProvider; | ||
import com._119.wepro.member.domain.Member; | ||
import com._119.wepro.member.domain.repository.MemberRepository; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Service; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class ReissueService { | ||
|
||
private final JwtTokenProvider jwtTokenProvider; | ||
private final MemberRepository memberRepository; | ||
|
||
public void reissue(String refreshToken, String accessToken, HttpServletResponse response) { | ||
refreshToken = extractToken(refreshToken); | ||
accessToken = extractToken(accessToken); | ||
|
||
validateAccessTokenExpired(accessToken); | ||
String providerId = jwtTokenProvider.parseExpiredToken(accessToken).getSubject(); | ||
|
||
validateRefreshToken(refreshToken, providerId); | ||
|
||
Member member = memberRepository.findByProviderId(providerId) | ||
.orElseThrow(() -> new RestApiException(UserErrorCode.USER_NOT_FOUND)); | ||
|
||
TokenInfo newTokenInfo = jwtTokenProvider.generateToken(providerId, member.getRole()); | ||
setTokenPairToResponseHeader(response, newTokenInfo.getAccessToken(), | ||
newTokenInfo.getRefreshToken()); | ||
} | ||
|
||
private String extractToken(String token) { | ||
if (!token.startsWith(GRANT_TYPE)) { | ||
throw new RestApiException(NOT_EXIST_BEARER_SUFFIX); | ||
} | ||
|
||
return token.replace(GRANT_TYPE, ""); | ||
} | ||
|
||
private void validateAccessTokenExpired(String accessToken) { | ||
try { | ||
jwtTokenProvider.validateToken(accessToken); | ||
throw new RestApiException(REFRESH_DENIED); | ||
} catch (RestApiException e) { | ||
if (e.getErrorCode() != EXPIRED_TOKEN) { | ||
throw e; | ||
} | ||
} | ||
} | ||
|
||
private void validateRefreshToken(String refreshToken, String memberId) { | ||
String savedRefreshToken = jwtTokenProvider.getRefreshToken(memberId); | ||
if (!refreshToken.equals(savedRefreshToken)) { | ||
throw new RestApiException(INVALID_TOKEN); | ||
} | ||
} | ||
|
||
private void setTokenPairToResponseHeader( | ||
HttpServletResponse response, String accessToken, String refreshToken) { | ||
response.setHeader(ACCESS_TOKEN_HEADER, GRANT_TYPE + accessToken); | ||
response.setHeader(REFRESH_TOKEN_HEADER, GRANT_TYPE + refreshToken); | ||
} | ||
} |