two_factors_controller.rb 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. class User::TwoFactorsController < ApplicationController
  3. prepend_before_action :authenticate_and_authorize!
  4. def two_factor_remove_authentication_method
  5. params_user.two_factor_destroy_authentication_method(params[:method])
  6. render json: {}, status: :ok
  7. end
  8. def two_factor_remove_all_authentication_methods
  9. params_user.two_factor_destroy_all_authentication_methods
  10. render json: {}, status: :ok
  11. end
  12. def two_factor_enabled_authentication_methods
  13. render json: params_user.two_factor_enabled_authentication_methods, status: :ok
  14. end
  15. def two_factor_personal_configuration
  16. result = {
  17. enabled_authentication_methods: current_user.two_factor_enabled_authentication_methods,
  18. recovery_codes_exist: current_user.auth_two_factor.user_recovery_codes_exists?,
  19. }
  20. render json: result, status: :ok
  21. end
  22. def two_factor_verify_configuration
  23. raise Exceptions::UnprocessableEntity, __('The required parameter "method" is missing.') if !params[:method]
  24. raise Exceptions::UnprocessableEntity, __('The required parameter "payload" is missing.') if !params[:payload]
  25. verified = two_factor_verify_configuration?
  26. result = {
  27. verified: verified,
  28. }
  29. if verified
  30. result[:recovery_codes] = current_user.two_factor_recovery_codes_generate
  31. end
  32. render json: result, status: :ok
  33. end
  34. def two_factor_authentication_method_initiate_configuration
  35. check_method!
  36. check_two_factor_method!
  37. render json: { configuration: @two_factor_method.initiate_configuration }, status: :ok
  38. end
  39. def two_factor_recovery_codes_generate
  40. render json: current_user.two_factor_recovery_codes_generate(force: true), status: :ok
  41. end
  42. def two_factor_default_authentication_method
  43. check_method!
  44. check_two_factor_method!
  45. current_user.two_factor_update_default_method(@method_name)
  46. render json: {}, status: :ok
  47. end
  48. def two_factor_authentication_method_configuration
  49. check_method!
  50. check_two_factor_method!
  51. fetch_user_two_factor_preference!(raise_exception: false)
  52. return render json: { configuration: {} }, status: :ok if @user_two_factor_preference.nil?
  53. render json: { configuration: @user_two_factor_preference.configuration }, status: :ok
  54. end
  55. def two_factor_authentication_method_configuration_save
  56. check_method!
  57. check_two_factor_method!
  58. fetch_user_two_factor_preference!
  59. if params[:configuration].nil?
  60. current_user.two_factor_destroy_authentication_method(params[:method])
  61. else
  62. @user_two_factor_preference.update!(configuration: params[:configuration].permit!.to_h)
  63. end
  64. render json: {}, status: :ok
  65. end
  66. private
  67. def check_method!
  68. raise Exceptions::UnprocessableEntity, __('The required parameter "method" is missing.') if params[:method].blank?
  69. @method_name ||= params[:method]
  70. true
  71. end
  72. def check_two_factor_method!
  73. two_factor_method = current_user.auth_two_factor.authentication_method_object(@method_name)
  74. raise Exceptions::UnprocessableEntity, __('The two-factor authentication method is not enabled.') if !two_factor_method&.enabled? || !two_factor_method&.available?
  75. @two_factor_method ||= two_factor_method
  76. true
  77. end
  78. def fetch_user_two_factor_preference!(raise_exception: true)
  79. pref = @two_factor_method.user_two_factor_preference
  80. if pref.blank? || pref.configuration.blank?
  81. raise Exceptions::UnprocessableEntity, __('There is no stored configuration for this two-factor authentication method.') if raise_exception
  82. return
  83. end
  84. @user_two_factor_preference ||= pref
  85. true
  86. end
  87. def params_user
  88. User.find(params[:id])
  89. end
  90. def two_factor_verify_configuration?
  91. current_user.two_factor_verify_configuration?(params[:method], params[:payload], params[:configuration].permit!.to_h)
  92. end
  93. end