fix: APRequestServiceImplを修正

This commit is contained in:
usbharu 2023-10-14 23:25:18 +09:00
parent 0814258bfa
commit f796f27880
2 changed files with 31 additions and 10 deletions

View File

@ -49,8 +49,8 @@ class APRequestServiceImpl(
val sign = httpSignatureSigner.sign( val sign = httpSignatureSigner.sign(
url, HttpMethod.Get, headers, "", Key( url, HttpMethod.Get, headers, "", Key(
keyId = "${signer.url}#pubkey", keyId = "${signer.url}#pubkey",
privateKey = RsaUtil.decodeRsaPrivateKey(signer.privateKey), privateKey = RsaUtil.decodeRsaPrivateKeyPem(signer.privateKey),
publicKey = RsaUtil.decodeRsaPublicKey(signer.publicKey) publicKey = RsaUtil.decodeRsaPublicKeyPem(signer.publicKey)
), listOf("(request-target)", "date", "host", "accept") ), listOf("(request-target)", "date", "host", "accept")
) )
@ -61,6 +61,7 @@ class APRequestServiceImpl(
remove("Host") remove("Host")
} }
} }
contentType(ContentType.Application.Activity)
}.bodyAsText() }.bodyAsText()
return objectMapper.readValue(bodyAsText, responseClass) return objectMapper.readValue(bodyAsText, responseClass)
} }
@ -77,6 +78,13 @@ class APRequestServiceImpl(
override suspend fun <T : Object> apPost(url: String, body: T?, signer: User?): String { override suspend fun <T : Object> apPost(url: String, body: T?, signer: User?): String {
if (body != null) {
val mutableListOf = mutableListOf<String>()
mutableListOf.add("https://www.w3.org/ns/activitystreams")
mutableListOf.addAll(body.context)
body.context = mutableListOf
}
val requestBody = objectMapper.writeValueAsString(body) val requestBody = objectMapper.writeValueAsString(body)
val sha256 = MessageDigest.getInstance("SHA-256") val sha256 = MessageDigest.getInstance("SHA-256")
@ -88,26 +96,25 @@ class APRequestServiceImpl(
if (signer?.privateKey == null) { if (signer?.privateKey == null) {
return httpClient.post(url) { return httpClient.post(url) {
header("Accept", ContentType.Application.Activity) header("Accept", ContentType.Application.Activity)
header("ContentType", ContentType.Application.Activity)
header("Date", date) header("Date", date)
header("Digest", digest) header("Digest", "sha-256=$digest")
setBody(requestBody) setBody(requestBody)
contentType(ContentType.Application.Activity)
}.bodyAsText() }.bodyAsText()
} }
val headers = headers { val headers = headers {
append("Accept", ContentType.Application.Activity) append("Accept", ContentType.Application.Activity)
append("ContentType", ContentType.Application.Activity)
append("Date", date) append("Date", date)
append("Host", u.host) append("Host", u.host)
append("Digest", digest) append("Digest", "sha-256=$digest")
} }
val sign = httpSignatureSigner.sign( val sign = httpSignatureSigner.sign(
url, HttpMethod.Get, headers, "", Key( url, HttpMethod.Post, headers, "", Key(
keyId = "${signer.url}#pubkey", keyId = "${signer.url}#pubkey",
privateKey = RsaUtil.decodeRsaPrivateKey(signer.privateKey), privateKey = RsaUtil.decodeRsaPrivateKeyPem(signer.privateKey),
publicKey = RsaUtil.decodeRsaPublicKey(signer.publicKey) publicKey = RsaUtil.decodeRsaPublicKeyPem(signer.publicKey)
), listOf("(request-target)", "date", "host", "digest") ), listOf("(request-target)", "date", "host", "digest")
) )
@ -115,10 +122,10 @@ class APRequestServiceImpl(
headers { headers {
headers { headers {
appendAll(sign.headers) appendAll(sign.headers)
remove("Host")
} }
} }
setBody(requestBody) setBody(requestBody)
contentType(ContentType.Application.Activity)
}.bodyAsText() }.bodyAsText()
} }
} }

View File

@ -14,10 +14,24 @@ object RsaUtil {
fun decodeRsaPublicKey(encoded: String): RSAPublicKey = decodeRsaPublicKey(Base64Util.decode(encoded)) 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 { fun decodeRsaPrivateKey(byteArray: ByteArray): RSAPrivateKey {
val pkcS8EncodedKeySpec = PKCS8EncodedKeySpec(byteArray) val pkcS8EncodedKeySpec = PKCS8EncodedKeySpec(byteArray)
return KeyFactory.getInstance("RSA").generatePrivate(pkcS8EncodedKeySpec) as RSAPrivateKey return KeyFactory.getInstance("RSA").generatePrivate(pkcS8EncodedKeySpec) as RSAPrivateKey
} }
fun decodeRsaPrivateKey(encoded: String): RSAPrivateKey = decodeRsaPrivateKey(Base64Util.decode(encoded)) 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("-----.*?-----")
} }