2026-01-02 13:41:09 +00:00
|
|
|
# Copyright (C) 2012-2026 Zammad Foundation, https://zammad-foundation.org/
|
2023-05-19 14:29:47 +00:00
|
|
|
|
|
|
|
|
class Auth::TwoFactor::Method
|
|
|
|
|
attr_reader :user
|
|
|
|
|
|
|
|
|
|
def initialize(user = nil)
|
|
|
|
|
@user = user.presence
|
|
|
|
|
end
|
|
|
|
|
|
2023-05-31 09:54:14 +00:00
|
|
|
def verify(payload)
|
2023-05-19 14:29:47 +00:00
|
|
|
raise NotImplementedError
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def enabled?
|
2023-05-31 09:54:14 +00:00
|
|
|
@enabled ||= Setting.get(related_setting_name)
|
2023-05-19 14:29:47 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def method_name(human: false)
|
|
|
|
|
return self.class.name.demodulize.underscore if !human
|
|
|
|
|
|
|
|
|
|
self.class.name.demodulize.titleize
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def create_user_config(configuration)
|
|
|
|
|
return if configuration.blank?
|
2023-06-01 09:50:23 +00:00
|
|
|
return update_user_config(configuration) if user_two_factor_preference.present?
|
2023-05-19 14:29:47 +00:00
|
|
|
|
|
|
|
|
two_factor_prefs = User::TwoFactorPreference.new(
|
|
|
|
|
method: method_name,
|
|
|
|
|
configuration: configuration,
|
|
|
|
|
user_id: user.id,
|
|
|
|
|
)
|
|
|
|
|
two_factor_prefs.save!
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def update_user_config(configuration)
|
|
|
|
|
return if configuration.blank?
|
|
|
|
|
return if user_two_factor_preference.blank?
|
|
|
|
|
|
|
|
|
|
user_two_factor_preference.update!(configuration: user_two_factor_preference_configuration.merge(configuration))
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def destroy_user_config
|
2024-05-01 09:19:02 +00:00
|
|
|
user.two_factor_preferences.find_by(method: method_name)&.destroy!
|
2023-05-19 14:29:47 +00:00
|
|
|
end
|
|
|
|
|
|
2023-06-01 07:26:46 +00:00
|
|
|
def user_two_factor_preference
|
|
|
|
|
raise NotImplementedError
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def user_two_factor_preference_configuration
|
|
|
|
|
user_two_factor_preference&.configuration
|
|
|
|
|
end
|
|
|
|
|
|
2023-05-19 14:29:47 +00:00
|
|
|
private
|
|
|
|
|
|
2023-05-31 09:54:14 +00:00
|
|
|
def verify_result(verified, configuration: {}, new_configuration: {})
|
|
|
|
|
return { verified: false } if !verified
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
**configuration,
|
|
|
|
|
verified: true,
|
|
|
|
|
**new_configuration,
|
|
|
|
|
}
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def related_setting_name
|
|
|
|
|
raise NotImplementedError
|
|
|
|
|
end
|
2023-05-19 14:29:47 +00:00
|
|
|
end
|