Skip to content

Commit

Permalink
Improve error message for exp claim in payload
Browse files Browse the repository at this point in the history
A better error message is displayed if a Time object is used for the exp
claim instead of an integer unix time stamp.

Fixes #148.
  • Loading branch information
excpt committed Sep 16, 2016
1 parent 5cf4cb5 commit 9c44e72
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/jwt.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ def encoded_header(algorithm = 'HS256', header_fields = {})
end

def encoded_payload(payload)
raise InvalidPayload, "exp claim must be an integer" if payload['exp'] && payload['exp'].is_a?(Time)
base64url_encode(encode_json(payload))
end

Expand Down
1 change: 1 addition & 0 deletions lib/jwt/error.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ class InvalidIatError < DecodeError; end
class InvalidAudError < DecodeError; end
class InvalidSubError < DecodeError; end
class InvalidJtiError < DecodeError; end
class InvalidPayload < DecodeError; end
end
8 changes: 8 additions & 0 deletions spec/jwt_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,14 @@
expect(header['alg']).to eq alg
expect(jwt_payload).to eq payload
end

it 'should display a better error message if payload exp is_a?(Time)' do
payload['exp'] = Time.now

expect do
JWT.encode payload, nil, alg
end.to raise_error JWT::InvalidPayload
end
end

%w(HS256 HS384 HS512).each do |alg|
Expand Down

0 comments on commit 9c44e72

Please sign in to comment.