12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758 |
- class Auth
- attr_reader :user, :password, :auth_user
- delegate :user, to: :auth_user
- attr_accessor :increase_login_failed_attempts
- BRUTE_FORCE_SLEEP = 1.second
-
-
-
-
-
-
- def initialize(username, password)
- @auth_user = username.present? ? Auth::User.new(username) : nil
- @password = password
- @increase_login_failed_attempts = false
- end
-
-
-
-
- def valid?
-
- validated = auth_user&.user&.with_lock do
- next false if !auth_user.can_login?
- next true if backends.valid?
- auth_user.increase_login_failed if increase_login_failed_attempts
- false
- end
- if validated
- auth_user.update_last_login
- return true
- end
- avoid_brute_force_attack
- false
- end
- private
-
- def avoid_brute_force_attack
- sleep BRUTE_FORCE_SLEEP
- end
- def backends
- Auth::Backend.new(self)
- end
- end
|