From 0021061aa21079e3408f14c1984129dd7b68f2b1 Mon Sep 17 00:00:00 2001 From: usbharu <64310155+usbharu@users.noreply.github.com> Date: Thu, 19 Oct 2023 13:24:30 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20=E9=80=A3=E5=90=88=E3=81=AB=E5=A4=B1?= =?UTF-8?q?=E6=95=97=E3=81=99=E3=82=8B=E5=95=8F=E9=A1=8C=E3=82=92=E4=BF=AE?= =?UTF-8?q?=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../hideout/repository/UserRepositoryImpl.kt | 6 ++++++ .../hideout/service/ap/APReceiveFollowService.kt | 8 ++++---- .../hideout/service/ap/APRequestServiceImpl.kt | 14 ++++++++++++-- src/main/resources/application.yml | 2 +- 4 files changed, 23 insertions(+), 7 deletions(-) diff --git a/src/main/kotlin/dev/usbharu/hideout/repository/UserRepositoryImpl.kt b/src/main/kotlin/dev/usbharu/hideout/repository/UserRepositoryImpl.kt index f8b1c7c3..a6955a5d 100644 --- a/src/main/kotlin/dev/usbharu/hideout/repository/UserRepositoryImpl.kt +++ b/src/main/kotlin/dev/usbharu/hideout/repository/UserRepositoryImpl.kt @@ -29,6 +29,9 @@ class UserRepositoryImpl(private val idGenerateService: IdGenerateService) : it[createdAt] = user.createdAt.toEpochMilli() it[publicKey] = user.publicKey it[privateKey] = user.privateKey + it[keyId] = user.keyId + it[following] = user.following + it[followers] = user.followers } } else { Users.update({ Users.id eq user.id }) { @@ -43,6 +46,9 @@ class UserRepositoryImpl(private val idGenerateService: IdGenerateService) : it[createdAt] = user.createdAt.toEpochMilli() it[publicKey] = user.publicKey it[privateKey] = user.privateKey + it[keyId] = user.keyId + it[following] = user.following + it[followers] = user.followers } } return user diff --git a/src/main/kotlin/dev/usbharu/hideout/service/ap/APReceiveFollowService.kt b/src/main/kotlin/dev/usbharu/hideout/service/ap/APReceiveFollowService.kt index 38af4dd2..6d9321cd 100644 --- a/src/main/kotlin/dev/usbharu/hideout/service/ap/APReceiveFollowService.kt +++ b/src/main/kotlin/dev/usbharu/hideout/service/ap/APReceiveFollowService.kt @@ -49,18 +49,18 @@ class APReceiveFollowServiceImpl( val person = apUserService.fetchPerson(actor, targetActor) val follow = objectMapper.readValue(props[ReceiveFollowJob.follow]) - val signer = userQueryService.findByUrl(actor) + val signer = userQueryService.findByUrl(targetActor) val urlString = person.inbox ?: throw IllegalArgumentException("inbox is not found") apRequestService.apPost( - urlString, - Accept( + url = urlString, + body = Accept( name = "Follow", `object` = follow, actor = targetActor ), - signer + signer = signer ) val targetEntity = userQueryService.findByUrl(targetActor) diff --git a/src/main/kotlin/dev/usbharu/hideout/service/ap/APRequestServiceImpl.kt b/src/main/kotlin/dev/usbharu/hideout/service/ap/APRequestServiceImpl.kt index ba1c55ae..4c61bfb2 100644 --- a/src/main/kotlin/dev/usbharu/hideout/service/ap/APRequestServiceImpl.kt +++ b/src/main/kotlin/dev/usbharu/hideout/service/ap/APRequestServiceImpl.kt @@ -16,6 +16,7 @@ import io.ktor.client.request.* import io.ktor.client.statement.* import io.ktor.http.* import io.ktor.util.* +import org.slf4j.LoggerFactory import org.springframework.beans.factory.annotation.Qualifier import org.springframework.stereotype.Service import java.net.URL @@ -53,7 +54,7 @@ class APRequestServiceImpl( httpRequest = HttpRequest( url = u, headers = HttpHeaders(headers.toMap()), - dev.usbharu.httpsignature.common.HttpMethod.GET + HttpMethod.GET ), privateKey = PrivateKey( keyId = "${signer.url}#pubkey", @@ -102,6 +103,8 @@ class APRequestServiceImpl( val date = dateTimeFormatter.format(ZonedDateTime.now(ZoneId.of("GMT"))) val u = URL(url) if (signer?.privateKey == null) { + logger.debug("NOT SIGN Request: {}", url) + logger.trace("{}", signer) return httpClient.post(url) { header("Accept", ContentType.Application.Activity) header("Date", date) @@ -111,6 +114,8 @@ class APRequestServiceImpl( }.bodyAsText() } + logger.debug("SIGN Request: {}", url) + val headers = headers { append("Accept", ContentType.Application.Activity) append("Date", date) @@ -123,7 +128,7 @@ class APRequestServiceImpl( u, HttpHeaders(headers.toMap()), HttpMethod.POST ), privateKey = PrivateKey( - keyId = "${signer.url}#pubkey", + keyId = signer.keyId, privateKey = RsaUtil.decodeRsaPrivateKeyPem(signer.privateKey) ), signHeaders = listOf("(request-target)", "date", "host", "digest") @@ -134,10 +139,15 @@ class APRequestServiceImpl( headers { appendAll(headers) append("Signature", sign.signatureHeader) + remove("Host") } } setBody(requestBody) contentType(ContentType.Application.Activity) }.bodyAsText() } + + companion object { + private val logger = LoggerFactory.getLogger(APRequestServiceImpl::class.java) + } } diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index 60814a17..a2d2690f 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -19,7 +19,7 @@ spring: default-property-inclusion: always datasource: driver-class-name: org.h2.Driver - url: "jdbc:h2:./test-dev2;MODE=POSTGRESQL" + url: "jdbc:h2:./test-dev3;MODE=POSTGRESQL" username: "" password: "" data: