feat: webfingerを作り直した。

This commit is contained in:
usbharu 2023-04-07 17:45:53 +09:00
parent 41ed7ef193
commit e988d9d2b7
6 changed files with 75 additions and 1 deletions

View File

@ -59,6 +59,8 @@ dependencies {
implementation("io.insert-koin:koin-ktor:$koin_version") implementation("io.insert-koin:koin-ktor:$koin_version")
implementation("io.insert-koin:koin-logger-slf4j:$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-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("io.ktor:ktor-server-tests-jvm:$ktor_version")
testImplementation("org.jetbrains.kotlin:kotlin-test-junit:$kotlin_version") testImplementation("org.jetbrains.kotlin:kotlin-test-junit:$kotlin_version")

View File

@ -0,0 +1,5 @@
package dev.usbharu.hideout.domain.model.wellknown
data class WebFinger(val subject:String,val links:List<Link>){
data class Link(val rel:String,val type:String,val href:String)
}

View File

@ -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)
}

View File

@ -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)
}

View File

@ -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<Throwable> { call, cause ->
call.respondText(text = "500: $cause", status = HttpStatusCode.InternalServerError)
}
exception<IllegalArgumentException> { call, cause ->
call.respondText(text = "400: $cause", status = HttpStatusCode.BadRequest)
}
}
}

View File

@ -1,11 +1,44 @@
package dev.usbharu.hideout.routing.wellknown 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.* import io.ktor.server.routing.*
fun Routing.webfinger(){ fun Routing.webfinger(userService:UserService){
route("/.well-known/webfinger"){ route("/.well-known/webfinger"){
get { 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)
} }
} }
} }