fix: 鍵の読み込み方法を修正2

This commit is contained in:
usbharu 2023-03-31 13:30:13 +09:00
parent 796ba84c0b
commit d72b87a1dd
Signed by: usbharu
GPG Key ID: 6556747BF94EEBC8
2 changed files with 106 additions and 2 deletions

View File

@ -22,6 +22,7 @@ import java.net.URI
import java.security.KeyFactory
import java.security.PrivateKey
import java.security.PublicKey
import java.security.spec.PKCS8EncodedKeySpec
import java.security.spec.X509EncodedKeySpec
import java.text.SimpleDateFormat
import java.util.*
@ -160,10 +161,10 @@ class KtorKeyMap(private val userAuthRepository: IUserAuthService) : KeyMap {
val publicBytes = Base64.getDecoder().decode(
userAuthRepository.findByUsername(
username
).privateKey?.replace("-----BEGIN PUBLIC KEY-----", "-----END PUBLIC KEY-----")?.replace("", "")
).privateKey?.replace("-----BEGIN RSA PRIVATE KEY-----", "")?.replace("-----END RSA PRIVATE KEY-----", "")
?.replace("\\n", "")
)
val x509EncodedKeySpec = X509EncodedKeySpec(publicBytes)
val x509EncodedKeySpec = PKCS8EncodedKeySpec(publicBytes)
return@runBlocking KeyFactory.getInstance("RSA").generatePrivate(x509EncodedKeySpec)
}

View File

@ -0,0 +1,103 @@
package dev.usbharu.hideout.plugins
import dev.usbharu.hideout.domain.model.User
import dev.usbharu.hideout.domain.model.UserAuthentication
import dev.usbharu.hideout.domain.model.UserAuthenticationEntity
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 org.junit.jupiter.api.Test
import java.security.KeyPairGenerator
import java.security.interfaces.RSAPrivateKey
import java.security.interfaces.RSAPublicKey
import java.util.*
class KtorKeyMapTest {
@Test
fun getPrivateKey() {
val ktorKeyMap = KtorKeyMap(UserAuthService(object : IUserRepository {
override suspend fun create(user: User): UserEntity {
TODO("Not yet implemented")
}
override suspend fun findById(id: Long): UserEntity? {
TODO("Not yet implemented")
}
override suspend fun findByName(name: String): UserEntity? {
return UserEntity(1, "test", "localhost", "test", "")
}
override suspend fun update(userEntity: UserEntity) {
TODO("Not yet implemented")
}
override suspend fun delete(id: Long) {
TODO("Not yet implemented")
}
override suspend fun findAll(): List<User> {
TODO("Not yet implemented")
}
override suspend fun findAllByLimitAndByOffset(limit: Int, offset: Long): List<UserEntity> {
TODO("Not yet implemented")
}
override suspend fun createFollower(id: Long, follower: Long) {
TODO("Not yet implemented")
}
override suspend fun deleteFollower(id: Long, follower: Long) {
TODO("Not yet implemented")
}
override suspend fun findFollowersById(id: Long): List<UserEntity> {
TODO("Not yet implemented")
}
}, object : IUserAuthRepository {
override suspend fun create(userAuthentication: UserAuthentication): UserAuthenticationEntity {
TODO("Not yet implemented")
}
override suspend fun findById(id: Long): UserAuthenticationEntity? {
TODO("Not yet implemented")
}
override suspend fun update(userAuthenticationEntity: UserAuthenticationEntity) {
TODO("Not yet implemented")
}
override suspend fun delete(id: Long) {
TODO("Not yet implemented")
}
override suspend fun findByUserId(id: Long): UserAuthenticationEntity? {
val keyPairGenerator = KeyPairGenerator.getInstance("RSA")
keyPairGenerator.initialize(1024)
val generateKeyPair = keyPairGenerator.generateKeyPair()
return UserAuthenticationEntity(
1, 1, "test", (generateKeyPair.public as RSAPublicKey).toPem(),
(generateKeyPair.private as RSAPrivateKey).toPem()
)
}
}))
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-----"
}
}