Skip to main content

How to Decode a JWT Without Verifying It in Ruby with JWT.decode

· 5 min read
Jagdish Kumawat
Founder @ Dewiride

The jwt gem's JWT.decode normally verifies a token. Pass nil as the key and false as the third argument, and it only reads the token, returning its payload and header without checking anything. That's right for reading a token and wrong for trusting one.

Syntax​

Syntax
require 'jwt'

<payload>, <header> = JWT.decode(<token>, nil, false)
PartWhat it does
<token>The JWT as a string.
nilThe key. None is needed when nothing is verified.
falseThe verify argument: turns the signature and claim checks off.
<payload>, <header>The two hashes JWT.decode returns, unpacked into two variables.

The jwt gem documents JWT.decode as "Decodes a JWT to extract the payload and header", with the third parameter described as "verify [Boolean] whether to verify the JWT signature." Its decoder checks the algorithm, the key, the signature and the claims only if @verify.

The Example​

The example reads a token that another service signed with a key this program doesn't have. The token expired on 1 December 2025, and its header names the key that signed it, key-2025-12, in a kid.

Prerequisites​

  • Ruby, which comes with Bundler. This guide uses Ruby 4.0.10.

Step 1: Create the Project​

Terminal
mkdir demo
cd demo
bundle init
bundle add jwt

bundle init creates a Gemfile, and bundle add jwt adds the jwt gem, version 3.3.2 here, and installs it.

Step 2: Write the Code​

Create main.rb in the demo folder:

main.rb, which requires jwt, holds a token from another service that expired in December 2025, decodes it with JWT.decode, nil and false into payload and header, and prints both with p.

main.rb
require 'jwt'

# A token from another service, signed with a key this program doesn't have.
# It expired on 1 December 2025.
token = 'eyJhbGciOiJIUzI1NiIsImtpZCI6ImtleS0yMDI1LTEyIiwidHlwIjoiSldUIn0.eyJzdWIiOiJhZGEiLCJyb2xlIjoicmVhZGVyIiwiaWF0IjoxNzY0NTQ3MjAwLCJleHAiOjE3NjQ1NTA4MDB9.2EjUXD-_uc78GAdKpsWk7GOlMQztlsdWKQkT-RdKFKE'

payload, header = JWT.decode(token, nil, false)
p header
p payload

Step 3: Run It​

Terminal
bundle exec ruby main.rb
Output
{"alg" => "HS256", "kid" => "key-2025-12", "typ" => "JWT"}
{"sub" => "ada", "role" => "reader", "iat" => 1764547200, "exp" => 1764550800}

A zsh terminal where bundle exec ruby main.rb prints the header with alg HS256, kid key-2025-12 and typ JWT, then the payload with sub ada, role reader, iat and exp.

JWT.decode returned the header and every claim without the key, and didn't mind that exp, 1764550800, is 1 December 2025 at 01:00 UTC.

Decoding Is Not Verifying​

Anyone can write a token with any claims, so these claims prove nothing. Use them to read, never to decide who someone is or what they may do. To trust a token, verify it with the key and the algorithm you expect:

main.rb
payload, _header = JWT.decode(token, key, true, algorithm: 'HS256')

That call checks the signature and the expiry, and raises a JWT::DecodeError when either fails.

Reading the header first is the one common server-side use: a service with several keys reads kid and picks that key from its own list. The header is just as forgeable as the claims, so look it up only among keys you already trust.

Common Mistakes​

MistakeWhat happensDo this instead
Trusting the payload from JWT.decode(token, nil, false)Anyone can send a token with any claimsDecode with the key, true and the algorithm
Expecting the expiry to be checkedWith false, no claim is checked: an expired token decoded without an errorVerify; that checks exp
Forgetting the order of the return valuesJWT.decode returns the payload first, then the headerUnpack as payload, header = ...

Troubleshooting​

SymptomLikely causeFix
JWT::MalformedTokenError: Not enough or too many segmentsThe string isn't three pieces separated by dotsCheck what you passed, including a Bearer prefix
cannot load such file -- jwt (LoadError)The jwt gem isn't installed for the Ruby running the scriptRun bundle add jwt in the project, then bundle exec ruby main.rb

FAQ​

Is there another way to read a token without verifying it?​

Yes. The gem's newer JWT::EncodedToken class has unverified_payload, which "Returns the payload of the JWT token without requiring the signature to have been verified." Its plain payload refuses until you verify.

Is it safe to decode tokens this way in a client?​

For reading, yes, for example to show the user's name or to refresh before exp. The server that receives the token still verifies it.

Conclusion​

JWT.decode(token, nil, false) returns a token's payload and header without a key and without any checks. Use it to look inside a token; decode with the key, true and the algorithm before you trust it.

Additional Resources​