-
Notifications
You must be signed in to change notification settings - Fork 81
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add option for skipping secret generation (#56)
* Add option for skipping secret generation * Use #nil? * Send options to callbacks
- Loading branch information
1 parent
18107db
commit 3280ecd
Showing
3 changed files
with
29 additions
and
1 deletion.
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,16 @@ | ||
# frozen_string_literal: true | ||
|
||
class OptInTwoFactor | ||
extend ActiveModel::Callbacks | ||
include ActiveModel::Validations | ||
include ActiveModel::OneTimePassword | ||
|
||
define_model_callbacks :create | ||
attr_accessor :otp_secret_key, :email | ||
|
||
has_one_time_password unless: :otp_opt_in? | ||
|
||
def otp_opt_in? | ||
true | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,6 +17,10 @@ def setup | |
@ar_user = ActiverecordUser.new | ||
@ar_user.email = '[email protected]' | ||
@ar_user.run_callbacks :create | ||
|
||
@opt_in = OptInTwoFactor.new | ||
@opt_in.email = '[email protected]' | ||
@opt_in.run_callbacks :create | ||
end | ||
|
||
def test_authenticate_with_otp | ||
|
@@ -49,6 +53,14 @@ def test_counter_based_otp_active_record | |
assert code != @ar_user.otp_code(auto_increment: true) | ||
end | ||
|
||
def test_opt_in_two_factor | ||
assert @opt_in.otp_column.nil? | ||
|
||
@opt_in.otp_regenerate_secret | ||
code = @opt_in.otp_code | ||
assert @opt_in.authenticate_otp(code) | ||
end | ||
|
||
def test_authenticate_with_otp_when_drift_is_allowed | ||
code = @user.otp_code(Time.now - 30) | ||
assert @user.authenticate_otp(code, drift: 60) | ||
|