# Copyright (C) 2012-2026 Zammad Foundation, https://zammad-foundation.org/

class Auth

  attr_reader :password, :auth_user, :two_factor_method, :two_factor_payload, :only_verify_password, :skip_login_failed_tracking

  delegate :user, to: :auth_user

  attr_accessor :increase_login_failed_attempts

  BRUTE_FORCE_SLEEP = 1.second

  # Initializes a Auth object for the given user.
  #
  # @param username [String] the user name for the user object which needs an authentication.
  # @param skip_login_failed_tracking [Boolean] opts out of counting failed attempts towards account
  #   lockout. Only appropriate for flows that are already gated behind an existing authenticated
  #   session (e.g. re-confirming the current password), since exempting an unauthenticated flow
  #   would leave it brute-forceable without limit.
  #
  # @example
  #  auth = Auth.new('admin@example.com', 'some+password')
  def initialize(username, password, two_factor_method: nil, two_factor_payload: nil, only_verify_password: false, skip_login_failed_tracking: false)
    @auth_user                      = username.present? ? Auth::User.new(username) : nil
    @password                       = password
    @two_factor_payload             = two_factor_payload
    @two_factor_method              = two_factor_method
    @increase_login_failed_attempts = false
    @only_verify_password           = only_verify_password
    @skip_login_failed_tracking     = skip_login_failed_tracking
    @two_factor_failed              = false

    return if !@two_factor_payload.is_a?(Hash)

    @two_factor_payload = @two_factor_payload.symbolize_keys
  end

  # Validates the given credentials for the user to the configured auth backends which should
  # be performed.
  #
  # @return true if the user was authenticated, otherwise it raises an Exception.
  def valid!

    if verify_credentials!
      auth_user.update_last_login if !only_verify_password
      return true
    end

    wait_and_raise failure_error
  end

  private

  def verify_credentials!
    # Wrap in a lock to synchronize concurrent requests.
    auth_user&.user&.with_lock do
      next false if !auth_user.can_login?

      if !backends.valid?
        # Failed log-in attempts are only recorded if the password backend requests so.
        auth_user.increase_login_failed if increase_login_failed_attempts && !skip_login_failed_tracking
        next false
      end
      verify_two_factor!
    end
  end

  def verify_two_factor!
    return true if only_verify_password
    return true if !auth_user.requires_two_factor?

    if two_factor_method.blank?
      raise Auth::Error::TwoFactorRequired, auth_user
    end

    return true if auth_user.two_factor_payload_valid?(two_factor_method, two_factor_payload)

    auth_user.increase_login_failed if !skip_login_failed_tracking
    @two_factor_failed = true
    false
  end

  def two_factor_failed? = @two_factor_failed

  def failure_error
    two_factor_failed? ? Auth::Error::TwoFactorFailed : Auth::Error::AuthenticationFailed
  end

  def wait_and_raise(...)
    # Sleep for a second to avoid brute force attacks.
    sleep BRUTE_FORCE_SLEEP
    raise(...)
  end

  def backends
    Auth::Backend.new(self)
  end
end
