summaryrefslogtreecommitdiffstats
path: root/app/controllers/twofa_backup_codes_controller.rb
blob: 6fe97712194bd885853b32614992d25c98190af3 (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
# 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 TwofaBackupCodesController < ApplicationController
  include TwofaHelper

  self.main_menu = false

  before_action :require_login, :require_active_twofa

  before_action :twofa_setup

  require_sudo_mode :init

  def init
    if @twofa.send_code(controller: 'twofa_backup_codes', action: 'create')
      flash[:notice] = l('twofa_code_sent')
    end
    redirect_to action: 'confirm'
  end

  def confirm
    @twofa_view = @twofa.otp_confirm_view_variables
  end

  def create
    if @twofa.verify!(params[:twofa_code].to_s)
      if time = @twofa.backup_codes.map(&:created_on).max
        flash[:warning] = t('twofa_warning_backup_codes_generated_invalidated', time: format_time(time))
      else
        flash[:notice] = t('twofa_notice_backup_codes_generated')
      end
      tokens = @twofa.init_backup_codes!
      flash[:twofa_backup_token_ids] = tokens.collect(&:id)
      redirect_to action: 'show'
    else
      flash[:error] = l('twofa_invalid_code')
      redirect_to action: 'confirm'
    end
  end

  def show
    # make sure we get only the codes that we should show
    tokens = @twofa.backup_codes.where(id: flash[:twofa_backup_token_ids])
    # Redmine will show all flash contents at the top of the rendered html
    # page, so we need to explicitely delete this here
    flash.delete(:twofa_backup_token_ids)

    if tokens.present? && (@created_at = tokens.collect(&:created_on).max) > 5.minutes.ago
      @backup_codes = tokens.collect(&:value)
    else
      flash[:warning] = l('twofa_backup_codes_already_shown', bc_path: my_twofa_backup_codes_init_path)
      redirect_to controller: 'my', action: 'account'
    end
  end

  private

  def twofa_setup
    @user = User.current
    @twofa = Redmine::Twofa.for_user(@user)
  end
end