From 5bab0d4c55aef38f84b21fa554bb1bea723c6ff5 Mon Sep 17 00:00:00 2001 From: usbharu <64310155+usbharu@users.noreply.github.com> Date: Thu, 13 Apr 2023 11:36:15 +0900 Subject: [PATCH 1/2] =?UTF-8?q?refactor:=20=E4=B8=8D=E8=A6=81=E3=81=AA?= =?UTF-8?q?=E3=83=95=E3=82=A1=E3=82=A4=E3=83=AB=E3=82=92=E5=89=8A=E9=99=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../dev/usbharu/hideout/plugins/Sockets.kt | 29 ------- .../usbharu/hideout/routing/LoginRouting.kt | 45 ---------- .../usbharu/hideout/routing/UserRouting.kt | 86 ------------------- .../hideout/routing/WellKnownRouting.kt | 85 ------------------ .../hideout/service/IWebFingerService.kt | 18 ---- .../service/impl/ActivityPubService.kt | 20 ----- .../service/impl/ActivityPubUserService.kt | 57 ------------ .../hideout/service/impl/HttpSignService.kt | 9 -- .../hideout/service/impl/WebFingerService.kt | 77 ----------------- .../usbharu/hideout/webfinger/WebFinger.kt | 5 -- 10 files changed, 431 deletions(-) delete mode 100644 src/main/kotlin/dev/usbharu/hideout/plugins/Sockets.kt delete mode 100644 src/main/kotlin/dev/usbharu/hideout/routing/LoginRouting.kt delete mode 100644 src/main/kotlin/dev/usbharu/hideout/routing/UserRouting.kt delete mode 100644 src/main/kotlin/dev/usbharu/hideout/routing/WellKnownRouting.kt delete mode 100644 src/main/kotlin/dev/usbharu/hideout/service/IWebFingerService.kt delete mode 100644 src/main/kotlin/dev/usbharu/hideout/service/impl/ActivityPubService.kt delete mode 100644 src/main/kotlin/dev/usbharu/hideout/service/impl/ActivityPubUserService.kt delete mode 100644 src/main/kotlin/dev/usbharu/hideout/service/impl/HttpSignService.kt delete mode 100644 src/main/kotlin/dev/usbharu/hideout/service/impl/WebFingerService.kt delete mode 100644 src/main/kotlin/dev/usbharu/hideout/webfinger/WebFinger.kt diff --git a/src/main/kotlin/dev/usbharu/hideout/plugins/Sockets.kt b/src/main/kotlin/dev/usbharu/hideout/plugins/Sockets.kt deleted file mode 100644 index 0c6f217f..00000000 --- a/src/main/kotlin/dev/usbharu/hideout/plugins/Sockets.kt +++ /dev/null @@ -1,29 +0,0 @@ -package dev.usbharu.hideout.plugins - -import io.ktor.server.websocket.* -import io.ktor.websocket.* -import java.time.Duration -import io.ktor.server.application.* -import io.ktor.server.routing.* - -fun Application.configureSockets() { - install(WebSockets) { - pingPeriod = Duration.ofSeconds(15) - timeout = Duration.ofSeconds(15) - maxFrameSize = Long.MAX_VALUE - masking = false - } - routing { - webSocket("/ws") { // websocketSession - for (frame in incoming) { - if (frame is Frame.Text) { - val text = frame.readText() - outgoing.send(Frame.Text("YOU SAID: $text")) - if (text.equals("bye", ignoreCase = true)) { - close(CloseReason(CloseReason.Codes.NORMAL, "Client said BYE")) - } - } - } - } - } -} diff --git a/src/main/kotlin/dev/usbharu/hideout/routing/LoginRouting.kt b/src/main/kotlin/dev/usbharu/hideout/routing/LoginRouting.kt deleted file mode 100644 index f3e44b7d..00000000 --- a/src/main/kotlin/dev/usbharu/hideout/routing/LoginRouting.kt +++ /dev/null @@ -1,45 +0,0 @@ -package dev.usbharu.hideout.routing - -import dev.usbharu.hideout.plugins.UserSession -import dev.usbharu.hideout.plugins.tokenAuth -import io.ktor.http.* -import io.ktor.server.application.* -import io.ktor.server.auth.* -import io.ktor.server.response.* -import io.ktor.server.routing.* -import io.ktor.server.sessions.* - -fun Application.login(){ - routing { - authenticate(tokenAuth) { - post("/login") { - println("aaaaaaaaaaaaaaaaaaaaa") - val principal = call.principal() -// call.sessions.set(UserSession(principal!!.name)) - call.respondRedirect("/users/${principal!!.name}") - } - } - - get("/login"){ - call.respondText(contentType = ContentType.Text.Html) { - - //language=HTML - """ - - - - - -

login

-
- - - -
- - - """.trimIndent() - } - } - } -} diff --git a/src/main/kotlin/dev/usbharu/hideout/routing/UserRouting.kt b/src/main/kotlin/dev/usbharu/hideout/routing/UserRouting.kt deleted file mode 100644 index d43b070d..00000000 --- a/src/main/kotlin/dev/usbharu/hideout/routing/UserRouting.kt +++ /dev/null @@ -1,86 +0,0 @@ -package dev.usbharu.hideout.routing - -import dev.usbharu.hideout.domain.model.User -import dev.usbharu.hideout.plugins.UserSession -import dev.usbharu.hideout.plugins.respondAp -import dev.usbharu.hideout.plugins.tokenAuth -import dev.usbharu.hideout.service.impl.ActivityPubUserService -import dev.usbharu.hideout.service.impl.UserService -import dev.usbharu.hideout.util.HttpUtil -import io.ktor.http.* -import io.ktor.server.application.* -import io.ktor.server.auth.* -import io.ktor.server.request.* -import io.ktor.server.response.* -import io.ktor.server.routing.* - -@Suppress("unused") -fun Application.user(userService: UserService, activityPubUserService: ActivityPubUserService) { - routing { - route("/users") { - authenticate(tokenAuth, optional = true) { - - get { - val limit = call.request.queryParameters["limit"]?.toInt() - val offset = call.request.queryParameters["offset"]?.toLong() - val result = userService.findAll(limit, offset) - call.respond(result) - } - post { - val user = call.receive() - userService.create(user) - call.response.header( - HttpHeaders.Location, - call.request.path() + "/${user.name}" - ) - call.respond(HttpStatusCode.Created) - } - get("/{name}") { - val contentType = ContentType.parse(call.request.accept() ?: "*/*") - call.application.environment.log.debug("Accept Content-Type : ${contentType.contentType}/${contentType.contentSubtype} ${contentType.parameters}") - val typeOfActivityPub = HttpUtil.isContentTypeOfActivityPub( - contentType.contentType, - contentType.contentSubtype, - contentType.parameter("profile").orEmpty() - ) - val name = call.parameters["name"] - if (typeOfActivityPub) { - println("Required Activity !!") - val userModel = activityPubUserService.generateUserModel(name!!) - return@get call.respondAp(userModel) - } - name?.let { it1 -> userService.findByName(it1).id } - ?.let { it2 -> println(userService.findFollowersById(it2)) } - val principal = call.principal() - if (principal != null && name != null) { -// iUserService.findByName(name) - if (principal.name == name) { - call.respondText { - principal.name - } - //todo - } - } - call.respondText { - "hello $name !!" - } - } - get("/{name}/icon.png"){ - call.respondBytes(String.javaClass.classLoader.getResourceAsStream("icon.png").readAllBytes(),ContentType.Image.PNG) - } - } - - authenticate(tokenAuth) { - get("/admin") { - println("cccccccccccc " + call.principal()) - println("cccccccccccc " + call.principal()) - - return@get call.respondText { - "you alredy in admin !! hello " + - call.principal()?.name.toString() - } - } - } - } - } -} diff --git a/src/main/kotlin/dev/usbharu/hideout/routing/WellKnownRouting.kt b/src/main/kotlin/dev/usbharu/hideout/routing/WellKnownRouting.kt deleted file mode 100644 index f1e6a7a6..00000000 --- a/src/main/kotlin/dev/usbharu/hideout/routing/WellKnownRouting.kt +++ /dev/null @@ -1,85 +0,0 @@ -package dev.usbharu.hideout.routing - -import dev.usbharu.hideout.config.Config -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 kotlinx.serialization.Serializable -import org.intellij.lang.annotations.Language - -fun Application.wellKnown(userService: UserService) { - routing { - route("/.well-known") { - get("/host-meta") { - //language=XML - val xml = """ - - """.trimIndent() - return@get call.respondText( - contentType = ContentType("application", "xrd+xml"), - status = HttpStatusCode.OK, - text = xml - ) - } - - get("/host-meta.json") { - @Language("JSON") val json = """ - { - "links": [ - { - "rel": "lrdd", - "type": "application/jrd+json", - "template": "${Config.configData.url}/.well-known/webfinger?resource={uri}" - } - ] - } - """.trimIndent() - return@get call.respondText( - contentType = ContentType("application", "xrd+json"), - status = HttpStatusCode.OK, - text = json - ) - } - - get("/webfinger") { - val uri = call.request.queryParameters["resource"] ?: return@get call.respondText( - "resource was not found", - status = HttpStatusCode.BadRequest - ) - val decodeURLPart = uri.decodeURLPart() - if (!decodeURLPart.startsWith("acct:")) { - return@get call.respondText( - "$uri was not found.", - status = HttpStatusCode.BadRequest - ) - } - val accountName = - uri.substringBeforeLast("@").substringAfter("acct:").trimStart('@') - val userEntity = userService.findByName(accountName) - - return@get call.respond( - WebFingerResource( - subject = decodeURLPart, - listOf( - WebFingerResource.Link( - rel = "self", - type = ContentType.Application.Activity.toString(), - href = "${Config.configData.url}/users/${userEntity.name}" - ) - ) - ) - ) - } - - } - } -} - -@Serializable -data class WebFingerResource(val subject: String, val links: List) { - @Serializable - data class Link(val rel: String, val type: String, val href: String) -} diff --git a/src/main/kotlin/dev/usbharu/hideout/service/IWebFingerService.kt b/src/main/kotlin/dev/usbharu/hideout/service/IWebFingerService.kt deleted file mode 100644 index 2d01e147..00000000 --- a/src/main/kotlin/dev/usbharu/hideout/service/IWebFingerService.kt +++ /dev/null @@ -1,18 +0,0 @@ -package dev.usbharu.hideout.service - -import dev.usbharu.hideout.ap.Person -import dev.usbharu.hideout.domain.model.UserEntity -import dev.usbharu.hideout.webfinger.WebFinger - -interface IWebFingerService { - suspend fun fetch(acct:String): WebFinger? - - suspend fun sync(webFinger: WebFinger):UserEntity - - suspend fun fetchAndSync(acct: String):UserEntity{ - val webFinger = fetch(acct)?: throw IllegalArgumentException() - return sync(webFinger) - } - - suspend fun fetchUserModel(actor: String): Person? -} diff --git a/src/main/kotlin/dev/usbharu/hideout/service/impl/ActivityPubService.kt b/src/main/kotlin/dev/usbharu/hideout/service/impl/ActivityPubService.kt deleted file mode 100644 index e693bd57..00000000 --- a/src/main/kotlin/dev/usbharu/hideout/service/impl/ActivityPubService.kt +++ /dev/null @@ -1,20 +0,0 @@ -package dev.usbharu.hideout.service.impl - -import dev.usbharu.hideout.config.Config - -class ActivityPubService() { - - enum class ActivityType{ - Follow, - Undo - } - - fun switchApType(json:String): ActivityType { - val typeAsText = Config.configData.objectMapper.readTree(json).get("type").asText() - return when(typeAsText){ - "Follow" -> ActivityType.Follow - "Undo" -> ActivityType.Undo - else -> ActivityType.Undo - } - } -} diff --git a/src/main/kotlin/dev/usbharu/hideout/service/impl/ActivityPubUserService.kt b/src/main/kotlin/dev/usbharu/hideout/service/impl/ActivityPubUserService.kt deleted file mode 100644 index fd26123c..00000000 --- a/src/main/kotlin/dev/usbharu/hideout/service/impl/ActivityPubUserService.kt +++ /dev/null @@ -1,57 +0,0 @@ -package dev.usbharu.hideout.service.impl - -import dev.usbharu.hideout.ap.* -import dev.usbharu.hideout.config.Config -import dev.usbharu.hideout.plugins.postAp -import dev.usbharu.hideout.service.IUserAuthService -import dev.usbharu.hideout.service.IWebFingerService -import io.ktor.client.* - -class ActivityPubUserService( - private val httpClient: HttpClient, - private val userService: UserService, - private val userAuthService: IUserAuthService, - private val webFingerService: IWebFingerService -) { - suspend fun generateUserModel(name: String): Person { - val userEntity = userService.findByName(name) - val userAuthEntity = userAuthService.findByUserId(userEntity.id) - val userUrl = "${Config.configData.url}/users/$name" - return Person( - type = emptyList(), - name = userEntity.name, - id = userUrl, - preferredUsername = name, - summary = userEntity.description, - inbox = "$userUrl/inbox", - outbox = "$userUrl/outbox", - url = userUrl, - icon = Image( - type = emptyList(), - name = "$userUrl/icon.png", - mediaType = "image/png", - url = "$userUrl/icon.png" - ), - publicKey = Key( - type = emptyList(), - name = "Public Key", - id = "$userUrl#pubkey", - owner = userUrl, - publicKeyPem = userAuthEntity.publicKey - ) - ) - } - - suspend fun receiveFollow(follow: Follow) { - val actor = follow.actor ?: throw IllegalArgumentException("actor is null") - val person = webFingerService.fetchUserModel(actor) ?: throw IllegalArgumentException("actor is not found") - val inboxUrl = person.inbox ?: throw IllegalArgumentException("inbox is not found") - httpClient.postAp( - inboxUrl, "${follow.`object`!!}#pubkey", Accept( - name = "Follow", - `object` = follow, - actor = follow.`object`.orEmpty() - ) - ) - } -} diff --git a/src/main/kotlin/dev/usbharu/hideout/service/impl/HttpSignService.kt b/src/main/kotlin/dev/usbharu/hideout/service/impl/HttpSignService.kt deleted file mode 100644 index 07cf8941..00000000 --- a/src/main/kotlin/dev/usbharu/hideout/service/impl/HttpSignService.kt +++ /dev/null @@ -1,9 +0,0 @@ -package dev.usbharu.hideout.service.impl - -import java.security.PrivateKey - -class HttpSignService { - suspend fun sign(){ - - } -} diff --git a/src/main/kotlin/dev/usbharu/hideout/service/impl/WebFingerService.kt b/src/main/kotlin/dev/usbharu/hideout/service/impl/WebFingerService.kt deleted file mode 100644 index fe928bf6..00000000 --- a/src/main/kotlin/dev/usbharu/hideout/service/impl/WebFingerService.kt +++ /dev/null @@ -1,77 +0,0 @@ -package dev.usbharu.hideout.service.impl - -import dev.usbharu.hideout.ap.Person -import dev.usbharu.hideout.domain.model.User -import dev.usbharu.hideout.domain.model.UserEntity -import dev.usbharu.hideout.service.IWebFingerService -import dev.usbharu.hideout.util.HttpUtil -import dev.usbharu.hideout.webfinger.WebFinger -import io.ktor.client.* -import io.ktor.client.call.* -import io.ktor.client.plugins.* -import io.ktor.client.request.* -import io.ktor.http.* - -class WebFingerService( - private val httpClient: HttpClient, - private val userService: UserService -) : IWebFingerService { - override suspend fun fetch(acct: String): WebFinger? { - - val fullName = acct.substringAfter("acct:") - val domain = fullName.substringAfterLast("@") - - return try { - httpClient.get("https://$domain/.well-known/webfinger?resource=acct:$fullName") - .body() - } catch (e: ResponseException) { - if (e.response.status == HttpStatusCode.NotFound) { - return null - } - throw e - } - } - - override suspend fun fetchUserModel(url: String): Person? { - return try { - httpClient.get(url) { - header("Accept", "application/activity+json") - }.body() - } catch (e: ResponseException) { - if (e.response.status == HttpStatusCode.NotFound) { - e.printStackTrace() - return null - } - throw e - } - } - - override suspend fun sync(webFinger: WebFinger): UserEntity { - - val link = webFinger.links.find { - it.rel == "self" && HttpUtil.isContentTypeOfActivityPub( - ContentType.parse( - it.type.orEmpty() - ) - ) - }?.href ?: throw Exception() - - val fullName = webFinger.subject.substringAfter("acct:") - val domain = fullName.substringAfterLast("@") - val userName = fullName.substringBeforeLast("@") - - val userModel = fetchUserModel(link) ?: throw Exception() - - val user = User( - userModel.preferredUsername ?: throw IllegalStateException(), - domain, - userName, - userModel.summary.orEmpty(), - "", - "", - "" - ) - TODO() - return userService.create(user) - } -} diff --git a/src/main/kotlin/dev/usbharu/hideout/webfinger/WebFinger.kt b/src/main/kotlin/dev/usbharu/hideout/webfinger/WebFinger.kt deleted file mode 100644 index 22fa25e5..00000000 --- a/src/main/kotlin/dev/usbharu/hideout/webfinger/WebFinger.kt +++ /dev/null @@ -1,5 +0,0 @@ -package dev.usbharu.hideout.webfinger - -class WebFinger(val subject: String, val aliases: List, val links: List) { - class Link(val rel: String, val type: String?, val href: String?, val template: String) -} From a9bbcdcf62a2f796a564d0c4a6e1e0740c149dbd Mon Sep 17 00:00:00 2001 From: usbharu <64310155+usbharu@users.noreply.github.com> Date: Thu, 13 Apr 2023 11:41:58 +0900 Subject: [PATCH 2/2] =?UTF-8?q?fix:=20=E5=89=8A=E9=99=A4=E3=81=97=E3=81=8D?= =?UTF-8?q?=E3=82=8C=E3=81=A6=E3=81=84=E3=81=AA=E3=81=8B=E3=81=A3=E3=81=9F?= =?UTF-8?q?=E8=A8=AD=E5=AE=9A=E3=82=92=E5=89=8A=E9=99=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/kotlin/dev/usbharu/hideout/Application.kt | 1 - 1 file changed, 1 deletion(-) diff --git a/src/main/kotlin/dev/usbharu/hideout/Application.kt b/src/main/kotlin/dev/usbharu/hideout/Application.kt index 320ec162..a6e9e357 100644 --- a/src/main/kotlin/dev/usbharu/hideout/Application.kt +++ b/src/main/kotlin/dev/usbharu/hideout/Application.kt @@ -90,7 +90,6 @@ fun Application.parent() { configureKoin(module) configureHTTP() - configureSockets() configureMonitoring() configureSerialization() register(inject().value)