refactor: 不要なクラスを削除

This commit is contained in:
usbharu 2023-09-30 13:26:27 +09:00
parent 171a5ad26c
commit b641c4b5fb
7 changed files with 1 additions and 258 deletions

View File

@ -1,17 +0,0 @@
package dev.usbharu.hideout.config
import org.jetbrains.exposed.spring.SpringTransactionManager
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.springframework.transaction.PlatformTransactionManager
import org.springframework.transaction.annotation.EnableTransactionManagement
import org.springframework.transaction.annotation.TransactionManagementConfigurer
import javax.sql.DataSource
@Configuration
@EnableTransactionManagement
class SpringTransactionConfig(val datasource: DataSource) : TransactionManagementConfigurer {
@Bean
override fun annotationDrivenTransactionManager(): PlatformTransactionManager =
SpringTransactionManager(datasource)
}

View File

@ -1,46 +0,0 @@
package dev.usbharu.hideout.query
import dev.usbharu.hideout.domain.model.hideout.entity.JwtRefreshToken
import dev.usbharu.hideout.exception.FailedToGetResourcesException
import dev.usbharu.hideout.repository.JwtRefreshTokens
import dev.usbharu.hideout.repository.toJwtRefreshToken
import dev.usbharu.hideout.util.singleOr
import org.jetbrains.exposed.sql.SqlExpressionBuilder.eq
import org.jetbrains.exposed.sql.deleteAll
import org.jetbrains.exposed.sql.deleteWhere
import org.jetbrains.exposed.sql.select
import org.springframework.stereotype.Repository
@Repository
class JwtRefreshTokenQueryServiceImpl : JwtRefreshTokenQueryService {
override suspend fun findById(id: Long): JwtRefreshToken =
JwtRefreshTokens.select { JwtRefreshTokens.id.eq(id) }
.singleOr { FailedToGetResourcesException("id: $id is a duplicate or does not exist.", it) }
.toJwtRefreshToken()
override suspend fun findByToken(token: String): JwtRefreshToken =
JwtRefreshTokens.select { JwtRefreshTokens.refreshToken.eq(token) }
.singleOr { FailedToGetResourcesException("token: $token is a duplicate or does not exist.", it) }
.toJwtRefreshToken()
override suspend fun findByUserId(userId: Long): JwtRefreshToken =
JwtRefreshTokens.select { JwtRefreshTokens.userId.eq(userId) }
.singleOr { FailedToGetResourcesException("userId: $userId is a duplicate or does not exist.", it) }
.toJwtRefreshToken()
override suspend fun deleteById(id: Long) {
JwtRefreshTokens.deleteWhere { JwtRefreshTokens.id eq id }
}
override suspend fun deleteByToken(token: String) {
JwtRefreshTokens.deleteWhere { refreshToken eq token }
}
override suspend fun deleteByUserId(userId: Long) {
JwtRefreshTokens.deleteWhere { JwtRefreshTokens.userId eq userId }
}
override suspend fun deleteAll() {
JwtRefreshTokens.deleteAll()
}
}

View File

@ -1,15 +0,0 @@
package dev.usbharu.hideout.repository
import dev.usbharu.hideout.domain.model.hideout.entity.JwtRefreshToken
import org.springframework.stereotype.Repository
@Repository
interface JwtRefreshTokenRepository {
suspend fun generateId(): Long
suspend fun save(token: JwtRefreshToken)
suspend fun findById(id: Long): JwtRefreshToken?
suspend fun delete(token: JwtRefreshToken)
}

View File

@ -1,71 +0,0 @@
package dev.usbharu.hideout.repository
import dev.usbharu.hideout.domain.model.hideout.entity.JwtRefreshToken
import dev.usbharu.hideout.service.core.IdGenerateService
import org.jetbrains.exposed.sql.*
import org.jetbrains.exposed.sql.SqlExpressionBuilder.eq
import org.jetbrains.exposed.sql.transactions.transaction
import org.springframework.stereotype.Repository
import java.time.Instant
@Repository
class JwtRefreshTokenRepositoryImpl(
private val database: Database,
private val idGenerateService: IdGenerateService
) :
JwtRefreshTokenRepository {
init {
transaction(database) {
SchemaUtils.create(JwtRefreshTokens)
SchemaUtils.createMissingTablesAndColumns(JwtRefreshTokens)
}
}
override suspend fun generateId(): Long = idGenerateService.generateId()
override suspend fun save(token: JwtRefreshToken) {
if (JwtRefreshTokens.select { JwtRefreshTokens.id.eq(token.id) }.empty()) {
JwtRefreshTokens.insert {
it[id] = token.id
it[userId] = token.userId
it[refreshToken] = token.refreshToken
it[createdAt] = token.createdAt.toEpochMilli()
it[expiresAt] = token.expiresAt.toEpochMilli()
}
} else {
JwtRefreshTokens.update({ JwtRefreshTokens.id eq token.id }) {
it[userId] = token.userId
it[refreshToken] = token.refreshToken
it[createdAt] = token.createdAt.toEpochMilli()
it[expiresAt] = token.expiresAt.toEpochMilli()
}
}
}
override suspend fun findById(id: Long): JwtRefreshToken? =
JwtRefreshTokens.select { JwtRefreshTokens.id.eq(id) }.singleOrNull()?.toJwtRefreshToken()
override suspend fun delete(token: JwtRefreshToken) {
JwtRefreshTokens.deleteWhere { id eq token.id }
}
}
fun ResultRow.toJwtRefreshToken(): JwtRefreshToken {
return JwtRefreshToken(
this[JwtRefreshTokens.id],
this[JwtRefreshTokens.userId],
this[JwtRefreshTokens.refreshToken],
Instant.ofEpochMilli(this[JwtRefreshTokens.createdAt]),
Instant.ofEpochMilli(this[JwtRefreshTokens.expiresAt])
)
}
object JwtRefreshTokens : Table("jwt_refresh_tokens") {
val id = long("id")
val userId = long("user_id")
val refreshToken = varchar("refresh_token", 1000)
val createdAt = long("created_at")
val expiresAt = long("expires_at")
override val primaryKey = PrimaryKey(id)
}

