summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com>2015-05-14 20:23:30 +0000
committerDmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com>2015-05-14 20:23:30 +0000
commitaa4f085184f24272eef48e1da23557459957fbe7 (patch)
treec3f41c33e644b04a4cd9bf2aaa5f3084ac62e0c7
parent37bc4bb19934c4a0eb84fd5e2556363f55d4d601 (diff)
parentc802d8eee171d3e4c0cd7e6a16f69f1001255d07 (diff)
downloadgitlab-ce-aa4f085184f24272eef48e1da23557459957fbe7.tar.gz
Merge branch 'rs-refactor-2fa' into 'master'
Refactor SessionsController to use a controller concern See merge request !659
-rw-r--r--app/controllers/concerns/authenticates_with_two_factor.rb30
-rw-r--r--app/controllers/sessions_controller.rb12
2 files changed, 33 insertions, 9 deletions
diff --git a/app/controllers/concerns/authenticates_with_two_factor.rb b/app/controllers/concerns/authenticates_with_two_factor.rb
new file mode 100644
index 00000000000..d5918a7af3b
--- /dev/null
+++ b/app/controllers/concerns/authenticates_with_two_factor.rb
@@ -0,0 +1,30 @@
+# == AuthenticatesWithTwoFactor
+#
+# Controller concern to handle two-factor authentication
+#
+# Upon inclusion, skips `require_no_authentication` on `:create`.
+module AuthenticatesWithTwoFactor
+ extend ActiveSupport::Concern
+
+ included do
+ # This action comes from DeviseController, but because we call `sign_in`
+ # manually, not skipping this action would cause a "You are already signed
+ # in." error message to be shown upon successful login.
+ skip_before_action :require_no_authentication, only: [:create]
+ end
+
+ # Store the user's ID in the session for later retrieval and render the
+ # two factor code prompt
+ #
+ # The user must have been authenticated with a valid login and password
+ # before calling this method!
+ #
+ # user - User record
+ #
+ # Returns nil
+ def prompt_for_two_factor(user)
+ session[:otp_user_id] = user.id
+
+ render 'devise/sessions/two_factor' and return
+ end
+end
diff --git a/app/controllers/sessions_controller.rb b/app/controllers/sessions_controller.rb
index d4ff0d97561..b89b4c27350 100644
--- a/app/controllers/sessions_controller.rb
+++ b/app/controllers/sessions_controller.rb
@@ -1,11 +1,7 @@
class SessionsController < Devise::SessionsController
- prepend_before_action :authenticate_with_two_factor, only: [:create]
+ include AuthenticatesWithTwoFactor
- # This action comes from DeviseController, but because we call `sign_in`
- # manually inside `authenticate_with_two_factor`, not skipping this action
- # would cause a "You are already signed in." error message to be shown upon
- # successful login.
- skip_before_action :require_no_authentication, only: [:create]
+ prepend_before_action :authenticate_with_two_factor, only: [:create]
def new
redirect_path =
@@ -74,9 +70,7 @@ class SessionsController < Devise::SessionsController
end
else
if user && user.valid_password?(user_params[:password])
- # Save the user's ID to session so we can ask for a one-time password
- session[:otp_user_id] = user.id
- render :two_factor and return
+ prompt_for_two_factor(user)
end
end
end