summaryrefslogtreecommitdiffstats
path: root/app/controllers/twofa_controller.rb
blob: 0583889074034eadc2d305044a7a42073c099ec1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# frozen_string_literal: true

# Redmine - project management software
# Copyright (C) 2006-2021  Jean-Philippe Lang
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

class TwofaController < ApplicationController
  include TwofaHelper

  self.main_menu = false

  before_action :require_login
  before_action :require_admin, only: :admin_deactivate

  before_action :require_active_twofa

  require_sudo_mode :activate_init, :deactivate_init

  skip_before_action :check_twofa_activation, only: [:select_scheme, :activate_init, :activate_confirm, :activate]

  def select_scheme
    @user = User.current
  end

  before_action :activate_setup, only: [:activate_init, :activate_confirm, :activate]

  def activate_init
    init_twofa_pairing_and_send_code_for(@twofa)
  end

  def activate_confirm
    @twofa_view = @twofa.init_pairing_view_variables
  end

  def activate
    if @twofa.confirm_pairing!(params[:twofa_code].to_s)
      flash[:notice] = l('twofa_activated', bc_path: my_twofa_backup_codes_init_path)
      redirect_to my_account_path
    else
      flash[:error] = l('twofa_invalid_code')
      redirect_to action: :activate_confirm, scheme: @twofa.scheme_name
    end
  end

  before_action :deactivate_setup, only: [:deactivate_init, :deactivate_confirm, :deactivate]

  def deactivate_init
    if @twofa.send_code(controller: 'twofa', action: 'deactivate')
      flash[:notice] = l('twofa_code_sent')
    end
    redirect_to action: :deactivate_confirm, scheme: @twofa.scheme_name
  end

  def deactivate_confirm
    @twofa_view = @twofa.otp_confirm_view_variables
  end

  def deactivate
    if @twofa.destroy_pairing!(params[:twofa_code].to_s)
      flash[:notice] = l('twofa_deactivated')
      redirect_to my_account_path
    else
      flash[:error] = l('twofa_invalid_code')
      redirect_to action: :deactivate_confirm, scheme: @twofa.scheme_name
    end
  end

  def admin_deactivate
    @user = User.find(params[:user_id])
    # do not allow administrators to unpair 2FA without confirmation for themselves
    if @user == User.current
      render_403
      return false
    end

    twofa = Redmine::Twofa.for_user(@user)
    twofa.destroy_pairing_without_verify!
    flash[:notice] = l('twofa_deactivated')
    redirect_to edit_user_path(@user)
  end

  private

  def activate_setup
    twofa_scheme = Redmine::Twofa.for_twofa_scheme(params[:scheme].to_s)

    if twofa_scheme.blank?
      redirect_to my_account_path
      return
    end
    @user = User.current
    @twofa = twofa_scheme.new(@user)
  end

  def deactivate_setup
    @user = User.current
    @twofa = Redmine::Twofa.for_user(@user)
    if params[:scheme].to_s != @twofa.scheme_name
      redirect_to my_account_path
    end
  end
end