A database hiccup is not a sign-out
Browse files at this commita672cd417f8895647220afa798e31330fa80e1aeParent: 659e9a2- fix
- a database hiccup is not a sign-out
3 changed files +44 -14
- crates/izlek-web/src/auth.rs +4 -1
- crates/izlek-web/src/files.rs +19 -8
- crates/izlek-web/src/server.rs +21 -5
crates/izlek-web/src/auth.rsmodified+4 -1
| @@ -338,7 +338,10 @@ pub const WORKSPACE_NAME: &str = "Izlek"; | |||
| 338 | pub async fn current_gate() -> Result<Gate, ServerFnError> { | 338 | pub async fn current_gate() -> Result<Gate, ServerFnError> { |
| 339 | use crate::server::{accounts, current_user}; | 339 | use crate::server::{accounts, current_user}; |
| 340 | 340 | ||
| 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 | + { | ||
| 342 | return Ok(Gate::SignedIn(Me { | 345 | return Ok(Gate::SignedIn(Me { |
| 343 | id: user.id, | 346 | id: user.id, |
| 344 | display_name: user.display_name, | 347 | display_name: user.display_name, |
crates/izlek-web/src/files.rsmodified+19 -8
| @@ -9,7 +9,7 @@ | |||
| 9 | use axum::extract::{Extension, Multipart, Path}; | 9 | use axum::extract::{Extension, Multipart, Path}; |
| 10 | use axum::http::{HeaderMap, HeaderValue, StatusCode}; | 10 | use axum::http::{HeaderMap, HeaderValue, StatusCode}; |
| 11 | use axum::response::{IntoResponse, Redirect, Response}; | 11 | use axum::response::{IntoResponse, Redirect, Response}; |
| 12 | -use izlek_core::accounts::Accounts; | 12 | +use izlek_core::accounts::{AccountError, Accounts}; |
| 13 | use izlek_core::store::{NewAttachment, User}; | 13 | use izlek_core::store::{NewAttachment, User}; |
| 14 | use time::OffsetDateTime; | 14 | use time::OffsetDateTime; |
| 15 | 15 | ||
| @@ -18,9 +18,16 @@ use crate::detail::guard; | |||
| 18 | 18 | ||
| 19 | /// The person behind this request, read off the session cookie in `headers` | 19 | /// The person behind this request, read off the session cookie in `headers` |
| 20 | /// rather than the leptos request context a plain axum handler has none of. | 20 | /// 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> { | 21 | +/// |
| 22 | - let session = crate::server::session_in(headers)?; | 22 | +/// A store error is returned as such, not folded into "nobody" — the callers |
| 23 | - accounts.authenticate(&session).await.ok().flatten() | 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 | ||
| 24 | } | 31 | } |
| 25 | 32 | ||
| 26 | /// Where the browser lands once the upload is settled, success or refusal | 33 | /// Where the browser lands once the upload is settled, success or refusal |
| @@ -89,8 +96,10 @@ pub async fn upload( | |||
| 89 | headers: HeaderMap, | 96 | headers: HeaderMap, |
| 90 | mut multipart: Multipart, | 97 | mut multipart: Multipart, |
| 91 | ) -> Response { | 98 | ) -> Response { |
| 92 | - let Some(user) = user_of(&accounts, &headers).await else { | 99 | + let user = match user_of(&accounts, &headers).await { |
| 93 | - return home(Refusal::SignInFirst); | 100 | + Ok(Some(user)) => user, |
| 101 | + Ok(None) => return home(Refusal::SignInFirst), | ||
| 102 | + Err(error) => return home(error.into()), | ||
| 94 | }; | 103 | }; |
| 95 | if !user.role.can_comment() { | 104 | if !user.role.can_comment() { |
| 96 | return home(Refusal::Forbidden); | 105 | return home(Refusal::Forbidden); |
| @@ -239,8 +248,10 @@ pub async fn download( | |||
| 239 | headers: HeaderMap, | 248 | headers: HeaderMap, |
| 240 | Path(id): Path<String>, | 249 | Path(id): Path<String>, |
| 241 | ) -> Response { | 250 | ) -> Response { |
| 242 | - let Some(user) = user_of(&accounts, &headers).await else { | 251 | + let user = match user_of(&accounts, &headers).await { |
| 243 | - return home(Refusal::SignInFirst); | 252 | + Ok(Some(user)) => user, |
| 253 | + Ok(None) => return home(Refusal::SignInFirst), | ||
| 254 | + Err(error) => return home(error.into()), | ||
| 244 | }; | 255 | }; |
| 245 | 256 | ||
| 246 | let store = accounts.store().clone(); | 257 | let store = accounts.store().clone(); |
crates/izlek-web/src/server.rsmodified+21 -5
| @@ -197,15 +197,31 @@ fn write_cookie(value: &str) { | |||
| 197 | } | 197 | } |
| 198 | } | 198 | } |
| 199 | 199 | ||
| 200 | -/// The person behind this request, or nobody. | 200 | +/// The person behind this request, or nobody, or the store failing to say. |
| 201 | -pub async fn current_user() -> Option<User> { | 201 | +/// |
| 202 | - let presented = presented_session()?; | 202 | +/// A store error is not "nobody" — a busy database mid-drag is a fact about |
| 203 | - accounts().authenticate(&presented).await.ok().flatten() | 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 | ||
| 204 | } | 211 | } |
| 205 | 212 | ||
| 206 | /// The person behind this request, or a refusal the caller can return as-is. | 213 | /// 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. | ||
| 207 | pub async fn require_user() -> Result<User, Refusal> { | 219 | 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 | + } | ||
| 209 | } | 225 | } |
| 210 | 226 | ||
| 211 | /// The admin behind this request. A member or a viewer is refused *here*, not | 227 | /// The admin behind this request. A member or a viewer is refused *here*, not |