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 171066d2..972c75b5 100644 --- a/src/main/kotlin/dev/usbharu/hideout/service/ap/APRequestServiceImpl.kt +++ b/src/main/kotlin/dev/usbharu/hideout/service/ap/APRequestServiceImpl.kt @@ -49,8 +49,8 @@ class APRequestServiceImpl( val sign = httpSignatureSigner.sign( url, HttpMethod.Get, headers, "", Key( keyId = "${signer.url}#pubkey", - privateKey = RsaUtil.decodeRsaPrivateKey(signer.privateKey), - publicKey = RsaUtil.decodeRsaPublicKey(signer.publicKey) + privateKey = RsaUtil.decodeRsaPrivateKeyPem(signer.privateKey), + publicKey = RsaUtil.decodeRsaPublicKeyPem(signer.publicKey) ), listOf("(request-target)", "date", "host", "accept") ) @@ -61,6 +61,7 @@ class APRequestServiceImpl( remove("Host") } } + contentType(ContentType.Application.Activity) }.bodyAsText() return objectMapper.readValue(bodyAsText, responseClass) } @@ -77,6 +78,13 @@ class APRequestServiceImpl( override suspend fun apPost(url: String, body: T?, signer: User?): String { + if (body != null) { + val mutableListOf = mutableListOf() + mutableListOf.add("https://www.w3.org/ns/activitystreams") + mutableListOf.addAll(body.context) + body.context = mutableListOf + } + val requestBody = objectMapper.writeValueAsString(body) val sha256 = MessageDigest.getInstance("SHA-256") @@ -88,26 +96,25 @@ class APRequestServiceImpl( if (signer?.privateKey == null) { return httpClient.post(url) { header("Accept", ContentType.Application.Activity) - header("ContentType", ContentType.Application.Activity) header("Date", date) - header("Digest", digest) + header("Digest", "sha-256=$digest") setBody(requestBody) + contentType(ContentType.Application.Activity) }.bodyAsText() } val headers = headers { append("Accept", ContentType.Application.Activity) - append("ContentType", ContentType.Application.Activity) append("Date", date) append("Host", u.host) - append("Digest", digest) + append("Digest", "sha-256=$digest") } val sign = httpSignatureSigner.sign( - url, HttpMethod.Get, headers, "", Key( + url, HttpMethod.Post, headers, "", Key( keyId = "${signer.url}#pubkey", - privateKey = RsaUtil.decodeRsaPrivateKey(signer.privateKey), - publicKey = RsaUtil.decodeRsaPublicKey(signer.publicKey) + privateKey = RsaUtil.decodeRsaPrivateKeyPem(signer.privateKey), + publicKey = RsaUtil.decodeRsaPublicKeyPem(signer.publicKey) ), listOf("(request-target)", "date", "host", "digest") ) @@ -115,10 +122,10 @@ class APRequestServiceImpl( headers { headers { appendAll(sign.headers) - remove("Host") } } setBody(requestBody) + contentType(ContentType.Application.Activity) }.bodyAsText() } } diff --git a/src/main/kotlin/dev/usbharu/hideout/util/RsaUtil.kt b/src/main/kotlin/dev/usbharu/hideout/util/RsaUtil.kt index e0ebbfc8..307654f3 100644 --- a/src/main/kotlin/dev/usbharu/hideout/util/RsaUtil.kt +++ b/src/main/kotlin/dev/usbharu/hideout/util/RsaUtil.kt @@ -14,10 +14,24 @@ object RsaUtil { fun decodeRsaPublicKey(encoded: String): RSAPublicKey = decodeRsaPublicKey(Base64Util.decode(encoded)) + fun decodeRsaPublicKeyPem(pem: String): RSAPublicKey { + val replace = pem.replace(replaceHeaderAndFooterRegex, "") + .replace("\n", "") + return decodeRsaPublicKey(replace) + } + fun decodeRsaPrivateKey(byteArray: ByteArray): RSAPrivateKey { val pkcS8EncodedKeySpec = PKCS8EncodedKeySpec(byteArray) return KeyFactory.getInstance("RSA").generatePrivate(pkcS8EncodedKeySpec) as RSAPrivateKey } fun decodeRsaPrivateKey(encoded: String): RSAPrivateKey = decodeRsaPrivateKey(Base64Util.decode(encoded)) + + fun decodeRsaPrivateKeyPem(pem: String): RSAPrivateKey { + val replace = pem.replace(replaceHeaderAndFooterRegex, "") + .replace("\n", "") + return decodeRsaPrivateKey(replace) + } + + private val replaceHeaderAndFooterRegex = Regex("-----.*?-----") }