Merge branch 'main' of gitlab.com:PhilippHofer/rot

This commit is contained in:
Philipp 2023-07-11 09:16:23 +02:00
commit 530478d688
2 changed files with 11 additions and 4 deletions

View File

@ -2,6 +2,7 @@ use std::ops::Deref;
use argon2::{password_hash::SaltString, Argon2, PasswordHasher}; use argon2::{password_hash::SaltString, Argon2, PasswordHasher};
use chrono::{Datelike, Local, NaiveDate}; use chrono::{Datelike, Local, NaiveDate};
use log::info;
use rocket::{ use rocket::{
async_trait, async_trait,
http::{Cookie, Status}, http::{Cookie, Status},
@ -109,11 +110,14 @@ ORDER BY last_access DESC
} }
pub async fn login(db: &SqlitePool, name: &str, pw: &str) -> Result<Self, LoginError> { pub async fn login(db: &SqlitePool, name: &str, pw: &str) -> Result<Self, LoginError> {
info!("User '{name}' is trying to login...");
let Some(user) = User::find_by_name(db, name).await else { let Some(user) = User::find_by_name(db, name).await else {
info!("Username ({name}) not found");
return Err(LoginError::InvalidAuthenticationCombo); // Username not found return Err(LoginError::InvalidAuthenticationCombo); // Username not found
}; };
if user.deleted { if user.deleted {
info!("User ({name}) already deleted.");
return Err(LoginError::InvalidAuthenticationCombo); //User existed sometime ago; has return Err(LoginError::InvalidAuthenticationCombo); //User existed sometime ago; has
//been deleted //been deleted
} }
@ -122,12 +126,17 @@ ORDER BY last_access DESC
Some(user_pw) => { Some(user_pw) => {
let password_hash = &Self::get_hashed_pw(pw); let password_hash = &Self::get_hashed_pw(pw);
if password_hash == user_pw { if password_hash == user_pw {
info!("User {name} successfully logged in");
return Ok(user); return Ok(user);
} }
info!("User {name} supplied the wrong PW");
Err(LoginError::InvalidAuthenticationCombo) Err(LoginError::InvalidAuthenticationCombo)
} }
None => Err(LoginError::NoPasswordSet(user)), None => {
info!("User {name} has no PW set");
Err(LoginError::NoPasswordSet(user))
}
} }
} }

View File

@ -41,9 +41,7 @@ async fn login(
db: &State<SqlitePool>, db: &State<SqlitePool>,
cookies: &CookieJar<'_>, cookies: &CookieJar<'_>,
) -> Flash<Redirect> { ) -> Flash<Redirect> {
let user = User::login(db, login.name, login.password).await; let user = match User::login(db, login.name, login.password).await {
let user = match user {
Ok(user) => user, Ok(user) => user,
Err(LoginError::NoPasswordSet(user)) => { Err(LoginError::NoPasswordSet(user)) => {
return Flash::warning( return Flash::warning(