Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
the description for this gist
case class Keys(keys: Seq[KeyData])
case class KeyData(kid: String, n: String, e: String)
implicit val keyDataFormat = jsonFormat3(KeyData)
implicit val keysFormat = jsonFormat1(Keys)
lazy val publicKeys: Future[Map[String, PublicKey]] =
Http().singleRequest(HttpRequest(uri = keycloakDeployment.getJwksUrl)).flatMap(response => {
Unmarshal(response).to[Keys].map(_.keys.map(k => (k.kid, generateKey(k))).toMap)
})
private def generateKey(keyData: KeyData): PublicKey = {
val keyFactory = KeyFactory.getInstance(AlgorithmType.RSA.toString)
val urlDecoder = Base64.getUrlDecoder
val modulus = new BigInteger(1, urlDecoder.decode(keyData.n))
val publicExponent = new BigInteger(1, urlDecoder.decode(keyData.e))
keyFactory.generatePublic(new RSAPublicKeySpec(modulus, publicExponent))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.