Skip to main content

How to Check a JWT's Issuer and Audience in Ruby with JWT.decode

· 5 min read
Jagdish Kumawat
Founder @ Dewiride

The jwt gem checks a token's issuer and audience when you pass iss: and aud: to JWT.decode together with verify_iss: true and verify_aud: true. Leave out the two verify_ options and the expected values are silently ignored.

Syntax​

Syntax
claims, _header = JWT.decode(<token>, <key>, true,
algorithm: '<alg>',
iss: '<expected iss>', verify_iss: true,
aud: '<expected aud>', verify_aud: true)
PartWhat it does
trueVerify the signature.
algorithm:The signing algorithm you accept.
iss: and verify_iss: trueThe issuer the token must have, and the switch that turns the check on.
aud: and verify_aud: trueThe audience the token's aud must contain, and the switch that turns the check on.

The ruby-jwt README shows both together, JWT.decode(token, hmac_secret, true, { iss: iss, verify_iss: true, algorithm: 'HS256' }), and says that "A token without an iss claim does not pass, and nil never matches."

The Example​

The example signs one token, issued by https://login.example.org for https://photos.example.org. A check method decodes it with an issuer, an audience and a verify: flag. It's called with the right values, with the audience of another API, with another issuer, and finally with another issuer and another audience but verify: false.

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 signs a token for the photos API and defines check with iss, aud and verify keyword arguments passed to JWT.decode as iss, verify_iss, aud and verify_aud, then calls it four times, the last with verify false.

main.rb
require 'jwt'

# 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')

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

def check(token, key, iss:, aud:, verify:)
claims, _header = JWT.decode(token, key, true,
algorithm: 'HS256',
iss: iss, verify_iss: verify,
aud: aud, verify_aud: verify)
puts "accepted: sub #{claims['sub']}"
rescue JWT::DecodeError => e
puts "rejected: #{e.class}: #{e.message}"
end

check(token, key, iss: 'https://login.example.org', aud: 'https://photos.example.org', verify: true)
check(token, key, iss: 'https://login.example.org', aud: 'https://videos.example.org', verify: true)
check(token, key, iss: 'https://other.example.org', aud: 'https://photos.example.org', verify: true)
check(token, key, iss: 'https://other.example.org', aud: 'https://videos.example.org', verify: false)

check takes keyword arguments and passes verify to both verify_iss: and verify_aud:, so one call can show what happens when they're off.

Step 3: Run It​

Terminal
bundle exec ruby main.rb
Output
accepted: sub ada
rejected: JWT::InvalidAudError: Invalid audience. Expected https://videos.example.org, received https://photos.example.org
rejected: JWT::InvalidIssuerError: Invalid issuer. Expected ["https://other.example.org"], received https://login.example.org
accepted: sub ada

A zsh terminal where bundle exec ruby main.rb prints accepted sub ada, then rejected with JWT::InvalidAudError and JWT::InvalidIssuerError, then accepted sub ada again for the call without the verify options.

What Happened​

  • The right issuer and audience passed.
  • The videos audience failed with JWT::InvalidAudError, and the message names what was expected and what the token had.
  • Another issuer failed with JWT::InvalidIssuerError. The expected issuers print as an array, because ruby-jwt keeps them in a list.
  • The last call passed, though both the issuer and the audience were wrong. With verify_iss and verify_aud off, which is their default, ruby-jwt doesn't look at iss: or aud: at all.

Common Mistakes​

MistakeWhat happensDo this instead
Passing iss: and aud: without the verify_ optionsNothing is checked, and tokens from any issuer for any API passAdd verify_iss: true and verify_aud: true
Rescuing only JWT::InvalidAudErrorOther rejections, such as an expired token, go uncaughtRescue JWT::DecodeError, which they all extend
Expecting aud: to match a whole arrayruby-jwt passes when any audience matchesPass the one audience your API is

Troubleshooting​

SymptomLikely causeFix
A token for another API is acceptedverify_aud: true is missingAdd it next to aud:
JWT::InvalidAudError: Invalid audience. Expected ..., received ...The token was made for another APICheck which API the token was made for
JWT::InvalidIssuerError: Invalid issuer. Expected [...], received ...The token comes from another issuerUse the issuer exactly as the token has it

FAQ​

Can I accept several issuers?​

Yes. Pass an array to iss:; the README says "verification will pass if one of them matches the iss value in the payload."

What happens when the token has no aud?​

With verify_aud: true, it fails: the message ends in received <none>.

Conclusion​

Pass iss: and aud: with verify_iss: true and verify_aud: true, and ruby-jwt rejects tokens from other issuers and tokens made for other APIs. Without the two verify_ options, it checks neither.

Additional Resources​