diff --git a/src/main/kotlin/dev/usbharu/hideout/activitypub/service/objects/user/APUserService.kt b/src/main/kotlin/dev/usbharu/hideout/activitypub/service/objects/user/APUserService.kt index 03de36a3..9e8cb4b8 100644 --- a/src/main/kotlin/dev/usbharu/hideout/activitypub/service/objects/user/APUserService.kt +++ b/src/main/kotlin/dev/usbharu/hideout/activitypub/service/objects/user/APUserService.kt @@ -111,6 +111,36 @@ class APUserServiceImpl( val person = apResourceResolveService.resolve(url, null as Long?) val id = person.id ?: throw IllegalActivityPubObjectException("id is null") + try { + val userEntity = userQueryService.findByUrl(id) + return Person( + type = emptyList(), + name = userEntity.name, + id = id, + preferredUsername = userEntity.name, + summary = userEntity.description, + inbox = "$id/inbox", + outbox = "$id/outbox", + url = id, + icon = Image( + type = emptyList(), + name = "$id/icon.png", + mediaType = "image/png", + url = "$id/icon.png" + ), + publicKey = Key( + type = emptyList(), + name = "Public Key", + id = userEntity.keyId, + owner = id, + publicKeyPem = userEntity.publicKey + ), + endpoints = mapOf("sharedInbox" to "${applicationConfig.url}/inbox"), + followers = userEntity.followers, + following = userEntity.following + ) to userEntity + } catch (_: FailedToGetResourcesException) { + } person to userService.createRemoteUser( RemoteUserCreateDto( name = person.preferredUsername diff --git a/src/main/kotlin/dev/usbharu/hideout/application/config/SecurityConfig.kt b/src/main/kotlin/dev/usbharu/hideout/application/config/SecurityConfig.kt index 30b505c2..ecdc3ac9 100644 --- a/src/main/kotlin/dev/usbharu/hideout/application/config/SecurityConfig.kt +++ b/src/main/kotlin/dev/usbharu/hideout/application/config/SecurityConfig.kt @@ -117,9 +117,11 @@ class SecurityConfig { fun getHttpSignatureFilter( authenticationManager: AuthenticationManager, transaction: Transaction, - apUserService: APUserService + apUserService: APUserService, + userQueryService: UserQueryService ): HttpSignatureFilter { - val httpSignatureFilter = HttpSignatureFilter(DefaultSignatureHeaderParser(), transaction, apUserService) + val httpSignatureFilter = + HttpSignatureFilter(DefaultSignatureHeaderParser(), transaction, apUserService, userQueryService) httpSignatureFilter.setAuthenticationManager(authenticationManager) httpSignatureFilter.setContinueFilterChainOnUnsuccessfulAuthentication(false) val authenticationEntryPointFailureHandler = diff --git a/src/main/kotlin/dev/usbharu/hideout/core/infrastructure/exposedrepository/UserRepositoryImpl.kt b/src/main/kotlin/dev/usbharu/hideout/core/infrastructure/exposedrepository/UserRepositoryImpl.kt index 5cd94ccf..ecd910a6 100644 --- a/src/main/kotlin/dev/usbharu/hideout/core/infrastructure/exposedrepository/UserRepositoryImpl.kt +++ b/src/main/kotlin/dev/usbharu/hideout/core/infrastructure/exposedrepository/UserRepositoryImpl.kt @@ -17,7 +17,7 @@ class UserRepositoryImpl( UserRepository { override suspend fun save(user: User): User { - val singleOrNull = Users.select { Users.id eq user.id or (Users.url eq user.url) }.empty() + val singleOrNull = Users.select { Users.id eq user.id }.empty() if (singleOrNull) { Users.insert { it[id] = user.id diff --git a/src/main/kotlin/dev/usbharu/hideout/core/infrastructure/springframework/httpsignature/HttpSignatureFilter.kt b/src/main/kotlin/dev/usbharu/hideout/core/infrastructure/springframework/httpsignature/HttpSignatureFilter.kt index 8d03463c..6a68e267 100644 --- a/src/main/kotlin/dev/usbharu/hideout/core/infrastructure/springframework/httpsignature/HttpSignatureFilter.kt +++ b/src/main/kotlin/dev/usbharu/hideout/core/infrastructure/springframework/httpsignature/HttpSignatureFilter.kt @@ -2,6 +2,8 @@ package dev.usbharu.hideout.core.infrastructure.springframework.httpsignature import dev.usbharu.hideout.activitypub.service.objects.user.APUserService import dev.usbharu.hideout.application.external.Transaction +import dev.usbharu.hideout.core.domain.exception.FailedToGetResourcesException +import dev.usbharu.hideout.core.query.UserQueryService import dev.usbharu.httpsignature.common.HttpHeaders import dev.usbharu.httpsignature.common.HttpMethod import dev.usbharu.httpsignature.common.HttpRequest @@ -14,7 +16,8 @@ import java.net.URL class HttpSignatureFilter( private val httpSignatureHeaderParser: SignatureHeaderParser, private val transaction: Transaction, - private val apUserService: APUserService + private val apUserService: APUserService, + private val userQueryService: UserQueryService ) : AbstractPreAuthenticatedProcessingFilter() { override fun getPreAuthenticatedPrincipal(request: HttpServletRequest?): Any? { @@ -32,7 +35,11 @@ class HttpSignatureFilter( } runBlocking { transaction.transaction { - apUserService.fetchPerson(signature.keyId) + try { + userQueryService.findByKeyId(signature.keyId) + } catch (e: FailedToGetResourcesException) { + apUserService.fetchPerson(signature.keyId) + } } } return signature.keyId