How to Check a JWT's Issuer and Audience in Ruby with JWT.decode
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
claims, _header = JWT.decode(<token>, <key>, true,
algorithm: '<alg>',
iss: '<expected iss>', verify_iss: true,
aud: '<expected aud>', verify_aud: true)
| Part | What it does |
|---|---|
true | Verify the signature. |
algorithm: | The signing algorithm you accept. |
iss: and verify_iss: true | The issuer the token must have, and the switch that turns the check on. |
aud: and verify_aud: true | The 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
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:

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
bundle exec ruby main.rb
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

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_issandverify_audoff, which is their default, ruby-jwt doesn't look atiss:oraud:at all.
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
Passing iss: and aud: without the verify_ options | Nothing is checked, and tokens from any issuer for any API pass | Add verify_iss: true and verify_aud: true |
Rescuing only JWT::InvalidAudError | Other rejections, such as an expired token, go uncaught | Rescue JWT::DecodeError, which they all extend |
Expecting aud: to match a whole array | ruby-jwt passes when any audience matches | Pass the one audience your API is |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
| A token for another API is accepted | verify_aud: true is missing | Add it next to aud: |
JWT::InvalidAudError: Invalid audience. Expected ..., received ... | The token was made for another API | Check which API the token was made for |
JWT::InvalidIssuerError: Invalid issuer. Expected [...], received ... | The token comes from another issuer | Use 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
- Issuer Claim and Audience Claim, in the ruby-jwt README.
- RFC 8725 section 3.9: Use and Validate Audience.
