1
1
require 'spec_helper'
2
2
3
- TEST_TIME = Time . utc 2016 , 9 , 23 , 9 # 2016-09-23 09:00:00 UTC
4
- TEST_TOKEN = " 082630"
3
+ TEST_TIME = Time . utc 2016 , 9 , 23 , 9 # 2016-09-23 09:00:00 UTC
4
+ TEST_TOKEN = ' 082630' . freeze
5
5
6
6
RSpec . describe ROTP ::TOTP do
7
7
let ( :now ) { TEST_TIME }
19
19
let ( :totp ) { ROTP ::TOTP . new ( 'GEZDGNBVGY3TQOJQGEZDGNBVGY3TQOJQ' ) }
20
20
21
21
it 'matches the RFC documentation examples' do
22
- expect ( totp . at 1111111111 ) . to eq '050471'
23
- expect ( totp . at 1234567890 ) . to eq '005924'
24
- expect ( totp . at 2000000000 ) . to eq '279037'
22
+ expect ( totp . at ( 1_111_111_111 ) ) . to eq '050471'
23
+ expect ( totp . at ( 1_234_567_890 ) ) . to eq '005924'
24
+ expect ( totp . at ( 2_000_000_000 ) ) . to eq '279037'
25
25
end
26
-
27
26
end
28
27
end
29
28
30
29
describe '#verify' do
31
30
let ( :verification ) { totp . verify token , at : now }
32
31
33
32
context 'numeric token' do
34
- let ( :token ) { 82630 }
33
+ let ( :token ) { 82_630 }
35
34
36
35
it 'raises an error with an integer' do
37
36
expect { verification } . to raise_error ( ArgumentError )
53
52
end
54
53
55
54
context 'RFC compatibility' do
56
- let ( :totp ) { ROTP ::TOTP . new 'wrn3pqx5uqxqvnqr' }
55
+ let ( :totp ) { ROTP ::TOTP . new 'wrn3pqx5uqxqvnqr' }
57
56
58
57
before do
59
58
Timecop . freeze now
60
59
end
61
60
62
61
context 'correct time based OTP' do
63
62
let ( :token ) { '102705' }
64
- let ( :now ) { Time . at 1297553958 }
63
+ let ( :now ) { Time . at 1_297_553_958 }
65
64
66
65
it 'verifies' do
67
66
expect ( totp . verify ( '102705' ) ) . to be_truthy
75
74
end
76
75
end
77
76
context 'invalidating reused tokens' do
78
- let ( :verification ) {
77
+ let ( :verification ) do
79
78
totp . verify token ,
80
- after : after ,
81
- at : now
82
- }
79
+ after : after ,
80
+ at : now
81
+ end
83
82
let ( :after ) { nil }
84
83
85
84
context 'passing in the `after` timestamp' do
86
- let ( :after ) {
85
+ let ( :after ) do
87
86
totp . verify TEST_TOKEN , after : nil , at : now
88
- }
87
+ end
89
88
90
89
it 'returns a timecode' do
91
90
expect ( after ) . to be_kind_of ( Integer )
@@ -106,23 +105,23 @@ def get_timecodes(at, b, a)
106
105
totp . send ( 'get_timecodes' , at , b , a )
107
106
end
108
107
109
- describe " drifting timecodes" do
108
+ describe ' drifting timecodes' do
110
109
it 'should get timecodes behind' do
111
- expect ( get_timecodes ( TEST_TIME + 15 , 15 , 0 ) ) . to eq ( [ 49154040 ] )
112
- expect ( get_timecodes ( TEST_TIME , 15 , 0 ) ) . to eq ( [ 49154039 , 49154040 ] )
113
- expect ( get_timecodes ( TEST_TIME , 40 , 0 ) ) . to eq ( [ 49154038 , 49154039 , 49154040 ] )
114
- expect ( get_timecodes ( TEST_TIME , 90 , 0 ) ) . to eq ( [ 49154037 , 49154038 , 49154039 , 49154040 ] )
110
+ expect ( get_timecodes ( TEST_TIME + 15 , 15 , 0 ) ) . to eq ( [ 49_154_040 ] )
111
+ expect ( get_timecodes ( TEST_TIME , 15 , 0 ) ) . to eq ( [ 49_154_039 , 49_154_040 ] )
112
+ expect ( get_timecodes ( TEST_TIME , 40 , 0 ) ) . to eq ( [ 49_154_038 , 49_154_039 , 49_154_040 ] )
113
+ expect ( get_timecodes ( TEST_TIME , 90 , 0 ) ) . to eq ( [ 49_154_037 , 49_154_038 , 49_154_039 , 49_154_040 ] )
115
114
end
116
115
it 'should get timecodes ahead' do
117
- expect ( get_timecodes ( TEST_TIME , 0 , 15 ) ) . to eq ( [ 49154040 ] )
118
- expect ( get_timecodes ( TEST_TIME + 15 , 0 , 15 ) ) . to eq ( [ 49154040 , 49154041 ] )
119
- expect ( get_timecodes ( TEST_TIME , 0 , 30 ) ) . to eq ( [ 49154040 , 49154041 ] )
120
- expect ( get_timecodes ( TEST_TIME , 0 , 70 ) ) . to eq ( [ 49154040 , 49154041 , 49154042 ] )
121
- expect ( get_timecodes ( TEST_TIME , 0 , 90 ) ) . to eq ( [ 49154040 , 49154041 , 49154042 , 49154043 ] )
116
+ expect ( get_timecodes ( TEST_TIME , 0 , 15 ) ) . to eq ( [ 49_154_040 ] )
117
+ expect ( get_timecodes ( TEST_TIME + 15 , 0 , 15 ) ) . to eq ( [ 49_154_040 , 49_154_041 ] )
118
+ expect ( get_timecodes ( TEST_TIME , 0 , 30 ) ) . to eq ( [ 49_154_040 , 49_154_041 ] )
119
+ expect ( get_timecodes ( TEST_TIME , 0 , 70 ) ) . to eq ( [ 49_154_040 , 49_154_041 , 49_154_042 ] )
120
+ expect ( get_timecodes ( TEST_TIME , 0 , 90 ) ) . to eq ( [ 49_154_040 , 49_154_041 , 49_154_042 , 49_154_043 ] )
122
121
end
123
122
it 'should get timecodes behind and ahead' do
124
- expect ( get_timecodes ( TEST_TIME , 30 , 30 ) ) . to eq ( [ 49154039 , 49154040 , 49154041 ] )
125
- expect ( get_timecodes ( TEST_TIME , 60 , 60 ) ) . to eq ( [ 49154038 , 49154039 , 49154040 , 49154041 , 49154042 ] )
123
+ expect ( get_timecodes ( TEST_TIME , 30 , 30 ) ) . to eq ( [ 49_154_039 , 49_154_040 , 49_154_041 ] )
124
+ 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
125
end
127
126
end
128
127
@@ -131,7 +130,6 @@ def get_timecodes(at, b, a)
131
130
let ( :drift_ahead ) { 0 }
132
131
let ( :drift_behind ) { 0 }
133
132
134
-
135
133
context 'with an old OTP' do
136
134
let ( :token ) { totp . at TEST_TIME - 30 } # Previous token at 2016-09-23 08:59:30 UTC
137
135
let ( :drift_behind ) { 15 }
@@ -151,7 +149,6 @@ def get_timecodes(at, b, a)
151
149
expect ( verification ) . to be_nil
152
150
end
153
151
end
154
-
155
152
end
156
153
157
154
context 'with a future OTP' do
@@ -166,14 +163,13 @@ def get_timecodes(at, b, a)
166
163
# Tested at 2016-09-23 09:00:20 UTC, and with drift ahead to 2016-09-23 09:00:35 UTC
167
164
# This would therefore include 2 intervals
168
165
context 'inside of drift range' do
169
- let ( :now ) { TEST_TIME + 20 }
166
+ let ( :now ) { TEST_TIME + 20 }
170
167
171
168
it 'is true' do
172
169
expect ( verification ) . to be_truthy
173
170
end
174
171
end
175
172
end
176
-
177
173
end
178
174
179
175
describe '#verify with drift and prevent token reuse' do
@@ -183,7 +179,6 @@ def get_timecodes(at, b, a)
183
179
let ( :after ) { nil }
184
180
185
181
context 'with the `after` timestamp set' do
186
-
187
182
context 'older token' do
188
183
let ( :token ) { totp . at TEST_TIME - 30 }
189
184
let ( :drift_behind ) { 15 }
@@ -194,14 +189,13 @@ def get_timecodes(at, b, a)
194
189
end
195
190
196
191
context 'after it has been used' do
197
- let ( :after ) {
192
+ let ( :after ) do
198
193
totp . verify token , after : nil , at : now , drift_behind : drift_behind
199
- }
194
+ end
200
195
it 'is false' do
201
196
expect ( verification ) . to be_falsey
202
197
end
203
198
end
204
-
205
199
end
206
200
207
201
context 'newer token' do
@@ -215,21 +209,20 @@ def get_timecodes(at, b, a)
215
209
end
216
210
217
211
context 'after it has been used' do
218
- let ( :after ) {
212
+ let ( :after ) do
219
213
totp . verify token , after : nil , at : now , drift_ahead : drift_ahead
220
- }
214
+ end
221
215
it 'is false' do
222
216
expect ( verification ) . to be_falsey
223
217
end
224
218
end
225
-
226
219
end
227
220
end
228
221
end
229
222
230
223
describe '#provisioning_uri' do
231
224
let ( :uri ) { totp . provisioning_uri ( 'mark@percival' ) }
232
- let ( :params ) { CGI :: parse URI :: parse ( uri ) . query }
225
+ let ( :params ) { CGI . parse URI . parse ( uri ) . query }
233
226
234
227
context 'without issuer' do
235
228
it 'has the correct format' do
@@ -302,7 +295,6 @@ def get_timecodes(at, b, a)
302
295
expect ( params [ 'algorithm' ] . first ) . to eq 'SHA256'
303
296
end
304
297
end
305
-
306
298
end
307
299
308
300
describe '#now' do
@@ -312,7 +304,7 @@ def get_timecodes(at, b, a)
312
304
313
305
context 'Google Authenticator' do
314
306
let ( :totp ) { ROTP ::TOTP . new 'wrn3pqx5uqxqvnqr' }
315
- let ( :now ) { Time . at 1297553958 }
307
+ let ( :now ) { Time . at 1_297_553_958 }
316
308
317
309
it 'matches the known output' do
318
310
expect ( totp . now ) . to eq '102705'
@@ -321,12 +313,11 @@ def get_timecodes(at, b, a)
321
313
322
314
context 'Dropbox 26 char secret output' do
323
315
let ( :totp ) { ROTP ::TOTP . new 'tjtpqea6a42l56g5eym73go2oa' }
324
- let ( :now ) { Time . at 1378762454 }
316
+ let ( :now ) { Time . at 1_378_762_454 }
325
317
326
318
it 'matches the known output' do
327
319
expect ( totp . now ) . to eq '747864'
328
320
end
329
321
end
330
322
end
331
-
332
323
end
0 commit comments