add PublicKeyProvider
This commit is contained in:
35
src/main/kotlin/ltd/hlaeja/jwt/util/PublicKeyProvider.kt
Normal file
35
src/main/kotlin/ltd/hlaeja/jwt/util/PublicKeyProvider.kt
Normal file
@@ -0,0 +1,35 @@
|
||||
package ltd.hlaeja.jwt.util
|
||||
|
||||
import java.security.KeyException
|
||||
import java.security.KeyFactory
|
||||
import java.security.interfaces.RSAPublicKey
|
||||
import java.security.spec.X509EncodedKeySpec
|
||||
import java.util.Base64.getDecoder
|
||||
|
||||
object PublicKeyProvider {
|
||||
|
||||
fun load(
|
||||
pemFile: String,
|
||||
): RSAPublicKey = readPublicPemFile(pemFile)
|
||||
.let(PublicKeyProvider::makePublicKey)
|
||||
|
||||
private fun makePublicKey(
|
||||
publicKeyBytes: ByteArray,
|
||||
): RSAPublicKey = KeyFactory.getInstance("RSA")
|
||||
.generatePublic(X509EncodedKeySpec(publicKeyBytes)) as RSAPublicKey
|
||||
|
||||
private fun readPublicPemFile(
|
||||
publicKey: String,
|
||||
): ByteArray = javaClass.classLoader
|
||||
?.getResource(publicKey)
|
||||
?.readText()
|
||||
?.let(PublicKeyProvider::getPublicKeyByteArray)
|
||||
?: throw KeyException("Could not load public key")
|
||||
|
||||
private fun getPublicKeyByteArray(
|
||||
keyText: String,
|
||||
): ByteArray = keyText.replace(Regex("[\r\n]+"), "")
|
||||
.removePrefix("-----BEGIN PUBLIC KEY-----")
|
||||
.removeSuffix("-----END PUBLIC KEY-----")
|
||||
.let { getDecoder().decode(it) }
|
||||
}
|
||||
50
src/test/kotlin/ltd/hlaeja/jwt/util/PublicKeyProviderTest.kt
Normal file
50
src/test/kotlin/ltd/hlaeja/jwt/util/PublicKeyProviderTest.kt
Normal file
@@ -0,0 +1,50 @@
|
||||
package ltd.hlaeja.jwt.util
|
||||
|
||||
import java.security.KeyException
|
||||
import java.security.interfaces.RSAPublicKey
|
||||
import org.assertj.core.api.Assertions.assertThat
|
||||
import org.junit.jupiter.api.Test
|
||||
|
||||
class PublicKeyProviderTest {
|
||||
|
||||
@Test
|
||||
fun `load public key - success`() {
|
||||
// given
|
||||
val pemFilePath = "cert/valid-public-key.pem"
|
||||
|
||||
// when
|
||||
val publicKey: RSAPublicKey = PublicKeyProvider.load(pemFilePath)
|
||||
|
||||
// then
|
||||
assertThat(publicKey).isNotNull
|
||||
assertThat(publicKey.algorithm).isEqualTo("RSA")
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `load public key - file does not exist`() {
|
||||
// given
|
||||
val nonExistentPemFilePath = "cert/non-existent.pem"
|
||||
|
||||
// when exception
|
||||
val exception = org.junit.jupiter.api.assertThrows<KeyException> {
|
||||
PublicKeyProvider.load(nonExistentPemFilePath)
|
||||
}
|
||||
|
||||
// then
|
||||
assertThat(exception.message).isEqualTo("Could not load public key")
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `load public key - file is invalid`() {
|
||||
// given
|
||||
val invalidPemFilePath = "cert/invalid-public-key.pem"
|
||||
|
||||
// when exception
|
||||
val exception = org.junit.jupiter.api.assertThrows<IllegalArgumentException> {
|
||||
PrivateKeyProvider.load(invalidPemFilePath)
|
||||
}
|
||||
|
||||
// then
|
||||
assertThat(exception.message).contains("Illegal base64 character 2d")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user