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

Tests for iat verification behaviour #423

Merged
merged 1 commit into from Jun 17, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
30 changes: 30 additions & 0 deletions spec/jwt_spec.rb
Expand Up @@ -537,4 +537,34 @@
end.to raise_error(NotImplementedError)
end
end

describe '::JWT.decode with verify_iat parameter' do
let!(:time_now) { Time.now }
let(:token) { ::JWT.encode({ pay: 'load', iat: iat}, 'secret', 'HS256') }

subject(:decoded_token) { ::JWT.decode(token, 'secret', true, verify_iat: true) }

before { allow(Time).to receive(:now) { time_now } }

context 'when iat is exactly the same as Time.now and iat is given as a float' do
let(:iat) { time_now.to_f }
it 'considers iat valid' do
expect(decoded_token).to be_an(Array)
end
end

context 'when iat is exactly the same as Time.now and iat is given as floored integer' do
let(:iat) { time_now.to_f.floor }
it 'considers iat valid' do
expect(decoded_token).to be_an(Array)
end
end

context 'when iat is 1 second before Time.now' do
let(:iat) { time_now.to_i + 1 }
it 'raises an error' do
expect { decoded_token }.to raise_error(::JWT::InvalidIatError, 'Invalid iat')
end
end
end
end