auth.rb 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. # Copyright (C) 2012-2022 Zammad Foundation, https://zammad-foundation.org/
  2. class Auth
  3. attr_reader :user, :password, :auth_user
  4. delegate :user, to: :auth_user
  5. attr_accessor :increase_login_failed_attempts
  6. # Initializes a Auth object for the given user.
  7. #
  8. # @param username [String] the user name for the user object which needs an authentication.
  9. #
  10. # @example
  11. # auth = Auth.new('admin@example.com', 'some+password')
  12. def initialize(username, password)
  13. @lookup_backend_instance = {}
  14. @auth_user = username.present? ? Auth::User.new(username) : nil
  15. @password = password
  16. @increase_login_failed_attempts = false
  17. end
  18. # Validates the given credentials for the user to the configured auth backends which should
  19. # be performed.
  20. #
  21. # @return [Boolean] true if the user was authenticated, otherwise false.
  22. def valid?
  23. if !auth_user || !auth_user.can_login?
  24. avoid_brute_force_attack
  25. return false
  26. end
  27. if backends.valid?
  28. auth_user.update_last_login
  29. return true
  30. end
  31. avoid_brute_force_attack
  32. auth_user.increase_login_failed if increase_login_failed_attempts
  33. false
  34. end
  35. private
  36. # Sleep for a second to avoid brute force attacks.
  37. def avoid_brute_force_attack
  38. sleep 1
  39. end
  40. def backends
  41. Auth::Backend.new(self)
  42. end
  43. end