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

Improve error message for exp claim in payload #164

Merged
merged 1 commit into from
Sep 16, 2016
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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