Defect #44461 ยป twofa_keep_pending_totp_key.patch
| app/controllers/application_controller.rb | ||
|---|---|---|
|
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
|
||
| app/controllers/twofa_controller.rb | ||
|---|---|---|
|
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)
|
||
| ... | ... | |
|
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
|
||
| lib/redmine/twofa/base.rb | ||
|---|---|---|
|
@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)
|
||
| lib/redmine/twofa/totp.rb | ||
|---|---|---|
|
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
|
||
| test/integration/twofa_test.rb | ||
|---|---|---|
|
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"
|
||