Skip to content

Commit

Permalink
Merge pull request #164 from excpt/master
Browse files Browse the repository at this point in the history
Improve error message for exp claim in payload
  • Loading branch information
excpt authored Sep 16, 2016
2 parents 5cf4cb5 + 9c44e72 commit 673113d
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 673113d

Please sign in to comment.