forge
Explore
Log in

Don't you dare go Hollow

A database hiccup is not a sign-out

Browse files at this commit

a672cd417f8895647220afa798e31330fa80e1aeAhmet Kaan GümüşParent: 659e9a2
  • fix
    • a database hiccup is not a sign-out
Show full message
🐛 fix(auth): a database hiccup is not a sign-out

current_user swallowed authenticate's store error into None, so a
locked-database read during a drag's write burst turned into
Refusal::SignInFirst for a genuinely signed-in person. current_user
and files::user_of now return the store's Result instead of folding
it away; require_user maps a store error onto the existing
Refusal::Unavailable path (via From<AccountError> for Refusal) rather
than SignInFirst, and current_gate propagates it as a ServerFnError.
crates/izlek-web/src/auth.rsmodified+4 -1
@@ -338,7 +338,10 @@ pub const WORKSPACE_NAME: &str = "Izlek";
338338 pub async fn current_gate() -> Result<Gate, ServerFnError> {
339339 use crate::server::{accounts, current_user};
340340
341- if let Some(user) = current_user().await {
341+ if let Some(user) = current_user()
342+ .await
343+ .map_err(|e| ServerFnError::new(e.to_string()))?
344+ {
342345 return Ok(Gate::SignedIn(Me {
343346 id: user.id,
344347 display_name: user.display_name,
crates/izlek-web/src/files.rsmodified+19 -8
@@ -9,7 +9,7 @@
99 use axum::extract::{Extension, Multipart, Path};
1010 use axum::http::{HeaderMap, HeaderValue, StatusCode};
1111 use axum::response::{IntoResponse, Redirect, Response};
12-use izlek_core::accounts::Accounts;
12+use izlek_core::accounts::{AccountError, Accounts};
1313 use izlek_core::store::{NewAttachment, User};
1414 use time::OffsetDateTime;
1515
@@ -18,9 +18,16 @@ use crate::detail::guard;
1818
1919 /// The person behind this request, read off the session cookie in `headers`
2020 /// rather than the leptos request context a plain axum handler has none of.
21-pub async fn user_of(accounts: &Accounts, headers: &HeaderMap) -> Option<User> {
22- let session = crate::server::session_in(headers)?;
23- accounts.authenticate(&session).await.ok().flatten()
21+///
22+/// A store error is returned as such, not folded into "nobody" — the callers
23+/// below tell it apart from a missing session the same way `server::require_user`
24+/// does, so a database hiccup answers "something went wrong" rather than
25+/// sending a signed-in person back through the door.
26+pub async fn user_of(accounts: &Accounts, headers: &HeaderMap) -> Result<Option<User>, AccountError> {
27+ let Some(session) = crate::server::session_in(headers) else {
28+ return Ok(None);
29+ };
30+ accounts.authenticate(&session).await
2431 }
2532
2633 /// Where the browser lands once the upload is settled, success or refusal
@@ -89,8 +96,10 @@ pub async fn upload(
8996 headers: HeaderMap,
9097 mut multipart: Multipart,
9198 ) -> Response {
92- let Some(user) = user_of(&accounts, &headers).await else {
93- return home(Refusal::SignInFirst);
99+ let user = match user_of(&accounts, &headers).await {
100+ Ok(Some(user)) => user,
101+ Ok(None) => return home(Refusal::SignInFirst),
102+ Err(error) => return home(error.into()),
94103 };
95104 if !user.role.can_comment() {
96105 return home(Refusal::Forbidden);
@@ -239,8 +248,10 @@ pub async fn download(
239248 headers: HeaderMap,
240249 Path(id): Path<String>,
241250 ) -> Response {
242- let Some(user) = user_of(&accounts, &headers).await else {
243- return home(Refusal::SignInFirst);
251+ let user = match user_of(&accounts, &headers).await {
252+ Ok(Some(user)) => user,
253+ Ok(None) => return home(Refusal::SignInFirst),
254+ Err(error) => return home(error.into()),
244255 };
245256
246257 let store = accounts.store().clone();
crates/izlek-web/src/server.rsmodified+21 -5
@@ -197,15 +197,31 @@ fn write_cookie(value: &str) {
197197 }
198198 }
199199
200-/// The person behind this request, or nobody.
201-pub async fn current_user() -> Option<User> {
202- let presented = presented_session()?;
203- accounts().authenticate(&presented).await.ok().flatten()
200+/// The person behind this request, or nobody, or the store failing to say.
201+///
202+/// A store error is not "nobody" — a busy database mid-drag is a fact about
203+/// the database, not about whether this browser is signed in, and a caller
204+/// that folded the two together would send a signed-in person to a sign-in
205+/// screen because a write elsewhere held a lock for a moment.
206+pub async fn current_user() -> Result<Option<User>, AccountError> {
207+ let Some(presented) = presented_session() else {
208+ return Ok(None);
209+ };
210+ accounts().authenticate(&presented).await
204211 }
205212
206213 /// The person behind this request, or a refusal the caller can return as-is.
214+///
215+/// A store error becomes `Refusal::Unavailable` — the same place every other
216+/// account error already lands, via `From<AccountError> for Refusal` below —
217+/// rather than `SignInFirst`, which would tell a signed-in person to sign in
218+/// again over what was only a database hiccup.
207219 pub async fn require_user() -> Result<User, Refusal> {
208- current_user().await.ok_or(Refusal::SignInFirst)
220+ match current_user().await {
221+ Ok(Some(user)) => Ok(user),
222+ Ok(None) => Err(Refusal::SignInFirst),
223+ Err(error) => Err(error.into()),
224+ }
209225 }
210226
211227 /// The admin behind this request. A member or a viewer is refused *here*, not