Skip to main content

How to Add Registered Claims to a JWT in Ruby with JWT.encode

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

With the jwt gem, the registered claims are ordinary keys in the payload hash you pass to JWT.encode: iss, sub, aud, iat, nbf, exp and jti. The three times are Unix seconds, which Time.now.to_i gives you.

Syntax​

Syntax
payload = {
iss: '<issuer>',
sub: '<subject>',
aud: '<audience>',
iat: <seconds>,
nbf: <seconds>,
exp: <seconds>,
jti: '<id>'
}
token = JWT.encode(payload, <key>, '<alg>')
PartWhat it does
iss:, sub:, jti:Strings: who made the token, who it's about and its own ID.
aud:Who the token is for: one string, or an array of strings.
<seconds>A Unix time in seconds, such as Time.now.to_i.
JWT.encode(...)Signs the payload with the key and returns the token.

The ruby-jwt README: "JWT supports these reserved claim names", followed by exp, nbf, iss, aud, jti, iat and sub.

The Example​

The example signs a token for a user named ada, issued by https://login.example.org for https://photos.example.org. It sets iat and nbf to now, exp two hours later and jti to a random UUID, then verifies the token with JWT.decode and prints the claims.

Prerequisites​

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

Step 1: Create the Project​

Terminal
mkdir demo
cd demo
bundle init
bundle add jwt

bundle init writes a Gemfile, and bundle add jwt installs the gem, version 3.3.2 here.

Step 2: Write the Code​

Create main.rb:

main.rb, which builds a payload hash with iss, sub, aud, iat and nbf set to Time.now.to_i, exp two hours later and a SecureRandom.uuid jti, encodes it with JWT.encode, then decodes it with JWT.decode and prints the claims.

main.rb
require 'jwt'
require 'securerandom'

# The signing key: 32 random bytes, written as base64.
# It's in the code only for this example; a real app loads it from its configuration.
key = 'C4iNOOORzmu+fnrPK29yq/9n92b/zf5mOs5KvOMaY64='.unpack1('m0')

now = Time.now.to_i
payload = {
iss: 'https://login.example.org',
sub: 'ada',
aud: 'https://photos.example.org',
iat: now,
nbf: now,
exp: now + (2 * 60 * 60),
jti: SecureRandom.uuid
}
token = JWT.encode(payload, key, 'HS256')
puts token

claims, _header = JWT.decode(token, key, true, algorithm: 'HS256')
p claims

'...'.unpack1('m0') decodes the base64 key into its 32 bytes, and JWT.decode returns the claims and the header, of which _header takes the second.

Step 3: Run It​

Terminal
bundle exec ruby main.rb
Output
eyJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJodHRwczovL2xvZ2luLmV4YW1wbGUub3JnIiwic3ViIjoiYWRhIiwiYXVkIjoiaHR0cHM6Ly9waG90b3MuZXhhbXBsZS5vcmciLCJpYXQiOjE3OTYwNTk0ODcsIm5iZiI6MTc5NjA1OTQ4NywiZXhwIjoxNzk2MDY2Njg3LCJqdGkiOiJkYzk5ZTVhZS03OGZkLTQ3NTEtODA1YS1kNTU0M2ZiNzI2YmIifQ.v-CBpCcA_3dhbbSVXqUEs0rlP-zl9amAGmArnXtGWWY
{"iss" => "https://login.example.org", "sub" => "ada", "aud" => "https://photos.example.org", "iat" => 1796059487, "nbf" => 1796059487, "exp" => 1796066687, "jti" => "dc99e5ae-78fd-4751-805a-d5543fb726bb"}

A zsh terminal where bundle exec ruby main.rb prints the token, then a hash with string keys iss, sub ada, aud, iat and nbf the same number, exp 7200 higher and a jti UUID.

What Happened​

  • The decoded claims have string keys, such as "iss", even though the payload used symbols such as iss:. JSON has no symbols, so the claims come back as strings.
  • exp is 7200 higher than iat: 2 * 60 * 60 seconds.
  • JWT.decode accepted the token: the signature matched, exp wasn't past and nbf wasn't in the future, which ruby-jwt checks by default.

ruby-jwt doesn't check iss or aud unless you ask it to, with verify_iss: true and verify_aud: true.

Common Mistakes​

MistakeWhat happensDo this instead
Reading claims[:iss] after decodingIt's nil: decoded claims have string keysRead claims['iss']
Writing times in millisecondsThe token looks valid for tens of thousands of yearsUse Time.now.to_i, which is in seconds
Leaving out expruby-jwt accepts the token forever unless you require expAlways set an expiry

Troubleshooting​

SymptomLikely causeFix
JWT::ExpiredSignature: Signature has expiredexp is in the pastSet exp in the future
JWT::ImmatureSignature: Signature nbf has not been reachednbf is in the futureSet nbf to now, or leave it out

FAQ​

Do I have to set all seven claims?​

No. The JWT standard makes every registered claim optional. Set the ones your API checks, and always an expiry.

Can aud hold several audiences?​

Yes. Pass an array, as the README does with aud = ['Young', 'Old'].

Conclusion​

Registered claims in ruby-jwt are keys in the payload hash, with Unix seconds for the times. Read them back with string keys after JWT.decode.

Additional Resources​