<div dir="ltr">You can also use <a href="https://github.com/keycloak/keycloak/blob/master/core/src/main/java/org/keycloak/RSATokenVerifier.java">https://github.com/keycloak/keycloak/blob/master/core/src/main/java/org/keycloak/RSATokenVerifier.java</a></div><div class="gmail_extra"><br><div class="gmail_quote">On 11 May 2016 at 15:50, Josh Cain <span dir="ltr">&lt;<a href="mailto:josh.cain@redhat.com" target="_blank">josh.cain@redhat.com</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div>I recently put together a quick test for this as well using jjwt: <a href="https://github.com/cainj13/jwtExamples/blob/master/src/test/java/jcain/example/TokenParseTest.java" target="_blank">https://github.com/cainj13/jwtExamples/blob/master/src/test/java/jcain/example/TokenParseTest.java</a><br><br></div>Pretty similar to the gist that Thomas mentioned above.<span class="HOEnZb"><font color="#888888"><br></font></span></div><div class="gmail_extra"><span class="HOEnZb"><font color="#888888"><br clear="all"><div><div><div dir="ltr"><span><div><div>Josh Cain | Software Applications Engineer<br></div><i>Identity and Access Management</i><br></div><b>Red Hat</b><br><a href="tel:%2B1%20843-737-1735" value="+18437371735" target="_blank">+1 843-737-1735</a><br></span></div></div></div></font></span><div><div class="h5">
<br><div class="gmail_quote">On Wed, May 11, 2016 at 4:09 AM, Thomas Darimont <span dir="ltr">&lt;<a href="mailto:thomas.darimont@googlemail.com" target="_blank">thomas.darimont@googlemail.com</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">Hello,<div><br></div><div>another example for (Parsing) &amp; 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" target="_blank">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><div><div class="gmail_extra"><br><div class="gmail_quote">2016-05-11 10:45 GMT+02:00 Gerard Laissard <span dir="ltr">&lt;<a href="mailto:glaissard@axway.com" target="_blank">glaissard@axway.com</a>&gt;</span>:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">





