From 7086322a399dc778d5c42e05683c28a22fc2257e Mon Sep 17 00:00:00 2001 From: usbharu <64310155+usbharu@users.noreply.github.com> Date: Fri, 31 Mar 2023 17:39:38 +0900 Subject: [PATCH] =?UTF-8?q?fix:=20=E9=8D=B5=E3=81=AE=E5=BD=A2=E5=BC=8F?= =?UTF-8?q?=E3=82=92=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../usbharu/hideout/plugins/ActivityPub.kt | 4 ++-- .../hideout/service/UserAuthService.kt | 24 +++++++++++-------- .../hideout/plugins/ActivityPubKtTest.kt | 13 +--------- .../usbharu/hideout/plugins/KtorKeyMapTest.kt | 13 +--------- 4 files changed, 18 insertions(+), 36 deletions(-) diff --git a/src/main/kotlin/dev/usbharu/hideout/plugins/ActivityPub.kt b/src/main/kotlin/dev/usbharu/hideout/plugins/ActivityPub.kt index 8cd9cfdc..a11146ae 100644 --- a/src/main/kotlin/dev/usbharu/hideout/plugins/ActivityPub.kt +++ b/src/main/kotlin/dev/usbharu/hideout/plugins/ActivityPub.kt @@ -152,7 +152,7 @@ class KtorKeyMap(private val userAuthRepository: IUserAuthService) : KeyMap { userAuthRepository.findByUsername( username ).publicKey?.replace("-----BEGIN PUBLIC KEY-----", "-----END PUBLIC KEY-----")?.replace("", "") - ?.replace("\\n", "") + ?.replace("\n", "") ) val x509EncodedKeySpec = X509EncodedKeySpec(publicBytes) return@runBlocking KeyFactory.getInstance("RSA").generatePublic(x509EncodedKeySpec) @@ -165,7 +165,7 @@ class KtorKeyMap(private val userAuthRepository: IUserAuthService) : KeyMap { userAuthRepository.findByUsername( username ).privateKey?.replace("-----BEGIN RSA PRIVATE KEY-----", "")?.replace("-----END RSA PRIVATE KEY-----", "") - ?.replace("\\n", "") + ?.replace("\n", "") ) val x509EncodedKeySpec = PKCS8EncodedKeySpec(publicBytes) return@runBlocking KeyFactory.getInstance("RSA").generatePrivate(x509EncodedKeySpec) diff --git a/src/main/kotlin/dev/usbharu/hideout/service/UserAuthService.kt b/src/main/kotlin/dev/usbharu/hideout/service/UserAuthService.kt index 5f740315..35812420 100644 --- a/src/main/kotlin/dev/usbharu/hideout/service/UserAuthService.kt +++ b/src/main/kotlin/dev/usbharu/hideout/service/UserAuthService.kt @@ -12,6 +12,8 @@ import io.ktor.util.* import java.security.KeyPair import java.security.KeyPairGenerator import java.security.MessageDigest +import java.security.PrivateKey +import java.security.PublicKey import java.security.interfaces.RSAPrivateKey import java.security.interfaces.RSAPublicKey import java.util.* @@ -81,19 +83,21 @@ class UserAuthService( } - private fun RSAPublicKey.toPem(): String { - return "-----BEGIN RSA PUBLIC KEY-----" + - Base64.getEncoder().encodeToString(encoded) + - "-----END RSA PUBLIC KEY-----" - } - private fun RSAPrivateKey.toPem(): String { - return "-----BEGIN RSA PRIVATE KEY-----" + - Base64.getEncoder().encodeToString(encoded) + - "-----END RSA PRIVATE KEY-----" - } companion object { val sha256: MessageDigest = MessageDigest.getInstance("SHA-256") } } + +public fun PublicKey.toPem(): String { + return "-----BEGIN RSA PUBLIC KEY-----" + + Base64.getEncoder().encodeToString(encoded).chunked(64).joinToString("\n") + + "-----END RSA PUBLIC KEY-----" +} + +public fun PrivateKey.toPem(): String { + return "-----BEGIN RSA PRIVATE KEY-----" + + Base64.getEncoder().encodeToString(encoded).chunked(64).joinToString("\n") + + "-----END RSA PRIVATE KEY-----" +} diff --git a/src/test/kotlin/dev/usbharu/hideout/plugins/ActivityPubKtTest.kt b/src/test/kotlin/dev/usbharu/hideout/plugins/ActivityPubKtTest.kt index 8d303ef8..8c7e7d76 100644 --- a/src/test/kotlin/dev/usbharu/hideout/plugins/ActivityPubKtTest.kt +++ b/src/test/kotlin/dev/usbharu/hideout/plugins/ActivityPubKtTest.kt @@ -8,6 +8,7 @@ import dev.usbharu.hideout.domain.model.UserEntity import dev.usbharu.hideout.repository.IUserAuthRepository import dev.usbharu.hideout.repository.IUserRepository import dev.usbharu.hideout.service.UserAuthService +import dev.usbharu.hideout.service.toPem import io.ktor.client.* import io.ktor.client.engine.mock.* import io.ktor.client.plugins.logging.* @@ -107,16 +108,4 @@ class ActivityPubKtTest { } - - fun RSAPublicKey.toPem(): String { - return "-----BEGIN RSA PUBLIC KEY-----" + - Base64.getEncoder().encodeToString(encoded) + - "-----END RSA PUBLIC KEY-----" - } - - fun RSAPrivateKey.toPem(): String { - return "-----BEGIN RSA PRIVATE KEY-----" + - Base64.getEncoder().encodeToString(encoded) + - "-----END RSA PRIVATE KEY-----" - } } diff --git a/src/test/kotlin/dev/usbharu/hideout/plugins/KtorKeyMapTest.kt b/src/test/kotlin/dev/usbharu/hideout/plugins/KtorKeyMapTest.kt index b89418e0..9f9a7cfe 100644 --- a/src/test/kotlin/dev/usbharu/hideout/plugins/KtorKeyMapTest.kt +++ b/src/test/kotlin/dev/usbharu/hideout/plugins/KtorKeyMapTest.kt @@ -7,6 +7,7 @@ import dev.usbharu.hideout.domain.model.UserEntity import dev.usbharu.hideout.repository.IUserAuthRepository import dev.usbharu.hideout.repository.IUserRepository import dev.usbharu.hideout.service.UserAuthService +import dev.usbharu.hideout.service.toPem import org.junit.jupiter.api.Test import java.security.KeyPairGenerator import java.security.interfaces.RSAPrivateKey @@ -88,16 +89,4 @@ class KtorKeyMapTest { ktorKeyMap.getPrivateKey("test") } - - private fun RSAPublicKey.toPem(): String { - return "-----BEGIN RSA PUBLIC KEY-----" + - Base64.getEncoder().encodeToString(encoded) + - "-----END RSA PUBLIC KEY-----" - } - - private fun RSAPrivateKey.toPem(): String { - return "-----BEGIN RSA PRIVATE KEY-----" + - Base64.getEncoder().encodeToString(encoded) + - "-----END RSA PRIVATE KEY-----" - } }