Skip to content

Commit fb8b178

Browse files
authored
Method authenticate_otp must return falsey if code is nil or empty string (#96)
1 parent a7fee70 commit fb8b178

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

lib/active_model/one_time_password.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ def otp_regenerate_counter
7272
end
7373

7474
def authenticate_otp(code, options = {})
75+
return false if code.nil? || code.empty?
7576
return true if backup_codes_enabled? && authenticate_backup_code(code)
7677

7778
if otp_counter_based

test/one_time_password_test.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,23 @@ def test_authenticate_with_otp
3333
assert @visitor.authenticate_otp(code)
3434
end
3535

36+
def test_authenticate_with_otp_passing_false_or_empty_codes
37+
refute @user.authenticate_otp(nil)
38+
refute @user.authenticate_otp('')
39+
40+
refute @visitor.authenticate_otp(nil)
41+
refute @visitor.authenticate_otp('')
42+
43+
refute @member.authenticate_otp(nil)
44+
refute @member.authenticate_otp('')
45+
46+
refute @ar_user.authenticate_otp(nil)
47+
refute @ar_user.authenticate_otp('')
48+
49+
refute @opt_in.authenticate_otp(nil)
50+
refute @opt_in.authenticate_otp('')
51+
end
52+
3653
def test_counter_based_otp
3754
code = @member.otp_code
3855
assert @member.authenticate_otp(code)

0 commit comments

Comments
 (0)