two_factors_controller.rb 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. # Copyright (C) 2012-2025 Zammad Foundation, https://zammad-foundation.org/
  2. class User::TwoFactorsController < ApplicationController
  3. prepend_before_action :authenticate_and_authorize!
  4. before_action :validate_token!, except: %i[personal_configuration default_authentication_method]
  5. def remove_authentication_method
  6. Service::User::TwoFactor::RemoveMethod
  7. .new(user: current_user, method_name: params[:method])
  8. .execute
  9. render json: {}
  10. token_object.destroy
  11. end
  12. def enabled_authentication_methods
  13. render json: current_user.two_factor_enabled_authentication_methods
  14. end
  15. def 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
  21. end
  22. def verify_configuration
  23. verify_method_configuration = Service::User::TwoFactor::VerifyMethodConfiguration
  24. .new(user: current_user, method_name: params_method_name, payload: params_payload, configuration: params[:configuration].permit!.to_h)
  25. render json: verify_method_configuration.execute.merge({ verified: true })
  26. token_object.destroy
  27. rescue Service::User::TwoFactor::VerifyMethodConfiguration::Failed
  28. render json: { verified: false }
  29. end
  30. def authentication_method_initiate_configuration
  31. initiate_authentication_method_configuration = Service::User::TwoFactor::InitiateMethodConfiguration
  32. .new(user: current_user, method_name: params_method_name)
  33. render json: { configuration: initiate_authentication_method_configuration.execute }
  34. end
  35. def recovery_codes_generate
  36. codes = Service::User::TwoFactor::GenerateRecoveryCodes
  37. .new(user: current_user, force: true)
  38. .execute
  39. render json: codes
  40. token_object.destroy
  41. end
  42. def default_authentication_method
  43. Service::User::TwoFactor::SetDefaultMethod
  44. .new(user: current_user, method_name: params_method_name)
  45. .execute
  46. render json: {}
  47. end
  48. def authentication_method_configuration
  49. configuration = Service::User::TwoFactor::GetMethodConfiguration
  50. .new(user: current_user, method_name: params_method_name)
  51. .execute
  52. render json: { configuration: configuration || {} }
  53. end
  54. def authentication_remove_credentials
  55. Service::User::TwoFactor::RemoveMethodCredentials
  56. .new(user: current_user, method_name: params_method_name, credential_id: params[:credential_id])
  57. .execute
  58. render json: {}
  59. end
  60. private
  61. def params_method_name
  62. params.require(:method)
  63. end
  64. def params_payload
  65. params.require(:payload)
  66. end
  67. def token_object
  68. @token_object ||= Token.validate! action: 'PasswordCheck', token: params[:token]
  69. end
  70. def validate_token!
  71. token_object
  72. rescue Token::TokenInvalid
  73. render json: { invalid_password_token: true }
  74. end
  75. end