Skip to content

Commit c1c3b1d

Browse files
committed
1 parent 5526b5d commit c1c3b1d

File tree

1 file changed

+15
-9
lines changed

1 file changed

+15
-9
lines changed

src/routes.rs

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -79,22 +79,22 @@ pub async fn handle_login(
7979
Form(form): Form<LoginForm>,
8080
) -> Result<(CookieJar, &'static str), (StatusCode, &'static str)> {
8181

82-
// Validate username
82+
// Validate input data: username
8383
let username = form.username.trim();
8484
if username.is_empty() {
8585
return Err((StatusCode::BAD_REQUEST, "Username cannot be empty"));
8686
}
8787

88-
// Validate TOTP format
88+
// TOTP format
8989
let code = match form.totp.trim().parse::<u32>() {
9090
Ok(c) => c,
9191
Err(_) => { return Err((StatusCode::BAD_REQUEST, "Invalid TOTP format")); }
9292
};
9393

94-
// 1. First verify TOTP (to check if user is in TOTP shadow file)
95-
if !verify_totp(username, code, &state.totp_cache).await {
96-
println!("Login denied: Invalid TOTP for {}", username);
97-
return Err((StatusCode::UNAUTHORIZED, "Invalid TOTP"));
94+
// Username is not in TOTP shadow file
95+
if !state.totp_cache.read().await.contains_key(username) {
96+
println!("Login denied: User {} not in TOTP shadow file", username);
97+
return Err((StatusCode::UNAUTHORIZED, "Invalid username, password or TOTP"));
9898
}
9999

100100
// Create PAM-Client
@@ -106,11 +106,17 @@ pub async fn handle_login(
106106
}
107107
};
108108

109-
// 2. Then check against system users (to prevent information leakage about system users not in TOTP file)
109+
// 1. Check password
110110
client.conversation_mut().set_credentials(username, &form.password);
111111
if client.authenticate().is_err() {
112-
println!("Login denied: Username {} or password invalid", username);
113-
return Err((StatusCode::UNAUTHORIZED, "Invalid username or password"));
112+
println!("Login denied: Wrong password for user {}", username);
113+
return Err((StatusCode::UNAUTHORIZED, "Invalid username, password or TOTP"));
114+
}
115+
116+
// 2. Verify TOTP
117+
if !verify_totp(username, code, &state.totp_cache).await {
118+
println!("Login denied: Invalid TOTP for {}", username);
119+
return Err((StatusCode::UNAUTHORIZED, "Invalid username, password or TOTP"));
114120
}
115121

116122
// Create session

0 commit comments

Comments
 (0)