mirror of https://github.com/usbharu/Hideout.git
fix: 鍵の読み込み方法を修正2
This commit is contained in:
parent
a5188bbb65
commit
5b32b04549
|
@ -22,6 +22,7 @@ import java.net.URI
|
||||||
import java.security.KeyFactory
|
import java.security.KeyFactory
|
||||||
import java.security.PrivateKey
|
import java.security.PrivateKey
|
||||||
import java.security.PublicKey
|
import java.security.PublicKey
|
||||||
|
import java.security.spec.PKCS8EncodedKeySpec
|
||||||
import java.security.spec.X509EncodedKeySpec
|
import java.security.spec.X509EncodedKeySpec
|
||||||
import java.text.SimpleDateFormat
|
import java.text.SimpleDateFormat
|
||||||
import java.util.*
|
import java.util.*
|
||||||
|
@ -160,10 +161,10 @@ class KtorKeyMap(private val userAuthRepository: IUserAuthService) : KeyMap {
|
||||||
val publicBytes = Base64.getDecoder().decode(
|
val publicBytes = Base64.getDecoder().decode(
|
||||||
userAuthRepository.findByUsername(
|
userAuthRepository.findByUsername(
|
||||||
username
|
username
|
||||||
).privateKey?.replace("-----BEGIN PUBLIC KEY-----", "-----END PUBLIC KEY-----")?.replace("", "")
|
).privateKey?.replace("-----BEGIN RSA PRIVATE KEY-----", "")?.replace("-----END RSA PRIVATE KEY-----", "")
|
||||||
?.replace("\\n", "")
|
?.replace("\\n", "")
|
||||||
)
|
)
|
||||||
val x509EncodedKeySpec = X509EncodedKeySpec(publicBytes)
|
val x509EncodedKeySpec = PKCS8EncodedKeySpec(publicBytes)
|
||||||
return@runBlocking KeyFactory.getInstance("RSA").generatePrivate(x509EncodedKeySpec)
|
return@runBlocking KeyFactory.getInstance("RSA").generatePrivate(x509EncodedKeySpec)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -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-----"
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue