From a14aee4e292c25ae198ca8db2573e8fec592164c Mon Sep 17 00:00:00 2001 From: jake Date: Tue, 27 Oct 2020 06:29:20 -0600 Subject: [PATCH] handle unactivated accounts in the correct spot --- src/bin/main.rs | 1 + src/entity/account.rs | 1 + src/entity/gateway/inmemory.rs | 2 +- src/entity/gateway/postgres/postgres.rs | 3 ++- src/login/login.rs | 4 ++++ src/ship/packet/handler/auth.rs | 31 +++++++++++-------------- tests/common.rs | 1 + 7 files changed, 23 insertions(+), 20 deletions(-) diff --git a/src/bin/main.rs b/src/bin/main.rs index 5611f45..16af628 100644 --- a/src/bin/main.rs +++ b/src/bin/main.rs @@ -60,6 +60,7 @@ fn main() { muted_until: None, created_at: chrono::Utc::now(), flags: 0, + activated: true, }; let fake_user = entity_gateway.create_user(fake_user).await.unwrap(); entity_gateway.create_user_settings(NewUserSettingsEntity::new(fake_user.id)).await.unwrap(); diff --git a/src/entity/account.rs b/src/entity/account.rs index 7071553..1276cf8 100644 --- a/src/entity/account.rs +++ b/src/entity/account.rs @@ -23,6 +23,7 @@ pub struct NewUserAccountEntity { pub muted_until: Option>, pub created_at: chrono::DateTime, pub flags: u32, + pub activated: bool, } #[derive(Clone, Debug)] diff --git a/src/entity/gateway/inmemory.rs b/src/entity/gateway/inmemory.rs index 01ec302..6341638 100644 --- a/src/entity/gateway/inmemory.rs +++ b/src/entity/gateway/inmemory.rs @@ -47,7 +47,7 @@ impl EntityGateway for InMemoryGateway { muted_until: user.muted_until, created_at: user.created_at, flags: user.flags, - activated: true, + activated: user.activated, }; users.insert(user.id, user.clone()); Ok(user) diff --git a/src/entity/gateway/postgres/postgres.rs b/src/entity/gateway/postgres/postgres.rs index a57893a..ffa11cd 100644 --- a/src/entity/gateway/postgres/postgres.rs +++ b/src/entity/gateway/postgres/postgres.rs @@ -93,10 +93,11 @@ impl PostgresGateway { #[async_trait::async_trait] impl EntityGateway for PostgresGateway { async fn create_user(&mut self, user: NewUserAccountEntity) -> Result { - let new_user = sqlx::query_as::<_, PgUserAccount>("insert into user_accounts (email, username, password) values ($1, $2, $3) returning *;") + let new_user = sqlx::query_as::<_, PgUserAccount>("insert into user_accounts (email, username, password, activated) values ($1, $2, $3, $4) returning *;") .bind(user.email) .bind(user.username) .bind(user.password) + .bind(user.activated) .fetch_one(&self.pool).await?; Ok(new_user.into()) } diff --git a/src/login/login.rs b/src/login/login.rs index 05013bc..95e8b08 100644 --- a/src/login/login.rs +++ b/src/login/login.rs @@ -61,6 +61,10 @@ pub async fn get_login_status(entity_gateway: &impl EntityGateway, pkt: &Login) let username = array_to_utf8(pkt.username).map_err(|_err| AccountStatus::Error)?; let password = array_to_utf8(pkt.password).map_err(|_err| AccountStatus::Error)?; let user = entity_gateway.get_user_by_name(username).await.map_err(|_| AccountStatus::InvalidUser)?; + if !user.activated { + return Err(AccountStatus::PayUp) + } + let verified = bcrypt::verify(password, user.password.as_str()).map_err(|_err| AccountStatus::Error)?; match verified { true => if user.banned_until.map(|banned| banned > chrono::Utc::now()).unwrap_or(false) { diff --git a/src/ship/packet/handler/auth.rs b/src/ship/packet/handler/auth.rs index 93476a3..9617998 100644 --- a/src/ship/packet/handler/auth.rs +++ b/src/ship/packet/handler/auth.rs @@ -15,25 +15,20 @@ pub async fn validate_login(id: ClientId, -> Result, ShipError> { Ok(match get_login_status(entity_gateway, pkt).await { Ok(user) => { - if user.activated { - let mut response = LoginResponse::by_status(AccountStatus::Ok, Session::new()); - response.guildcard = user.id.0 as u32; - response.team_id = user.team_id.map_or(31, |ti| ti) as u32; - let characters = entity_gateway.get_characters_by_user(&user).await?; - let character = characters - .get(pkt.session.character_slot as usize) - .ok_or(ShipError::InvalidSlot(id, pkt.session.character_slot as u32))?.as_ref() - .ok_or(ShipError::NoCharacterInSlot(id, pkt.session.character_slot as u32))? - .clone(); - let settings = entity_gateway.get_user_settings_by_user(&user).await?; + let mut response = LoginResponse::by_status(AccountStatus::Ok, Session::new()); + response.guildcard = user.id.0 as u32; + response.team_id = user.team_id.map_or(31, |ti| ti) as u32; + let characters = entity_gateway.get_characters_by_user(&user).await?; + let character = characters + .get(pkt.session.character_slot as usize) + .ok_or(ShipError::InvalidSlot(id, pkt.session.character_slot as u32))?.as_ref() + .ok_or(ShipError::NoCharacterInSlot(id, pkt.session.character_slot as u32))? + .clone(); + let settings = entity_gateway.get_user_settings_by_user(&user).await?; - item_manager.load_character(entity_gateway, &character).await?; - clients.insert(id, ClientState::new(user, settings, character, pkt.session)); - vec![SendShipPacket::LoginResponse(response), SendShipPacket::ShipBlockList(ShipBlockList::new(&&ship_name, 3))] - } - else { - vec![SendShipPacket::LoginResponse(LoginResponse::by_status(AccountStatus::Locked, Session::new()))] - } + item_manager.load_character(entity_gateway, &character).await?; + clients.insert(id, ClientState::new(user, settings, character, pkt.session)); + vec![SendShipPacket::LoginResponse(response), SendShipPacket::ShipBlockList(ShipBlockList::new(&&ship_name, 3))] }, Err(err) => { vec![SendShipPacket::LoginResponse(LoginResponse::by_status(err, Session::new()))] diff --git a/tests/common.rs b/tests/common.rs index 2d59654..0b66360 100644 --- a/tests/common.rs +++ b/tests/common.rs @@ -23,6 +23,7 @@ pub async fn new_user_character(entity_gateway: &mut EG, user muted_until: None, created_at: chrono::Utc::now(), flags: 0, + activated: true, }; let user = entity_gateway.create_user(new_user).await.unwrap();