You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

twofa_controller.rb 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. # frozen_string_literal: true
  2. # Redmine - project management software
  3. # Copyright (C) 2006- Jean-Philippe Lang
  4. #
  5. # This program is free software; you can redistribute it and/or
  6. # modify it under the terms of the GNU General Public License
  7. # as published by the Free Software Foundation; either version 2
  8. # of the License, or (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program; if not, write to the Free Software
  17. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. class TwofaController < ApplicationController
  19. include TwofaHelper
  20. self.main_menu = false
  21. before_action :require_login
  22. before_action :require_admin, only: :admin_deactivate
  23. before_action :require_active_twofa
  24. require_sudo_mode :activate_init, :deactivate_init
  25. skip_before_action :check_twofa_activation, only: [:select_scheme, :activate_init, :activate_confirm, :activate]
  26. def select_scheme
  27. @user = User.current
  28. end
  29. before_action :activate_setup, only: [:activate_init, :activate_confirm, :activate]
  30. def activate_init
  31. init_twofa_pairing_and_send_code_for(@twofa)
  32. end
  33. def activate_confirm
  34. @twofa_view = @twofa.init_pairing_view_variables
  35. end
  36. def activate
  37. if @twofa.confirm_pairing!(params[:twofa_code].to_s)
  38. # The session token was destroyed by the twofa pairing, generate a new one
  39. session[:tk] = @user.generate_session_token
  40. flash[:notice] = l('twofa_activated', bc_path: my_twofa_backup_codes_init_path)
  41. redirect_to my_account_path
  42. else
  43. flash[:error] = l('twofa_invalid_code')
  44. redirect_to action: :activate_confirm, scheme: @twofa.scheme_name
  45. end
  46. end
  47. before_action :deactivate_setup, only: [:deactivate_init, :deactivate_confirm, :deactivate]
  48. def deactivate_init
  49. if @twofa.send_code(controller: 'twofa', action: 'deactivate')
  50. flash[:notice] = l('twofa_code_sent')
  51. end
  52. redirect_to action: :deactivate_confirm, scheme: @twofa.scheme_name
  53. end
  54. def deactivate_confirm
  55. @twofa_view = @twofa.otp_confirm_view_variables
  56. end
  57. def deactivate
  58. if @twofa.destroy_pairing!(params[:twofa_code].to_s)
  59. flash[:notice] = l('twofa_deactivated')
  60. redirect_to my_account_path
  61. else
  62. flash[:error] = l('twofa_invalid_code')
  63. redirect_to action: :deactivate_confirm, scheme: @twofa.scheme_name
  64. end
  65. end
  66. def admin_deactivate
  67. @user = User.find(params[:user_id])
  68. # do not allow administrators to unpair 2FA without confirmation for themselves
  69. if @user == User.current
  70. render_403
  71. return false
  72. end
  73. twofa = Redmine::Twofa.for_user(@user)
  74. twofa.destroy_pairing_without_verify!
  75. flash[:notice] = l('twofa_deactivated')
  76. redirect_to edit_user_path(@user)
  77. end
  78. private
  79. def activate_setup
  80. twofa_scheme = Redmine::Twofa.for_twofa_scheme(params[:scheme].to_s)
  81. if twofa_scheme.blank?
  82. redirect_to my_account_path
  83. return
  84. end
  85. @user = User.current
  86. @twofa = twofa_scheme.new(@user)
  87. end
  88. def deactivate_setup
  89. @user = User.current
  90. @twofa = Redmine::Twofa.for_user(@user)
  91. if params[:scheme].to_s != @twofa.scheme_name
  92. redirect_to my_account_path
  93. end
  94. end
  95. end