diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 2118193ce..07ac31a77 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -249,22 +249,14 @@ class ApplicationController < ActionController::Base end end - def init_twofa_pairing_and_send_code_for(twofa) - twofa.init_pairing! - if twofa.send_code(controller: 'twofa', action: 'activate') - flash[:notice] = l('twofa_code_sent') - end - redirect_to controller: 'twofa', action: 'activate_confirm', scheme: twofa.scheme_name - end - def check_twofa_activation if session[:must_activate_twofa] if User.current.must_activate_twofa? flash[:warning] = l('twofa_warning_require') if Redmine::Twofa.available_schemes.length == 1 - twofa_scheme = Redmine::Twofa.for_twofa_scheme(Redmine::Twofa.available_schemes.first) - twofa = twofa_scheme.new(User.current) - init_twofa_pairing_and_send_code_for(twofa) + # The pairing is initialized by TwofaController#activate_confirm + redirect_to controller: 'twofa', action: 'activate_confirm', + scheme: Redmine::Twofa.available_schemes.first else redirect_to controller: 'twofa', action: 'select_scheme' end diff --git a/app/controllers/twofa_controller.rb b/app/controllers/twofa_controller.rb index 0f951cf20..eb8f315fb 100644 --- a/app/controllers/twofa_controller.rb +++ b/app/controllers/twofa_controller.rb @@ -42,16 +42,22 @@ class TwofaController < ApplicationController before_action :activate_setup, only: [:activate_init, :activate_confirm, :activate] def activate_init - init_twofa_pairing_and_send_code_for(@twofa) + init_pairing_and_send_code + redirect_to action: :activate_confirm, scheme: @twofa.scheme_name end def activate_confirm + # Initialize the pairing only once per session, so that reloading this page + # or being redirected here again does not invalidate the secret already + # shown to the user + init_pairing_and_send_code(now: true) unless twofa_pairing_initialized? @twofa_view = @twofa.init_pairing_view_variables no_store end def activate if @twofa.confirm_pairing!(params[:twofa_code].to_s) + session.delete(:twofa_pairing_scheme) # The session token was destroyed by the twofa pairing, generate a new one renew_session_token flash[:notice] = l('twofa_activated', bc_path: my_twofa_backup_codes_init_path) @@ -120,6 +126,18 @@ class TwofaController < ApplicationController end end + def init_pairing_and_send_code(now: false) + @twofa.init_pairing! + session[:twofa_pairing_scheme] = @twofa.scheme_name + if @twofa.send_code(controller: 'twofa', action: 'activate') + (now ? flash.now : flash)[:notice] = l('twofa_code_sent') + end + end + + def twofa_pairing_initialized? + session[:twofa_pairing_scheme] == @twofa.scheme_name && @twofa.pairing_initialized? + end + def ensure_user_has_no_twofa # Allow activating a new 2FA scheme / showing twofa secret only if no other # is already configured diff --git a/lib/redmine/twofa/base.rb b/lib/redmine/twofa/base.rb index b22531341..cc570aef1 100644 --- a/lib/redmine/twofa/base.rb +++ b/lib/redmine/twofa/base.rb @@ -42,6 +42,12 @@ module Redmine @user end + # Returns true if the pairing initialized by init_pairing! can be + # confirmed without initializing it again + def pairing_initialized? + false + end + def confirm_pairing!(code) # make sure an otp and not a backup code is used if verify_otp!(code) diff --git a/lib/redmine/twofa/totp.rb b/lib/redmine/twofa/totp.rb index 444ac9e61..6d99f7c7e 100644 --- a/lib/redmine/twofa/totp.rb +++ b/lib/redmine/twofa/totp.rb @@ -29,6 +29,10 @@ module Redmine super end + def pairing_initialized? + @user.twofa_totp_key.present? + end + def destroy_pairing_without_verify! @user.update!(twofa_totp_key: nil, twofa_totp_last_used_at: nil) # reset the cached totp as the key might have changed diff --git a/test/integration/twofa_test.rb b/test/integration/twofa_test.rb index 65dca4061..b66f3ec6c 100644 --- a/test/integration/twofa_test.rb +++ b/test/integration/twofa_test.rb @@ -250,6 +250,63 @@ class TwofaTest < Redmine::IntegrationTest end end + test "should not regenerate totp key on redirect to required twofa setup" do + with_settings twofa: "2" do + log_user('jsmith', 'jsmith') + follow_redirect! + assert_redirected_to "/my/twofa/totp/activate/confirm" + follow_redirect! + assert_response :success + assert key = User.find_by_login('jsmith').twofa_totp_key + assert_select 'code', text: key.scan(/.{4}/).join(' ') + + # Requests for other resources (e.g. an image loaded by the confirmation + # page) are redirected to the confirmation page again + get "/projects" + assert_redirected_to "/my/twofa/totp/activate/confirm" + follow_redirect! + assert_response :success + assert_equal key, User.find_by_login('jsmith').twofa_totp_key + + post "/my/twofa/totp/activate", params: {twofa_code: ROTP::TOTP.new(key).now} + assert_redirected_to "/my/account" + assert_equal 'totp', User.find_by_login('jsmith').twofa_scheme + end + end + + test "should regenerate totp key if it was removed after twofa setup was initialized" do + with_settings twofa: "2" do + log_user('jsmith', 'jsmith') + follow_redirect! + assert_redirected_to "/my/twofa/totp/activate/confirm" + follow_redirect! + assert User.find_by_login('jsmith').twofa_totp_key.present? + + # e.g. 2FA was activated and deactivated in another session + User.find_by_login('jsmith').update_column :twofa_totp_key, nil + + get "/my/twofa/totp/activate/confirm" + assert_response :success + assert User.find_by_login('jsmith').twofa_totp_key.present? + end + end + + test "should regenerate totp key when twofa setup is initialized explicitly" do + with_settings twofa: "2" do + log_user('jsmith', 'jsmith') + follow_redirect! + assert_redirected_to "/my/twofa/totp/activate/confirm" + follow_redirect! + assert key = User.find_by_login('jsmith').twofa_totp_key + + post "/my/twofa/totp/activate/init" + assert_redirected_to "/my/twofa/totp/activate/confirm" + follow_redirect! + assert_response :success + assert_not_equal key, User.find_by_login('jsmith').twofa_totp_key + end + end + test "should deny showing twofa information again" do log_user('jsmith', 'jsmith') get "/my/account"