Enable bruteforce protection on login endpoint

This commit is contained in:
2022-04-03 17:33:01 +02:00
parent 9943df4952
commit 886bae32c8
9 changed files with 209 additions and 38 deletions

View File

@ -1,11 +1,24 @@
use std::collections::HashMap;
use std::net::IpAddr;
use actix::{Actor, AsyncContext, Context};
use actix::{Actor, AsyncContext, Context, Handler, Message};
use crate::constants::{FAIL_LOGIN_ATTEMPT_CLEANUP_INTERVAL, KEEP_FAILED_LOGIN_ATTEMPTS_FOR};
use crate::utils::time::time;
#[derive(Message)]
#[rtype(result = "()")]
pub struct RecordFailedAttempt {
pub ip: IpAddr,
}
#[derive(Message)]
#[rtype(result = "usize")]
pub struct CountFailedAttempt {
pub ip: IpAddr,
}
#[derive(Debug, Default)]
pub struct BruteForceActor {
failed_attempts: HashMap<IpAddr, Vec<u64>>,
@ -55,6 +68,22 @@ impl Actor for BruteForceActor {
}
}
impl Handler<RecordFailedAttempt> for BruteForceActor {
type Result = ();
fn handle(&mut self, attempt: RecordFailedAttempt, _ctx: &mut Self::Context) -> Self::Result {
self.insert_failed_attempt(attempt.ip)
}
}
impl Handler<CountFailedAttempt> for BruteForceActor {
type Result = usize;
fn handle(&mut self, attempt: CountFailedAttempt, _ctx: &mut Self::Context) -> Self::Result {
self.count_failed_attempts(&attempt.ip)
}
}
#[cfg(test)]
mod test {
use std::net::{IpAddr, Ipv4Addr};