-
Notifications
You must be signed in to change notification settings - Fork 376
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #295 from jamesstonehill/claims-validation
Claims Validation
- Loading branch information
Showing
5 changed files
with
95 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
require_relative './error' | ||
|
||
module JWT | ||
class ClaimsValidator | ||
INTEGER_CLAIMS = %i[ | ||
exp | ||
iat | ||
nbf | ||
].freeze | ||
|
||
def initialize(payload) | ||
@payload = payload.each_with_object({}) { |(k, v), h| h[k.to_sym] = v } | ||
end | ||
|
||
def validate! | ||
validate_int_claims | ||
|
||
true | ||
end | ||
|
||
private | ||
|
||
def validate_int_claims | ||
INTEGER_CLAIMS.each do |claim| | ||
validate_is_int(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) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
require 'spec_helper' | ||
require 'jwt/claims_validator' | ||
|
||
RSpec.describe JWT::ClaimsValidator do | ||
describe '#validate!' do | ||
it 'returns true if the payload is valid' do | ||
valid_payload = { 'exp' => 12345 } | ||
subject = described_class.new(valid_payload) | ||
|
||
expect(subject.validate!).to eq(true) | ||
end | ||
|
||
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 | ||
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 | ||
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 | ||
|
||
string = described_class.new({ claim.to_s => true }) | ||
expect { string.validate! }.to raise_error JWT::InvalidPayload | ||
end | ||
end | ||
|
||
context 'exp claim' do | ||
it_should_behave_like 'an integer claim', :exp | ||
end | ||
|
||
context 'iat claim' do | ||
it_should_behave_like 'an integer claim', :iat | ||
end | ||
|
||
context 'nbf claim' do | ||
it_should_behave_like 'an integer claim', :nbf | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters