@@ -9,7 +9,8 @@ class Auth::SessionsController < Devise::SessionsController
99 skip_before_action :require_no_authentication , only : [ :create ]
1010 skip_before_action :require_functional!
1111
12- prepend_before_action :authenticate_with_two_factor , if : :two_factor_enabled? , only : [ :create ]
12+ include TwoFactorAuthenticationConcern
13+ include SignInTokenAuthenticationConcern
1314
1415 before_action :set_instance_presenter , only : [ :new ]
1516 before_action :set_body_classes
@@ -40,8 +41,8 @@ def destroy
4041 protected
4142
4243 def find_user
43- if session [ :otp_user_id ]
44- User . find ( session [ :otp_user_id ] )
44+ if session [ :attempt_user_id ]
45+ User . find ( session [ :attempt_user_id ] )
4546 else
4647 user = User . authenticate_with_ldap ( user_params ) if Devise . ldap_authentication
4748 user ||= User . authenticate_with_pam ( user_params ) if Devise . pam_authentication
@@ -51,7 +52,7 @@ def find_user
5152 end
5253
5354 def user_params
54- params . require ( :user ) . permit ( :email , :password , :otp_attempt )
55+ params . require ( :user ) . permit ( :email , :password , :otp_attempt , :sign_in_token_attempt )
5556 end
5657
5758 def after_sign_in_path_for ( resource )
@@ -72,47 +73,6 @@ def after_sign_out_path_for(_resource_or_scope)
7273 super
7374 end
7475
75- def two_factor_enabled?
76- find_user &.otp_required_for_login?
77- end
78-
79- def valid_otp_attempt? ( user )
80- user . validate_and_consume_otp! ( user_params [ :otp_attempt ] ) ||
81- user . invalidate_otp_backup_code! ( user_params [ :otp_attempt ] )
82- rescue OpenSSL ::Cipher ::CipherError
83- false
84- end
85-
86- def authenticate_with_two_factor
87- user = self . resource = find_user
88-
89- if user_params [ :otp_attempt ] . present? && session [ :otp_user_id ]
90- authenticate_with_two_factor_via_otp ( user )
91- elsif user . present? && ( user . encrypted_password . blank? || user . valid_password? ( user_params [ :password ] ) )
92- # If encrypted_password is blank, we got the user from LDAP or PAM,
93- # so credentials are already valid
94-
95- prompt_for_two_factor ( user )
96- end
97- end
98-
99- def authenticate_with_two_factor_via_otp ( user )
100- if valid_otp_attempt? ( user )
101- session . delete ( :otp_user_id )
102- remember_me ( user )
103- sign_in ( user )
104- else
105- flash . now [ :alert ] = I18n . t ( 'users.invalid_otp_token' )
106- prompt_for_two_factor ( user )
107- end
108- end
109-
110- def prompt_for_two_factor ( user )
111- session [ :otp_user_id ] = user . id
112- @body_classes = 'lighter'
113- render :two_factor
114- end
115-
11676 def require_no_authentication
11777 super
11878 # Delete flash message that isn't entirely useful and may be confusing in
0 commit comments