-
-
Notifications
You must be signed in to change notification settings - Fork 927
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move TOTP related methods to UserTotpMethods concern
- Loading branch information
1 parent
eeff20f
commit a946093
Showing
4 changed files
with
132 additions
and
116 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
module UserTotpMethods | ||
extend ActiveSupport::Concern | ||
|
||
def disable_totp! | ||
mfa_disabled! | ||
self.mfa_seed = "" | ||
self.mfa_recovery_codes = [] | ||
save!(validate: false) | ||
Mailer.mfa_disabled(id, Time.now.utc).deliver_later | ||
end | ||
|
||
def verify_and_enable_totp!(seed, level, otp, expiry) | ||
if expiry < Time.now.utc | ||
errors.add(:base, I18n.t("multifactor_auths.create.qrcode_expired")) | ||
elsif verify_digit_otp(seed, otp) | ||
enable_totp!(seed, level) | ||
else | ||
errors.add(:base, I18n.t("multifactor_auths.incorrect_otp")) | ||
end | ||
end | ||
|
||
def enable_totp!(seed, level) | ||
self.mfa_level = level | ||
self.mfa_seed = seed | ||
self.mfa_recovery_codes = Array.new(10).map { SecureRandom.hex(6) } | ||
save!(validate: false) | ||
Mailer.mfa_enabled(id, Time.now.utc).deliver_later | ||
end | ||
|
||
private | ||
|
||
def verify_digit_otp(seed, otp) | ||
return false if seed.blank? | ||
|
||
totp = ROTP::TOTP.new(seed) | ||
return false unless totp.verify(otp, drift_behind: 30, drift_ahead: 30) | ||
|
||
save!(validate: false) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
require "test_helper" | ||
|
||
class UserTotpMethodsTest < ActiveSupport::TestCase | ||
include ActionMailer::TestHelper | ||
|
||
setup do | ||
@user = create(:user) | ||
end | ||
|
||
context "#disable_mfa!" do | ||
setup do | ||
@user.enable_mfa!(ROTP::Base32.random_base32, :ui_only) | ||
|
||
perform_enqueued_jobs only: ActionMailer::MailDeliveryJob do | ||
@user.disable_mfa! | ||
end | ||
end | ||
|
||
should "disable mfa" do | ||
assert_predicate @user, :mfa_disabled? | ||
assert_empty @user.mfa_seed | ||
assert_empty @user.mfa_recovery_codes | ||
end | ||
|
||
should "send mfa disabled email" do | ||
assert_emails 1 | ||
|
||
assert_equal "Multi-factor authentication disabled on RubyGems.org", last_email.subject | ||
assert_equal [@user.email], last_email.to | ||
end | ||
end | ||
|
||
context "#verify_and_enable_mfa!" do | ||
setup do | ||
@seed = ROTP::Base32.random_base32 | ||
@expiry = 30.minutes.from_now | ||
end | ||
|
||
should "enable mfa" do | ||
@user.verify_and_enable_mfa!( | ||
@seed, | ||
:ui_and_api, | ||
ROTP::TOTP.new(@seed).now, | ||
@expiry | ||
) | ||
|
||
assert_predicate @user, :mfa_enabled? | ||
end | ||
|
||
should "add error if qr code expired" do | ||
@user.verify_and_enable_mfa!( | ||
@seed, | ||
:ui_and_api, | ||
ROTP::TOTP.new(@seed).now, | ||
5.minutes.ago | ||
) | ||
|
||
refute_predicate @user, :mfa_enabled? | ||
expected_error = "The QR-code and key is expired. Please try registering a new device again." | ||
|
||
assert_contains @user.errors[:base], expected_error | ||
end | ||
|
||
should "add error if otp code is incorrect" do | ||
@user.verify_and_enable_mfa!( | ||
@seed, | ||
:ui_and_api, | ||
ROTP::TOTP.new(ROTP::Base32.random_base32).now, | ||
@expiry | ||
) | ||
|
||
refute_predicate @user, :mfa_enabled? | ||
assert_contains @user.errors[:base], "Your OTP code is incorrect." | ||
end | ||
end | ||
|
||
context "#enable_mfa!" do | ||
setup do | ||
@seed = ROTP::Base32.random_base32 | ||
@level = :ui_and_api | ||
@user.enable_mfa!(@seed, @level) | ||
end | ||
|
||
should "enable mfa" do | ||
assert_equal @seed, @user.mfa_seed | ||
assert_predicate @user, :mfa_ui_and_api? | ||
assert_equal 10, @user.mfa_recovery_codes.length | ||
end | ||
end | ||
end |