diff --git a/app/controllers/devise/two_factor_authentication_controller.rb b/app/controllers/devise/two_factor_authentication_controller.rb index 7d756a0..5f5b4b4 100644 --- a/app/controllers/devise/two_factor_authentication_controller.rb +++ b/app/controllers/devise/two_factor_authentication_controller.rb @@ -11,6 +11,7 @@ class Devise::TwoFactorAuthenticationController < DeviseController if resource.authenticate_otp(params[:code]) warden.session(resource_name)[:need_two_factor_authentication] = false sign_in resource_name, resource, :bypass => true + set_flash_message :notice, :success redirect_to stored_location_for(resource_name) || :root resource.update_attribute(:second_factor_attempts_count, 0) else diff --git a/config/locales/en.yml b/config/locales/en.yml index ab459a0..9db4c28 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -1,4 +1,5 @@ en: devise: two_factor_authentication: + success: "Two factor authentication successful." attempt_failed: "Attempt failed." diff --git a/spec/features/two_factor_authenticatable_spec.rb b/spec/features/two_factor_authenticatable_spec.rb new file mode 100644 index 0000000..b16f6fb --- /dev/null +++ b/spec/features/two_factor_authenticatable_spec.rb @@ -0,0 +1,42 @@ +require 'spec_helper' + +feature "User of two factor authentication" do + + scenario "must be logged in" do + visit user_two_factor_authentication_path + + page.should have_content("Welcome Home") + end + + context "when logged in" do + let(:user) { create_user } + + background do + login_as user + end + + scenario "can fill in TFA code" do + visit user_two_factor_authentication_path + + page.should have_content("Enter your personal code") + + fill_in "code", with: user.otp_code + click_button "Submit" + + within(".flash.notice") do + expect(page).to have_content("Two factor authentication successful.") + end + end + + scenario "is redirected to TFA when path requires authentication" do + visit dashboard_path + + expect(page).to_not have_content("Your Personal Dashboard") + + fill_in "code", with: user.otp_code + click_button "Submit" + + expect(page).to have_content("Your Personal Dashboard") + end + end +end diff --git a/spec/rails_app/app/controllers/home_controller.rb b/spec/rails_app/app/controllers/home_controller.rb index 95f2992..740af84 100644 --- a/spec/rails_app/app/controllers/home_controller.rb +++ b/spec/rails_app/app/controllers/home_controller.rb @@ -1,4 +1,16 @@ class HomeController < ApplicationController + prepend_before_filter :store_location, only: :dashboard + before_filter :authenticate_user!, only: :dashboard + def index end + + def dashboard + end + + private + + def store_location + store_location_for(:user, dashboard_path) + end end diff --git a/spec/rails_app/app/models/user.rb b/spec/rails_app/app/models/user.rb index 743d176..b7b937f 100644 --- a/spec/rails_app/app/models/user.rb +++ b/spec/rails_app/app/models/user.rb @@ -4,4 +4,8 @@ class User < ActiveRecord::Base :two_factor_authenticatable has_one_time_password + + def send_two_factor_authentication_code + # No op + end end diff --git a/spec/rails_app/app/views/home/dashboard.html.erb b/spec/rails_app/app/views/home/dashboard.html.erb new file mode 100644 index 0000000..d48f903 --- /dev/null +++ b/spec/rails_app/app/views/home/dashboard.html.erb @@ -0,0 +1,5 @@ +
Your email is <%= current_user.email %>
+ +You will only be able to see this page after successfully completing two factor authentication
diff --git a/spec/rails_app/app/views/home/index.html.erb b/spec/rails_app/app/views/home/index.html.erb index 2085730..43a267a 100644 --- a/spec/rails_app/app/views/home/index.html.erb +++ b/spec/rails_app/app/views/home/index.html.erb @@ -1,2 +1,3 @@ -Find me in app/views/home/index.html.erb
diff --git a/spec/rails_app/app/views/layouts/application.html.erb b/spec/rails_app/app/views/layouts/application.html.erb index 7bc3a49..8d56308 100644 --- a/spec/rails_app/app/views/layouts/application.html.erb +++ b/spec/rails_app/app/views/layouts/application.html.erb @@ -7,8 +7,8 @@ <%= csrf_meta_tags %> -<%= notice %>
-<%= alert %>
+<%= notice %>
+<%= alert %>
<%= yield %>