Skip to content

Commit

Permalink
Rename ui_otp_verified? to ui_mfa_verified?
Browse files Browse the repository at this point in the history
  • Loading branch information
jenshenny committed May 19, 2023
1 parent a0cd4ae commit f8a5772
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 20 deletions.
2 changes: 1 addition & 1 deletion app/controllers/email_confirmations_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ def token_params
end

def mfa_update_conditions_met?
@user.mfa_enabled? && @user.ui_otp_verified?(params[:otp]) && session_active?
@user.mfa_enabled? && @user.ui_mfa_verified?(params[:otp]) && session_active?
end

def setup_mfa_authentication
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/multifactor_auths_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def create
end

def update
if current_user.ui_otp_verified?(otp_param)
if current_user.ui_mfa_verified?(otp_param)
handle_new_level_param
redirect_to session.fetch("mfa_redirect_uri", edit_settings_path)
session.delete("mfa_redirect_uri")
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/passwords_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ def setup_mfa_authentication
end

def mfa_edit_conditions_met?
@user.mfa_enabled? && @user.ui_otp_verified?(params[:otp]) && session_active?
@user.mfa_enabled? && @user.ui_mfa_verified?(params[:otp]) && session_active?
end

def login_failure(message)
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/sessions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ def setup_mfa_authentication
end

def login_conditions_met?
@user&.mfa_enabled? && @user&.ui_otp_verified?(params[:otp]) && session_active?
@user&.mfa_enabled? && @user&.ui_mfa_verified?(params[:otp]) && session_active?
end

def record_mfa_login_duration(mfa_type:)
Expand Down
4 changes: 2 additions & 2 deletions app/models/concerns/user_multifactor_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def mfa_required_weak_level_enabled?
mfa_required? && mfa_ui_only?
end

def ui_otp_verified?(otp)
def ui_mfa_verified?(otp)
otp = otp.to_s
return true if verify_totp(mfa_seed, otp)
return false unless mfa_recovery_codes.include? otp
Expand All @@ -43,7 +43,7 @@ def ui_otp_verified?(otp)

def api_otp_verified?(otp)
return true if verify_webauthn_otp(otp)
return true if ui_otp_verified?(otp)
return true if ui_mfa_verified?(otp)
false
end

Expand Down
16 changes: 8 additions & 8 deletions test/models/concerns/user_multifactor_methods_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -214,51 +214,51 @@ class UserMultifactorMethodsTest < ActiveSupport::TestCase
end
end

context "#ui_otp_verified?" do
context "#ui_mfa_verified?" do
setup do
@user.enable_totp!(ROTP::Base32.random_base32, :ui_and_api)
end

context "with totp" do
should "return true when correct" do
assert @user.ui_otp_verified?(ROTP::TOTP.new(@user.mfa_seed).now)
assert @user.ui_mfa_verified?(ROTP::TOTP.new(@user.mfa_seed).now)
end

should "return true when correct in last interval" do
last_otp = ROTP::TOTP.new(@user.mfa_seed).at(Time.current - 30)

assert @user.ui_otp_verified?(last_otp)
assert @user.ui_mfa_verified?(last_otp)
end

should "return true when correct in next interval" do
next_otp = ROTP::TOTP.new(@user.mfa_seed).at(Time.current + 30)

assert @user.ui_otp_verified?(next_otp)
assert @user.ui_mfa_verified?(next_otp)
end

should "return false when incorrect" do
refute @user.ui_otp_verified?(ROTP::TOTP.new(ROTP::Base32.random_base32).now)
refute @user.ui_mfa_verified?(ROTP::TOTP.new(ROTP::Base32.random_base32).now)
end

should "return false if the mfa_seed is blank" do
@user.update!(mfa_seed: nil)

refute @user.ui_otp_verified?(ROTP::TOTP.new(ROTP::Base32.random_base32).now)
refute @user.ui_mfa_verified?(ROTP::TOTP.new(ROTP::Base32.random_base32).now)
end
end

context "with webauthn otp" do
should "return false" do
webauthn_verification = create(:webauthn_verification, user: @user)

refute @user.ui_otp_verified?(webauthn_verification.otp)
refute @user.ui_mfa_verified?(webauthn_verification.otp)
end
end

should "return true if recovery code is correct" do
recovery_code = @user.mfa_recovery_codes.first

assert @user.ui_otp_verified?(recovery_code)
assert @user.ui_mfa_verified?(recovery_code)
refute_includes @user.mfa_recovery_codes, recovery_code
end
end
Expand Down
12 changes: 6 additions & 6 deletions test/models/user_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -360,12 +360,12 @@ class UserTest < ActiveSupport::TestCase
should "be able to use a recovery code only once" do
code = @user.mfa_recovery_codes.first

assert @user.ui_otp_verified?(code)
refute @user.ui_otp_verified?(code)
assert @user.ui_mfa_verified?(code)
refute @user.ui_mfa_verified?(code)
end

should "be able to verify correct OTP" do
assert @user.ui_otp_verified?(ROTP::TOTP.new(@user.mfa_seed).now)
assert @user.ui_mfa_verified?(ROTP::TOTP.new(@user.mfa_seed).now)
end

should "return true for mfa status check" do
Expand All @@ -376,13 +376,13 @@ class UserTest < ActiveSupport::TestCase
should "return true for otp in last interval" do
last_otp = ROTP::TOTP.new(@user.mfa_seed).at(Time.current - 30)

assert @user.ui_otp_verified?(last_otp)
assert @user.ui_mfa_verified?(last_otp)
end

should "return true for otp in next interval" do
next_otp = ROTP::TOTP.new(@user.mfa_seed).at(Time.current + 30)

assert @user.ui_otp_verified?(next_otp)
assert @user.ui_mfa_verified?(next_otp)
end

context "blocking user with api key" do
Expand Down Expand Up @@ -414,7 +414,7 @@ class UserTest < ActiveSupport::TestCase
end

should "return false for verifying OTP" do
refute @user.ui_otp_verified?("")
refute @user.ui_mfa_verified?("")
end

should "return false for mfa status check" do
Expand Down

0 comments on commit f8a5772

Please sign in to comment.