extra debugging

This commit is contained in:
Andrea Toska 2025-04-26 20:14:34 +02:00
commit 1423d995a4
No known key found for this signature in database
GPG key ID: 5B3C83807CCBE9A2

View file

@ -111,29 +111,64 @@ pub fn generate_password(pid: i32, cleartext_password: &str) -> Option<String>{
pub async fn read_basic_auth_token(connection: &Pool, token: &str) -> Option<User> {
let data = BASE64_STANDARD.decode(&token).ok()?;
println!("Received token (base64): {:?}", token);
let decoded_basic_token = String::from_utf8(data).ok()?;
let data = match BASE64_STANDARD.decode(&token) {
Ok(d) => d,
Err(e) => {
println!("Failed to decode base64: {:?}", e);
return None;
}
};
println!("Decoded base64 bytes: {:?}", data);
let (login_username, login_password) = decoded_basic_token.split_once(' ')?;
let decoded_basic_token = match String::from_utf8(data) {
Ok(s) => s,
Err(e) => {
println!("Failed to convert decoded bytes to UTF-8 string: {:?}", e);
return None;
}
};
println!("Decoded basic auth token string: {:?}", decoded_basic_token);
let (login_username, login_password) = match decoded_basic_token.split_once(' ') {
Some(parts) => parts,
None => {
println!("Failed to split basic token into username and password");
return None;
}
};
println!("Parsed login_username: {:?}", login_username);
println!("Parsed login_password: {:?}", login_password);
let mut user = sqlx::query_as!(
let user_result = sqlx::query_as!(
User,
"SELECT * FROM users WHERE username = $1",
login_username
).fetch_one(connection).await.ok()?;
).fetch_one(connection).await;
let mut user = match user_result {
Ok(u) => u,
Err(e) => {
println!("Failed to fetch user from database: {:?}", e);
return None;
}
};
println!("Fetched user from database: {:?}", user);
let password_valid = user.verify_cleartext_password(&login_password);
println!("Password verification result: {:?}", password_valid);
if password_valid == Some(true){
if password_valid == Some(true) {
println!("Password verification succeeded");
Some(user)
} else {
println!("Password verification failed");
None
}
}
pub async fn read_bearer_auth_token(connection: &Pool, token: &str) -> Option<User> {
let data = TokenData::decode(token)?;
@ -208,11 +243,11 @@ impl<'r, const FORCE_BEARER_AUTH: bool> FromRequest<'r> for Auth<FORCE_BEARER_AU
let user = match auth_type{
"Basic" if !FORCE_BEARER_AUTH => read_basic_auth_token(pool, token).await,
"Bearer" => read_bearer_auth_token(pool, token).await,
_ => return Outcome::Error((Status::BadRequest, INVALID_TOKEN_ERRORS_DBG)),
_ => return Outcome::Error((Status::BadRequest, INVALID_TOKEN_ERRORS)),
};
let Some(user) = user else {
return Outcome::Error((Status::BadRequest, INVALID_TOKEN_ERRORS));
return Outcome::Error((Status::BadRequest, INVALID_TOKEN_ERRORS_DBG));
};
Outcome::Success(Self(user))