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

Accept expiration claims as string #53

Merged
merged 2 commits into from
Jan 22, 2015
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
2 changes: 1 addition & 1 deletion Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ require 'rubygems'
require 'rake'
require 'echoe'

Echoe.new('jwt', '1.2.0') do |p|
Echoe.new('jwt', '1.2.1') do |p|
p.description = "JSON Web Token implementation in Ruby"
p.url = "http://github.com/progrium/ruby-jwt"
p.author = "Jeff Lindsay"
Expand Down
6 changes: 3 additions & 3 deletions lib/jwt.rb
Original file line number Diff line number Diff line change
Expand Up @@ -99,19 +99,19 @@ def decode(jwt, key=nil, verify=true, options={}, &keyfinder)

header, payload, signature, signing_input = decoded_segments(jwt, verify)
raise JWT::DecodeError.new("Not enough or too many segments") unless header && payload

default_options = {
:verify_expiration => true,
:leeway => 0
}
options = default_options.merge(options)

if verify
algo, key = signature_algorithm_and_key(header, key, &keyfinder)
verify_signature(algo, key, signing_input, signature)
end
if options[:verify_expiration] && payload.include?('exp')
raise JWT::ExpiredSignature.new("Signature has expired") unless payload['exp'] > (Time.now.to_i - options[:leeway])
raise JWT::ExpiredSignature.new("Signature has expired") unless payload['exp'].to_i > (Time.now.to_i - options[:leeway])
end
return payload,header
end
Expand Down
12 changes: 10 additions & 2 deletions spec/jwt_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,15 @@
jwt = JWT.encode(expired_payload, secret)
expect { JWT.decode(jwt, secret) }.to raise_error(JWT::ExpiredSignature)
end


it "raise ExpiredSignature even when exp claims is a string" do
expired_payload = @payload.clone
expired_payload['exp'] = (Time.now.to_i).to_s
secret = "secret"
jwt = JWT.encode(expired_payload, secret)
expect { JWT.decode(jwt, secret) }.to raise_error(JWT::ExpiredSignature)
end

it "performs normal decode with skipped expiration check" do
expired_payload = @payload.clone
expired_payload['exp'] = Time.now.to_i - 1
Expand All @@ -138,7 +146,7 @@
decoded_payload = JWT.decode(jwt, secret, true, {:verify_expiration => false})
expect(decoded_payload).to include(expired_payload)
end

it "performs normal decode using leeway" do
expired_payload = @payload.clone
expired_payload['exp'] = Time.now.to_i - 2
Expand Down