<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: 康栋普</title>
    <description>The latest articles on DEV Community by 康栋普 (@_3500661dde8a08068ca67).</description>
    <link>https://dev.to/_3500661dde8a08068ca67</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4181076%2F6edaad2e-9d04-4032-b720-aaeb82f53adc.png</url>
      <title>DEV Community: 康栋普</title>
      <link>https://dev.to/_3500661dde8a08068ca67</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/_3500661dde8a08068ca67"/>
    <language>en</language>
    <item>
      <title>How JWTs Work, and Why Decoding Is Not Verification</title>
      <dc:creator>康栋普</dc:creator>
      <pubDate>Tue, 13 Oct 2026 05:29:10 +0000</pubDate>
      <link>https://dev.to/_3500661dde8a08068ca67/how-jwts-work-and-why-decoding-is-not-verification-4pk5</link>
      <guid>https://dev.to/_3500661dde8a08068ca67/how-jwts-work-and-why-decoding-is-not-verification-4pk5</guid>
      <description>&lt;p&gt;JSON Web Tokens are everywhere: authentication, single sign-on, API authorization. They are also one of the most misunderstood pieces of the modern web — mostly because people confuse &lt;strong&gt;decoding&lt;/strong&gt; a token with &lt;strong&gt;verifying&lt;/strong&gt; it.&lt;/p&gt;

&lt;p&gt;Here is what is actually inside a JWT, and the one mistake that quietly turns a "secure" auth system into an open door.&lt;/p&gt;

&lt;h2&gt;
  
  
  The three parts
&lt;/h2&gt;

&lt;p&gt;A JWT is just a string with three dot-separated segments:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;header.payload.signature
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Header&lt;/strong&gt; — the token type and the signing algorithm (e.g. HS256, RS256).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Payload&lt;/strong&gt; — the claims: user ID, roles, expiry (&lt;code&gt;exp&lt;/code&gt;), issuer (&lt;code&gt;iss&lt;/code&gt;), audience (&lt;code&gt;aud&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Signature&lt;/strong&gt; — what lets a trusted party confirm the token was not tampered with.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The first two segments are Base64URL-encoded JSON. That is all. They are &lt;strong&gt;not encrypted&lt;/strong&gt;, and anyone holding the token can read them.&lt;/p&gt;

&lt;h2&gt;
  
  
  Decoding is not verification
&lt;/h2&gt;

&lt;p&gt;This is the part that bites people.&lt;/p&gt;

&lt;p&gt;Decoding a JWT reveals its contents — nothing more. It does not prove the token was issued by your server, or that it has not been modified.&lt;/p&gt;

&lt;p&gt;To actually trust a token you must:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Verify the signature with the correct key or secret.&lt;/li&gt;
&lt;li&gt;Check &lt;code&gt;exp&lt;/code&gt; — the token has not expired.&lt;/li&gt;
&lt;li&gt;Check &lt;code&gt;iss&lt;/code&gt; and &lt;code&gt;aud&lt;/code&gt; match what you expect.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Skip any of those and you have effectively built an API where anyone can mint themselves an admin token.&lt;/p&gt;

&lt;h2&gt;
  
  
  Decoding a payload by hand
&lt;/h2&gt;

&lt;p&gt;Sometimes you just want to see what is inside. The payload is Base64URL, so:&lt;/p&gt;

&lt;p&gt;JavaScript&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[,&lt;/span&gt; &lt;span class="nx"&gt;payload&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;token&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;split&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;.&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;json&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;JSON&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;parse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;atob&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;payload&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;replace&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sr"&gt;/-/g&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;+&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;replace&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sr"&gt;/_/g&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)))&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;json&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Python&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;base64&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;json&lt;/span&gt;

&lt;span class="n"&gt;payload&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;token&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;split&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;.&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="n"&gt;padding&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nf"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;payload&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;json&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;loads&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;base64&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;urlsafe_b64decode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;payload&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;padding&lt;/span&gt;&lt;span class="p"&gt;)))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;PHP&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nv"&gt;$header&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$payload&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$sig&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;explode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'.'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$token&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nb"&gt;base64_decode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;strtr&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$payload&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'-_'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'+/'&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Note that none of these snippets verify anything — they only decode.&lt;/p&gt;

&lt;h2&gt;
  
  
  Common pitfalls
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Putting secrets in the payload.&lt;/strong&gt; Anyone can read it.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Trusting claims without verifying the signature.&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Ignoring &lt;code&gt;exp&lt;/code&gt; and &lt;code&gt;aud&lt;/code&gt;.&lt;/strong&gt; An expired token that never expires is a gift to an attacker.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Accepting the algorithm from the token itself.&lt;/strong&gt; Always pin the expected &lt;code&gt;alg&lt;/code&gt; on the server side.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  FAQ
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Can anyone read a JWT?&lt;/strong&gt; Yes. The header and payload are encoded, not encrypted.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Does decoding prove the token is valid?&lt;/strong&gt; No. Validity comes from signature and claim verification.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Is my token uploaded when I inspect it?&lt;/strong&gt; It should not be. Some online decoders send the token to a server, which is a bad idea for a credential — decoding can happen entirely in the browser.&lt;/p&gt;




&lt;p&gt;If you need to eyeball a token quickly, I built a free &lt;a href="https://nulltools.dev/tools/jwt-decoder" rel="noopener noreferrer"&gt;JWT Decoder&lt;/a&gt; that runs entirely client-side, so nothing leaves your browser. (It is my own project — the explanation above stands on its own.)&lt;/p&gt;

</description>
      <category>jwt</category>
      <category>security</category>
      <category>webdev</category>
      <category>javascript</category>
    </item>
  </channel>
</rss>
