Skip to content

Instantly share code, notes, and snippets.

@kmikulski
Created December 5, 2018 21:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kmikulski/7341346fcc1ff5aff6810fa95994cf9e to your computer and use it in GitHub Desktop.
Save kmikulski/7341346fcc1ff5aff6810fa95994cf9e to your computer and use it in GitHub Desktop.
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