Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Document function and add tests for verify claims method #404

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 12 additions & 0 deletions README.md
Expand Up @@ -291,6 +291,12 @@ rescue JWT::ExpiredSignature
end
```

The Expiration Claim verification can be disabled.
```ruby
# Decode token without raising JWT::ExpiredSignature error
JWT.decode token, hmac_secret, true, { verify_expiration: false, algorithm: 'HS256' }
```

**Adding Leeway**

```ruby
Expand Down Expand Up @@ -331,6 +337,12 @@ rescue JWT::ImmatureSignature
end
```

The Not Before Claim verification can be disabled.
```ruby
# Decode token without raising JWT::ImmatureSignature error
JWT.decode token, hmac_secret, true, { verify_not_before: false, algorithm: 'HS256' }
```

**Adding Leeway**

```ruby
Expand Down
27 changes: 27 additions & 0 deletions spec/jwt/verify_spec.rb
Expand Up @@ -225,5 +225,32 @@ module JWT # rubocop:disable Metrics/ModuleLength
Verify.verify_sub(base_payload.merge('sub' => sub), options.merge(sub: sub))
end
end

context '.verify_claims' do
let(:fail_verifications_options) { { iss: 'mismatched-issuer', aud: 'no-match', sub: 'some subject' } }
let(:fail_verifications_payload) {
{
'exp' => (Time.now.to_i - 50),
'jti' => ' ',
'iss' => 'some-issuer',
'nbf' => (Time.now.to_i + 50),
'iat' => 'not a number',
'sub' => 'not-a-match'
}
}

%w[verify_aud verify_expiration verify_iat verify_iss verify_jti verify_not_before verify_sub].each do |method|
let(:payload) { base_payload.merge(fail_verifications_payload) }
it "must skip verification when #{method} option is set to false" do
Verify.verify_claims(payload, options.merge(method => false))
end

it "must raise error when #{method} option is set to true" do
expect do
Verify.verify_claims(payload, options.merge(method => true).merge(fail_verifications_options))
end.to raise_error
end
end
end
end
end