<div dir="ltr">Hello,<div><br></div><div>another example for (Parsing) & Validating a Keycloak JWT was posted on the ML a few months ago:</div><div><a href="http://lists.jboss.org/pipermail/keycloak-user/2016-March/005325.html">http://lists.jboss.org/pipermail/keycloak-user/2016-March/005325.html</a><br></div><div><br></div><div>In the example the token is only successfully parsed when the token is valid.</div><div><br></div><div>Cheers,</div><div>Thomas</div></div><div class="gmail_extra"><br><div class="gmail_quote">2016-05-11 10:45 GMT+02:00 Gerard Laissard <span dir="ltr"><<a href="mailto:glaissard@axway.com" target="_blank">glaissard@axway.com</a>></span>:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div lang="EN-US" link="blue" vlink="purple">
<div>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1f497d"><u></u> <u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1f497d">My 2 cents:<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1f497d">There is an openSSL example to verify a jwt:<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1f497d"><a href="https://gist.github.com/rolandyoung/176dd310a6948e094be6" target="_blank">https://gist.github.com/rolandyoung/176dd310a6948e094be6</a><u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1f497d"><u></u> <u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1f497d">By using jose4j<u></u><u></u></span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:8.0pt;font-family:"Courier New";color:black"> // be sure you do not have any EOL at the end of the token<u></u><u></u></span></p>
<p class="MsoNormal" style="text-indent:36.0pt;text-autospace:none"><span style="font-size:8.0pt;font-family:"Courier New";color:black">String accesToken = …;<u></u><u></u></span></p>
<p class="MsoNormal" style="text-indent:36.0pt;text-autospace:none"><span style="font-size:8.0pt;font-family:"Courier New";color:black">accesToken = accesToken.replaceAll(</span><span style="font-size:8.0pt;font-family:"Courier New";color:#2a00ff">"\r\n"</span><span style="font-size:8.0pt;font-family:"Courier New";color:black">,
</span><span style="font-size:8.0pt;font-family:"Courier New";color:#2a00ff">""</span><span style="font-size:8.0pt;font-family:"Courier New";color:black">);</span><span style="font-size:8.0pt;font-family:"Courier New""><u></u><u></u></span></p>
<p class="MsoNormal" style="text-indent:36.0pt;text-autospace:none"><span style="font-size:8.0pt;font-family:"Courier New";color:black">accesToken = accesToken.replaceAll(</span><span style="font-size:8.0pt;font-family:"Courier New";color:#2a00ff">"\n"</span><span style="font-size:8.0pt;font-family:"Courier New";color:black">,
</span><span style="font-size:8.0pt;font-family:"Courier New";color:#2a00ff">""</span><span style="font-size:8.0pt;font-family:"Courier New";color:black">);<u></u><u></u></span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:8.0pt;font-family:"Courier New""><u></u> <u></u></span></p>
<p class="MsoNormal" style="text-indent:36.0pt;text-autospace:none"><span style="font-size:8.0pt;font-family:"Courier New";color:black">JsonWebSignature
</span><span style="font-size:8.0pt;font-family:"Courier New";color:#6a3e3e">jws</span><span style="font-size:8.0pt;font-family:"Courier New";color:black"> =
</span><b><span style="font-size:8.0pt;font-family:"Courier New";color:#7f0055">new</span></b><span style="font-size:8.0pt;font-family:"Courier New";color:black"> JsonWebSignature();</span><span style="font-size:8.0pt;font-family:"Courier New""><u></u><u></u></span></p>
<p class="MsoNormal" style="text-indent:36.0pt;text-autospace:none"><span style="font-size:8.0pt;font-family:"Courier New";color:#6a3e3e">jws</span><span style="font-size:8.0pt;font-family:"Courier New";color:black">.setCompactSerialization(accesToken);</span><span style="font-size:8.0pt;font-family:"Courier New""><u></u><u></u></span></p>
<p class="MsoNormal" style="text-indent:36.0pt;text-autospace:none"><span style="font-size:8.0pt;font-family:"Courier New";color:#6a3e3e">jws</span><span style="font-size:8.0pt;font-family:"Courier New";color:black">.setKey(</span><span style="font-size:8.0pt;font-family:"Courier New";color:#6a3e3e">publicKey</span><span style="font-size:8.0pt;font-family:"Courier New";color:black">);<u></u><u></u></span></p>
<p class="MsoNormal" style="margin-left:36.0pt;text-autospace:none"><span style="font-size:8.0pt;font-family:"Courier New";color:black">boolean signatureVerified = jws.verifySignature();<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1f497d">To get a PublicKey : if you put the content of the realm public you get from keycloak admin
<u></u><u></u></span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:8.0pt;font-family:Consolas;color:black">
</span><b><span style="font-size:8.0pt;font-family:"Courier New";color:#7f0055">public</span></b><span style="font-size:8.0pt;font-family:"Courier New";color:black"> PublicKey
<span style="background:silver">getPublicKey</span>(String </span>
<span style="font-size:8.0pt;font-family:"Courier New";color:#6a3e3e">fileName</span><span style="font-size:8.0pt;font-family:"Courier New";color:black">) {</span><span style="font-size:8.0pt;font-family:"Courier New""><u></u><u></u></span></p>
<p class="MsoNormal" style="text-indent:36.0pt;text-autospace:none"><span style="font-size:8.0pt;font-family:"Courier New";color:black">File
</span><span style="font-size:8.0pt;font-family:"Courier New";color:#6a3e3e">f</span><span style="font-size:8.0pt;font-family:"Courier New";color:black"> =
</span><b><span style="font-size:8.0pt;font-family:"Courier New";color:#7f0055">new</span></b><span style="font-size:8.0pt;font-family:"Courier New";color:black"> File(</span><span style="font-size:8.0pt;font-family:"Courier New";color:#6a3e3e">fileName</span><span style="font-size:8.0pt;font-family:"Courier New";color:black">);</span><span style="font-size:8.0pt;font-family:"Courier New""><u></u><u></u></span></p>
<p class="MsoNormal" style="text-indent:36.0pt;text-autospace:none"><b><span style="font-size:8.0pt;font-family:"Courier New";color:#7f0055">try</span></b><span style="font-size:8.0pt;font-family:"Courier New";color:black"> (FileInputStream
</span><span style="font-size:8.0pt;font-family:"Courier New";color:#6a3e3e">fis</span><span style="font-size:8.0pt;font-family:"Courier New";color:black"> =
</span><b><span style="font-size:8.0pt;font-family:"Courier New";color:#7f0055">new</span></b><span style="font-size:8.0pt;font-family:"Courier New";color:black"> FileInputStream(</span><span style="font-size:8.0pt;font-family:"Courier New";color:#6a3e3e">f</span><span style="font-size:8.0pt;font-family:"Courier New";color:black">);
<u></u><u></u></span></p>
<p class="MsoNormal" style="margin-left:36.0pt;text-indent:36.0pt;text-autospace:none">
<span style="font-size:8.0pt;font-family:"Courier New";color:black">DataInputStream
</span><span style="font-size:8.0pt;font-family:"Courier New";color:#6a3e3e">dis</span><span style="font-size:8.0pt;font-family:"Courier New";color:black"> =
</span><b><span style="font-size:8.0pt;font-family:"Courier New";color:#7f0055">new</span></b><span style="font-size:8.0pt;font-family:"Courier New";color:black"> DataInputStream(</span><span style="font-size:8.0pt;font-family:"Courier New";color:#6a3e3e">fis</span><span style="font-size:8.0pt;font-family:"Courier New";color:black">);)
{</span><span style="font-size:8.0pt;font-family:"Courier New""><u></u><u></u></span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:8.0pt;font-family:"Courier New";color:black">
</span><b><span style="font-size:8.0pt;font-family:"Courier New";color:#7f0055">byte</span></b><span style="font-size:8.0pt;font-family:"Courier New";color:black">[]
</span><span style="font-size:8.0pt;font-family:"Courier New";color:#6a3e3e">keyBytes</span><span style="font-size:8.0pt;font-family:"Courier New";color:black"> =
</span><b><span style="font-size:8.0pt;font-family:"Courier New";color:#7f0055">new</span></b><span style="font-size:8.0pt;font-family:"Courier New";color:black">
</span><b><span style="font-size:8.0pt;font-family:"Courier New";color:#7f0055">byte</span></b><span style="font-size:8.0pt;font-family:"Courier New";color:black">[(</span><b><span style="font-size:8.0pt;font-family:"Courier New";color:#7f0055">int</span></b><span style="font-size:8.0pt;font-family:"Courier New";color:black">)
</span><span style="font-size:8.0pt;font-family:"Courier New";color:#6a3e3e">f</span><span style="font-size:8.0pt;font-family:"Courier New";color:black">.length()];</span><span style="font-size:8.0pt;font-family:"Courier New""><u></u><u></u></span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:8.0pt;font-family:"Courier New";color:black">
</span><span style="font-size:8.0pt;font-family:"Courier New";color:#6a3e3e">dis</span><span style="font-size:8.0pt;font-family:"Courier New";color:black">.readFully(</span><span style="font-size:8.0pt;font-family:"Courier New";color:#6a3e3e">keyBytes</span><span style="font-size:8.0pt;font-family:"Courier New";color:black">);</span><span style="font-size:8.0pt;font-family:"Courier New""><u></u><u></u></span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:8.0pt;font-family:"Courier New";color:black">
</span><span style="font-size:8.0pt;font-family:"Courier New";color:#6a3e3e">dis</span><span style="font-size:8.0pt;font-family:"Courier New";color:black">.close();<u></u><u></u></span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:8.0pt;font-family:"Courier New";color:black"> // convert to der format<u></u><u></u></span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:8.0pt;font-family:"Courier New";color:black"> String pem = new String(</span><span style="font-size:8.0pt;font-family:"Courier New";color:#6a3e3e">keyBytes</span><span style="font-size:8.0pt;font-family:"Courier New";color:black">);<u></u><u></u></span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:8.0pt;font-family:"Courier New";color:black">
</span><span style="font-size:8.0pt;font-family:"Courier New";color:#6a3e3e">pem</span><span style="font-size:8.0pt;font-family:"Courier New";color:black"> =
</span><span style="font-size:8.0pt;font-family:"Courier New";color:#6a3e3e">pem</span><span style="font-size:8.0pt;font-family:"Courier New";color:black">.replaceAll(</span><span style="font-size:8.0pt;font-family:"Courier New";color:#2a00ff">"-----BEGIN (.*)-----"</span><span style="font-size:8.0pt;font-family:"Courier New";color:black">,
</span><span style="font-size:8.0pt;font-family:"Courier New";color:#2a00ff">""</span><span style="font-size:8.0pt;font-family:"Courier New";color:black">);</span><span style="font-size:8.0pt;font-family:"Courier New""><u></u><u></u></span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:8.0pt;font-family:"Courier New";color:black">
</span><span style="font-size:8.0pt;font-family:"Courier New";color:#6a3e3e">pem</span><span style="font-size:8.0pt;font-family:"Courier New";color:black"> =
</span><span style="font-size:8.0pt;font-family:"Courier New";color:#6a3e3e">pem</span><span style="font-size:8.0pt;font-family:"Courier New";color:black">.replaceAll(</span><span style="font-size:8.0pt;font-family:"Courier New";color:#2a00ff">"-----END (.*)----"</span><span style="font-size:8.0pt;font-family:"Courier New";color:black">,
</span><span style="font-size:8.0pt;font-family:"Courier New";color:#2a00ff">""</span><span style="font-size:8.0pt;font-family:"Courier New";color:black">);</span><span style="font-size:8.0pt;font-family:"Courier New""><u></u><u></u></span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:8.0pt;font-family:"Courier New";color:black">
</span><span style="font-size:8.0pt;font-family:"Courier New";color:#6a3e3e">pem</span><span style="font-size:8.0pt;font-family:"Courier New";color:black"> =
</span><span style="font-size:8.0pt;font-family:"Courier New";color:#6a3e3e">pem</span><span style="font-size:8.0pt;font-family:"Courier New";color:black">.replaceAll(</span><span style="font-size:8.0pt;font-family:"Courier New";color:#2a00ff">"\r\n"</span><span style="font-size:8.0pt;font-family:"Courier New";color:black">,
</span><span style="font-size:8.0pt;font-family:"Courier New";color:#2a00ff">""</span><span style="font-size:8.0pt;font-family:"Courier New";color:black">);</span><span style="font-size:8.0pt;font-family:"Courier New""><u></u><u></u></span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:8.0pt;font-family:"Courier New";color:black">
</span><span style="font-size:8.0pt;font-family:"Courier New";color:#6a3e3e">pem</span><span style="font-size:8.0pt;font-family:"Courier New";color:black"> =
</span><span style="font-size:8.0pt;font-family:"Courier New";color:#6a3e3e">pem</span><span style="font-size:8.0pt;font-family:"Courier New";color:black">.replaceAll(</span><span style="font-size:8.0pt;font-family:"Courier New";color:#2a00ff">"\n"</span><span style="font-size:8.0pt;font-family:"Courier New";color:black">,
</span><span style="font-size:8.0pt;font-family:"Courier New";color:#2a00ff">""</span><span style="font-size:8.0pt;font-family:"Courier New";color:black">);</span><span style="font-size:8.0pt;font-family:"Courier New""><u></u><u></u></span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:8.0pt;font-family:"Courier New""> byte[] der = Base64.getDecoder().decode(pem); // java 8<u></u><u></u></span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:8.0pt;font-family:"Courier New";color:black">
</span><span lang="FR" style="font-size:8.0pt;font-family:"Courier New";color:black">X509EncodedKeySpec
</span><span lang="FR" style="font-size:8.0pt;font-family:"Courier New";color:#6a3e3e">spec</span><span lang="FR" style="font-size:8.0pt;font-family:"Courier New";color:black"> =
</span><b><span lang="FR" style="font-size:8.0pt;font-family:"Courier New";color:#7f0055">new</span></b><span lang="FR" style="font-size:8.0pt;font-family:"Courier New";color:black"> X509EncodedKeySpec(der);</span><span lang="FR" style="font-size:8.0pt;font-family:"Courier New""><u></u><u></u></span></p>
<p class="MsoNormal" style="text-autospace:none"><span lang="FR" style="font-size:8.0pt;font-family:"Courier New";color:black">
</span><span style="font-size:8.0pt;font-family:"Courier New";color:black">KeyFactory
</span><span style="font-size:8.0pt;font-family:"Courier New";color:#6a3e3e">kf</span><span style="font-size:8.0pt;font-family:"Courier New";color:black"> = KeyFactory.<i>getInstance</i>(</span><b><i><span style="font-size:8.0pt;font-family:"Courier New";color:#0000c0">RSA</span></i></b><span style="font-size:8.0pt;font-family:"Courier New";color:black">);</span><span style="font-size:8.0pt;font-family:"Courier New""><u></u><u></u></span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:8.0pt;font-family:"Courier New";color:black">
</span><b><span style="font-size:8.0pt;font-family:"Courier New";color:#7f0055">return</span></b><span style="font-size:8.0pt;font-family:"Courier New";color:black">
</span><span style="font-size:8.0pt;font-family:"Courier New";color:#6a3e3e">kf</span><span style="font-size:8.0pt;font-family:"Courier New";color:black">.generatePublic(</span><span style="font-size:8.0pt;font-family:"Courier New";color:#6a3e3e">spec</span><span style="font-size:8.0pt;font-family:"Courier New";color:black">);</span><span style="font-size:8.0pt;font-family:"Courier New""><u></u><u></u></span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:8.0pt;font-family:"Courier New""><u></u> <u></u></span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:8.0pt;font-family:"Courier New";color:black"> }
</span><b><span style="font-size:8.0pt;font-family:"Courier New";color:#7f0055">catch</span></b><span style="font-size:8.0pt;font-family:"Courier New";color:black"> (IOException | InvalidKeySpecException | NoSuchAlgorithmException
</span><span style="font-size:8.0pt;font-family:"Courier New";color:#6a3e3e">e</span><span style="font-size:8.0pt;font-family:"Courier New";color:black">) {</span><span style="font-size:8.0pt;font-family:"Courier New""><u></u><u></u></span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:8.0pt;font-family:"Courier New";color:black">
</span><b><span style="font-size:8.0pt;font-family:"Courier New";color:#7f0055">throw</span></b><span style="font-size:8.0pt;font-family:"Courier New";color:black">
</span><b><span style="font-size:8.0pt;font-family:"Courier New";color:#7f0055">new</span></b><span style="font-size:8.0pt;font-family:"Courier New";color:black"> RuntimeException(</span><span style="font-size:8.0pt;font-family:"Courier New";color:#2a00ff">"Failed
to load public key from file '"</span><span style="font-size:8.0pt;font-family:"Courier New";color:black"> +
</span><span style="font-size:8.0pt;font-family:"Courier New";color:#6a3e3e">fileName</span><span style="font-size:8.0pt;font-family:"Courier New";color:black"> +
</span><span style="font-size:8.0pt;font-family:"Courier New";color:#2a00ff">"'"</span><span style="font-size:8.0pt;font-family:"Courier New";color:black">,
</span><span style="font-size:8.0pt;font-family:"Courier New";color:#6a3e3e">e</span><span style="font-size:8.0pt;font-family:"Courier New";color:black">);</span><span style="font-size:8.0pt;font-family:"Courier New""><u></u><u></u></span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:8.0pt;font-family:"Courier New";color:black"> }</span><span style="font-size:8.0pt;font-family:"Courier New""><u></u><u></u></span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:8.0pt;font-family:"Courier New";color:black"> }</span><span style="font-size:8.0pt;font-family:"Courier New""><u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1f497d"><u></u> <u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1f497d">With Java 8, it is quite simple too<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1f497d">
</span><span style="font-size:8.0pt;font-family:Consolas;color:#6a3e3e">String[] tokenParts</span><span style="font-size:8.0pt;font-family:"Calibri","sans-serif";color:#1f497d"> =
</span><span style="font-size:8.0pt;font-family:Consolas;color:#6a3e3e">accessToken.split("\\.");</span><span style="font-size:8.0pt;font-family:"Calibri","sans-serif";color:#1f497d"><u></u><u></u></span></p>
<p class="MsoNormal" style="text-indent:36.0pt"><span style="font-size:8.0pt;font-family:Consolas;color:#6a3e3e">// detect algo from tokenParts[0] or put "SHA256withRSA” (for “RS256”)<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:8.0pt;font-family:"Calibri","sans-serif";color:#1f497d">
</span><span style="font-size:8.0pt;font-family:Consolas;color:#6a3e3e">String jwtSignAlgo = "SHA256withRSA";
<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:8.0pt;font-family:Consolas;color:#6a3e3e"> String jwtInputString = tokenParts[0] + “.” + tokenParts[1];<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:8.0pt;font-family:Consolas;color:#6a3e3e"> String jwtDecodedSign = new String(Base64.getUrlDecoder().decode(tokenParts[2]);</span><span style="font-size:8.0pt;font-family:"Calibri","sans-serif";color:#1f497d"><u></u><u></u></span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:8.0pt;font-family:Consolas;color:#6a3e3e">
</span><span lang="FR" style="font-size:8.0pt;font-family:Consolas;color:#6a3e3e">Signature verifier = Signature.getInstance(jwtSignAlgo);<u></u><u></u></span></p>
<p class="MsoNormal" style="text-autospace:none"><span lang="FR" style="font-size:8.0pt;font-family:Consolas;color:black">
</span><span style="font-size:8.0pt;font-family:Consolas;color:#6a3e3e">verifier</span><span style="font-size:8.0pt;font-family:Consolas;color:black">.initVerify(</span><span style="font-size:8.0pt;font-family:Consolas;color:#6a3e3e">publicKey</span><span style="font-size:8.0pt;font-family:Consolas;color:black">);</span><span style="font-size:8.0pt;font-family:Consolas"><u></u><u></u></span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:8.0pt;font-family:Consolas;color:black">
</span><span style="font-size:8.0pt;font-family:Consolas;color:#6a3e3e">verifier</span><span style="font-size:8.0pt;font-family:Consolas;color:black">.update(</span><span style="font-size:8.0pt;font-family:Consolas;color:#6a3e3e">jwtInputString</span><span style="font-size:8.0pt;font-family:Consolas;color:black">.getBytes("UTF-8"));</span><span style="font-size:8.0pt;font-family:Consolas"><u></u><u></u></span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:8.0pt;font-family:Consolas;color:black"> boolean
</span><span style="font-size:8.0pt;font-family:Consolas;color:#6a3e3e">signatureVerified</span><span style="font-size:8.0pt;font-family:Consolas;color:black"> =
</span><span style="font-size:8.0pt;font-family:Consolas;color:#6a3e3e">verifier</span><span style="font-size:8.0pt;font-family:Consolas;color:black">.verify(</span><span style="font-size:8.0pt;font-family:Consolas;color:#6a3e3e">jwtDecodedSign</span><span style="font-size:8.0pt;font-family:Consolas;color:black">);</span><span style="font-size:8.0pt;font-family:Consolas"><u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1f497d"><u></u> <u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1f497d"><u></u> <u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1f497d">gerard<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1f497d"><u></u> <u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1f497d"><u></u> <u></u></span></p>
<p class="MsoNormal"><b><span style="font-size:10.0pt;font-family:"Tahoma","sans-serif"">From:</span></b><span style="font-size:10.0pt;font-family:"Tahoma","sans-serif""> <a href="mailto:keycloak-user-bounces@lists.jboss.org" target="_blank">keycloak-user-bounces@lists.jboss.org</a> [mailto:<a href="mailto:keycloak-user-bounces@lists.jboss.org" target="_blank">keycloak-user-bounces@lists.jboss.org</a>]
<b>On Behalf Of </b>Stian Thorgersen<br>
<b>Sent:</b> vendredi 6 mai 2016 07:33<br>
<b>To:</b> Aikeaguinea<br>
<b>Cc:</b> keycloak-user<br>
<b>Subject:</b> Re: [keycloak-user] Validating JWT tokens<u></u><u></u></span></p><div><div class="h5">
<p class="MsoNormal"><u></u> <u></u></p>
<div>
<p class="MsoNormal"><u></u> <u></u></p>
<div>
<p class="MsoNormal"><u></u> <u></u></p>
<div>
<p class="MsoNormal">On 4 May 2016 at 18:37, Aikeaguinea <<a href="mailto:aikeaguinea@xsmail.com" target="_blank">aikeaguinea@xsmail.com</a>> wrote:<u></u><u></u></p>
<p class="MsoNormal">Figured it out, kinda. I have to use the Realm public key, and at least<br>
in <a href="http://jwt.io" target="_blank">jwt.io</a> it has to begin with "-----BEGIN PUBLIC KEY-----" and end with<br>
"-----END PUBLIC KEY-----" -- these can't be omitted.<br>
<br>
If I try using the Realm certificate, it won't work, however, whether or<br>
not I use "-----BEGIN CERTIFICATE-----"/"-----END CERTIFICATE-----".<br>
<br>
If I use the validator at <a href="http://kjur.github.io/jsjws/tool_jwt.html" target="_blank">
http://kjur.github.io/jsjws/tool_jwt.html</a> and<br>
select "default X509 Certificate (RSA z4) it tells me "Error: malformed<br>
X.509 certificate PEM (code:003)"<br>
<br>
I can use the Realm public key for validating the JWT, but shouldn't the<br>
certificate work as well?<u></u><u></u></p>
<div>
<p class="MsoNormal"><u></u> <u></u></p>
</div>
<div>
<p class="MsoNormal">The certificate is only used by SAML, so no you can't verify the JWT with the certificate only the public key.<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"> <u></u><u></u></p>
</div>
<blockquote style="border:none;border-left:solid #cccccc 1.0pt;padding:0cm 0cm 0cm 6.0pt;margin-left:4.8pt;margin-right:0cm">
<p class="MsoNormal"><br>
<span>On Wed, May 4, 2016, at 12:00 PM, Aikeaguinea wrote:</span><br>
<span>> I have a client with a service account and credentials using Signed Jwt.</span><br>
<span>> Authentication works fine. The service uses</span><br>
<span>> org.keycloak.adapters.authentication.ClientCredentialsProviderUtils#setClientCredentials</span><br>
<span>> to create the JWT token and set the headers, and I get back a JWT</span><br>
<span>> containing an access token from Keycloak.</span><br>
<span>></span><br>
<span>> However, when I use <a href="http://jwt.io" target="_blank">jwt.io</a> to look at the access token, I can't validate</span><br>
<span>> the signature. This is true whether I use the client Certificate (from</span><br>
<span>> the client's Credentials tab), the Realm public key, or the Realm</span><br>
<span>> Certificate. In addition, I have generated the client's public key from</span><br>
<span>> the certificate using</span><br>
<span>></span><br>
<span>> keytool -exportcert -alias x -keypass y -storepass z -rfc -keystore</span><br>
<span>> client-keystore.jks | openssl x509 -inform pem -pubkey</span><br>
<span>></span><br>
<span>> on the jks file supplied when I generated the client credentials, and</span><br>
<span>> that doesn't work either.</span><br>
<span>></span><br>
<span>> We've also been having trouble validating the signature programmatically</span><br>
<span>> using Java.</span><br>
<span>></span><br>
<span>> Any idea why I might be seeing this?</span><br>
<span>></span><br>
<span>> --</span><br>
<span>> <a href="http://www.fastmail.com" target="_blank">http://www.fastmail.com</a> - Or how I learned to stop worrying and</span><br>
<span>> love email again</span><br>
<span>></span><br>
<br>
<br>
<span><span style="color:#888888">--</span></span><span style="color:#888888"><br>
<span> Aikeaguinea</span><br>
<span> <a href="mailto:aikeaguinea@xsmail.com" target="_blank">aikeaguinea@xsmail.com</a></span><br>
<br>
<span>--</span><br>
<span><a href="http://www.fastmail.com" target="_blank">http://www.fastmail.com</a> - Send your email first class</span></span><u></u><u></u></p>
<div>
<div>
<p class="MsoNormal"><br>
_______________________________________________<br>
keycloak-user mailing list<br>
<a href="mailto:keycloak-user@lists.jboss.org" target="_blank">keycloak-user@lists.jboss.org</a><br>
<a href="https://lists.jboss.org/mailman/listinfo/keycloak-user" target="_blank">https://lists.jboss.org/mailman/listinfo/keycloak-user</a><u></u><u></u></p>
</div>
</div>
</blockquote>
</div>
<p class="MsoNormal"><u></u> <u></u></p>
</div>
</div>
</div></div></div>
</div>
<br>_______________________________________________<br>
keycloak-user mailing list<br>
<a href="mailto:keycloak-user@lists.jboss.org">keycloak-user@lists.jboss.org</a><br>
<a href="https://lists.jboss.org/mailman/listinfo/keycloak-user" rel="noreferrer" target="_blank">https://lists.jboss.org/mailman/listinfo/keycloak-user</a><br></blockquote></div><br></div>