Merge factors type for authentication
This commit is contained in:
@ -19,7 +19,6 @@ pub struct CountFailedAttempt {
|
||||
pub ip: IpAddr,
|
||||
}
|
||||
|
||||
|
||||
#[derive(Debug, Default)]
|
||||
pub struct BruteForceActor {
|
||||
failed_attempts: HashMap<IpAddr, Vec<u64>>,
|
||||
@ -28,10 +27,7 @@ pub struct BruteForceActor {
|
||||
impl BruteForceActor {
|
||||
pub fn clean_attempts(&mut self) {
|
||||
#[allow(clippy::map_clone)]
|
||||
let keys = self.failed_attempts
|
||||
.keys()
|
||||
.map(|i| *i)
|
||||
.collect::<Vec<_>>();
|
||||
let keys = self.failed_attempts.keys().map(|i| *i).collect::<Vec<_>>();
|
||||
|
||||
for ip in keys {
|
||||
// Remove old attempts
|
||||
@ -102,7 +98,9 @@ mod test {
|
||||
let mut actor = BruteForceActor::default();
|
||||
actor.failed_attempts.insert(IP_1, vec![1, 10]);
|
||||
actor.failed_attempts.insert(IP_2, vec![1, 10, time() + 10]);
|
||||
actor.failed_attempts.insert(IP_3, vec![time() + 10, time() + 20]);
|
||||
actor
|
||||
.failed_attempts
|
||||
.insert(IP_3, vec![time() + 10, time() + 20]);
|
||||
|
||||
actor.clean_attempts();
|
||||
|
||||
|
@ -1,3 +1,3 @@
|
||||
pub mod users_actor;
|
||||
pub mod bruteforce_actor;
|
||||
pub mod openid_sessions_actor;
|
||||
pub mod openid_sessions_actor;
|
||||
pub mod users_actor;
|
||||
|
@ -1,5 +1,5 @@
|
||||
use actix::{Actor, AsyncContext, Context, Handler};
|
||||
use actix::Message;
|
||||
use actix::{Actor, AsyncContext, Context, Handler};
|
||||
|
||||
use crate::constants::*;
|
||||
use crate::data::access_token::AccessToken;
|
||||
@ -37,13 +37,16 @@ pub struct Session {
|
||||
|
||||
impl Session {
|
||||
pub fn is_expired(&self) -> bool {
|
||||
self.authorization_code_expire_at < time() && self.access_token_expire_at < time()
|
||||
self.authorization_code_expire_at < time()
|
||||
&& self.access_token_expire_at < time()
|
||||
&& self.refresh_token_expire_at < time()
|
||||
}
|
||||
|
||||
pub fn regenerate_access_and_refresh_tokens(&mut self,
|
||||
app_config: &AppConfig,
|
||||
jwt_signer: &JWTSigner) -> Res {
|
||||
pub fn regenerate_access_and_refresh_tokens(
|
||||
&mut self,
|
||||
app_config: &AppConfig,
|
||||
jwt_signer: &JWTSigner,
|
||||
) -> Res {
|
||||
let access_token = AccessToken {
|
||||
issuer: app_config.website_origin.to_string(),
|
||||
subject_identifier: self.user.clone().0,
|
||||
@ -116,7 +119,11 @@ impl Handler<PushNewSession> for OpenIDSessionsActor {
|
||||
impl Handler<FindSessionByAuthorizationCode> for OpenIDSessionsActor {
|
||||
type Result = Option<Session>;
|
||||
|
||||
fn handle(&mut self, msg: FindSessionByAuthorizationCode, _ctx: &mut Self::Context) -> Self::Result {
|
||||
fn handle(
|
||||
&mut self,
|
||||
msg: FindSessionByAuthorizationCode,
|
||||
_ctx: &mut Self::Context,
|
||||
) -> Self::Result {
|
||||
self.session
|
||||
.iter()
|
||||
.find(|f| f.authorization_code.eq(&msg.0))
|
||||
@ -141,7 +148,12 @@ impl Handler<FindSessionByAccessToken> for OpenIDSessionsActor {
|
||||
fn handle(&mut self, msg: FindSessionByAccessToken, _ctx: &mut Self::Context) -> Self::Result {
|
||||
self.session
|
||||
.iter()
|
||||
.find(|f| f.access_token.as_ref().map(|t| t.eq(&msg.0)).unwrap_or(false))
|
||||
.find(|f| {
|
||||
f.access_token
|
||||
.as_ref()
|
||||
.map(|t| t.eq(&msg.0))
|
||||
.unwrap_or(false)
|
||||
})
|
||||
.cloned()
|
||||
}
|
||||
}
|
||||
@ -150,9 +162,14 @@ impl Handler<UpdateSession> for OpenIDSessionsActor {
|
||||
type Result = ();
|
||||
|
||||
fn handle(&mut self, msg: UpdateSession, _ctx: &mut Self::Context) -> Self::Result {
|
||||
if let Some(r) = self.session.iter().enumerate()
|
||||
.find(|f| f.1.session_id.eq(&msg.0.session_id)).map(|f| f.0) {
|
||||
if let Some(r) = self
|
||||
.session
|
||||
.iter()
|
||||
.enumerate()
|
||||
.find(|f| f.1.session_id.eq(&msg.0.session_id))
|
||||
.map(|f| f.0)
|
||||
{
|
||||
self.session[r] = msg.0;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -123,7 +123,9 @@ impl Handler<FindUserByUsername> for UsersActor {
|
||||
type Result = MessageResult<FindUserByUsername>;
|
||||
|
||||
fn handle(&mut self, msg: FindUserByUsername, _ctx: &mut Self::Context) -> Self::Result {
|
||||
MessageResult(FindUserByUsernameResult(self.manager.find_by_username_or_email(&msg.0)))
|
||||
MessageResult(FindUserByUsernameResult(
|
||||
self.manager.find_by_username_or_email(&msg.0),
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
@ -155,10 +157,13 @@ impl Handler<DeleteUserRequest> for UsersActor {
|
||||
fn handle(&mut self, msg: DeleteUserRequest, _ctx: &mut Self::Context) -> Self::Result {
|
||||
let user = match self.manager.find_by_user_id(&msg.0) {
|
||||
None => {
|
||||
log::warn!("Could not delete account {:?} because it was not found!", msg.0);
|
||||
log::warn!(
|
||||
"Could not delete account {:?} because it was not found!",
|
||||
msg.0
|
||||
);
|
||||
return MessageResult(DeleteUserResult(false));
|
||||
}
|
||||
Some(s) => s
|
||||
Some(s) => s,
|
||||
};
|
||||
|
||||
MessageResult(DeleteUserResult(match self.manager.remove(&user) {
|
||||
@ -169,4 +174,4 @@ impl Handler<DeleteUserRequest> for UsersActor {
|
||||
}
|
||||
}))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user