Skip to content

Commit

Permalink
Merge pull request #146 from KUSITMS-29th-TEAM-B/refactor/flight-145
Browse files Browse the repository at this point in the history
refactor : 토큰 로직 수정(#145)
  • Loading branch information
isprogrammingfun authored May 22, 2024
2 parents b7d5836 + fb6cfbb commit 96dd571
Show file tree
Hide file tree
Showing 12 changed files with 77 additions and 58 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -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)
}
Expand All @@ -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)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -14,4 +16,4 @@ class AuthHandlerManager(
else -> throw AuthException.OAuthFailed()
}
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ class UserServiceConfiguration(
}

@Bean
fun tokenReader(): TokenReader {
return TokenReader(tokenRepository)
fun tokenModifier(): TokenModifier {
return TokenModifier(tokenRepository)
}

@Bean
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,22 @@ 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,
val createdAt: LocalDateTime,
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,
Expand Down
Original file line number Diff line number Diff line change
@@ -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?

}
Original file line number Diff line number Diff line change
Expand Up @@ -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) }

}

}
Original file line number Diff line number Diff line change
@@ -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) }
}

}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ class TokenRemover(
private val tokenRepository: TokenRepository
) {
fun removeToken(
token: Token
token: String
) {
tokenRepository.deleteByValue(token)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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<TokenJpaEntity, Long> {
Optional<TokenJpaEntity> findByValue(String value);

void deleteByValue(String value);

Optional<TokenJpaEntity> findByUserId(UUID userId);

}

0 comments on commit 96dd571

Please sign in to comment.