add PrivateKeyProvider
This commit is contained in:
35
src/main/kotlin/ltd/hlaeja/jwt/util/PrivateKeyProvider.kt
Normal file
35
src/main/kotlin/ltd/hlaeja/jwt/util/PrivateKeyProvider.kt
Normal file
@@ -0,0 +1,35 @@
|
|||||||
|
package ltd.hlaeja.jwt.util
|
||||||
|
|
||||||
|
import java.security.KeyException
|
||||||
|
import java.security.KeyFactory
|
||||||
|
import java.security.interfaces.RSAPrivateKey
|
||||||
|
import java.security.spec.PKCS8EncodedKeySpec
|
||||||
|
import java.util.Base64.getDecoder
|
||||||
|
|
||||||
|
object PrivateKeyProvider {
|
||||||
|
|
||||||
|
fun load(
|
||||||
|
pemFile: String,
|
||||||
|
): RSAPrivateKey = readPrivatePemFile(pemFile)
|
||||||
|
.let(PrivateKeyProvider::makePrivateKey)
|
||||||
|
|
||||||
|
private fun makePrivateKey(
|
||||||
|
privateKeyBytes: ByteArray,
|
||||||
|
): RSAPrivateKey = KeyFactory.getInstance("RSA")
|
||||||
|
.generatePrivate(PKCS8EncodedKeySpec(privateKeyBytes)) as RSAPrivateKey
|
||||||
|
|
||||||
|
private fun readPrivatePemFile(
|
||||||
|
privateKey: String,
|
||||||
|
): ByteArray = javaClass.classLoader
|
||||||
|
?.getResource(privateKey)
|
||||||
|
?.readText()
|
||||||
|
?.let(PrivateKeyProvider::getPrivateKeyByteArray)
|
||||||
|
?: throw KeyException("Could not load private key")
|
||||||
|
|
||||||
|
private fun getPrivateKeyByteArray(
|
||||||
|
keyText: String,
|
||||||
|
): ByteArray = keyText.replace(Regex("[\r\n]+"), "")
|
||||||
|
.removePrefix("-----BEGIN PRIVATE KEY-----")
|
||||||
|
.removeSuffix("-----END PRIVATE KEY-----")
|
||||||
|
.let { getDecoder().decode(it) }
|
||||||
|
}
|
||||||
@@ -0,0 +1,51 @@
|
|||||||
|
package ltd.hlaeja.jwt.util
|
||||||
|
|
||||||
|
import java.security.KeyException
|
||||||
|
import java.security.interfaces.RSAPrivateKey
|
||||||
|
import org.assertj.core.api.Assertions.assertThat
|
||||||
|
import org.junit.jupiter.api.Test
|
||||||
|
import org.junit.jupiter.api.assertThrows
|
||||||
|
|
||||||
|
class PrivateKeyProviderTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `load private key - success`() {
|
||||||
|
// given
|
||||||
|
val pemFilePath = "cert/valid-private-key.pem"
|
||||||
|
|
||||||
|
// when
|
||||||
|
val privateKey: RSAPrivateKey = PrivateKeyProvider.load(pemFilePath)
|
||||||
|
|
||||||
|
// then
|
||||||
|
assertThat(privateKey).isNotNull
|
||||||
|
assertThat(privateKey.algorithm).isEqualTo("RSA")
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `load private key - file does not exist`() {
|
||||||
|
// given
|
||||||
|
val nonExistentPemFilePath = "cert/non-existent.pem"
|
||||||
|
|
||||||
|
// when exception
|
||||||
|
val exception = assertThrows<KeyException> {
|
||||||
|
PrivateKeyProvider.load(nonExistentPemFilePath)
|
||||||
|
}
|
||||||
|
|
||||||
|
// then
|
||||||
|
assertThat(exception.message).isEqualTo("Could not load private key")
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `load private key - file is invalid`() {
|
||||||
|
// given
|
||||||
|
val invalidPemFilePath = "cert/invalid-private-key.pem"
|
||||||
|
|
||||||
|
// when exception
|
||||||
|
val exception = assertThrows<IllegalArgumentException> {
|
||||||
|
PrivateKeyProvider.load(invalidPemFilePath)
|
||||||
|
}
|
||||||
|
|
||||||
|
// then
|
||||||
|
assertThat(exception.message).contains("Input byte array has wrong 4-byte ending unit")
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user