Skip to content

Commit 90337f5

Browse files
committed
fix after second issue, and make the check time constant
1 parent d9be47a commit 90337f5

File tree

2 files changed

+6
-28
lines changed

2 files changed

+6
-28
lines changed

lib/rotp/totp.rb

+6-3
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,17 @@ def now
3939
def verify(otp, drift_ahead: 0, drift_behind: 0, after: nil, at: Time.now)
4040
now = timeint(at)
4141

42-
timecode_start = timecode([now - drift_behind, after].compact.max)
42+
# we need to skip time <= after
43+
timecode_start = [timecode(now - drift_behind), after ? timecode(after) + 1 : nil].compact.max
4344
timecode_end = timecode(now + drift_ahead)
4445

46+
result = nil
47+
4548
timecode_start.upto(timecode_end) do |t|
46-
return t * interval if super(otp, generate_otp(t))
49+
result = t * interval if super(otp, generate_otp(t))
4750
end
4851

49-
nil
52+
result
5053
end
5154

5255
# Returns the provisioning URI for the OTP

spec/lib/rotp/totp_spec.rb

-25
Original file line numberDiff line numberDiff line change
@@ -101,31 +101,6 @@
101101
end
102102
end
103103

104-
def get_timecodes(at, b, a)
105-
# Test the private method
106-
totp.send('get_timecodes', at, b, a)
107-
end
108-
109-
describe 'drifting timecodes' do
110-
it 'should get timecodes behind' do
111-
expect(get_timecodes(TEST_TIME + 15, 15, 0)).to eq([49_154_040])
112-
expect(get_timecodes(TEST_TIME, 15, 0)).to eq([49_154_039, 49_154_040])
113-
expect(get_timecodes(TEST_TIME, 40, 0)).to eq([49_154_038, 49_154_039, 49_154_040])
114-
expect(get_timecodes(TEST_TIME, 90, 0)).to eq([49_154_037, 49_154_038, 49_154_039, 49_154_040])
115-
end
116-
it 'should get timecodes ahead' do
117-
expect(get_timecodes(TEST_TIME, 0, 15)).to eq([49_154_040])
118-
expect(get_timecodes(TEST_TIME + 15, 0, 15)).to eq([49_154_040, 49_154_041])
119-
expect(get_timecodes(TEST_TIME, 0, 30)).to eq([49_154_040, 49_154_041])
120-
expect(get_timecodes(TEST_TIME, 0, 70)).to eq([49_154_040, 49_154_041, 49_154_042])
121-
expect(get_timecodes(TEST_TIME, 0, 90)).to eq([49_154_040, 49_154_041, 49_154_042, 49_154_043])
122-
end
123-
it 'should get timecodes behind and ahead' do
124-
expect(get_timecodes(TEST_TIME, 30, 30)).to eq([49_154_039, 49_154_040, 49_154_041])
125-
expect(get_timecodes(TEST_TIME, 60, 60)).to eq([49_154_038, 49_154_039, 49_154_040, 49_154_041, 49_154_042])
126-
end
127-
end
128-
129104
describe '#verify with drift' do
130105
let(:verification) { totp.verify token, drift_ahead: drift_ahead, drift_behind: drift_behind, at: now }
131106
let(:drift_ahead) { 0 }

0 commit comments

Comments
 (0)