From 3ac07822467b4ebd27a6d58b3d3fde131bf036a2 Mon Sep 17 00:00:00 2001 From: usbharu <64310155+usbharu@users.noreply.github.com> Date: Tue, 21 Nov 2023 16:40:57 +0900 Subject: [PATCH] =?UTF-8?q?fix:=20user=E3=81=AE=E4=B8=80=E6=84=8F=E6=80=A7?= =?UTF-8?q?=E3=82=92=E4=BF=9D=E8=A8=BC=E3=81=A7=E3=81=8D=E3=82=8B=E3=82=88?= =?UTF-8?q?=E3=81=86=E3=81=AB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../service/objects/user/APUserService.kt | 30 +++++++++++++++++++ .../application/config/SecurityConfig.kt | 6 ++-- .../exposedrepository/UserRepositoryImpl.kt | 2 +- .../httpsignature/HttpSignatureFilter.kt | 11 +++++-- 4 files changed, 44 insertions(+), 5 deletions(-) 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