-
Notifications
You must be signed in to change notification settings - Fork 0
feat: 중복 로그인 차단 정책 추가 #78
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
45 changes: 45 additions & 0 deletions
45
src/main/java/com/gpt/geumpumtabackend/global/oauth/service/OAuthLoginPolicyService.java
This file contains hidden or 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,45 @@ | ||
| package com.gpt.geumpumtabackend.global.oauth.service; | ||
|
|
||
| import com.gpt.geumpumtabackend.global.jwt.JwtHandler; | ||
| import com.gpt.geumpumtabackend.global.jwt.JwtUserClaim; | ||
| import com.gpt.geumpumtabackend.global.exception.BusinessException; | ||
| import com.gpt.geumpumtabackend.global.exception.ExceptionType; | ||
| import com.gpt.geumpumtabackend.token.domain.RefreshToken; | ||
| import com.gpt.geumpumtabackend.token.domain.Token; | ||
| import com.gpt.geumpumtabackend.token.repository.RefreshTokenRepository; | ||
| import com.gpt.geumpumtabackend.user.repository.UserRepository; | ||
| import lombok.RequiredArgsConstructor; | ||
| import org.springframework.stereotype.Service; | ||
| import org.springframework.transaction.annotation.Transactional; | ||
|
|
||
| import java.time.LocalDateTime; | ||
| import java.time.ZoneId; | ||
| import java.util.Optional; | ||
|
|
||
| @Service | ||
| @RequiredArgsConstructor | ||
| public class OAuthLoginPolicyService { | ||
|
|
||
| private static final ZoneId KST = ZoneId.of("Asia/Seoul"); | ||
|
|
||
| private final UserRepository userRepository; | ||
| private final RefreshTokenRepository refreshTokenRepository; | ||
| private final JwtHandler jwtHandler; | ||
|
|
||
| @Transactional | ||
| public Optional<Token> issueTokenIfNoActiveSession(JwtUserClaim jwtUserClaim) { | ||
| userRepository.findByIdForUpdate(jwtUserClaim.userId()) | ||
| .orElseThrow(() -> new BusinessException(ExceptionType.USER_NOT_FOUND)); | ||
|
|
||
| Optional<RefreshToken> existingToken = refreshTokenRepository.findByUserId(jwtUserClaim.userId()); | ||
| if (existingToken.isPresent()) { | ||
| LocalDateTime now = LocalDateTime.now(KST); | ||
| if (existingToken.get().getExpiredAt().isAfter(now)) { | ||
| return Optional.empty(); | ||
| } | ||
| refreshTokenRepository.deleteByUserId(jwtUserClaim.userId()); | ||
| } | ||
|
|
||
| return Optional.of(jwtHandler.createTokens(jwtUserClaim)); | ||
| } | ||
| } | ||
This file contains hidden or 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
102 changes: 102 additions & 0 deletions
102
src/test/java/com/gpt/geumpumtabackend/unit/oauth/service/OAuthLoginPolicyServiceTest.java
This file contains hidden or 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,102 @@ | ||
| package com.gpt.geumpumtabackend.unit.oauth.service; | ||
|
|
||
| import com.gpt.geumpumtabackend.global.exception.BusinessException; | ||
| import com.gpt.geumpumtabackend.global.jwt.JwtHandler; | ||
| import com.gpt.geumpumtabackend.global.jwt.JwtUserClaim; | ||
| import com.gpt.geumpumtabackend.global.oauth.service.OAuthLoginPolicyService; | ||
| import com.gpt.geumpumtabackend.token.domain.RefreshToken; | ||
| import com.gpt.geumpumtabackend.token.domain.Token; | ||
| import com.gpt.geumpumtabackend.token.repository.RefreshTokenRepository; | ||
| import com.gpt.geumpumtabackend.user.domain.User; | ||
| import com.gpt.geumpumtabackend.user.domain.UserRole; | ||
| import com.gpt.geumpumtabackend.user.repository.UserRepository; | ||
| import org.junit.jupiter.api.BeforeEach; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.junit.jupiter.api.extension.ExtendWith; | ||
| import org.mockito.Mock; | ||
| import org.mockito.junit.jupiter.MockitoExtension; | ||
|
|
||
| import java.util.Optional; | ||
|
|
||
| import static org.assertj.core.api.Assertions.assertThat; | ||
| import static org.assertj.core.api.Assertions.assertThatThrownBy; | ||
| import static org.mockito.Mockito.*; | ||
|
|
||
| @ExtendWith(MockitoExtension.class) | ||
| class OAuthLoginPolicyServiceTest { | ||
kon28289 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| @Mock | ||
| private UserRepository userRepository; | ||
|
|
||
| @Mock | ||
| private RefreshTokenRepository refreshTokenRepository; | ||
|
|
||
| @Mock | ||
| private JwtHandler jwtHandler; | ||
|
|
||
| private OAuthLoginPolicyService oAuthLoginPolicyService; | ||
|
|
||
| @BeforeEach | ||
| void setUp() { | ||
| oAuthLoginPolicyService = new OAuthLoginPolicyService(userRepository, refreshTokenRepository, jwtHandler); | ||
| } | ||
|
|
||
| @Test | ||
| void 활성_세션이_있으면_토큰_발급을_차단한다() { | ||
| Long userId = 1L; | ||
| JwtUserClaim claim = new JwtUserClaim(userId, UserRole.USER, false); | ||
| RefreshToken activeToken = RefreshToken.builder() | ||
| .userId(userId) | ||
| .refreshToken("active-token") | ||
| .times(3600L) | ||
| .build(); | ||
|
|
||
| when(userRepository.findByIdForUpdate(userId)).thenReturn(Optional.of(mock(User.class))); | ||
| when(refreshTokenRepository.findByUserId(userId)).thenReturn(Optional.of(activeToken)); | ||
|
|
||
| Optional<Token> result = oAuthLoginPolicyService.issueTokenIfNoActiveSession(claim); | ||
|
|
||
| assertThat(result).isEmpty(); | ||
| verify(jwtHandler, never()).createTokens(any()); | ||
| verify(refreshTokenRepository, never()).deleteByUserId(userId); | ||
| } | ||
|
|
||
| @Test | ||
| void 만료된_토큰만_있으면_삭제후_새_토큰을_발급한다() { | ||
| Long userId = 1L; | ||
| JwtUserClaim claim = new JwtUserClaim(userId, UserRole.USER, false); | ||
| RefreshToken expiredToken = RefreshToken.builder() | ||
| .userId(userId) | ||
| .refreshToken("expired-token") | ||
| .times(-1L) | ||
| .build(); | ||
| Token issuedToken = Token.builder() | ||
| .accessToken("new-access") | ||
| .refreshToken("new-refresh") | ||
| .build(); | ||
|
|
||
| when(userRepository.findByIdForUpdate(userId)).thenReturn(Optional.of(mock(User.class))); | ||
| when(refreshTokenRepository.findByUserId(userId)).thenReturn(Optional.of(expiredToken)); | ||
| when(jwtHandler.createTokens(claim)).thenReturn(issuedToken); | ||
|
|
||
| Optional<Token> result = oAuthLoginPolicyService.issueTokenIfNoActiveSession(claim); | ||
|
|
||
| assertThat(result).contains(issuedToken); | ||
| verify(refreshTokenRepository).deleteByUserId(userId); | ||
| verify(jwtHandler).createTokens(claim); | ||
| } | ||
|
|
||
| @Test | ||
| void 사용자_락_대상_없으면_예외를_던진다() { | ||
| Long userId = 1L; | ||
| JwtUserClaim claim = new JwtUserClaim(userId, UserRole.USER, false); | ||
|
|
||
| when(userRepository.findByIdForUpdate(userId)).thenReturn(Optional.empty()); | ||
|
|
||
| assertThatThrownBy(() -> oAuthLoginPolicyService.issueTokenIfNoActiveSession(claim)) | ||
| .isInstanceOf(BusinessException.class); | ||
|
|
||
| verify(refreshTokenRepository, never()).findByUserId(anyLong()); | ||
| verify(jwtHandler, never()).createTokens(any()); | ||
| } | ||
kon28289 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.