Skip to content

Commit

Permalink
Update to latest tower-sessions
Browse files Browse the repository at this point in the history
  • Loading branch information
itsjunetime committed Dec 24, 2023
1 parent 2efba98 commit 3c179e5
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 36 deletions.
59 changes: 30 additions & 29 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion backend/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ serde = { version = "1.0.193", features = ["serde_derive"] }
tower = { version = "0.4.13", default-features = false }
axum-auth = { version = "0.7.0", default-features = false, features = ["auth-basic"] }
argon2 = { version = "0.5.2", features = ["std"] }
tower-sessions = "0.7.0"
tower-sessions = "0.8.2"
syntect = { version = "5.1.0", default-features = false, features = ["default-themes", "parsing", "default-syntaxes", "html", "regex-onig"] }
horrorshow = { version = "0.8.4", default-features = false, features = ["std", "ops"] }
once_cell = { version = "1.19.0", default-features = false }
Expand Down
14 changes: 8 additions & 6 deletions backend/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ macro_rules! check_auth{
}
};
($session:ident, noret) => {
$session.get::<String>($crate::USERNAME_KEY).ok().flatten()
$session.get::<String>($crate::USERNAME_KEY).await.ok().flatten()
}
}

Expand Down Expand Up @@ -448,18 +448,20 @@ pub async fn login(
return Ok(());
};

let session_id = session.id();

// Only get the pass if it's not empty
let Some(pass) = password.and_then(|p| (!p.is_empty()).then_some(p)) else {
eprintln!("Session {} sent a login request with an empty password", session.id());
eprintln!("Session {session_id:?} sent a login request with an empty password");
return Err((StatusCode::PRECONDITION_FAILED, "Please include a password"));
};

if username.is_empty() {
eprintln!("Session {} sent a login request with an empty username", session.id());
eprintln!("Session {session_id:?} sent a login request with an empty username");
return Err((StatusCode::PRECONDITION_FAILED, "Please include a username"));
}

println!("User trying to login with session {} and username {username}", session.id());
println!("User trying to login with session {session_id:?} and username {username}");

let unauth = || (StatusCode::UNAUTHORIZED, "Incorrect username or password");

Expand All @@ -484,9 +486,9 @@ pub async fn login(

match Argon2::default().verify_password(pass.as_bytes(), &hash_struct) {
Ok(()) => {
println!("Trying to log in {username} with session_id {}", session.id());
println!("Trying to log in {username} with session_id {:?}", session.id());

if let Err(err) = session.insert(USERNAME_KEY, username) {
if let Err(err) = session.insert(USERNAME_KEY, username).await {
println!("Could not save session: {err}");
return Err((StatusCode::INTERNAL_SERVER_ERROR, "Failed to save session; unable to log you in"));
}
Expand Down

0 comments on commit 3c179e5

Please sign in to comment.