I am trying to limit failed login attempts per ip.
I have the following:
def validate(email, context)
attempt = insert_into_attempts(email, context)
return nil unless allow_login_by_ip(context.ip_address)
flag_successful_attempt(attempt, context.ip_address)
load_data
end
def allow_login_by_ip(ip_address)
limit = LoginLimits.new(ip_address).limit
last_5_attempts = AuthenticationAttempt.select("id","successful").where(ip: ip_address).last(5)
last_5_attempts.include?("true")
end
def insert_into_attempts(email, context)
attempt = AuthenticationAttempt.new(
:email => email,
:ip => context.ip_address)
attempt.save
end
def flag_successful_attempt(attempt, ip_address)
AuthenticationAttempt.where(ip: ip_address).last.update(successful: '1')
end
The issue I am having is that it always returns fasle. I must be searching the array incorrectly but I am not sure why. last_5_attempts is:
#<AuthenticationAttempt id: 1, successful: false>,
#<AuthenticationAttempt id: 2, successful: false>,
#<AuthenticationAttempt id: 3, successful: true>,
#<AuthenticationAttempt id: 4, successful: false>,
#<AuthenticationAttempt id: 5, successful: false>]
rack-attackgem instead of implementing this feature by yourself.