Merge pull request #624 from usbharu/uri-domain

ドメイン名だけじゃなくてポート番号も管理する
This commit is contained in:
usbharu 2024-09-22 11:59:47 +09:00 committed by GitHub
commit 32483abfe0
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
15 changed files with 41 additions and 23 deletions

View File

@ -24,6 +24,7 @@ import dev.usbharu.hideout.core.domain.model.actor.ActorId
import dev.usbharu.hideout.core.domain.model.actor.ActorRepository import dev.usbharu.hideout.core.domain.model.actor.ActorRepository
import dev.usbharu.hideout.core.domain.model.media.MediaRepository import dev.usbharu.hideout.core.domain.model.media.MediaRepository
import dev.usbharu.hideout.core.domain.model.support.acct.Acct import dev.usbharu.hideout.core.domain.model.support.acct.Acct
import dev.usbharu.hideout.core.domain.model.support.domain.apHost
import dev.usbharu.hideout.core.domain.model.support.principal.Principal import dev.usbharu.hideout.core.domain.model.support.principal.Principal
import org.slf4j.LoggerFactory import org.slf4j.LoggerFactory
import org.springframework.stereotype.Component import org.springframework.stereotype.Component
@ -45,7 +46,7 @@ class GetActorDetailApplicationService(
?: throw IllegalArgumentException("Actor ${command.id} not found.") ?: throw IllegalArgumentException("Actor ${command.id} not found.")
} else if (command.actorName != null) { } else if (command.actorName != null) {
val host = if (command.actorName.host.isEmpty()) { val host = if (command.actorName.host.isEmpty()) {
applicationConfig.url.host applicationConfig.url.apHost
} else { } else {
command.actorName.host command.actorName.host
} }

View File

@ -51,7 +51,7 @@ class RegisterLocalActorApplicationService(
if (actorDomainService.usernameAlreadyUse(command.name)) { if (actorDomainService.usernameAlreadyUse(command.name)) {
throw IllegalArgumentException("Username already exists") throw IllegalArgumentException("Username already exists")
} }
val instance = instanceRepository.findByUrl(applicationConfig.url.toURI()) val instance = instanceRepository.findByUrl(applicationConfig.url)
?: throw InternalServerException("Local instance not found.") ?: throw InternalServerException("Local instance not found.")
val actor = actorFactoryImpl.createLocal( val actor = actorFactoryImpl.createLocal(

View File

@ -46,7 +46,7 @@ class GetLocalInstanceApplicationService(
} }
val instance = ( val instance = (
instanceRepository.findByUrl(applicationConfig.url.toURI()) instanceRepository.findByUrl(applicationConfig.url)
?: throw InternalServerException("Local instance not found.") ?: throw InternalServerException("Local instance not found.")
) )

View File

@ -19,6 +19,7 @@ package dev.usbharu.hideout.core.application.instance
import dev.usbharu.hideout.core.application.shared.Transaction import dev.usbharu.hideout.core.application.shared.Transaction
import dev.usbharu.hideout.core.config.ApplicationConfig import dev.usbharu.hideout.core.config.ApplicationConfig
import dev.usbharu.hideout.core.domain.model.instance.* import dev.usbharu.hideout.core.domain.model.instance.*
import dev.usbharu.hideout.core.domain.model.support.domain.apHost
import dev.usbharu.hideout.core.domain.shared.id.IdGenerateService import dev.usbharu.hideout.core.domain.shared.id.IdGenerateService
import org.springframework.boot.context.event.ApplicationReadyEvent import org.springframework.boot.context.event.ApplicationReadyEvent
import org.springframework.boot.info.BuildProperties import org.springframework.boot.info.BuildProperties
@ -36,15 +37,15 @@ class InitLocalInstanceApplicationService(
) { ) {
@EventListener(ApplicationReadyEvent::class) @EventListener(ApplicationReadyEvent::class)
suspend fun init() = transaction.transaction { suspend fun init() = transaction.transaction {
val findByUrl = instanceRepository.findByUrl(applicationConfig.url.toURI()) val findByUrl = instanceRepository.findByUrl(applicationConfig.url)
if (findByUrl == null) { if (findByUrl == null) {
val instance = Instance( val instance = Instance(
id = InstanceId(idGenerateService.generateId()), id = InstanceId(idGenerateService.generateId()),
name = InstanceName(applicationConfig.url.host), name = InstanceName(applicationConfig.url.apHost),
description = InstanceDescription(""), description = InstanceDescription(""),
url = applicationConfig.url.toURI(), url = applicationConfig.url,
iconUrl = applicationConfig.url.toURI(), iconUrl = applicationConfig.url,
sharedInbox = null, sharedInbox = null,
software = InstanceSoftware("hideout"), software = InstanceSoftware("hideout"),
version = InstanceVersion(buildProperties.version), version = InstanceVersion(buildProperties.version),

View File

@ -18,6 +18,7 @@ package dev.usbharu.hideout.core.application.model
import dev.usbharu.hideout.core.domain.model.actor.Actor import dev.usbharu.hideout.core.domain.model.actor.Actor
import dev.usbharu.hideout.core.domain.model.media.Media import dev.usbharu.hideout.core.domain.model.media.Media
import dev.usbharu.hideout.core.domain.model.support.domain.apHost
import java.net.URI import java.net.URI
data class ActorDetail( data class ActorDetail(
@ -41,7 +42,7 @@ data class ActorDetail(
id = actor.id.id, id = actor.id.id,
name = actor.name.name, name = actor.name.name,
screenName = actor.screenName.screenName, screenName = actor.screenName.screenName,
host = actor.url.host, host = actor.url.apHost,
instanceId = actor.instance.instanceId, instanceId = actor.instance.instanceId,
remoteUrl = actor.url, remoteUrl = actor.url,
locked = actor.locked, locked = actor.locked,

View File

@ -17,11 +17,11 @@
package dev.usbharu.hideout.core.config package dev.usbharu.hideout.core.config
import org.springframework.boot.context.properties.ConfigurationProperties import org.springframework.boot.context.properties.ConfigurationProperties
import java.net.URL import java.net.URI
@ConfigurationProperties("hideout") @ConfigurationProperties("hideout")
data class ApplicationConfig( data class ApplicationConfig(
val url: URL, val url: URI,
val private: Boolean = true, val private: Boolean = true,
val keySize: Int = 2048, val keySize: Int = 2048,
) )

View File

@ -16,6 +16,8 @@
package dev.usbharu.hideout.core.domain.model.support.domain package dev.usbharu.hideout.core.domain.model.support.domain
import java.net.URI
@JvmInline @JvmInline
value class Domain(val domain: String) { value class Domain(val domain: String) {
init { init {
@ -23,6 +25,15 @@ value class Domain(val domain: String) {
} }
companion object { companion object {
const val LENGTH = 1000 const val LENGTH: Int = 1000
fun of(uri: URI): Domain = Domain(uri.apHost)
} }
} }
val URI.apHost: String
get() = if (port == -1) {
host
} else {
"$host:$port"
}

View File

@ -18,6 +18,7 @@ package dev.usbharu.hideout.core.domain.service.actor
import dev.usbharu.hideout.core.config.ApplicationConfig import dev.usbharu.hideout.core.config.ApplicationConfig
import dev.usbharu.hideout.core.domain.model.actor.Actor import dev.usbharu.hideout.core.domain.model.actor.Actor
import dev.usbharu.hideout.core.domain.model.support.domain.apHost
import org.springframework.stereotype.Service import org.springframework.stereotype.Service
interface IRemoteActorCheckDomainService { interface IRemoteActorCheckDomainService {
@ -26,5 +27,5 @@ interface IRemoteActorCheckDomainService {
@Service @Service
class RemoteActorCheckDomainService(private val applicationConfig: ApplicationConfig) : IRemoteActorCheckDomainService { class RemoteActorCheckDomainService(private val applicationConfig: ApplicationConfig) : IRemoteActorCheckDomainService {
override fun isRemoteActor(actor: Actor): Boolean = actor.domain.domain != applicationConfig.url.host override fun isRemoteActor(actor: Actor): Boolean = actor.domain.domain != applicationConfig.url.apHost
} }

View File

@ -20,6 +20,7 @@ import dev.usbharu.hideout.core.config.ApplicationConfig
import dev.usbharu.hideout.core.domain.model.actor.ActorPrivateKey import dev.usbharu.hideout.core.domain.model.actor.ActorPrivateKey
import dev.usbharu.hideout.core.domain.model.actor.ActorPublicKey import dev.usbharu.hideout.core.domain.model.actor.ActorPublicKey
import dev.usbharu.hideout.core.domain.model.actor.ActorRepository import dev.usbharu.hideout.core.domain.model.actor.ActorRepository
import dev.usbharu.hideout.core.domain.model.support.domain.apHost
import org.springframework.stereotype.Service import org.springframework.stereotype.Service
import java.security.KeyPairGenerator import java.security.KeyPairGenerator
@ -29,7 +30,7 @@ class LocalActorDomainServiceImpl(
private val applicationConfig: ApplicationConfig, private val applicationConfig: ApplicationConfig,
) : LocalActorDomainService { ) : LocalActorDomainService {
override suspend fun usernameAlreadyUse(name: String): Boolean = override suspend fun usernameAlreadyUse(name: String): Boolean =
actorRepository.findByNameAndDomain(name, applicationConfig.url.host) != null actorRepository.findByNameAndDomain(name, applicationConfig.url.apHost) != null
override suspend fun generateKeyPair(): Pair<ActorPublicKey, ActorPrivateKey> { override suspend fun generateKeyPair(): Pair<ActorPublicKey, ActorPrivateKey> {
val keyPairGenerator = KeyPairGenerator.getInstance("RSA") val keyPairGenerator = KeyPairGenerator.getInstance("RSA")

View File

@ -20,6 +20,7 @@ import dev.usbharu.hideout.core.config.ApplicationConfig
import dev.usbharu.hideout.core.domain.model.actor.* import dev.usbharu.hideout.core.domain.model.actor.*
import dev.usbharu.hideout.core.domain.model.instance.InstanceId import dev.usbharu.hideout.core.domain.model.instance.InstanceId
import dev.usbharu.hideout.core.domain.model.support.domain.Domain import dev.usbharu.hideout.core.domain.model.support.domain.Domain
import dev.usbharu.hideout.core.domain.model.support.domain.apHost
import dev.usbharu.hideout.core.domain.shared.id.IdGenerateService import dev.usbharu.hideout.core.domain.shared.id.IdGenerateService
import org.springframework.stereotype.Component import org.springframework.stereotype.Component
import java.net.URI import java.net.URI
@ -40,7 +41,7 @@ class ActorFactoryImpl(
return Actor( return Actor(
id = ActorId(idGenerateService.generateId()), id = ActorId(idGenerateService.generateId()),
name = actorName, name = actorName,
domain = Domain(applicationConfig.url.host), domain = Domain(applicationConfig.url.apHost),
screenName = ActorScreenName(name), screenName = ActorScreenName(name),
description = ActorDescription(""), description = ActorDescription(""),
inbox = URI.create("$userUrl/inbox"), inbox = URI.create("$userUrl/inbox"),

View File

@ -19,6 +19,7 @@ package dev.usbharu.hideout.core.infrastructure.springframework.oauth2
import dev.usbharu.hideout.core.application.shared.Transaction import dev.usbharu.hideout.core.application.shared.Transaction
import dev.usbharu.hideout.core.config.ApplicationConfig import dev.usbharu.hideout.core.config.ApplicationConfig
import dev.usbharu.hideout.core.domain.model.actor.ActorRepository import dev.usbharu.hideout.core.domain.model.actor.ActorRepository
import dev.usbharu.hideout.core.domain.model.support.domain.apHost
import dev.usbharu.hideout.core.domain.model.userdetails.UserDetailRepository import dev.usbharu.hideout.core.domain.model.userdetails.UserDetailRepository
import kotlinx.coroutines.runBlocking import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.slf4j.MDCContext import kotlinx.coroutines.slf4j.MDCContext
@ -39,7 +40,7 @@ class UserDetailsServiceImpl(
throw UsernameNotFoundException("Username not found") throw UsernameNotFoundException("Username not found")
} }
transaction.transaction { transaction.transaction {
val actor = actorRepository.findByNameAndDomain(username, applicationConfig.url.host) val actor = actorRepository.findByNameAndDomain(username, applicationConfig.url.apHost)
?: throw UsernameNotFoundException("$username not found") ?: throw UsernameNotFoundException("$username not found")
val userDetail = userDetailRepository.findByActorId(actor.id.id) val userDetail = userDetailRepository.findByActorId(actor.id.id)
?: throw UsernameNotFoundException("${actor.id.id} not found") ?: throw UsernameNotFoundException("${actor.id.id} not found")

View File

@ -22,7 +22,7 @@ import org.mockito.kotlin.doReturn
import org.mockito.kotlin.eq import org.mockito.kotlin.eq
import org.mockito.kotlin.whenever import org.mockito.kotlin.whenever
import utils.TestTransaction import utils.TestTransaction
import java.net.URL import java.net.URI
@ExtendWith(MockitoExtension::class) @ExtendWith(MockitoExtension::class)
class RegisterLocalActorApplicationServiceTest { class RegisterLocalActorApplicationServiceTest {
@ -51,7 +51,7 @@ class RegisterLocalActorApplicationServiceTest {
val transaction = TestTransaction val transaction = TestTransaction
@Spy @Spy
val applicationConfig = ApplicationConfig(URL("http://example.com")) val applicationConfig = ApplicationConfig(URI.create("http://example.com"))
@Spy @Spy
val idGenerateService = TwitterSnowflakeIdGenerateService val idGenerateService = TwitterSnowflakeIdGenerateService

View File

@ -15,7 +15,7 @@ class RemoteActorCheckDomainServiceTest {
val remoteActor = RemoteActorCheckDomainService( val remoteActor = RemoteActorCheckDomainService(
ApplicationConfig( ApplicationConfig(
URI.create("https://local.example.com").toURL() URI.create("https://local.example.com")
) )
).isRemoteActor( ).isRemoteActor(
actor actor
@ -30,7 +30,7 @@ class RemoteActorCheckDomainServiceTest {
val localActor = RemoteActorCheckDomainService( val localActor = RemoteActorCheckDomainService(
ApplicationConfig( ApplicationConfig(
URI.create("https://local.example.com").toURL() URI.create("https://local.example.com")
) )
).isRemoteActor( ).isRemoteActor(
actor actor

View File

@ -15,7 +15,7 @@ import org.mockito.junit.jupiter.MockitoExtension
import org.mockito.kotlin.doReturn import org.mockito.kotlin.doReturn
import org.mockito.kotlin.eq import org.mockito.kotlin.eq
import org.mockito.kotlin.whenever import org.mockito.kotlin.whenever
import java.net.URL import java.net.URI
@ExtendWith(MockitoExtension::class) @ExtendWith(MockitoExtension::class)
class LocalActorDomainServiceImplTest { class LocalActorDomainServiceImplTest {
@ -26,7 +26,7 @@ class LocalActorDomainServiceImplTest {
lateinit var actorRepository: ActorRepository lateinit var actorRepository: ActorRepository
@Spy @Spy
val applicationConfig = ApplicationConfig(URL("http://example.com")) val applicationConfig = ApplicationConfig(URI.create("http://example.com"))
@Test @Test
fun findByNameAndDomainがnullならfalse() = runTest { fun findByNameAndDomainがnullならfalse() = runTest {

View File

@ -19,7 +19,7 @@ import org.mockito.junit.jupiter.MockitoExtension
import org.mockito.kotlin.* import org.mockito.kotlin.*
import org.springframework.security.core.userdetails.UsernameNotFoundException import org.springframework.security.core.userdetails.UsernameNotFoundException
import utils.TestTransaction import utils.TestTransaction
import java.net.URL import java.net.URI
import kotlin.test.assertEquals import kotlin.test.assertEquals
@ExtendWith(MockitoExtension::class) @ExtendWith(MockitoExtension::class)
@ -34,7 +34,7 @@ class UserDetailsServiceImplTest {
lateinit var userDetailRepository: UserDetailRepository lateinit var userDetailRepository: UserDetailRepository
@Spy @Spy
val applicationConfig = ApplicationConfig(URL("http://example.com")) val applicationConfig = ApplicationConfig(URI.create("http://example.com"))
@Spy @Spy
val transaction = TestTransaction val transaction = TestTransaction