diff options
Diffstat (limited to 'src/http/routes.rs')
| -rw-r--r-- | src/http/routes.rs | 39 |
1 files changed, 26 insertions, 13 deletions
diff --git a/src/http/routes.rs b/src/http/routes.rs index 9efb90b..abd7345 100644 --- a/src/http/routes.rs +++ b/src/http/routes.rs @@ -12,13 +12,14 @@ use super::auth; use super::render::*; use super::{sse, Req, Res, State}; -/// Redirect an unauthenticated visitor to the login page. -pub fn gate(state: &State, req: &Req) -> Option<Res> { +/// Redirect an unauthenticated visitor to the login page. The session id is +/// the credential: a cookie a client typed by hand proves nothing, only a sid +/// this process minted and marked authenticated at a successful login does. +pub fn gate(state: &State, req: &Req, sid: &str) -> Option<Res> { if !auth::auth_required(&state.cfg) { return None; } - let signed_in = req.cookie("auth").as_deref() == Some("1"); - if signed_in || req.path == "/login" { + if auth::is_authenticated(sid) || req.path == "/login" { return None; } Some(Res::redirect("/login")) @@ -29,7 +30,7 @@ pub fn login_form(state: &State, req: &Req) -> Res { page_to_res(Login { page, error: String::new() }, 200) } -/// Forget the session server-side and clear both cookies. The CSRF token is +/// Forget the session server-side and clear the cookie. The CSRF token is /// derived from the sid, so dropping the sid invalidates every form a browser /// still has open — which is exactly what logout should do. pub fn logout(req: &Req) -> Res { @@ -37,7 +38,6 @@ pub fn logout(req: &Req) -> Res { auth::drop_session(&sid); } let mut res = Res::redirect("/login"); - res.header("Set-Cookie", "auth=; Path=/; Max-Age=0"); res.header("Set-Cookie", "sid=; Path=/; Max-Age=0"); res } @@ -46,8 +46,17 @@ pub fn login(state: &State, req: &Req) -> Res { let given = req.form_get("password").unwrap_or_default(); let page = Page::new("Sign in", csrf(state, req)); if auth::password_ok(&state.cfg, &given) { + // Rotate rather than promote: the sid the visitor arrived with (if any) + // is dropped, so the session that existed before the password was typed + // is never the one that carries the privilege afterwards. + if let Some(old) = req.cookie("sid") { + auth::drop_session(&old); + } + let sid = auth::new_session(); + auth::authenticate(&sid); let mut res = Res::redirect("/"); - res.header("Set-Cookie", "auth=1; Path=/; HttpOnly; SameSite=Strict"); + // `serve` turns this into the `Set-Cookie: sid=…` header. + res.header("X-New-Session", &sid); res } else { page_to_res(Login { page, error: "wrong password".into() }, 401) @@ -335,7 +344,7 @@ fn review_approve(state: &State, req: &Req, id: &str) -> Res { let identity = req.form_get("identity_hash").unwrap_or_default(); let digest = req.form_get("diff_digest").unwrap_or_default(); let note = req.form_get("note").filter(|s| !s.trim().is_empty()); - match ap::approve(db, actor(req), id, &identity, &digest, note.as_deref()) { + match ap::approve(db, actor(state, req), id, &identity, &digest, note.as_deref()) { Ok(a) => { let mut page = chrome(state, req, "Reviews", "nav_reviews"); page.flash = format!("approved {} (approval {}); build queued", c.pkgbase, a.id); @@ -400,7 +409,7 @@ fn review_reject(state: &State, req: &Req, id: &str) -> Res { page.flash_err = true; return page_to_res(ReviewsPage { page, rows: review_rows(state) }, 400); }; - match ap::reject(db, actor(req), id, ¬e) { + match ap::reject(db, actor(state, req), id, ¬e) { Ok(()) => { let mut page = chrome(state, req, "Reviews", "nav_reviews"); page.flash = format!("rejected #{id}; it can be re-opened from the package page"); @@ -453,12 +462,12 @@ fn reviews_bulk(state: &State, req: &Req) -> Res { continue; } let res = if decision == "approve" { - ap::approve(db, actor(req), id, &c.identity_hash, &c.diff_digest, Some("bulk")) + ap::approve(db, actor(state, req), id, &c.identity_hash, &c.diff_digest, Some("bulk")) .map(|_| ()) .map_err(|e| e.to_string()) } else { let note = req.form_get("note").unwrap_or_else(|| "bulk reject".into()); - ap::reject(db, actor(req), id, ¬e).map_err(|e| e.to_string()) + ap::reject(db, actor(state, req), id, ¬e).map_err(|e| e.to_string()) }; match res { Ok(()) => { @@ -482,8 +491,12 @@ fn reviews_bulk(state: &State, req: &Req) -> Res { page_to_res(ReviewsPage { page, rows: review_rows(state) }, 200) } -fn actor(req: &Req) -> &str { - if req.cookie("auth").is_some() { +/// Who the audit trail says made a decision. With no password configured the +/// UI is open on loopback and every session is the operator's; with one, only +/// a session that passed the gate gets a name. +fn actor(state: &State, req: &Req) -> &'static str { + let sid = req.cookie("sid").unwrap_or_default(); + if !auth::auth_required(&state.cfg) || auth::is_authenticated(&sid) { "web" } else { "web-anon" |
