From c37e442dd7c9681816fd44fc5ffbb12e7ad9cd0d Mon Sep 17 00:00:00 2001 From: usbharu Date: Wed, 19 Feb 2025 12:33:52 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20WebFinger=E3=82=92=E5=AE=9F=E8=A3=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../webfinger/WebFingerApplicationService.kt | 51 +++++++++++++++++++ .../wellknown/WebFingerController.kt | 18 +++++++ 2 files changed, 69 insertions(+) create mode 100644 hideout/hideout-activitypub/src/main/kotlin/dev/usbharu/hideout/activitypub/application/webfinger/WebFingerApplicationService.kt create mode 100644 hideout/hideout-activitypub/src/main/kotlin/dev/usbharu/hideout/activitypub/interfaces/wellknown/WebFingerController.kt diff --git a/hideout/hideout-activitypub/src/main/kotlin/dev/usbharu/hideout/activitypub/application/webfinger/WebFingerApplicationService.kt b/hideout/hideout-activitypub/src/main/kotlin/dev/usbharu/hideout/activitypub/application/webfinger/WebFingerApplicationService.kt new file mode 100644 index 00000000..3d10f4a1 --- /dev/null +++ b/hideout/hideout-activitypub/src/main/kotlin/dev/usbharu/hideout/activitypub/application/webfinger/WebFingerApplicationService.kt @@ -0,0 +1,51 @@ +package dev.usbharu.hideout.activitypub.application.webfinger + +import dev.usbharu.hideout.activitypub.interfaces.wellknown.Link +import dev.usbharu.hideout.activitypub.interfaces.wellknown.XRD +import dev.usbharu.hideout.core.application.shared.AbstractApplicationService +import dev.usbharu.hideout.core.application.shared.Transaction +import dev.usbharu.hideout.core.config.ApplicationConfig +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.support.principal.Principal +import org.slf4j.LoggerFactory +import org.springframework.stereotype.Service +import java.net.URI + +@Service +class WebFingerApplicationService( + transaction: Transaction, + private val applicationConfig: ApplicationConfig, + private val actorRepository: ActorRepository, +) : AbstractApplicationService(transaction, logger) { + + + override suspend fun internalExecute(resource: String, principal: Principal): XRD { + if (resource.startsWith("acct:").not()) { + throw IllegalArgumentException("Parameter (resource) is invalid.") + } + val acct = resource.substringAfter("acct:") + + val host = acct.substringAfter('@', "") + if (applicationConfig.url.apHost != host) { + throw IllegalArgumentException("Parameter (resource) is invalid.") + } + val username = acct.substringBefore('@', "") + if (username.isEmpty()) { + throw IllegalArgumentException("Actor is not found.") + } + + val actor = actorRepository.findByNameAndDomain(username, applicationConfig.url.apHost) + ?: throw IllegalArgumentException("Actor $username not found.") + + return XRD( + listOf(Link("self", null, "application/activity+json", actor.url.toString())), + URI.create("acct:${actor.name.name}@${actor.domain.domain}") + ) + + } + + companion object { + private val logger = LoggerFactory.getLogger(WebFingerApplicationService::class.java) + } +} \ No newline at end of file diff --git a/hideout/hideout-activitypub/src/main/kotlin/dev/usbharu/hideout/activitypub/interfaces/wellknown/WebFingerController.kt b/hideout/hideout-activitypub/src/main/kotlin/dev/usbharu/hideout/activitypub/interfaces/wellknown/WebFingerController.kt new file mode 100644 index 00000000..2af12b52 --- /dev/null +++ b/hideout/hideout-activitypub/src/main/kotlin/dev/usbharu/hideout/activitypub/interfaces/wellknown/WebFingerController.kt @@ -0,0 +1,18 @@ +package dev.usbharu.hideout.activitypub.interfaces.wellknown + +import dev.usbharu.hideout.activitypub.application.webfinger.WebFingerApplicationService +import dev.usbharu.hideout.core.domain.model.support.principal.Anonymous +import org.springframework.web.bind.annotation.GetMapping +import org.springframework.web.bind.annotation.RequestMapping +import org.springframework.web.bind.annotation.RequestParam +import org.springframework.web.bind.annotation.RestController + +@RestController +@RequestMapping("/.well-known") +class WebFingerController( + private val webFingerApplicationService: WebFingerApplicationService, +) { + @GetMapping("/webfinger", produces = ["application/json"]) + suspend fun webfinger(@RequestParam(name = "resource", required = true) resource: String): XRD = + webFingerApplicationService.execute(resource, Anonymous) +} \ No newline at end of file