two_factor_method_initiate_authentication.rb 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. module Gql::Mutations
  3. class TwoFactorMethodInitiateAuthentication < BaseMutation
  4. description 'Fetches the initiation phase data for a two-factor authentication method.'
  5. argument :login, String, description: 'User name'
  6. argument :password, String, description: 'Password'
  7. argument :two_factor_method, Gql::Types::Enum::TwoFactor::AuthenticationMethodType, description: 'Two-factor authentication method'
  8. field :initiation_data, GraphQL::Types::JSON, description: ''
  9. def self.authorize(...)
  10. true
  11. end
  12. def resolve(login:, password:, two_factor_method:)
  13. initiate(login:, password:, two_factor_method:)
  14. end
  15. private
  16. def initiate(login:, password:, two_factor_method:)
  17. auth = Auth.new(login, password, only_verify_password: true)
  18. begin
  19. auth.valid!
  20. rescue Auth::Error::Base
  21. return error_response({ message: __('The username or password is incorrect.') })
  22. end
  23. two_factor_method_object = auth.user.auth_two_factor.authentication_method_object(two_factor_method)
  24. if !two_factor_method_object&.enabled? || !two_factor_method_object&.available?
  25. return error_response({ message: __('The two-factor authentication method is not enabled.') })
  26. end
  27. { initiation_data: two_factor_method_object.initiate_authentication }
  28. end
  29. end
  30. end