Merge pull request #678 from usbharu/host-meta

Web Host Metadata(host-meta)を実装
This commit is contained in:
usbharu 2025-02-18 17:07:59 +09:00 committed by GitHub
commit 26b9d640b5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 139 additions and 1 deletions

View File

@ -37,6 +37,7 @@ dependencies {
implementation("org.springframework.boot:spring-boot-starter-web")
implementation("org.springframework.boot:spring-boot-starter-security")
implementation(libs.bundles.jackson)
implementation(libs.jackson.xml)
implementation(libs.owl.producer.api)
implementation(libs.owl.producer.embedded)
implementation(libs.owl.common.serialize.jackson)

View File

@ -0,0 +1,20 @@
package dev.usbharu.hideout.activitypub.application.hostmeta
import com.fasterxml.jackson.annotation.JsonProperty
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlElementWrapper
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement
@JacksonXmlRootElement(localName = "XRD", namespace = "http://docs.oasis-open.org/ns/xri/xrd-1.0")
class WebHostMetadata(
@JacksonXmlProperty(localName = "Link", namespace = "http://docs.oasis-open.org/ns/xri/xrd-1.0")
@JacksonXmlElementWrapper(useWrapping = false)
@JsonProperty("links")
val links: List<Link>,
)
class Link(
@JacksonXmlProperty(localName = "rel", isAttribute = true) val rel: String,
@JacksonXmlProperty(localName = "template", isAttribute = true) val template: String,
@JacksonXmlProperty(localName = "type", isAttribute = true) val type: String,
)

View File

@ -23,6 +23,8 @@ class ActivityPubSecurityConfig {
}
)
authorizeHttpRequests {
authorize(GET, "/.well-known/**", permitAll)
authorize(GET, "/error", permitAll)
authorize(POST, "/inbox", permitAll)
authorize(POST, "/users/{username}/inbox", permitAll)
authorize(GET, "/outbox", permitAll)

View File

@ -0,0 +1,18 @@
package dev.usbharu.hideout.activitypub.config
import dev.usbharu.hideout.activitypub.application.hostmeta.Link
import dev.usbharu.hideout.core.config.ApplicationConfig
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
@Configuration
class WebFingerHostMetaLinkConfiguration(private val applicationConfig: ApplicationConfig) {
@Bean
fun webFingerHostMetaLink(): Link {
return Link(
rel = "lrdd",
type = "application/jrd+json",
template = applicationConfig.url.resolve(".well-known/webfinger").toString() + "?resource={uri}"
)
}
}

View File

@ -0,0 +1,32 @@
package dev.usbharu.hideout.activitypub.interfaces.wellknown
import dev.usbharu.hideout.activitypub.application.hostmeta.Link
import dev.usbharu.hideout.activitypub.application.hostmeta.WebHostMetadata
import org.springframework.core.annotation.Order
import org.springframework.http.MediaType
import org.springframework.http.ResponseEntity
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController
@RestController
@RequestMapping("/.well-known")
class HostmetaController(private val linkList: List<Link> = emptyList()) {
@Order(1)
@GetMapping("/host-meta")
fun hostmeta(): ResponseEntity<WebHostMetadata> {
return ResponseEntity.ok().contentType(MediaType("application", "xrd+xml"))
.body(WebHostMetadata(linkList))
}
@Order(2)
@GetMapping("/host-meta", produces = ["application/json"])
fun hostmetaJson(): WebHostMetadata {
return WebHostMetadata(linkList)
}
@GetMapping("/host-meta.json", produces = ["application/json"])
fun hostmetaJson2(): WebHostMetadata {
return WebHostMetadata(linkList)
}
}

View File

@ -0,0 +1,65 @@
package wellknown
import dev.usbharu.hideout.SpringApplication
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc
import org.springframework.boot.test.context.SpringBootTest
import org.springframework.http.MediaType
import org.springframework.security.test.web.servlet.setup.SecurityMockMvcConfigurers.springSecurity
import org.springframework.test.web.servlet.MockMvc
import org.springframework.test.web.servlet.get
import org.springframework.test.web.servlet.setup.DefaultMockMvcBuilder
import org.springframework.test.web.servlet.setup.MockMvcBuilders
import org.springframework.web.context.WebApplicationContext
@SpringBootTest(classes = [SpringApplication::class])
@AutoConfigureMockMvc
class HostMetaControllerTest {
@Autowired
private lateinit var context: WebApplicationContext
private lateinit var mockMvc: MockMvc
@BeforeEach
fun setUp() {
mockMvc = MockMvcBuilders.webAppContextSetup(context)
.apply<DefaultMockMvcBuilder>(springSecurity())
.build()
}
@Test
fun hostmeta() {
mockMvc
.get("/.well-known/host-meta") {
accept(MediaType.APPLICATION_XML)
}
.andDo { print() }
.andExpect { status { isOk() } }
.andExpect { content { contentType(MediaType("application", "xrd+xml")) } }
}
@Test
fun hostmetaJson() {
mockMvc
.get("/.well-known/host-meta") {
accept(MediaType.APPLICATION_JSON)
}
.andDo { print() }
.andExpect { status { isOk() } }
.andExpect { content { contentType(MediaType("application", "json")) } }
}
@Test
fun hostmetaJson2() {
mockMvc
.get("/.well-known/host-meta.json") {
accept(MediaType.APPLICATION_JSON)
}
.andDo { print() }
.andExpect { status { isOk() } }
.andExpect { content { contentType(MediaType("application", "json")) } }
}
}

View File

@ -62,7 +62,7 @@ owl-common-serialize-jackson = { module = "dev.usbharu:owl-common-serialize-jack
jackson-databind = { module = "com.fasterxml.jackson.core:jackson-databind", version.ref = "jackson" }
jackson-module-kotlin = { module = "com.fasterxml.jackson.module:jackson-module-kotlin", version.ref = "jackson" }
jackson-xml = { module = "com.fasterxml.jackson.dataformat:jackson-dataformat-xml", version.ref = "jackson" }
blurhash = { module = "io.trbl:blurhash", version = "1.0.0" }
aws-s3 = { module = "software.amazon.awssdk:s3", version = "2.27.22" }