From e988d9d2b71d60393c83782787227e146c92d8ae Mon Sep 17 00:00:00 2001
From: usbharu <64310155+usbharu@users.noreply.github.com>
Date: Fri, 7 Apr 2023 17:45:53 +0900
Subject: [PATCH] =?UTF-8?q?feat:=20webfinger=E3=82=92=E4=BD=9C=E3=82=8A?=
=?UTF-8?q?=E7=9B=B4=E3=81=97=E3=81=9F=E3=80=82?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
build.gradle.kts | 2 ++
.../domain/model/wellknown/WebFinger.kt | 5 +++
.../exception/IllegalParameterException.kt | 8 +++++
.../exception/ParameterNotExistException.kt | 8 +++++
.../usbharu/hideout/plugins/StatusPages.kt | 18 ++++++++++
.../routing/wellknown/WebfingerRouting.kt | 35 ++++++++++++++++++-
6 files changed, 75 insertions(+), 1 deletion(-)
create mode 100644 src/main/kotlin/dev/usbharu/hideout/domain/model/wellknown/WebFinger.kt
create mode 100644 src/main/kotlin/dev/usbharu/hideout/exception/IllegalParameterException.kt
create mode 100644 src/main/kotlin/dev/usbharu/hideout/exception/ParameterNotExistException.kt
create mode 100644 src/main/kotlin/dev/usbharu/hideout/plugins/StatusPages.kt
diff --git a/build.gradle.kts b/build.gradle.kts
index 146f725f..9afedfb0 100644
--- a/build.gradle.kts
+++ b/build.gradle.kts
@@ -59,6 +59,8 @@ dependencies {
implementation("io.insert-koin:koin-ktor:$koin_version")
implementation("io.insert-koin:koin-logger-slf4j:$koin_version")
implementation("io.ktor:ktor-client-logging-jvm:2.2.4")
+ implementation("io.ktor:ktor-server-host-common-jvm:2.2.4")
+ implementation("io.ktor:ktor-server-status-pages-jvm:2.2.4")
testImplementation("io.ktor:ktor-server-tests-jvm:$ktor_version")
testImplementation("org.jetbrains.kotlin:kotlin-test-junit:$kotlin_version")
diff --git a/src/main/kotlin/dev/usbharu/hideout/domain/model/wellknown/WebFinger.kt b/src/main/kotlin/dev/usbharu/hideout/domain/model/wellknown/WebFinger.kt
new file mode 100644
index 00000000..b8542f23
--- /dev/null
+++ b/src/main/kotlin/dev/usbharu/hideout/domain/model/wellknown/WebFinger.kt
@@ -0,0 +1,5 @@
+package dev.usbharu.hideout.domain.model.wellknown
+
+data class WebFinger(val subject:String,val links:List){
+ data class Link(val rel:String,val type:String,val href:String)
+}
diff --git a/src/main/kotlin/dev/usbharu/hideout/exception/IllegalParameterException.kt b/src/main/kotlin/dev/usbharu/hideout/exception/IllegalParameterException.kt
new file mode 100644
index 00000000..dd94d127
--- /dev/null
+++ b/src/main/kotlin/dev/usbharu/hideout/exception/IllegalParameterException.kt
@@ -0,0 +1,8 @@
+package dev.usbharu.hideout.exception
+
+class IllegalParameterException : IllegalArgumentException {
+ constructor() : super()
+ constructor(s: String?) : super(s)
+ constructor(message: String?, cause: Throwable?) : super(message, cause)
+ constructor(cause: Throwable?) : super(cause)
+}
diff --git a/src/main/kotlin/dev/usbharu/hideout/exception/ParameterNotExistException.kt b/src/main/kotlin/dev/usbharu/hideout/exception/ParameterNotExistException.kt
new file mode 100644
index 00000000..d3ca6693
--- /dev/null
+++ b/src/main/kotlin/dev/usbharu/hideout/exception/ParameterNotExistException.kt
@@ -0,0 +1,8 @@
+package dev.usbharu.hideout.exception
+
+class ParameterNotExistException : IllegalArgumentException {
+ constructor() : super()
+ constructor(s: String?) : super(s)
+ constructor(message: String?, cause: Throwable?) : super(message, cause)
+ constructor(cause: Throwable?) : super(cause)
+}
diff --git a/src/main/kotlin/dev/usbharu/hideout/plugins/StatusPages.kt b/src/main/kotlin/dev/usbharu/hideout/plugins/StatusPages.kt
new file mode 100644
index 00000000..990a7acb
--- /dev/null
+++ b/src/main/kotlin/dev/usbharu/hideout/plugins/StatusPages.kt
@@ -0,0 +1,18 @@
+package dev.usbharu.hideout.plugins
+
+import io.ktor.http.*
+import io.ktor.server.application.*
+import io.ktor.server.plugins.statuspages.*
+import io.ktor.server.response.*
+import io.ktor.server.routing.*
+
+fun Application.configureStatusPages() {
+ install(StatusPages) {
+ exception { call, cause ->
+ call.respondText(text = "500: $cause", status = HttpStatusCode.InternalServerError)
+ }
+ exception { call, cause ->
+ call.respondText(text = "400: $cause", status = HttpStatusCode.BadRequest)
+ }
+ }
+}
diff --git a/src/main/kotlin/dev/usbharu/hideout/routing/wellknown/WebfingerRouting.kt b/src/main/kotlin/dev/usbharu/hideout/routing/wellknown/WebfingerRouting.kt
index 62bf0c81..f862f745 100644
--- a/src/main/kotlin/dev/usbharu/hideout/routing/wellknown/WebfingerRouting.kt
+++ b/src/main/kotlin/dev/usbharu/hideout/routing/wellknown/WebfingerRouting.kt
@@ -1,11 +1,44 @@
package dev.usbharu.hideout.routing.wellknown
+import dev.usbharu.hideout.config.Config
+import dev.usbharu.hideout.domain.model.wellknown.WebFinger
+import dev.usbharu.hideout.exception.IllegalParameterException
+import dev.usbharu.hideout.exception.ParameterNotExistException
+import dev.usbharu.hideout.service.impl.UserService
+import dev.usbharu.hideout.util.HttpUtil.Activity
+import io.ktor.http.*
+import io.ktor.server.application.*
+import io.ktor.server.response.*
import io.ktor.server.routing.*
-fun Routing.webfinger(){
+fun Routing.webfinger(userService:UserService){
route("/.well-known/webfinger"){
get {
+ val acct = call.request.queryParameters["resource"]?.decodeURLPart()
+ ?: throw ParameterNotExistException("Parameter(name='resource') does not exist.")
+ if (acct.startsWith("acct:").not()) {
+ throw IllegalParameterException("Parameter(name='resource') is not start with 'acct:'.")
+ }
+
+ val accountName = acct.substringBeforeLast("@")
+ .substringAfter("acct:")
+ .trimStart('@')
+
+ val userEntity = userService.findByName(accountName)
+
+ val webFinger = WebFinger(
+ subject = acct,
+ links = listOf(
+ WebFinger.Link(
+ rel = "self",
+ type = ContentType.Application.Activity.toString(),
+ href = "${Config.configData.url}/users/${userEntity.name}"
+ )
+ )
+ )
+
+ return@get call.respond(webFinger)
}
}
}