Repository navigation
Add 2FA interface for extension gems to hook into #5843
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
ca7fdd4
a234984
13cba3f
a8b0923
640052a
b8f9041
1fa776a
ad8659e
03de4ca
365815c
29c9db7
2d4416e
d4d599c
09c4c89
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| class Devise::TwoFactorController < DeviseController | ||
| include Devise::TwoFactor::PendingSignIn | ||
|
|
||
| prepend_before_action :require_no_authentication | ||
| prepend_before_action :set_authenticating_resource | ||
|
|
||
| # Extensions can inject custom actions or override defaults via on_load | ||
| ActiveSupport.run_load_hooks(:devise_two_factor_controller, self) | ||
|
|
||
| # Auto-generate default new_<method> actions for each registered 2FA module. | ||
| # Extensions that injected a custom action via on_load won't be overwritten. | ||
| Devise.two_factor_method_configs.each_key do |mod| | ||
| define_method(:"new_#{mod}") {} unless method_defined?(:"new_#{mod}") | ||
| end | ||
|
|
||
| # POST /users/two_factor | ||
| # All methods POST here. Warden picks the right strategy via valid?. | ||
| # Only 2FA strategies run, so a first-factor strategy can't take the request. | ||
| def create | ||
| self.resource = warden.authenticate!(*devise_mapping.two_factor_strategies, auth_options) | ||
| set_flash_message!(:notice, :signed_in, scope: :"devise.sessions") | ||
| sign_in(resource_name, resource) | ||
| yield resource if block_given? | ||
| respond_with resource, location: after_sign_in_path_for(resource) | ||
| end | ||
|
|
||
| protected | ||
|
|
||
| def auth_options | ||
| default_method = @authenticating_resource.enabled_two_factors.first | ||
| { scope: resource_name, recall: "#{controller_path}#new_#{default_method}" } | ||
| end | ||
|
|
||
| def translation_scope | ||
| 'devise.two_factor' | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def set_authenticating_resource | ||
| @authenticating_resource = resource_from_pending_sign_in_for(resource_name) | ||
| return if @authenticating_resource | ||
|
|
||
| set_flash_message!(:alert, :sign_in_not_initiated, scope: :"devise.failure") | ||
| redirect_to new_session_path(resource_name) | ||
| end | ||
| end | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| module Devise | ||
| module TwoFactorHelper | ||
| # Renders the link partials provided by each registered 2FA extension, | ||
| # excluding the method currently being challenged. Each extension is | ||
| # expected to ship a `devise/two_factor/<method>_link` partial. | ||
| def two_factor_method_links(resource, current_method) | ||
| methods = resource.enabled_two_factors - [current_method] | ||
| safe_join(methods.map { |method| render "devise/two_factor/#{method}_link" }) | ||
| end | ||
| end | ||
| end |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| module Devise | ||
| module Models | ||
| module TwoFactorAuthenticatable | ||
| extend ActiveSupport::Concern | ||
|
|
||
| def self.required_fields(klass) | ||
| [] | ||
| end | ||
|
|
||
| module ClassMethods | ||
| Devise::Models.config(self, :two_factor_methods, :two_factor_within) | ||
|
|
||
| def two_factor_methods=(methods) | ||
| @two_factor_methods = methods | ||
| Array(methods).each do |method_name| | ||
| config = Devise.two_factor_method_configs[method_name] | ||
| raise "Unknown two-factor method: #{method_name}. " \ | ||
| "Did you call Devise.register_two_factor_method?" unless config | ||
| begin | ||
| require config[:model] | ||
| rescue LoadError | ||
| raise unless config[:model].camelize.safe_constantize | ||
| end | ||
| mod = config[:model].camelize.constantize | ||
| include mod | ||
| end | ||
| end | ||
|
|
||
| def two_factor_modules | ||
| Array(two_factor_methods) | ||
| end | ||
| end | ||
|
|
||
| def enabled_two_factors | ||
| self.class.two_factor_modules.select do |method_name| | ||
| send(:"#{method_name}_two_factor_enabled?") | ||
| end | ||
| end | ||
|
|
||
| def two_factor_enabled? | ||
| enabled_two_factors.any? | ||
| end | ||
| end | ||
| end | ||
| end |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,14 +6,20 @@ module Devise | |
| module Strategies | ||
| # Default strategy for signing in a user, based on their email and password in the database. | ||
| class DatabaseAuthenticatable < Authenticatable | ||
| include Devise::TwoFactor::PendingSignIn | ||
|
|
||
| def authenticate! | ||
| resource = password.present? && mapping.to.find_for_database_authentication(authentication_hash) | ||
| hashed = false | ||
|
|
||
| if validate(resource){ hashed = true; resource.valid_password?(password) } | ||
| remember_me(resource) | ||
| resource.after_database_authentication | ||
| success!(resource) | ||
| if resource.respond_to?(:two_factor_enabled?) && resource.two_factor_enabled? | ||
| initiate_two_factor_authentication!(resource) | ||
| else | ||
| remember_me(resource) | ||
| resource.after_database_authentication | ||
| success!(resource) | ||
| end | ||
| end | ||
|
|
||
| # In paranoid mode, hash the password even when a resource doesn't exist for the given authentication key. | ||
|
|
@@ -24,6 +30,19 @@ def authenticate! | |
| Devise.paranoid ? fail(:invalid) : fail(:not_found_in_database) | ||
| end | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def initiate_two_factor_authentication!(resource) | ||
| store_pending_sign_in_for(scope, resource, remember_me: remember_me?) | ||
| default_method = resource.enabled_two_factors.first | ||
| redirect!(new_two_factor_challenge_path(scope, default_method)) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. API-only apps have no session, and a JSON client cannot follow this redirect. devise-webauthn returns a 401 JSON with a signed
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmmm I looked at how Devise handles this today... Overall, I think that for apps without a session, the README suggests |
||
| end | ||
|
|
||
| def new_two_factor_challenge_path(scope, method) | ||
| Rails.application.routes.url_helpers | ||
| .send(:"#{scope}_new_two_factor_#{method}_path") | ||
| end | ||
| end | ||
| end | ||
| end | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
recallalways points to the default method. A user who switches to their other method and enters a wrong code lands on the default method's page. Could the form post the method, checked againstenabled_two_factors?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually, I intentionally did it like this in order to keep it simple for now. I believe we can iterate on this on a follow up PR to tackle this if we want. What do you think?