feat: Getリクエストも署名できるように

This commit is contained in:
usbharu 2023-04-29 14:16:48 +09:00
parent 8aab1e5907
commit f9c6ed539a
5 changed files with 19 additions and 7 deletions

View File

@ -44,6 +44,13 @@ suspend fun HttpClient.postAp(urlString: String, username: String, jsonLd: JsonL
}
}
suspend fun HttpClient.getAp(urlString: String,username: String):HttpResponse {
return this.get(urlString){
header("Accept",ContentType.Application.Activity)
header("Signature","keyId=\"$username\",algorithm=\"#rsa-sha\",headers=\"(request-target) digest date\"")
}
}
class HttpSignaturePluginConfig {
lateinit var keyMap: KeyMap
}

View File

@ -32,9 +32,9 @@ class ActivityPubFollowServiceImpl(
override suspend fun receiveFollowJob(props: JobProps<ReceiveFollowJob>) {
val actor = props[ReceiveFollowJob.actor]
val person = activityPubUserService.fetchPerson(actor)
val follow = Config.configData.objectMapper.readValue<Follow>(props[ReceiveFollowJob.follow])
val targetActor = props[ReceiveFollowJob.targetActor]
val person = activityPubUserService.fetchPerson(actor,targetActor)
val follow = Config.configData.objectMapper.readValue<Follow>(props[ReceiveFollowJob.follow])
httpClient.postAp(
urlString = person.inbox ?: throw IllegalArgumentException("inbox is not found"),
username = "$targetActor#pubkey",

View File

@ -5,5 +5,5 @@ import dev.usbharu.hideout.domain.model.ap.Person
interface ActivityPubUserService {
suspend fun getPersonByName(name:String): Person
suspend fun fetchPerson(url:String): Person
suspend fun fetchPerson(url: String, targetActor: String? = null): Person
}

View File

@ -8,6 +8,7 @@ import dev.usbharu.hideout.domain.model.ap.Person
import dev.usbharu.hideout.domain.model.hideout.dto.RemoteUserCreateDto
import dev.usbharu.hideout.exception.UserNotFoundException
import dev.usbharu.hideout.exception.ap.IllegalActivityPubObjectException
import dev.usbharu.hideout.plugins.getAp
import dev.usbharu.hideout.service.impl.IUserService
import dev.usbharu.hideout.util.HttpUtil.Activity
import io.ktor.client.*
@ -52,7 +53,7 @@ class ActivityPubUserServiceImpl(
)
}
override suspend fun fetchPerson(url: String): Person {
override suspend fun fetchPerson(url: String, targetActor: String?): Person {
return try {
val userEntity = userService.findByUrl(url)
return Person(
@ -80,8 +81,12 @@ class ActivityPubUserServiceImpl(
)
} catch (e: UserNotFoundException) {
val httpResponse = httpClient.get(url) {
accept(ContentType.Application.Activity)
val httpResponse = if (targetActor != null) {
httpClient.getAp(url,"$targetActor#pubkey")
}else {
httpClient.get(url) {
accept(ContentType.Application.Activity)
}
}
val person = Config.configData.objectMapper.readValue<Person>(httpResponse.bodyAsText())

View File

@ -85,7 +85,7 @@ class ActivityPubFollowServiceImplTest {
)
val activityPubUserService = mock<ActivityPubUserService> {
onBlocking { fetchPerson(anyString()) } doReturn person
onBlocking { fetchPerson(anyString(), any()) } doReturn person
}
val userService = mock<IUserService> {
onBlocking { findByUrls(any()) } doReturn listOf(