Created
December 5, 2018 21:30
-
-
Save kmikulski/7341346fcc1ff5aff6810fa95994cf9e to your computer and use it in GitHub Desktop.
the description for this gist
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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