From 3009ca153290a5c9d8e8f26f7ed73cf77e6bfa68 Mon Sep 17 00:00:00 2001 From: usbharu <64310155+usbharu@users.noreply.github.com> Date: Wed, 22 Nov 2023 02:14:21 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20Create=E3=81=AE=E3=82=A2=E3=82=AF?= =?UTF-8?q?=E3=83=86=E3=82=A3=E3=83=93=E3=83=86=E3=82=A3=E3=81=AB=E5=AF=BE?= =?UTF-8?q?=E5=BF=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../HttpSignatureUnauthorizedException.kt | 14 ++++++++++++ .../tmp/AbstractActivityPubProcessor.kt | 9 +++++++- .../tmp/impl/CreateActivityProcessor.kt | 22 +++++++++++++++++++ 3 files changed, 44 insertions(+), 1 deletion(-) create mode 100644 src/main/kotlin/dev/usbharu/hideout/activitypub/domain/exception/HttpSignatureUnauthorizedException.kt create mode 100644 src/main/kotlin/dev/usbharu/hideout/activitypub/service/tmp/impl/CreateActivityProcessor.kt diff --git a/src/main/kotlin/dev/usbharu/hideout/activitypub/domain/exception/HttpSignatureUnauthorizedException.kt b/src/main/kotlin/dev/usbharu/hideout/activitypub/domain/exception/HttpSignatureUnauthorizedException.kt new file mode 100644 index 00000000..9abccec6 --- /dev/null +++ b/src/main/kotlin/dev/usbharu/hideout/activitypub/domain/exception/HttpSignatureUnauthorizedException.kt @@ -0,0 +1,14 @@ +package dev.usbharu.hideout.activitypub.domain.exception + +class HttpSignatureUnauthorizedException : RuntimeException { + constructor() : super() + constructor(message: String?) : super(message) + constructor(message: String?, cause: Throwable?) : super(message, cause) + constructor(cause: Throwable?) : super(cause) + constructor(message: String?, cause: Throwable?, enableSuppression: Boolean, writableStackTrace: Boolean) : super( + message, + cause, + enableSuppression, + writableStackTrace + ) +} diff --git a/src/main/kotlin/dev/usbharu/hideout/activitypub/service/tmp/AbstractActivityPubProcessor.kt b/src/main/kotlin/dev/usbharu/hideout/activitypub/service/tmp/AbstractActivityPubProcessor.kt index 6a3aba7e..c5d927c9 100644 --- a/src/main/kotlin/dev/usbharu/hideout/activitypub/service/tmp/AbstractActivityPubProcessor.kt +++ b/src/main/kotlin/dev/usbharu/hideout/activitypub/service/tmp/AbstractActivityPubProcessor.kt @@ -2,14 +2,21 @@ package dev.usbharu.hideout.activitypub.service.tmp import dev.usbharu.hideout.activitypub.domain.exception.ActivityPubProcessException import dev.usbharu.hideout.activitypub.domain.exception.FailedProcessException +import dev.usbharu.hideout.activitypub.domain.exception.HttpSignatureUnauthorizedException import dev.usbharu.hideout.activitypub.domain.model.objects.Object import dev.usbharu.hideout.application.external.Transaction import org.slf4j.LoggerFactory -abstract class AbstractActivityPubProcessor(val transaction: Transaction) : ActivityPubProcessor { +abstract class AbstractActivityPubProcessor( + private val transaction: Transaction, + private val allowUnauthorized: Boolean = false +) : ActivityPubProcessor { private val logger = LoggerFactory.getLogger(this::class.java) override suspend fun process(activity: ActivityPubProcessContext) { + if (activity.isAuthorized.not() && allowUnauthorized.not()) { + throw HttpSignatureUnauthorizedException() + } logger.info("START ActivityPub process") try { transaction.transaction { diff --git a/src/main/kotlin/dev/usbharu/hideout/activitypub/service/tmp/impl/CreateActivityProcessor.kt b/src/main/kotlin/dev/usbharu/hideout/activitypub/service/tmp/impl/CreateActivityProcessor.kt new file mode 100644 index 00000000..60918d01 --- /dev/null +++ b/src/main/kotlin/dev/usbharu/hideout/activitypub/service/tmp/impl/CreateActivityProcessor.kt @@ -0,0 +1,22 @@ +package dev.usbharu.hideout.activitypub.service.tmp.impl + +import dev.usbharu.hideout.activitypub.domain.model.Create +import dev.usbharu.hideout.activitypub.domain.model.Note +import dev.usbharu.hideout.activitypub.service.common.ActivityType +import dev.usbharu.hideout.activitypub.service.objects.note.APNoteService +import dev.usbharu.hideout.activitypub.service.tmp.AbstractActivityPubProcessor +import dev.usbharu.hideout.activitypub.service.tmp.ActivityPubProcessContext +import dev.usbharu.hideout.application.external.Transaction +import org.springframework.stereotype.Service + +@Service +class CreateActivityProcessor(transaction: Transaction, private val apNoteService: APNoteService) : + AbstractActivityPubProcessor(transaction, false) { + override suspend fun internalProcess(activity: ActivityPubProcessContext) { + apNoteService.fetchNote(activity.activity.`object` as Note) + } + + override fun isSupported(activityType: ActivityType): Boolean = activityType == ActivityType.Create + + override fun type(): Class = Create::class.java +}