-
Notifications
You must be signed in to change notification settings - Fork 0
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]: TokenValidator와 TokenGenerator 추가로 TokenService 가독성 증대 및 패키지 경로 수정 #109
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
eed7257
[REFACTOR]: domain module 에서 exception 던지게 수정
sejineer f343a2d
[FIX]: User, Token DB 조회 삭제되지 않은 데이터만 조회하게 변경
sejineer 238ceb6
[REFACTOR]: TokenValidator의 refresh method로 토큰 검증 + 갱신을 묶어 TokenServi…
sejineer e0a3b20
[REFACTOR]: core-domain 모듈 test package 경로 수정 및 TokenValidator test 작성
sejineer 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 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
4 changes: 3 additions & 1 deletion
4
core/core-domain/src/main/kotlin/com/myongjiway/core/domain/token/TokenReader.kt
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 |
---|---|---|
@@ -1,10 +1,12 @@ | ||
package com.myongjiway.core.domain.token | ||
|
||
import com.myongjiway.core.domain.error.CoreErrorType | ||
import com.myongjiway.core.domain.error.CoreException | ||
import org.springframework.stereotype.Component | ||
|
||
@Component | ||
class TokenReader( | ||
private val tokenRepository: TokenRepository, | ||
) { | ||
fun findByToken(token: String): Token? = tokenRepository.find(token) | ||
fun find(token: String): Token = tokenRepository.find(token) ?: throw CoreException(CoreErrorType.TOKEN_NOT_FOUND) | ||
} |
26 changes: 9 additions & 17 deletions
26
core/core-domain/src/main/kotlin/com/myongjiway/core/domain/token/TokenService.kt
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 |
---|---|---|
@@ -1,41 +1,33 @@ | ||
package com.myongjiway.core.domain.token | ||
|
||
import com.myongjiway.core.domain.error.CoreErrorType | ||
import com.myongjiway.core.domain.user.UserReader | ||
import org.springframework.stereotype.Service | ||
|
||
@Service | ||
class TokenService( | ||
private val tokenAppender: TokenAppender, | ||
private val tokenGenerator: TokenGenerator, | ||
private val tokenReader: TokenReader, | ||
private val userReader: UserReader, | ||
private val tokenProcessor: TokenProcessor, | ||
) { | ||
/** | ||
* RefreshToken을 이용하여 AccessToken을 재발급한다. RefreshToken 또한 만료되었다면 갱신한다. | ||
* @param refreshData AccessToken을 재발급하기 위한 RefreshToken | ||
* @return TokenResult 재발급된 AccessToken과 RefreshToken | ||
*/ | ||
fun refresh(refreshData: RefreshData): TokenResult { | ||
var refreshToken = tokenReader.findByToken(refreshData.refreshToken) | ||
?: throw com.myongjiway.core.domain.error.CoreException(CoreErrorType.UNAUTHORIZED_TOKEN) | ||
var refreshToken = tokenReader.find(refreshData.refreshToken) | ||
|
||
val user = userReader.find(refreshToken.userId.toLong()) | ||
?: throw com.myongjiway.core.domain.error.CoreException(CoreErrorType.USER_NOT_FOUND) | ||
|
||
if (isExpired(refreshToken)) { | ||
refreshToken = tokenGenerator.generateRefreshTokenByUserId(user.id.toString()) | ||
tokenAppender.upsert(user.id, refreshToken.token, refreshToken.expiration) | ||
} | ||
refreshToken = tokenGenerator.refresh(refreshToken) | ||
|
||
val newAccessToken = tokenGenerator.generateAccessTokenByUserId(user.id.toString()) | ||
return TokenResult(newAccessToken.token, refreshToken.token) | ||
} | ||
|
||
fun delete(refreshToken: String) { | ||
val findRefreshToken = ( | ||
tokenReader.findByToken(refreshToken) | ||
?: throw com.myongjiway.core.domain.error.CoreException(CoreErrorType.NOT_FOUND_TOKEN) | ||
) | ||
|
||
tokenProcessor.deleteToken(findRefreshToken.token) | ||
val foundRefreshToken = tokenReader.find(refreshToken) | ||
tokenProcessor.deleteToken(foundRefreshToken.token) | ||
} | ||
|
||
private fun isExpired(refreshToken: Token?): Boolean = refreshToken?.expiration!! <= System.currentTimeMillis() | ||
} |
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
6 changes: 4 additions & 2 deletions
6
core/core-domain/src/main/kotlin/com/myongjiway/core/domain/user/UserReader.kt
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 |
---|---|---|
@@ -1,11 +1,13 @@ | ||
package com.myongjiway.core.domain.user | ||
|
||
import com.myongjiway.core.domain.error.CoreErrorType | ||
import com.myongjiway.core.domain.error.CoreException | ||
import org.springframework.stereotype.Component | ||
|
||
@Component | ||
class UserReader( | ||
private val userRepository: UserRepository, | ||
) { | ||
fun find(id: Long): User? = userRepository.findUserById(id) | ||
fun find(providerId: String): User? = userRepository.findUserByProviderId(providerId) | ||
fun find(id: Long): User = userRepository.findUserById(id) ?: throw CoreException(CoreErrorType.USER_NOT_FOUND) | ||
fun find(providerId: String): User = userRepository.findUserByProviderId(providerId) ?: throw CoreException(CoreErrorType.USER_NOT_FOUND) | ||
} |
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
2 changes: 1 addition & 1 deletion
2
...n/com/myongjiway/CoreDomainContextTest.kt → ...iway/core/domain/CoreDomainContextTest.kt
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
2 changes: 1 addition & 1 deletion
2
...m/myongjiway/CoreDomainTestApplication.kt → .../core/domain/CoreDomainTestApplication.kt
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
File renamed without changes.
6 changes: 1 addition & 5 deletions
6
...om/myongjiway/notice/NoticeCreatorTest.kt → ...y/core/domain/notice/NoticeCreatorTest.kt
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
3 changes: 1 addition & 2 deletions
3
...om/myongjiway/notice/NoticeDeleterTest.kt → ...y/core/domain/notice/NoticeDeleterTest.kt
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
7 changes: 1 addition & 6 deletions
7
...com/myongjiway/notice/NoticeFinderTest.kt → ...ay/core/domain/notice/NoticeFinderTest.kt
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
7 changes: 1 addition & 6 deletions
7
...om/myongjiway/notice/NoticeUpdaterTest.kt → ...y/core/domain/notice/NoticeUpdaterTest.kt
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
4 changes: 1 addition & 3 deletions
4
...com/myongjiway/token/TokenAppenderTest.kt → ...ay/core/domain/token/TokenAppenderTest.kt
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
19 changes: 11 additions & 8 deletions
19
...om/myongjiway/token/TokenGeneratorTest.kt → ...y/core/domain/token/TokenGeneratorTest.kt
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
4 changes: 1 addition & 3 deletions
4
...om/myongjiway/token/TokenProcessorTest.kt → ...y/core/domain/token/TokenProcessorTest.kt
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
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
원래 secret을 저렇게 하드코딩 하나요?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
HS512 알고리즘을 적용하려면 어느 정도 길이의 key가 필요해서 무작위로 넣었습니다. 나중에 fixture로 빼서 refactoring할 예정입니다
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
무작위가 아니라 저희 키값을 넣어놓으셨는데요 ;;
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
자세히 보면 다릅니다. 자세히 보아야 아름다워요