From c87d5915411c3eaf1057c949b994d3313bd849cf Mon Sep 17 00:00:00 2001 From: Laust Rud Jacobsen Date: Mon, 28 Apr 2014 11:30:56 +0200 Subject: [PATCH 1/2] Extract reused Warden key constant --- .../devise/two_factor_authentication_controller.rb | 2 +- lib/two_factor_authentication.rb | 2 ++ lib/two_factor_authentication/controllers/helpers.rb | 6 ++++-- .../hooks/two_factor_authenticatable.rb | 2 +- 4 files changed, 8 insertions(+), 4 deletions(-) diff --git a/app/controllers/devise/two_factor_authentication_controller.rb b/app/controllers/devise/two_factor_authentication_controller.rb index 1f2ac65..e302e01 100644 --- a/app/controllers/devise/two_factor_authentication_controller.rb +++ b/app/controllers/devise/two_factor_authentication_controller.rb @@ -9,7 +9,7 @@ class Devise::TwoFactorAuthenticationController < DeviseController render :show and return if params[:code].nil? if resource.authenticate_otp(params[:code]) - warden.session(resource_name)[:need_two_factor_authentication] = false + warden.session(resource_name)[TwoFactorAuthentication::NEED_AUTHENTICATION] = false sign_in resource_name, resource, :bypass => true set_flash_message :notice, :success redirect_to stored_location_for(resource_name) || :root diff --git a/lib/two_factor_authentication.rb b/lib/two_factor_authentication.rb index 3e6e67e..0d985ac 100644 --- a/lib/two_factor_authentication.rb +++ b/lib/two_factor_authentication.rb @@ -16,6 +16,8 @@ module Devise end module TwoFactorAuthentication + NEED_AUTHENTICATION = 'need_two_factor_authentication' + autoload :Schema, 'two_factor_authentication/schema' module Controllers autoload :Helpers, 'two_factor_authentication/controllers/helpers' diff --git a/lib/two_factor_authentication/controllers/helpers.rb b/lib/two_factor_authentication/controllers/helpers.rb index ef38330..5c151b2 100644 --- a/lib/two_factor_authentication/controllers/helpers.rb +++ b/lib/two_factor_authentication/controllers/helpers.rb @@ -12,7 +12,7 @@ module TwoFactorAuthentication def handle_two_factor_authentication unless devise_controller? Devise.mappings.keys.flatten.any? do |scope| - if signed_in?(scope) and warden.session(scope)[:need_two_factor_authentication] + if signed_in?(scope) and warden.session(scope)[TwoFactorAuthentication::NEED_AUTHENTICATION] handle_failed_second_factor(scope) end end @@ -42,7 +42,9 @@ module Devise module Controllers module Helpers def is_fully_authenticated? - !session["warden.user.user.session"].try(:[], :need_two_factor_authentication) + !session["warden.user.user.session"].try(:[], + TwoFactorAuthentication::NEED_AUTHENTICATION + ) end end end diff --git a/lib/two_factor_authentication/hooks/two_factor_authenticatable.rb b/lib/two_factor_authentication/hooks/two_factor_authenticatable.rb index 2abb4a4..985dc34 100644 --- a/lib/two_factor_authentication/hooks/two_factor_authenticatable.rb +++ b/lib/two_factor_authentication/hooks/two_factor_authenticatable.rb @@ -1,6 +1,6 @@ Warden::Manager.after_authentication do |user, auth, options| if user.respond_to?(:need_two_factor_authentication?) - if auth.session(options[:scope])[:need_two_factor_authentication] = user.need_two_factor_authentication?(auth.request) + if auth.session(options[:scope])[TwoFactorAuthentication::NEED_AUTHENTICATION] = user.need_two_factor_authentication?(auth.request) user.send_two_factor_authentication_code end end From 20703c0397be2482d03d469a4b76f3a3aeaa621c Mon Sep 17 00:00:00 2001 From: Laust Rud Jacobsen Date: Mon, 28 Apr 2014 11:32:04 +0200 Subject: [PATCH 2/2] Warden hook: refactor to not perform inline assignment for clarity --- .../hooks/two_factor_authenticatable.rb | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/two_factor_authentication/hooks/two_factor_authenticatable.rb b/lib/two_factor_authentication/hooks/two_factor_authenticatable.rb index 985dc34..c9022f5 100644 --- a/lib/two_factor_authentication/hooks/two_factor_authenticatable.rb +++ b/lib/two_factor_authentication/hooks/two_factor_authenticatable.rb @@ -1,6 +1,8 @@ Warden::Manager.after_authentication do |user, auth, options| if user.respond_to?(:need_two_factor_authentication?) - if auth.session(options[:scope])[TwoFactorAuthentication::NEED_AUTHENTICATION] = user.need_two_factor_authentication?(auth.request) + need_code = user.need_two_factor_authentication?(auth.request) + auth.session(options[:scope])[TwoFactorAuthentication::NEED_AUTHENTICATION] = need_code + if need_code user.send_two_factor_authentication_code end end