Skip to content

Commit 44aa0bf

Browse files
authored
Merge pull request #3 from clef/fix/add-passwords-controller
Add passwords controller for reset password case
2 parents 8c1d96d + 1cdd4ff commit 44aa0bf

File tree

6 files changed

+64
-18
lines changed

6 files changed

+64
-18
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class DeviseInstant2fa::PasswordsController < Devise::PasswordsController
2+
def sign_in(resource_or_scope, *args)
3+
resource = args.last || resource_or_scope
4+
5+
if resource.respond_to?(:with_instant2fa_verification_url) && resource.with_instant2fa_verification_url
6+
7+
true
8+
else
9+
super
10+
end
11+
end
12+
end

lib/devise_instant2fa.rb

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,10 @@ module DeviseInstant2fa
3535
NEED_AUTHENTICATION = 'need_two_factor_authentication'
3636
HOSTED_PAGE_URL = 'hosted_page_url'
3737

38+
autoload :Mapping, 'devise_instant2fa/mapping'
39+
3840
module Controllers
41+
autoload :Passwords, 'devise_instant2fa/controllers/passwords'
3942
autoload :Helpers, 'devise_instant2fa/controllers/helpers'
4043
end
4144

@@ -44,18 +47,9 @@ module Views
4447
end
4548
end
4649

47-
module Devise
48-
module Models
49-
module Instant2faAuthenticatable
50-
def instant2fa_settings_url
51-
Instant2fa.create_settings(self.id)
52-
end
53-
end
54-
end
55-
end
50+
require 'devise_instant2fa/routes'
51+
require 'devise_instant2fa/rails'
52+
require 'devise_instant2fa/models/instant2fa_authenticatable'
5653

5754
Devise.add_module :instant2fa_authenticatable, :controller => :instant2fa, :route => :instant2fa
5855

59-
require 'devise_instant2fa/hooks/instant2fa_authenticatable'
60-
require 'devise_instant2fa/routes'
61-
require 'devise_instant2fa/rails'
Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
require 'instant2fa'
22

33
Warden::Manager.after_authentication do |user, auth, options|
4-
begin
5-
hosted_page_url = Instant2fa.create_verification(user.id.to_s)
6-
auth.session(options[:scope])[DeviseInstant2fa::NEED_AUTHENTICATION] = true
7-
auth.session(options[:scope])[DeviseInstant2fa::HOSTED_PAGE_URL] = hosted_page_url
8-
auth.session(options[:scope])[:id] = user.id
9-
rescue Instant2fa::Errors::MFANotEnabled
4+
if user.respond_to?(:with_instant2fa_verification_url)
5+
if hosted_page_url = user.with_instant2fa_verification_url
6+
auth.session(options[:scope])[DeviseInstant2fa::NEED_AUTHENTICATION] = true
7+
auth.session(options[:scope])[DeviseInstant2fa::HOSTED_PAGE_URL] = hosted_page_url
8+
auth.session(options[:scope])[:id] = user.id
9+
end
1010
end
1111
end

lib/devise_instant2fa/mapping.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
module DeviseInstant2fa
2+
module Mapping
3+
private
4+
5+
def default_controllers(options)
6+
options[:controllers] ||= {}
7+
options[:controllers][:passwords] ||= "devise_instant2fa/passwords"
8+
super
9+
end
10+
end
11+
end
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
require 'devise_instant2fa/hooks/instant2fa_authenticatable'
2+
3+
module Devise
4+
module Models
5+
module Instant2faAuthenticatable
6+
def instant2fa_settings_url
7+
Instant2fa.create_settings(self.id.to_s)
8+
end
9+
10+
def with_instant2fa_verification_url
11+
begin
12+
hosted_page_url = Instant2fa.create_verification(self.id.to_s)
13+
return hosted_page_url
14+
rescue Instant2fa::Errors::MFANotEnabled
15+
end
16+
17+
return false
18+
end
19+
end
20+
end
21+
end

lib/devise_instant2fa/rails.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,13 @@ class Engine < ::Rails::Engine
33
ActiveSupport.on_load(:action_controller) do
44
include DeviseInstant2fa::Controllers::Helpers
55
end
6+
7+
ActiveSupport.on_load(:action_view) do
8+
include DeviseInstant2fa::Views::Helpers
9+
end
10+
11+
config.after_initialize do
12+
Devise::Mapping.send :prepend, DeviseInstant2fa::Mapping
13+
end
614
end
715
end

0 commit comments

Comments
 (0)