From 1c1ab811d21304bfe64be120b19cce641b9b65dd Mon Sep 17 00:00:00 2001 From: seeunjang Date: Thu, 23 May 2024 01:48:27 +0900 Subject: [PATCH 1/8] =?UTF-8?q?refactor=20:=20AuthHandler=20=EC=BB=B4?= =?UTF-8?q?=ED=8F=AC=EB=84=8C=ED=8A=B8=20=EB=B0=A9=EC=8B=9D=EC=9C=BC?= =?UTF-8?q?=EB=A1=9C=20=EC=88=98=EC=A0=95(#145)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../service/handler/AuthHandlerManager.kt | 4 +++- .../config/AuthHandlerConfiguration.kt | 19 ------------------- 2 files changed, 3 insertions(+), 20 deletions(-) delete mode 100644 Api-Module/src/main/kotlin/com/bamyanggang/apimodule/domain/user/application/service/handler/config/AuthHandlerConfiguration.kt diff --git a/Api-Module/src/main/kotlin/com/bamyanggang/apimodule/domain/user/application/service/handler/AuthHandlerManager.kt b/Api-Module/src/main/kotlin/com/bamyanggang/apimodule/domain/user/application/service/handler/AuthHandlerManager.kt index 376cbc6c..21166c14 100644 --- a/Api-Module/src/main/kotlin/com/bamyanggang/apimodule/domain/user/application/service/handler/AuthHandlerManager.kt +++ b/Api-Module/src/main/kotlin/com/bamyanggang/apimodule/domain/user/application/service/handler/AuthHandlerManager.kt @@ -2,7 +2,9 @@ package com.bamyanggang.apimodule.domain.user.application.service.handler import com.bamyanggang.apimodule.domain.user.application.exception.AuthException import com.bamyanggang.domainmodule.domain.user.enums.SocialLoginProvider +import org.springframework.stereotype.Component +@Component class AuthHandlerManager( private val kakaoOAuthHandler: AuthHandler, private val googleOAuthHandler: AuthHandler @@ -14,4 +16,4 @@ class AuthHandlerManager( else -> throw AuthException.OAuthFailed() } } -} \ No newline at end of file +} diff --git a/Api-Module/src/main/kotlin/com/bamyanggang/apimodule/domain/user/application/service/handler/config/AuthHandlerConfiguration.kt b/Api-Module/src/main/kotlin/com/bamyanggang/apimodule/domain/user/application/service/handler/config/AuthHandlerConfiguration.kt deleted file mode 100644 index 02687068..00000000 --- a/Api-Module/src/main/kotlin/com/bamyanggang/apimodule/domain/user/application/service/handler/config/AuthHandlerConfiguration.kt +++ /dev/null @@ -1,19 +0,0 @@ -package com.bamyanggang.apimodule.domain.user.application.service.handler.config - -import com.bamyanggang.apimodule.domain.user.application.service.handler.AuthHandlerManager -import com.bamyanggang.apimodule.domain.user.application.service.handler.impl.GoogleOAuthHandler -import com.bamyanggang.apimodule.domain.user.application.service.handler.impl.KakaoOAuthHandler -import org.springframework.context.annotation.Bean -import org.springframework.context.annotation.Configuration - -@Configuration -class AuthHandlerConfiguration( - private val kakaoOAuthHandler: KakaoOAuthHandler, - private val googleOAuthHandler: GoogleOAuthHandler -) { - - @Bean - fun authHandlerManager(): AuthHandlerManager { - return AuthHandlerManager(kakaoOAuthHandler, googleOAuthHandler) - } -} \ No newline at end of file From 9fe20856e57a5adbd70bdea8b7fb1442f92ec2fe Mon Sep 17 00:00:00 2001 From: seeunjang Date: Thu, 23 May 2024 01:48:58 +0900 Subject: [PATCH 2/8] =?UTF-8?q?refactor=20:=20=ED=86=A0=ED=81=B0=20?= =?UTF-8?q?=EB=8F=84=EB=A9=94=EC=9D=B8=20update=20=EB=A1=9C=EC=A7=81=20?= =?UTF-8?q?=EC=B6=94=EA=B0=80(#145)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../domainmodule/domain/user/aggregate/Token.kt | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/Domain-Module/src/main/kotlin/com/bamyanggang/domainmodule/domain/user/aggregate/Token.kt b/Domain-Module/src/main/kotlin/com/bamyanggang/domainmodule/domain/user/aggregate/Token.kt index 68f8193b..137245a3 100644 --- a/Domain-Module/src/main/kotlin/com/bamyanggang/domainmodule/domain/user/aggregate/Token.kt +++ b/Domain-Module/src/main/kotlin/com/bamyanggang/domainmodule/domain/user/aggregate/Token.kt @@ -5,7 +5,7 @@ import com.example.uuid.UuidCreator import java.time.LocalDateTime import java.util.* -class Token( +data class Token( override val id: UUID = UuidCreator.create(), val userId: UUID, val value: String, @@ -13,6 +13,14 @@ class Token( val updatedAt: LocalDateTime, ): DomainEntity { + fun update(userId: UUID, refreshToken: String): Token { + return copy( + userId = userId, + value = refreshToken, + updatedAt = LocalDateTime.now() + ) + } + companion object { fun create( userId: UUID, From bdfef727b9839fd6e7085eb8f252324900b9a87a Mon Sep 17 00:00:00 2001 From: seeunjang Date: Thu, 23 May 2024 01:49:13 +0900 Subject: [PATCH 3/8] =?UTF-8?q?refactor=20:=20=ED=86=A0=ED=81=B0=20?= =?UTF-8?q?=EC=A0=80=EC=9E=A5=20=EB=A1=9C=EC=A7=81=20=EC=88=98=EC=A0=95(#1?= =?UTF-8?q?45)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../domainmodule/domain/user/service/TokenAppender.kt | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/Domain-Module/src/main/kotlin/com/bamyanggang/domainmodule/domain/user/service/TokenAppender.kt b/Domain-Module/src/main/kotlin/com/bamyanggang/domainmodule/domain/user/service/TokenAppender.kt index 5f0ccb43..e3b47f02 100644 --- a/Domain-Module/src/main/kotlin/com/bamyanggang/domainmodule/domain/user/service/TokenAppender.kt +++ b/Domain-Module/src/main/kotlin/com/bamyanggang/domainmodule/domain/user/service/TokenAppender.kt @@ -10,11 +10,15 @@ class TokenAppender ( fun appendToken( userId: UUID, refreshToken: String, - ): Token { - return Token.create( + ) { + val existingToken = tokenRepository.findByUserId(userId) + existingToken?.let { + it.update(userId = userId, refreshToken = refreshToken) + tokenRepository.save(it) + } ?: Token.create( userId = userId, value = refreshToken ).also { tokenRepository.save(it) } - } + } From 772c3f7abf3b12c93402c9f99895ff6649a09e71 Mon Sep 17 00:00:00 2001 From: seeunjang Date: Thu, 23 May 2024 01:50:03 +0900 Subject: [PATCH 4/8] =?UTF-8?q?feat=20:=20=ED=86=A0=ED=81=B0=20=EC=88=98?= =?UTF-8?q?=EC=A0=95=20domain=20service=20=EC=83=9D=EC=84=B1,=20=EB=B9=88?= =?UTF-8?q?=20=EB=93=B1=EB=A1=9D(#145)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../user/config/UserServiceConfiguration.kt | 4 +-- .../domain/user/service/TokenModifier.kt | 25 +++++++++++++++++++ 2 files changed, 27 insertions(+), 2 deletions(-) create mode 100644 Domain-Module/src/main/kotlin/com/bamyanggang/domainmodule/domain/user/service/TokenModifier.kt diff --git a/Api-Module/src/main/kotlin/com/bamyanggang/apimodule/domain/user/config/UserServiceConfiguration.kt b/Api-Module/src/main/kotlin/com/bamyanggang/apimodule/domain/user/config/UserServiceConfiguration.kt index 802826bd..2fd2711e 100644 --- a/Api-Module/src/main/kotlin/com/bamyanggang/apimodule/domain/user/config/UserServiceConfiguration.kt +++ b/Api-Module/src/main/kotlin/com/bamyanggang/apimodule/domain/user/config/UserServiceConfiguration.kt @@ -18,8 +18,8 @@ class UserServiceConfiguration( } @Bean - fun tokenReader(): TokenReader { - return TokenReader(tokenRepository) + fun tokenModifier(): TokenModifier { + return TokenModifier(tokenRepository) } @Bean diff --git a/Domain-Module/src/main/kotlin/com/bamyanggang/domainmodule/domain/user/service/TokenModifier.kt b/Domain-Module/src/main/kotlin/com/bamyanggang/domainmodule/domain/user/service/TokenModifier.kt new file mode 100644 index 00000000..a7cbddeb --- /dev/null +++ b/Domain-Module/src/main/kotlin/com/bamyanggang/domainmodule/domain/user/service/TokenModifier.kt @@ -0,0 +1,25 @@ +package com.bamyanggang.domainmodule.domain.user.service + +import com.bamyanggang.domainmodule.domain.user.aggregate.Token +import com.bamyanggang.domainmodule.domain.user.repository.TokenRepository +import java.util.* + +class TokenModifier( + private val tokenRepository: TokenRepository +) { + + fun modifyToken( + userId: UUID, + refreshToken: String, + newRefreshToken: String + ): Token { + return tokenRepository + .findByValue(refreshToken) + .update( + userId = userId, + refreshToken = newRefreshToken + ) + .also { tokenRepository.save(it) } + } + +} From 8430d6acbf902a745152b2f40362390b7562daa7 Mon Sep 17 00:00:00 2001 From: seeunjang Date: Thu, 23 May 2024 01:50:20 +0900 Subject: [PATCH 5/8] =?UTF-8?q?refactor=20:=20=ED=86=A0=ED=81=B0=20?= =?UTF-8?q?=EC=A1=B0=ED=9A=8C=20domain=20service=20=EC=A0=9C=EA=B1=B0(#145?= =?UTF-8?q?)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../domain/user/service/TokenReader.kt | 15 --------------- 1 file changed, 15 deletions(-) delete mode 100644 Domain-Module/src/main/kotlin/com/bamyanggang/domainmodule/domain/user/service/TokenReader.kt diff --git a/Domain-Module/src/main/kotlin/com/bamyanggang/domainmodule/domain/user/service/TokenReader.kt b/Domain-Module/src/main/kotlin/com/bamyanggang/domainmodule/domain/user/service/TokenReader.kt deleted file mode 100644 index 468784cd..00000000 --- a/Domain-Module/src/main/kotlin/com/bamyanggang/domainmodule/domain/user/service/TokenReader.kt +++ /dev/null @@ -1,15 +0,0 @@ -package com.bamyanggang.domainmodule.domain.user.service - -import com.bamyanggang.domainmodule.domain.user.aggregate.Token -import com.bamyanggang.domainmodule.domain.user.repository.TokenRepository - -class TokenReader( - private val tokenRepository: TokenRepository -) { - fun readToken( - refreshToken: String - ): Token { - return tokenRepository.findByValue(refreshToken) - } - -} From f4d264706c38c03fea5fa00e6292c767ee1abf71 Mon Sep 17 00:00:00 2001 From: seeunjang Date: Thu, 23 May 2024 01:50:35 +0900 Subject: [PATCH 6/8] =?UTF-8?q?refactor=20:=20=ED=86=A0=ED=81=B0=20?= =?UTF-8?q?=EC=82=AD=EC=A0=9C=20domain=20service=20=EB=A1=9C=EC=A7=81=20?= =?UTF-8?q?=EC=88=98=EC=A0=95(#145)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../domainmodule/domain/user/service/TokenRemover.kt | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Domain-Module/src/main/kotlin/com/bamyanggang/domainmodule/domain/user/service/TokenRemover.kt b/Domain-Module/src/main/kotlin/com/bamyanggang/domainmodule/domain/user/service/TokenRemover.kt index 38da3f33..dd5e1397 100644 --- a/Domain-Module/src/main/kotlin/com/bamyanggang/domainmodule/domain/user/service/TokenRemover.kt +++ b/Domain-Module/src/main/kotlin/com/bamyanggang/domainmodule/domain/user/service/TokenRemover.kt @@ -7,8 +7,9 @@ class TokenRemover( private val tokenRepository: TokenRepository ) { fun removeToken( - token: Token + token: String ) { tokenRepository.deleteByValue(token) } + } From cb815772dc55f753ba4ed4049d9696d9b8b9a459 Mon Sep 17 00:00:00 2001 From: seeunjang Date: Thu, 23 May 2024 01:50:55 +0900 Subject: [PATCH 7/8] =?UTF-8?q?refactor=20:=20AuthService=20=EB=A1=9C?= =?UTF-8?q?=EC=A7=81=20=EC=88=98=EC=A0=95(#145)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../user/application/service/AuthService.kt | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/Api-Module/src/main/kotlin/com/bamyanggang/apimodule/domain/user/application/service/AuthService.kt b/Api-Module/src/main/kotlin/com/bamyanggang/apimodule/domain/user/application/service/AuthService.kt index b00a0d64..0fa20056 100644 --- a/Api-Module/src/main/kotlin/com/bamyanggang/apimodule/domain/user/application/service/AuthService.kt +++ b/Api-Module/src/main/kotlin/com/bamyanggang/apimodule/domain/user/application/service/AuthService.kt @@ -7,10 +7,7 @@ import com.bamyanggang.apimodule.domain.user.application.service.handler.AuthHan import com.bamyanggang.cache.CacheManager import com.bamyanggang.commonmodule.util.TransactionUtils import com.bamyanggang.domainmodule.domain.user.enums.SocialLoginProvider -import com.bamyanggang.domainmodule.domain.user.service.TokenAppender -import com.bamyanggang.domainmodule.domain.user.service.TokenReader -import com.bamyanggang.domainmodule.domain.user.service.TokenRemover -import com.bamyanggang.domainmodule.domain.user.service.UserReader +import com.bamyanggang.domainmodule.domain.user.service.* import com.bamyanggang.jwt.* import com.bamyanggang.lock.LockManager import org.springframework.stereotype.Service @@ -25,8 +22,8 @@ class AuthService( private val tokenAppender: TokenAppender, private val claimsExtractor: ClaimsExtractor, private val tokenRemover: TokenRemover, - private val tokenReader : TokenReader, - private val tokenExtractor: TokenExtractor + private val tokenExtractor: TokenExtractor, + private val tokenModifier: TokenModifier ){ fun executeSocialLogin(provider: SocialLoginProvider, request: SocialLogin.Request): SocialLogin.Response { val socialLoginHandler = authHandlerManager.getHandler(provider) @@ -55,8 +52,7 @@ class AuthService( val accessToken = jwtProvider.generateAccessToken(userClaims) val newRefreshToken = jwtProvider.generateRefreshToken(userClaims) - tokenAppender.appendToken(userClaims.userId, newRefreshToken) - tokenReader.readToken(reissueRequest.refreshToken).also { tokenRemover.removeToken(it) } + tokenModifier.modifyToken(userClaims.userId, reissueRequest.refreshToken, newRefreshToken) return@cacheByKey Reissue.Response(accessToken, newRefreshToken) } @@ -65,6 +61,7 @@ class AuthService( @Transactional fun logout(refreshToken: String) { val refreshToken = tokenExtractor.extractValue(refreshToken) - tokenReader.readToken(refreshToken).also { tokenRemover.removeToken(it) } + tokenRemover.removeToken(refreshToken) } + } From fb6cfbbc6e7166c26aa253e81df76b6613a72656 Mon Sep 17 00:00:00 2001 From: seeunjang Date: Thu, 23 May 2024 01:51:53 +0900 Subject: [PATCH 8/8] =?UTF-8?q?refactor=20:=20=ED=86=A0=ED=81=B0=20?= =?UTF-8?q?=EB=A1=9C=EC=A7=81=20=EC=88=98=EC=A0=95=EC=9C=BC=EB=A1=9C=20?= =?UTF-8?q?=EC=9D=B8=ED=95=9C=20persistence=20=EA=B3=84=EC=B8=B5=20?= =?UTF-8?q?=EB=A9=94=EC=84=9C=EB=93=9C=20=EC=B6=94=EA=B0=80=20=EB=B0=8F=20?= =?UTF-8?q?=EC=88=98=EC=A0=95(#145)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../domain/user/repository/TokenRepository.kt | 5 ++++- .../persistence/user/TokenRepositoryImpl.java | 20 +++++++++++++------ .../jpa/repository/TokenJpaRepository.java | 5 +++++ 3 files changed, 23 insertions(+), 7 deletions(-) diff --git a/Domain-Module/src/main/kotlin/com/bamyanggang/domainmodule/domain/user/repository/TokenRepository.kt b/Domain-Module/src/main/kotlin/com/bamyanggang/domainmodule/domain/user/repository/TokenRepository.kt index 016a796d..39aba39c 100644 --- a/Domain-Module/src/main/kotlin/com/bamyanggang/domainmodule/domain/user/repository/TokenRepository.kt +++ b/Domain-Module/src/main/kotlin/com/bamyanggang/domainmodule/domain/user/repository/TokenRepository.kt @@ -1,9 +1,12 @@ package com.bamyanggang.domainmodule.domain.user.repository import com.bamyanggang.domainmodule.domain.user.aggregate.Token +import java.util.UUID interface TokenRepository { fun save(token: Token) fun findByValue(value: String): Token - fun deleteByValue(token: Token) + fun deleteByValue(value: String) + fun findByUserId(userId: UUID): Token? + } diff --git a/Infrastructure-Module/persistence/src/main/java/com/bamyanggang/persistence/user/TokenRepositoryImpl.java b/Infrastructure-Module/persistence/src/main/java/com/bamyanggang/persistence/user/TokenRepositoryImpl.java index bd303b05..7059d1e7 100644 --- a/Infrastructure-Module/persistence/src/main/java/com/bamyanggang/persistence/user/TokenRepositoryImpl.java +++ b/Infrastructure-Module/persistence/src/main/java/com/bamyanggang/persistence/user/TokenRepositoryImpl.java @@ -6,6 +6,7 @@ import com.bamyanggang.persistence.user.jpa.entity.TokenJpaEntity; import com.bamyanggang.persistence.user.jpa.repository.TokenJpaRepository; import com.bamyanggang.persistence.user.mapper.UserMapper; +import java.util.UUID; import lombok.RequiredArgsConstructor; import org.jetbrains.annotations.NotNull; import org.springframework.stereotype.Repository; @@ -22,16 +23,23 @@ public void save(Token token) { tokenJpaRepository.save(tokenJpaEntity); } - @Override - public void deleteByValue(Token token) { - TokenJpaEntity tokenJpaEntity = userMapper.toJpaEntity(token); - tokenJpaRepository.delete(tokenJpaEntity); - } - @Override public Token findByValue(String value) { TokenJpaEntity tokenJpaEntity = tokenJpaRepository.findByValue(value) .orElseThrow(() -> new PersistenceException.NotFound()); return userMapper.toDomainEntity(tokenJpaEntity); } + + @Override + public void deleteByValue(@NotNull String value) { + tokenJpaRepository.deleteByValue(value); + } + + @Override + public Token findByUserId(@NotNull UUID userId) { + return tokenJpaRepository.findByUserId(userId) + .map(userMapper::toDomainEntity) + .orElse(null); + } + } diff --git a/Infrastructure-Module/persistence/src/main/java/com/bamyanggang/persistence/user/jpa/repository/TokenJpaRepository.java b/Infrastructure-Module/persistence/src/main/java/com/bamyanggang/persistence/user/jpa/repository/TokenJpaRepository.java index 5aefb028..4df1160a 100644 --- a/Infrastructure-Module/persistence/src/main/java/com/bamyanggang/persistence/user/jpa/repository/TokenJpaRepository.java +++ b/Infrastructure-Module/persistence/src/main/java/com/bamyanggang/persistence/user/jpa/repository/TokenJpaRepository.java @@ -2,9 +2,14 @@ import com.bamyanggang.persistence.user.jpa.entity.TokenJpaEntity; import java.util.Optional; +import java.util.UUID; import org.springframework.data.jpa.repository.JpaRepository; public interface TokenJpaRepository extends JpaRepository { Optional findByValue(String value); + void deleteByValue(String value); + + Optional findByUserId(UUID userId); + }