Skip to content

Commit

Permalink
Allow Numeric values during encoding
Browse files Browse the repository at this point in the history
  • Loading branch information
fanfilmu authored and excpt committed Sep 1, 2020
1 parent ba2244f commit 9251b31
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 27 deletions.
16 changes: 9 additions & 7 deletions lib/jwt/claims_validator.rb
Expand Up @@ -2,7 +2,7 @@

module JWT
class ClaimsValidator
INTEGER_CLAIMS = %i[
NUMERIC_CLAIMS = %i[
exp
iat
nbf
Expand All @@ -13,21 +13,23 @@ def initialize(payload)
end

def validate!
validate_int_claims
validate_numeric_claims

true
end

private

def validate_int_claims
INTEGER_CLAIMS.each do |claim|
validate_is_int(claim) if @payload.key?(claim)
def validate_numeric_claims
NUMERIC_CLAIMS.each do |claim|
validate_is_numeric(claim) if @payload.key?(claim)
end
end

def validate_is_int(claim)
raise InvalidPayload, "#{claim} claim must be an Integer but it is a #{@payload[claim].class}" unless @payload[claim].is_a?(Integer)
def validate_is_numeric(claim)
return if @payload[claim].is_a?(Numeric)

raise InvalidPayload, "#{claim} claim must be a Numeric value but it is a #{@payload[claim].class}"
end
end
end
75 changes: 55 additions & 20 deletions spec/jwt/claims_validator_spec.rb
Expand Up @@ -2,44 +2,79 @@
require 'jwt/claims_validator'

RSpec.describe JWT::ClaimsValidator do
let(:validator) { described_class.new(claims) }

describe '#validate!' do
it 'returns true if the payload is valid' do
valid_payload = { 'exp' => 12345 }
subject = described_class.new(valid_payload)
subject { validator.validate! }

expect(subject.validate!).to eq(true)
end
shared_examples_for 'a NumericDate claim' do |claim|
context "when #{claim} payload is an integer" do
let(:claims) { { claim => 12345 } }

it 'does not raise error' do
expect { subject }.not_to raise_error
end

context 'and key is a string' do
let(:claims) { { claim.to_s => 43.32 } }

it 'does not raise error' do
expect { subject }.not_to raise_error
end
end
end

context "when #{claim} payload is a float" do
let(:claims) { { claim => 43.32 } }

shared_examples_for 'an integer claim' do |claim|
it "raises an error when the value of the #{claim} claim is a string" do
subject = described_class.new({ claim => '1' })
expect { subject.validate! }.to raise_error JWT::InvalidPayload
it 'does not raise error' do
expect { subject }.not_to raise_error
end
end

it "raises an error when the value of the #{claim} claim is a Time object" do
subject = described_class.new({ claim => Time.now })
expect { subject.validate! }.to raise_error JWT::InvalidPayload
context "when #{claim} payload is a string" do
let(:claims) { { claim => '1' } }

it 'raises error' do
expect { subject }.to raise_error JWT::InvalidPayload
end

context 'and key is a string' do
let(:claims) { { claim.to_s => '1' } }

it 'raises error' do
expect { subject }.to raise_error JWT::InvalidPayload
end
end
end

context "when #{claim} payload is a Time object" do
let(:claims) { { claim => Time.now } }

it 'raises error' do
expect { subject }.to raise_error JWT::InvalidPayload
end
end

it "validates the #{claim} claim when the key is either a string or a symbol" do
symbol = described_class.new({ claim.to_sym => true })
expect { symbol.validate! }.to raise_error JWT::InvalidPayload
context "when #{claim} payload is a string" do
let(:claims) { { claim => '1' } }

string = described_class.new({ claim.to_s => true })
expect { string.validate! }.to raise_error JWT::InvalidPayload
it 'raises error' do
expect { subject }.to raise_error JWT::InvalidPayload
end
end
end

context 'exp claim' do
it_should_behave_like 'an integer claim', :exp
it_should_behave_like 'a NumericDate claim', :exp
end

context 'iat claim' do
it_should_behave_like 'an integer claim', :iat
it_should_behave_like 'a NumericDate claim', :iat
end

context 'nbf claim' do
it_should_behave_like 'an integer claim', :nbf
it_should_behave_like 'a NumericDate claim', :nbf
end
end
end

0 comments on commit 9251b31

Please sign in to comment.