<div link="blue" vlink="purple" lang="EN-US">
<div>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:&quot;Calibri&quot;,&quot;sans-serif&quot;;color:#1f497d"><u></u> <u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:&quot;Calibri&quot;,&quot;sans-serif&quot;;color:#1f497d">My 2 cents:<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:&quot;Calibri&quot;,&quot;sans-serif&quot;;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:&quot;Calibri&quot;,&quot;sans-serif&quot;;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:&quot;Calibri&quot;,&quot;sans-serif&quot;;color:#1f497d"><u></u> <u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:&quot;Calibri&quot;,&quot;sans-serif&quot;;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:&quot;Courier New&quot;;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:&quot;Courier New&quot;;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:&quot;Courier New&quot;;color:black">accesToken = accesToken.replaceAll(</span><span style="font-size:8.0pt;font-family:&quot;Courier New&quot;;color:#2a00ff">&quot;\r\n&quot;</span><span style="font-size:8.0pt;font-family:&quot;Courier New&quot;;color:black">,
</span><span style="font-size:8.0pt;font-family:&quot;Courier New&quot;;color:#2a00ff">&quot;&quot;</span><span style="font-size:8.0pt;font-family:&quot;Courier New&quot;;color:black">);</span><span style="font-size:8.0pt;font-family:&quot;Courier New&quot;"><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:&quot;Courier New&quot;;color:black">accesToken = accesToken.replaceAll(</span><span style="font-size:8.0pt;font-family:&quot;Courier New&quot;;color:#2a00ff">&quot;\n&quot;</span><span style="font-size:8.0pt;font-family:&quot;Courier New&quot;;color:black">,
</span><span style="font-size:8.0pt;font-family:&quot;Courier New&quot;;color:#2a00ff">&quot;&quot;</span><span style="font-size:8.0pt;font-family:&quot;Courier New&quot;;color:black">);<u></u><u></u></span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:8.0pt;font-family:&quot;Courier New&quot;"><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:&quot;Courier New&quot;;color:black">JsonWebSignature
</span><span style="font-size:8.0pt;font-family:&quot;Courier New&quot;;color:#6a3e3e">jws</span><span style="font-size:8.0pt;font-family:&quot;Courier New&quot;;color:black"> =
</span><b><span style="font-size:8.0pt;font-family:&quot;Courier New&quot;;color:#7f0055">new</span></b><span style="font-size:8.0pt;font-family:&quot;Courier New&quot;;color:black"> JsonWebSignature();</span><span style="font-size:8.0pt;font-family:&quot;Courier New&quot;"><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:&quot;Courier New&quot;;color:#6a3e3e">jws</span><span style="font-size:8.0pt;font-family:&quot;Courier New&quot;;color:black">.setCompactSerialization(accesToken);</span><span style="font-size:8.0pt;font-family:&quot;Courier New&quot;"><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:&quot;Courier New&quot;;color:#6a3e3e">jws</span><span style="font-size:8.0pt;font-family:&quot;Courier New&quot;;color:black">.setKey(</span><span style="font-size:8.0pt;font-family:&quot;Courier New&quot;;color:#6a3e3e">publicKey</span><span style="font-size:8.0pt;font-family:&quot;Courier New&quot;;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:&quot;Courier New&quot;;color:black">boolean signatureVerified = jws.verifySignature();<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:&quot;Calibri&quot;,&quot;sans-serif&quot;;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:&quot;Courier New&quot;;color:#7f0055">public</span></b><span style="font-size:8.0pt;font-family:&quot;Courier New&quot;;color:black"> PublicKey
<span style="background:silver">getPublicKey</span>(String </span>
<span style="font-size:8.0pt;font-family:&quot;Courier New&quot;;color:#6a3e3e">fileName</span><span style="font-size:8.0pt;font-family:&quot;Courier New&quot;;color:black">) {</span><span style="font-size:8.0pt;font-family:&quot;Courier New&quot;"><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:&quot;Courier New&quot;;color:black">File
</span><span style="font-size:8.0pt;font-family:&quot;Courier New&quot;;color:#6a3e3e">f</span><span style="font-size:8.0pt;font-family:&quot;Courier New&quot;;color:black"> =
</span><b><span style="font-size:8.0pt;font-family:&quot;Courier New&quot;;color:#7f0055">new</span></b><span style="font-size:8.0pt;font-family:&quot;Courier New&quot;;color:black"> File(</span><span style="font-size:8.0pt;font-family:&quot;Courier New&quot;;color:#6a3e3e">fileName</span><span style="font-size:8.0pt;font-family:&quot;Courier New&quot;;color:black">);</span><span style="font-size:8.0pt;font-family:&quot;Courier New&quot;"><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:&quot;Courier New&quot;;color:#7f0055">try</span></b><span style="font-size:8.0pt;font-family:&quot;Courier New&quot;;color:black"> (FileInputStream
</span><span style="font-size:8.0pt;font-family:&quot;Courier New&quot;;color:#6a3e3e">fis</span><span style="font-size:8.0pt;font-family:&quot;Courier New&quot;;color:black"> =
</span><b><span style="font-size:8.0pt;font-family:&quot;Courier New&quot;;color:#7f0055">new</span></b><span style="font-size:8.0pt;font-family:&quot;Courier New&quot;;color:black"> FileInputStream(</span><span style="font-size:8.0pt;font-family:&quot;Courier New&quot;;color:#6a3e3e">f</span><span style="font-size:8.0pt;font-family:&quot;Courier New&quot;;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:&quot;Courier New&quot;;color:black">DataInputStream
</span><span style="font-size:8.0pt;font-family:&quot;Courier New&quot;;color:#6a3e3e">dis</span><span style="font-size:8.0pt;font-family:&quot;Courier New&quot;;color:black"> =
</span><b><span style="font-size:8.0pt;font-family:&quot;Courier New&quot;;color:#7f0055">new</span></b><span style="font-size:8.0pt;font-family:&quot;Courier New&quot;;color:black"> DataInputStream(</span><span style="font-size:8.0pt;font-family:&quot;Courier New&quot;;color:#6a3e3e">fis</span><span style="font-size:8.0pt;font-family:&quot;Courier New&quot;;color:black">);)
 {</span><span style="font-size:8.0pt;font-family:&quot;Courier New&quot;"><u></u><u></u></span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:8.0pt;font-family:&quot;Courier New&quot;;color:black">              
</span><b><span style="font-size:8.0pt;font-family:&quot;Courier New&quot;;color:#7f0055">byte</span></b><span style="font-size:8.0pt;font-family:&quot;Courier New&quot;;color:black">[]
</span><span style="font-size:8.0pt;font-family:&quot;Courier New&quot;;color:#6a3e3e">keyBytes</span><span style="font-size:8.0pt;font-family:&quot;Courier New&quot;;color:black"> =
</span><b><span style="font-size:8.0pt;font-family:&quot;Courier New&quot;;color:#7f0055">new</span></b><span style="font-size:8.0pt;font-family:&quot;Courier New&quot;;color:black">
</span><b><span style="font-size:8.0pt;font-family:&quot;Courier New&quot;;color:#7f0055">byte</span></b><span style="font-size:8.0pt;font-family:&quot;Courier New&quot;;color:black">[(</span><b><span style="font-size:8.0pt;font-family:&quot;Courier New&quot;;color:#7f0055">int</span></b><span style="font-size:8.0pt;font-family:&quot;Courier New&quot;;color:black">)
</span><span style="font-size:8.0pt;font-family:&quot;Courier New&quot;;color:#6a3e3e">f</span><span style="font-size:8.0pt;font-family:&quot;Courier New&quot;;color:black">.length()];</span><span style="font-size:8.0pt;font-family:&quot;Courier New&quot;"><u></u><u></u></span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:8.0pt;font-family:&quot;Courier New&quot;;color:black">              
</span><span style="font-size:8.0pt;font-family:&quot;Courier New&quot;;color:#6a3e3e">dis</span><span style="font-size:8.0pt;font-family:&quot;Courier New&quot;;color:black">.readFully(</span><span style="font-size:8.0pt;font-family:&quot;Courier New&quot;;color:#6a3e3e">keyBytes</span><span style="font-size:8.0pt;font-family:&quot;Courier New&quot;;color:black">);</span><span style="font-size:8.0pt;font-family:&quot;Courier New&quot;"><u></u><u></u></span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:8.0pt;font-family:&quot;Courier New&quot;;color:black">              
</span><span style="font-size:8.0pt;font-family:&quot;Courier New&quot;;color:#6a3e3e">dis</span><span style="font-size:8.0pt;font-family:&quot;Courier New&quot;;color:black">.close();<u></u><u></u></span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:8.0pt;font-family:&quot;Courier New&quot;;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:&quot;Courier New&quot;;color:black">               String pem = new String(</span><span style="font-size:8.0pt;font-family:&quot;Courier New&quot;;color:#6a3e3e">keyBytes</span><span style="font-size:8.0pt;font-family:&quot;Courier New&quot;;color:black">);<u></u><u></u></span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:8.0pt;font-family:&quot;Courier New&quot;;color:black">              
</span><span style="font-size:8.0pt;font-family:&quot;Courier New&quot;;color:#6a3e3e">pem</span><span style="font-size:8.0pt;font-family:&quot;Courier New&quot;;color:black"> =
</span><span style="font-size:8.0pt;font-family:&quot;Courier New&quot;;color:#6a3e3e">pem</span><span style="font-size:8.0pt;font-family:&quot;Courier New&quot;;color:black">.replaceAll(</span><span style="font-size:8.0pt;font-family:&quot;Courier New&quot;;color:#2a00ff">&quot;-----BEGIN (.*)-----&quot;</span><span style="font-size:8.0pt;font-family:&quot;Courier New&quot;;color:black">,
</span><span style="font-size:8.0pt;font-family:&quot;Courier New&quot;;color:#2a00ff">&quot;&quot;</span><span style="font-size:8.0pt;font-family:&quot;Courier New&quot;;color:black">);</span><span style="font-size:8.0pt;font-family:&quot;Courier New&quot;"><u></u><u></u></span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:8.0pt;font-family:&quot;Courier New&quot;;color:black">              
</span><span style="font-size:8.0pt;font-family:&quot;Courier New&quot;;color:#6a3e3e">pem</span><span style="font-size:8.0pt;font-family:&quot;Courier New&quot;;color:black"> =
</span><span style="font-size:8.0pt;font-family:&quot;Courier New&quot;;color:#6a3e3e">pem</span><span style="font-size:8.0pt;font-family:&quot;Courier New&quot;;color:black">.replaceAll(</span><span style="font-size:8.0pt;font-family:&quot;Courier New&quot;;color:#2a00ff">&quot;-----END (.*)----&quot;</span><span style="font-size:8.0pt;font-family:&quot;Courier New&quot;;color:black">,
</span><span style="font-size:8.0pt;font-family:&quot;Courier New&quot;;color:#2a00ff">&quot;&quot;</span><span style="font-size:8.0pt;font-family:&quot;Courier New&quot;;color:black">);</span><span style="font-size:8.0pt;font-family:&quot;Courier New&quot;"><u></u><u></u></span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:8.0pt;font-family:&quot;Courier New&quot;;color:black">              
</span><span style="font-size:8.0pt;font-family:&quot;Courier New&quot;;color:#6a3e3e">pem</span><span style="font-size:8.0pt;font-family:&quot;Courier New&quot;;color:black"> =
</span><span style="font-size:8.0pt;font-family:&quot;Courier New&quot;;color:#6a3e3e">pem</span><span style="font-size:8.0pt;font-family:&quot;Courier New&quot;;color:black">.replaceAll(</span><span style="font-size:8.0pt;font-family:&quot;Courier New&quot;;color:#2a00ff">&quot;\r\n&quot;</span><span style="font-size:8.0pt;font-family:&quot;Courier New&quot;;color:black">,
</span><span style="font-size:8.0pt;font-family:&quot;Courier New&quot;;color:#2a00ff">&quot;&quot;</span><span style="font-size:8.0pt;font-family:&quot;Courier New&quot;;color:black">);</span><span style="font-size:8.0pt;font-family:&quot;Courier New&quot;"><u></u><u></u></span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:8.0pt;font-family:&quot;Courier New&quot;;color:black">              
</span><span style="font-size:8.0pt;font-family:&quot;Courier New&quot;;color:#6a3e3e">pem</span><span style="font-size:8.0pt;font-family:&quot;Courier New&quot;;color:black"> =
</span><span style="font-size:8.0pt;font-family:&quot;Courier New&quot;;color:#6a3e3e">pem</span><span style="font-size:8.0pt;font-family:&quot;Courier New&quot;;color:black">.replaceAll(</span><span style="font-size:8.0pt;font-family:&quot;Courier New&quot;;color:#2a00ff">&quot;\n&quot;</span><span style="font-size:8.0pt;font-family:&quot;Courier New&quot;;color:black">,
</span><span style="font-size:8.0pt;font-family:&quot;Courier New&quot;;color:#2a00ff">&quot;&quot;</span><span style="font-size:8.0pt;font-family:&quot;Courier New&quot;;color:black">);</span><span style="font-size:8.0pt;font-family:&quot;Courier New&quot;"><u></u><u></u></span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:8.0pt;font-family:&quot;Courier New&quot;">                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:&quot;Courier New&quot;;color:black">              
</span><span style="font-size:8.0pt;font-family:&quot;Courier New&quot;;color:black" lang="FR">X509EncodedKeySpec
</span><span style="font-size:8.0pt;font-family:&quot;Courier New&quot;;color:#6a3e3e" lang="FR">spec</span><span style="font-size:8.0pt;font-family:&quot;Courier New&quot;;color:black" lang="FR"> =
</span><b><span style="font-size:8.0pt;font-family:&quot;Courier New&quot;;color:#7f0055" lang="FR">new</span></b><span style="font-size:8.0pt;font-family:&quot;Courier New&quot;;color:black" lang="FR"> X509EncodedKeySpec(der);</span><span style="font-size:8.0pt;font-family:&quot;Courier New&quot;" lang="FR"><u></u><u></u></span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:8.0pt;font-family:&quot;Courier New&quot;;color:black" lang="FR">              
</span><span style="font-size:8.0pt;font-family:&quot;Courier New&quot;;color:black">KeyFactory
</span><span style="font-size:8.0pt;font-family:&quot;Courier New&quot;;color:#6a3e3e">kf</span><span style="font-size:8.0pt;font-family:&quot;Courier New&quot;;color:black"> = KeyFactory.<i>getInstance</i>(</span><b><i><span style="font-size:8.0pt;font-family:&quot;Courier New&quot;;color:#0000c0">RSA</span></i></b><span style="font-size:8.0pt;font-family:&quot;Courier New&quot;;color:black">);</span><span style="font-size:8.0pt;font-family:&quot;Courier New&quot;"><u></u><u></u></span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:8.0pt;font-family:&quot;Courier New&quot;;color:black">              
</span><b><span style="font-size:8.0pt;font-family:&quot;Courier New&quot;;color:#7f0055">return</span></b><span style="font-size:8.0pt;font-family:&quot;Courier New&quot;;color:black">
</span><span style="font-size:8.0pt;font-family:&quot;Courier New&quot;;color:#6a3e3e">kf</span><span style="font-size:8.0pt;font-family:&quot;Courier New&quot;;color:black">.generatePublic(</span><span style="font-size:8.0pt;font-family:&quot;Courier New&quot;;color:#6a3e3e">spec</span><span style="font-size:8.0pt;font-family:&quot;Courier New&quot;;color:black">);</span><span style="font-size:8.0pt;font-family:&quot;Courier New&quot;"><u></u><u></u></span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:8.0pt;font-family:&quot;Courier New&quot;"><u></u> <u></u></span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:8.0pt;font-family:&quot;Courier New&quot;;color:black">        }
</span><b><span style="font-size:8.0pt;font-family:&quot;Courier New&quot;;color:#7f0055">catch</span></b><span style="font-size:8.0pt;font-family:&quot;Courier New&quot;;color:black"> (IOException | InvalidKeySpecException | NoSuchAlgorithmException
</span><span style="font-size:8.0pt;font-family:&quot;Courier New&quot;;color:#6a3e3e">e</span><span style="font-size:8.0pt;font-family:&quot;Courier New&quot;;color:black">) {</span><span style="font-size:8.0pt;font-family:&quot;Courier New&quot;"><u></u><u></u></span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:8.0pt;font-family:&quot;Courier New&quot;;color:black">               
</span><b><span style="font-size:8.0pt;font-family:&quot;Courier New&quot;;color:#7f0055">throw</span></b><span style="font-size:8.0pt;font-family:&quot;Courier New&quot;;color:black">
</span><b><span style="font-size:8.0pt;font-family:&quot;Courier New&quot;;color:#7f0055">new</span></b><span style="font-size:8.0pt;font-family:&quot;Courier New&quot;;color:black"> RuntimeException(</span><span style="font-size:8.0pt;font-family:&quot;Courier New&quot;;color:#2a00ff">&quot;Failed
 to load public key from file &#39;&quot;</span><span style="font-size:8.0pt;font-family:&quot;Courier New&quot;;color:black"> +
</span><span style="font-size:8.0pt;font-family:&quot;Courier New&quot;;color:#6a3e3e">fileName</span><span style="font-size:8.0pt;font-family:&quot;Courier New&quot;;color:black"> +
</span><span style="font-size:8.0pt;font-family:&quot;Courier New&quot;;color:#2a00ff">&quot;&#39;&quot;</span><span style="font-size:8.0pt;font-family:&quot;Courier New&quot;;color:black">,
</span><span style="font-size:8.0pt;font-family:&quot;Courier New&quot;;color:#6a3e3e">e</span><span style="font-size:8.0pt;font-family:&quot;Courier New&quot;;color:black">);</span><span style="font-size:8.0pt;font-family:&quot;Courier New&quot;"><u></u><u></u></span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:8.0pt;font-family:&quot;Courier New&quot;;color:black">        }</span><span style="font-size:8.0pt;font-family:&quot;Courier New&quot;"><u></u><u></u></span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:8.0pt;font-family:&quot;Courier New&quot;;color:black">        }</span><span style="font-size:8.0pt;font-family:&quot;Courier New&quot;"><u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:&quot;Calibri&quot;,&quot;sans-serif&quot;;color:#1f497d"><u></u> <u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:&quot;Calibri&quot;,&quot;sans-serif&quot;;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:&quot;Calibri&quot;,&quot;sans-serif&quot;;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:&quot;Calibri&quot;,&quot;sans-serif&quot;;color:#1f497d">  =
</span><span style="font-size:8.0pt;font-family:Consolas;color:#6a3e3e">accessToken.split(&quot;\\.&quot;);</span><span style="font-size:8.0pt;font-family:&quot;Calibri&quot;,&quot;sans-serif&quot;;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 &quot;SHA256withRSA” (for “RS256”)<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:8.0pt;font-family:&quot;Calibri&quot;,&quot;sans-serif&quot;;color:#1f497d">                   
</span><span style="font-size:8.0pt;font-family:Consolas;color:#6a3e3e">String jwtSignAlgo = &quot;SHA256withRSA&quot;;
<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:&quot;Calibri&quot;,&quot;sans-serif&quot;;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 style="font-size:8.0pt;font-family:Consolas;color:#6a3e3e" lang="FR">Signature verifier = Signature.getInstance(jwtSignAlgo);<u></u><u></u></span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:8.0pt;font-family:Consolas;color:black" lang="FR">        
</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(&quot;UTF-8&quot;));</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:&quot;Calibri&quot;,&quot;sans-serif&quot;;color:#1f497d"><u></u> <u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:&quot;Calibri&quot;,&quot;sans-serif&quot;;color:#1f497d"><u></u> <u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:&quot;Calibri&quot;,&quot;sans-serif&quot;;color:#1f497d">gerard<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:&quot;Calibri&quot;,&quot;sans-serif&quot;;color:#1f497d"><u></u> <u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:&quot;Calibri&quot;,&quot;sans-serif&quot;;color:#1f497d"><u></u> <u></u></span></p>
<p class="MsoNormal"><b><span style="font-size:10.0pt;font-family:&quot;Tahoma&quot;,&quot;sans-serif&quot;">From:</span></b><span style="font-size:10.0pt;font-family:&quot;Tahoma&quot;,&quot;sans-serif&quot;"> <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>
<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 &lt;<a href="mailto:aikeaguinea@xsmail.com" target="_blank">aikeaguinea@xsmail.com</a>&gt; 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 &quot;-----BEGIN PUBLIC KEY-----&quot; and end with<br>
&quot;-----END PUBLIC KEY-----&quot; -- these can&#39;t be omitted.<br>
<br>
If I try using the Realm certificate, it won&#39;t work, however, whether or<br>
not I use &quot;-----BEGIN CERTIFICATE-----&quot;/&quot;-----END CERTIFICATE-----&quot;.<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 &quot;default X509 Certificate (RSA z4) it tells me &quot;Error: malformed<br>
X.509 certificate PEM (code:003)&quot;<br>
<br>
I can use the Realm public key for validating the JWT, but shouldn&#39;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&#39;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>&gt; I have a client with a service account and credentials using Signed Jwt.</span><br>
<span>&gt; Authentication works fine. The service uses</span><br>
<span>&gt; org.keycloak.adapters.authentication.ClientCredentialsProviderUtils#setClientCredentials</span><br>
<span>&gt; to create the JWT token and set the headers, and I get back a JWT</span><br>
<span>&gt; containing an access token from Keycloak.</span><br>
<span>&gt;</span><br>
<span>&gt; However, when I use <a href="http://jwt.io" target="_blank">jwt.io</a> to look at the access token, I can&#39;t validate</span><br>
<span>&gt; the signature. This is true whether I use the client Certificate (from</span><br>
<span>&gt; the client&#39;s Credentials tab), the Realm public key, or the Realm</span><br>
<span>&gt; Certificate. In addition, I have generated the client&#39;s public key from</span><br>
<span>&gt; the certificate using</span><br>
<span>&gt;</span><br>
<span>&gt; keytool -exportcert -alias x -keypass y -storepass z -rfc -keystore</span><br>
<span>&gt; client-keystore.jks | openssl x509 -inform pem -pubkey</span><br>
<span>&gt;</span><br>
<span>&gt; on the jks file supplied when I generated the client credentials, and</span><br>
<span>&gt; that doesn&#39;t work either.</span><br>
<span>&gt;</span><br>
<span>&gt; We&#39;ve also been having trouble validating the signature programmatically</span><br>
<span>&gt; using Java.</span><br>
<span>&gt;</span><br>
<span>&gt; Any idea why I might be seeing this?</span><br>
<span>&gt;</span><br>
<span>&gt; --</span><br>
<span>&gt; <a href="http://www.fastmail.com" target="_blank">http://www.fastmail.com</a> - Or how I learned to stop worrying and</span><br>
<span>&gt;                           love email again</span><br>
<span>&gt;</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" target="_blank">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>
</div></div><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" rel="noreferrer" target="_blank">https://lists.jboss.org/mailman/listinfo/keycloak-user</a><br></blockquote></div><br></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>