Build: enable ktlint+detekt, format sources, add core unit tests
This commit is contained in:
@@ -2,7 +2,8 @@ package ru.ysu.lms.security
|
||||
|
||||
enum class LmsRole {
|
||||
ADMIN,
|
||||
STUDENT;
|
||||
STUDENT,
|
||||
;
|
||||
|
||||
fun authority(): String = "ROLE_$name"
|
||||
}
|
||||
|
||||
@@ -19,14 +19,15 @@ import org.springframework.security.oauth2.jwt.NimbusJwtEncoder
|
||||
@AutoConfiguration
|
||||
@EnableConfigurationProperties(JwtProperties::class)
|
||||
class LmsSecurityAutoConfiguration {
|
||||
|
||||
@Bean
|
||||
@ConditionalOnMissingBean
|
||||
@ConditionalOnProperty(prefix = "lms.jwt", name = ["private-key"])
|
||||
fun jwtEncoder(properties: JwtProperties): JwtEncoder {
|
||||
val rsaKey = RSAKey.Builder(RsaKeys.publicKey(properties.publicKey))
|
||||
.privateKey(RsaKeys.privateKey(properties.privateKey))
|
||||
.build()
|
||||
val rsaKey =
|
||||
RSAKey
|
||||
.Builder(RsaKeys.publicKey(properties.publicKey))
|
||||
.privateKey(RsaKeys.privateKey(properties.privateKey))
|
||||
.build()
|
||||
return NimbusJwtEncoder(ImmutableJWKSet<SecurityContext>(JWKSet(rsaKey)))
|
||||
}
|
||||
|
||||
|
||||
@@ -9,24 +9,26 @@ import java.util.Base64
|
||||
|
||||
/** Parses PEM-encoded RSA keys (PKCS#8 private, X.509 public). */
|
||||
object RsaKeys {
|
||||
|
||||
fun publicKey(pem: String): RSAPublicKey {
|
||||
val der = decode(pem)
|
||||
return KeyFactory.getInstance("RSA")
|
||||
return KeyFactory
|
||||
.getInstance("RSA")
|
||||
.generatePublic(X509EncodedKeySpec(der)) as RSAPublicKey
|
||||
}
|
||||
|
||||
fun privateKey(pem: String): RSAPrivateKey {
|
||||
val der = decode(pem)
|
||||
return KeyFactory.getInstance("RSA")
|
||||
return KeyFactory
|
||||
.getInstance("RSA")
|
||||
.generatePrivate(PKCS8EncodedKeySpec(der)) as RSAPrivateKey
|
||||
}
|
||||
|
||||
private fun decode(pem: String): ByteArray {
|
||||
val body = pem
|
||||
.replace(Regex("-----BEGIN [A-Z ]+-----"), "")
|
||||
.replace(Regex("-----END [A-Z ]+-----"), "")
|
||||
.replace(Regex("\\s"), "")
|
||||
val body =
|
||||
pem
|
||||
.replace(Regex("-----BEGIN [A-Z ]+-----"), "")
|
||||
.replace(Regex("-----END [A-Z ]+-----"), "")
|
||||
.replace(Regex("\\s"), "")
|
||||
return Base64.getDecoder().decode(body)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user