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 +}