View File

@ -11,5 +11,4 @@ interface UserAuthService {
suspend fun generateKeyPair(): KeyPair suspend fun generateKeyPair(): KeyPair
suspend fun verifyAccount(username: String, password: String): Boolean
} }

View File

@ -1,6 +1,5 @@
package dev.usbharu.hideout.service.user package dev.usbharu.hideout.service.user
import dev.usbharu.hideout.config.ApplicationConfig
import dev.usbharu.hideout.query.UserQueryService import dev.usbharu.hideout.query.UserQueryService
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder
import org.springframework.stereotype.Service import org.springframework.stereotype.Service
@ -9,8 +8,7 @@ import java.util.*
@Service @Service
class UserAuthServiceImpl( class UserAuthServiceImpl(
val userQueryService: UserQueryService, val userQueryService: UserQueryService
private val applicationConfig: ApplicationConfig
) : UserAuthService { ) : UserAuthService {
override fun hash(password: String): String = BCryptPasswordEncoder().encode(password) override fun hash(password: String): String = BCryptPasswordEncoder().encode(password)
@ -20,11 +18,6 @@ class UserAuthServiceImpl(
return true return true
} }
override suspend fun verifyAccount(username: String, password: String): Boolean {
val userEntity = userQueryService.findByNameAndDomain(username, applicationConfig.url.host)
return userEntity.password == hash(password)
}
override suspend fun generateKeyPair(): KeyPair { override suspend fun generateKeyPair(): KeyPair {
val keyPairGenerator = KeyPairGenerator.getInstance("RSA") val keyPairGenerator = KeyPairGenerator.getInstance("RSA")
keyPairGenerator.initialize(keySize) keyPairGenerator.initialize(keySize)

View File

@ -1,100 +0,0 @@
@file:OptIn(ExperimentalCoroutinesApi::class, ExperimentalCoroutinesApi::class)
package dev.usbharu.hideout.repository
import dev.usbharu.hideout.domain.model.hideout.entity.JwtRefreshToken
import dev.usbharu.hideout.service.core.IdGenerateService
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.test.runTest
import org.jetbrains.exposed.sql.Database
import org.jetbrains.exposed.sql.SchemaUtils
import org.jetbrains.exposed.sql.insert
import org.jetbrains.exposed.sql.select
import org.jetbrains.exposed.sql.transactions.TransactionManager
import org.jetbrains.exposed.sql.transactions.experimental.newSuspendedTransaction
import org.jetbrains.exposed.sql.transactions.transaction
import org.junit.jupiter.api.AfterEach
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
import java.time.Clock
import java.time.Instant
import java.time.ZoneId
import java.time.temporal.ChronoUnit
import kotlin.test.assertEquals
class JwtRefreshTokenRepositoryImplTest {
lateinit var db: Database
@BeforeEach
fun setUp() {
db = Database.connect("jdbc:h2:mem:test;DB_CLOSE_DELAY=-1")
transaction(db) {
SchemaUtils.create(JwtRefreshTokens)
}
}
@AfterEach
fun tearDown() {
transaction(db) {
SchemaUtils.drop(JwtRefreshTokens)
}
TransactionManager.closeAndUnregister(db)
}
@Test
fun `save 存在しない場合はinsertする`() = runTest {
val repository = JwtRefreshTokenRepositoryImpl(
db,
object : IdGenerateService {
override suspend fun generateId(): Long {
TODO("Not yet implemented")
}
}
)
val now = Instant.now(Clock.tickMillis(ZoneId.systemDefault()))
val expiresAt = now.plus(10, ChronoUnit.MINUTES)
val expect = JwtRefreshToken(1L, 2L, "refreshToken", now, expiresAt)
newSuspendedTransaction {
repository.save(expect)
val actual = repository.findById(1L)
assertEquals(expect, actual)
}
}
@Test
fun `save 存在する場合はupdateする`() = runTest {
val repository = JwtRefreshTokenRepositoryImpl(
db,
object : IdGenerateService {
override suspend fun generateId(): Long {
TODO("Not yet implemented")
}
}
)
newSuspendedTransaction {
JwtRefreshTokens.insert {
it[id] = 1L
it[userId] = 2L
it[refreshToken] = "refreshToken1"
it[createdAt] = Instant.now().toEpochMilli()
it[expiresAt] = Instant.now().plus(10, ChronoUnit.MINUTES).toEpochMilli()
}
repository.save(
JwtRefreshToken(
id = 1L,
userId = 2L,
refreshToken = "refreshToken2",
createdAt = Instant.now(),
expiresAt = Instant.now().plus(10, ChronoUnit.MINUTES)
)
)
}
transaction {
val toJwtRefreshToken = JwtRefreshTokens.select { JwtRefreshTokens.id.eq(1L) }.single().toJwtRefreshToken()
assertEquals("refreshToken2", toJwtRefreshToken.refreshToken)
}
}
}