Skip to content

Commit

Permalink
Iat check uses leeway.
Browse files Browse the repository at this point in the history
  • Loading branch information
aj-michael committed Jul 3, 2015
1 parent af6c87a commit 9c720a6
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/jwt.rb
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ def decode(jwt, key=nil, verify=true, options={}, &keyfinder)
fail JWT::InvalidIssuerError.new("Invalid issuer. Expected #{options['iss']}, received #{payload['iss'] || '<none>'}") unless payload['iss'].to_s == options['iss'].to_s
end
if options[:verify_iat] && payload.include?('iat')
fail JWT::InvalidIatError.new('Invalid iat') unless payload['iat'].is_a?(Integer) && payload['iat'].to_i <= Time.now.to_i
fail JWT::InvalidIatError.new('Invalid iat') unless payload['iat'].is_a?(Integer) && payload['iat'].to_i <= (Time.now.to_i + options[:leeway])
end
if options[:verify_aud] && options['aud']
if payload['aud'].is_a?(Array)
Expand Down
16 changes: 16 additions & 0 deletions spec/jwt_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,22 @@
expect { JWT.decode(example_jwt, example_secret, true, :verify_iat => true, 'iat' => 1425917209) }.to raise_error(JWT::InvalidIatError)
end

it 'raises decode exception when iat is in the future' do
invalid_payload = @payload.clone
invalid_payload['iat'] = Time.now.to_i + 3
secret = 'secret'
jwt = JWT.encode(invalid_payload, secret)
expect { JWT.decode(jwt, secret, true, :verify_iat => true) }.to raise_error(JWT::InvalidIatError)
end

it 'performs normal decode if iat is within leeway' do
invalid_payload = @payload.clone
invalid_payload['iat'] = Time.now.to_i + 3
secret = 'secret'
jwt = JWT.encode(invalid_payload, secret)
expect { JWT.decode(jwt, secret, true, :verify_iat => true, :leeway => 3) }.to_not raise_error
end

it 'decodes valid JWTs with jti' do
example_payload = { 'hello' => 'world', 'iat' => 1425917209, 'jti' => Digest::MD5.hexdigest('secret:1425917209') }
example_secret = 'secret'
Expand Down

0 comments on commit 9c720a6

Please sign in to comment.