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_backup_codes_controller.rb 2.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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 TwofaBackupCodesController < ApplicationController
  19. include TwofaHelper
  20. self.main_menu = false
  21. before_action :require_login, :require_active_twofa
  22. before_action :twofa_setup
  23. require_sudo_mode :init
  24. def init
  25. if @twofa.send_code(controller: 'twofa_backup_codes', action: 'create')
  26. flash[:notice] = l('twofa_code_sent')
  27. end
  28. redirect_to action: 'confirm'
  29. end
  30. def confirm
  31. @twofa_view = @twofa.otp_confirm_view_variables
  32. end
  33. def create
  34. if @twofa.verify!(params[:twofa_code].to_s)
  35. if time = @twofa.backup_codes.map(&:created_on).max
  36. flash[:warning] = t('twofa_warning_backup_codes_generated_invalidated', time: format_time(time))
  37. else
  38. flash[:notice] = t('twofa_notice_backup_codes_generated')
  39. end
  40. tokens = @twofa.init_backup_codes!
  41. flash[:twofa_backup_token_ids] = tokens.collect(&:id)
  42. redirect_to action: 'show'
  43. else
  44. flash[:error] = l('twofa_invalid_code')
  45. redirect_to action: 'confirm'
  46. end
  47. end
  48. def show
  49. # make sure we get only the codes that we should show
  50. tokens = @twofa.backup_codes.where(id: flash[:twofa_backup_token_ids])
  51. # Redmine will show all flash contents at the top of the rendered html
  52. # page, so we need to explicitely delete this here
  53. flash.delete(:twofa_backup_token_ids)
  54. if tokens.present? && (@created_at = tokens.collect(&:created_on).max) > 5.minutes.ago
  55. @backup_codes = tokens.collect(&:value)
  56. else
  57. flash[:warning] = l('twofa_backup_codes_already_shown', bc_path: my_twofa_backup_codes_init_path)
  58. redirect_to controller: 'my', action: 'account'
  59. end
  60. end
  61. private
  62. def twofa_setup
  63. @user = User.current
  64. @twofa = Redmine::Twofa.for_user(@user)
  65. end
  66. end